1   /**
2    * Licensed to Neo Technology under one or more contributor
3    * license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright
5    * ownership. Neo Technology licenses this file to you under
6    * the Apache License, Version 2.0 (the "License"); you may
7    * not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.neo4j.examples;
20  
21  import org.neo4j.graphdb.Direction;
22  import org.neo4j.graphdb.GraphDatabaseService;
23  import org.neo4j.graphdb.Node;
24  import org.neo4j.graphdb.Relationship;
25  import org.neo4j.graphdb.RelationshipType;
26  import org.neo4j.graphdb.Transaction;
27  import org.neo4j.graphdb.index.Index;
28  import org.neo4j.kernel.EmbeddedGraphDatabase;
29  
30  public class EmbeddedNeo4jWithIndexing
31  {
32      private static final String DB_PATH = "neo4j-store";
33      private static final String USERNAME_KEY = "username";
34      private static GraphDatabaseService graphDb;
35      private static Index<Node> indexService;
36  
37      // START SNIPPET: createRelTypes
38      private static enum RelTypes implements RelationshipType
39      {
40          USERS_REFERENCE,
41          USER
42      }
43      // END SNIPPET: createRelTypes
44  
45      public static void main( final String[] args )
46      {
47          // START SNIPPET: startDb
48          graphDb = new EmbeddedGraphDatabase( DB_PATH );
49          indexService = graphDb.index().forNodes( "nodes" );
50          registerShutdownHook();
51          // END SNIPPET: startDb
52  
53          // START SNIPPET: addUsers
54          Transaction tx = graphDb.beginTx();
55          try
56          {
57              // Create users sub reference node (see design guidelines on
58              // http://wiki.neo4j.org/ )
59              Node usersReferenceNode = graphDb.createNode();
60              graphDb.getReferenceNode().createRelationshipTo(
61                  usersReferenceNode, RelTypes.USERS_REFERENCE );
62              // Create some users and index their names with the IndexService
63              for ( int id = 0; id < 100; id++ )
64              {
65                  Node userNode = createAndIndexUser( idToUserName( id ) );
66                  usersReferenceNode.createRelationshipTo( userNode,
67                      RelTypes.USER );
68              }
69              // END SNIPPET: addUsers
70              System.out.println( "Users created" );
71  
72              // Find a user through the search index
73              // START SNIPPET: findUser
74              int idToFind = 45;
75              Node foundUser = indexService.get( USERNAME_KEY,
76                  idToUserName( idToFind ) ).getSingle();
77              System.out.println( "The username of user " + idToFind + " is "
78                  + foundUser.getProperty( USERNAME_KEY ) );
79              // END SNIPPET: findUser
80  
81              // Delete the persons and remove them from the index
82              for ( Relationship relationship : usersReferenceNode.getRelationships(
83                      RelTypes.USER, Direction.OUTGOING ) )
84              {
85                  Node user = relationship.getEndNode();
86                  indexService.remove(  user, USERNAME_KEY,
87                          user.getProperty( USERNAME_KEY ) );
88                  user.delete();
89                  relationship.delete();
90              }
91              usersReferenceNode.getSingleRelationship( RelTypes.USERS_REFERENCE,
92                      Direction.INCOMING ).delete();
93              usersReferenceNode.delete();
94              tx.success();
95          }
96          finally
97          {
98              tx.finish();
99          }
100         System.out.println( "Shutting down database ..." );
101         shutdown();
102     }
103 
104     private static void shutdown()
105     {
106         graphDb.shutdown();
107     }
108 
109     // START SNIPPET: helperMethods
110     private static String idToUserName( final int id )
111     {
112         return "user" + id + "@neo4j.org";
113     }
114 
115     private static Node createAndIndexUser( final String username )
116     {
117         Node node = graphDb.createNode();
118         node.setProperty( USERNAME_KEY, username );
119         indexService.add( node, USERNAME_KEY, username );
120         return node;
121     }
122     // END SNIPPET: helperMethods
123 
124     private static void registerShutdownHook()
125     {
126         // Registers a shutdown hook for the Neo4j and index service instances
127         // so that it shuts down nicely when the VM exits (even if you
128         // "Ctrl-C" the running example before it's completed)
129         Runtime.getRuntime().addShutdownHook( new Thread()
130         {
131             @Override
132             public void run()
133             {
134                 shutdown();
135             }
136         } );
137     }
138 }