-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
EmbeddedNeo4jWithNewIndexing.java
187 lines (168 loc) · 6.41 KB
/
EmbeddedNeo4jWithNewIndexing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Licensed to Neo Technology under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Neo Technology licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.neo4j.examples;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.graphdb.schema.Schema;
public class EmbeddedNeo4jWithNewIndexing
{
private static final String DB_PATH = "target/neo4j-store-with-new-indexing";
public static void main( final String[] args )
{
System.out.println( "Starting database ..." );
// START SNIPPET: startDb
GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
// END SNIPPET: startDb
{
// START SNIPPET: createIndex
IndexDefinition indexDefinition;
try ( Transaction tx = graphDb.beginTx() )
{
Schema schema = graphDb.schema();
indexDefinition = schema.indexFor( DynamicLabel.label( "User" ) )
.on( "username" )
.create();
tx.success();
}
// END SNIPPET: createIndex
// START SNIPPET: wait
try ( Transaction tx = graphDb.beginTx() )
{
Schema schema = graphDb.schema();
schema.awaitIndexOnline( indexDefinition, 10, TimeUnit.SECONDS );
}
// END SNIPPET: wait
}
{
// START SNIPPET: addUsers
try ( Transaction tx = graphDb.beginTx() )
{
Label label = DynamicLabel.label( "User" );
// Create some users
for ( int id = 0; id < 100; id++ )
{
Node userNode = graphDb.createNode( label );
userNode.setProperty( "username", "user" + id + "@neo4j.org" );
}
System.out.println( "Users created" );
tx.success();
}
// END SNIPPET: addUsers
}
{
// START SNIPPET: findUsers
Label label = DynamicLabel.label( "User" );
int idToFind = 45;
String nameToFind = "user" + idToFind + "@neo4j.org";
try ( Transaction tx = graphDb.beginTx() )
{
try ( ResourceIterator<Node> users =
graphDb.findNodesByLabelAndProperty( label, "username", nameToFind ).iterator() )
{
ArrayList<Node> userNodes = new ArrayList<>();
while ( users.hasNext() )
{
userNodes.add( users.next() );
}
for ( Node node : userNodes )
{
System.out.println( "The username of user " + idToFind + " is " + node.getProperty( "username" ) );
}
}
}
// END SNIPPET: findUsers
}
{
// START SNIPPET: resourceIterator
Label label = DynamicLabel.label( "User" );
int idToFind = 45;
String nameToFind = "user" + idToFind + "@neo4j.org";
try ( Transaction tx = graphDb.beginTx();
ResourceIterator<Node> users = graphDb
.findNodesByLabelAndProperty( label, "username", nameToFind )
.iterator() )
{
Node firstUserNode;
if ( users.hasNext() )
{
firstUserNode = users.next();
}
users.close();
}
// END SNIPPET: resourceIterator
}
{
// START SNIPPET: updateUsers
try ( Transaction tx = graphDb.beginTx() )
{
Label label = DynamicLabel.label( "User" );
int idToFind = 45;
String nameToFind = "user" + idToFind + "@neo4j.org";
for ( Node node : graphDb.findNodesByLabelAndProperty( label, "username", nameToFind ) )
{
node.setProperty( "username", "user" + ( idToFind + 1 ) + "@neo4j.org" );
}
tx.success();
}
// END SNIPPET: updateUsers
}
{
// START SNIPPET: deleteUsers
try ( Transaction tx = graphDb.beginTx() )
{
Label label = DynamicLabel.label( "User" );
int idToFind = 46;
String nameToFind = "user" + idToFind + "@neo4j.org";
for ( Node node : graphDb.findNodesByLabelAndProperty( label, "username", nameToFind ) )
{
node.delete();
}
tx.success();
}
// END SNIPPET: deleteUsers
}
{
// START SNIPPET: dropIndex
try ( Transaction tx = graphDb.beginTx() )
{
Label label = DynamicLabel.label( "User" );
for ( IndexDefinition indexDefinition : graphDb.schema()
.getIndexes( label ) )
{
// There is only one index
indexDefinition.drop();
}
tx.success();
}
// END SNIPPET: dropIndex
}
System.out.println( "Shutting down database ..." );
// START SNIPPET: shutdownDb
graphDb.shutdown();
// END SNIPPET: shutdownDb
}
}