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  /**
31   * As of neo4j-1.2.M02, the {@link org.neo4j.graphdb.GraphDatabaseService} is
32   * always paired with an {@link org.neo4j.graphdb.index.IndexManager} for
33   * creating and managing indexes on Nodes and Relationships. It is a new index
34   * framework set to replace the {@link IndexService} interface and is tighter
35   * integrated with the graph database.
36   */
37  public class UsingIntegratedIndex
38  {
39      private static final String DB_PATH = "neo4j-store";
40      private static final String USERNAME_KEY = "username";
41      private static GraphDatabaseService graphDb;
42      private static final String INDEX_NAME = "user-index";
43      private static Index<Node> nodeIndex;
44  
45      private static enum RelTypes implements RelationshipType
46      {
47          USERS_REFERENCE,
48          USER
49      }
50  
51      public static void main( final String[] args )
52      {
53          // START SNIPPET: createIndex
54          graphDb = new EmbeddedGraphDatabase( DB_PATH );
55          nodeIndex = graphDb.index().forNodes( INDEX_NAME );
56          registerShutdownHook();
57          // END SNIPPET: createIndex
58  
59          Transaction tx = graphDb.beginTx();
60          try
61          {
62              // Create users sub reference node (see design guidelines on
63              // http://wiki.neo4j.org/ )
64              Node usersReferenceNode = graphDb.createNode();
65              graphDb.getReferenceNode().createRelationshipTo(
66                  usersReferenceNode, RelTypes.USERS_REFERENCE );
67              // Create some users and index their names with the IndexService
68              for ( int id = 0; id < 100; id++ )
69              {
70                  Node userNode = createAndIndexUser( idToUserName( id ) );
71                  usersReferenceNode.createRelationshipTo( userNode,
72                      RelTypes.USER );
73              }
74              System.out.println( "Users created" );
75  
76              // Find a user through the search index
77              int idToFind = 45;
78              Node foundUser = nodeIndex.get( USERNAME_KEY, idToUserName( idToFind) ).getSingle();
79  
80              System.out.println( "The username of user " + idToFind + " is "
81                  + foundUser.getProperty( USERNAME_KEY ) );
82  
83              // Delete the persons and remove them from the index
84              for ( Relationship relationship : usersReferenceNode.getRelationships(
85                      RelTypes.USER, Direction.OUTGOING ) )
86              {
87                  Node user = relationship.getEndNode();
88                  nodeIndex.remove(  user, USERNAME_KEY,
89                          user.getProperty( USERNAME_KEY ) );
90                  user.delete();
91                  relationship.delete();
92              }
93              usersReferenceNode.getSingleRelationship( RelTypes.USERS_REFERENCE,
94                      Direction.INCOMING ).delete();
95              usersReferenceNode.delete();
96              tx.success();
97          }
98          finally
99          {
100             tx.finish();
101         }
102         System.out.println( "Shutting down database ..." );
103         shutdown();
104     }
105 
106     static void shutdown()
107     {
108         // START SNIPPET: shutdownDatabase
109         graphDb.shutdown();
110         // END SNIPPET: shutdownDatabase
111     }
112 
113     private static String idToUserName( final int id )
114     {
115         return "user" + id + "@neo4j.org";
116     }
117 
118     private static Node createAndIndexUser( final String username )
119     {
120         // START SNIPPET: indexNode
121         Node node = graphDb.createNode();
122         node.setProperty( USERNAME_KEY, username );
123         nodeIndex.add( node, USERNAME_KEY, username );
124         // END SNIPPET: indexNode
125         return node;
126     }
127 
128     private static void registerShutdownHook()
129     {
130         // Registers a shutdown hook for the Neo4j and index service instances
131         // so that it shuts down nicely when the VM exits (even if you
132         // "Ctrl-C" the running example before it's completed)
133         Runtime.getRuntime().addShutdownHook( new Thread()
134         {
135             @Override
136             public void run()
137             {
138                 shutdown();
139             }
140         } );
141     }
142 }