1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.neo4j.server.rest.domain;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.neo4j.graphdb.DynamicRelationshipType;
28 import org.neo4j.graphdb.Node;
29 import org.neo4j.graphdb.PropertyContainer;
30 import org.neo4j.graphdb.Relationship;
31 import org.neo4j.graphdb.Transaction;
32 import org.neo4j.graphdb.index.Index;
33 import org.neo4j.helpers.collection.MapUtil;
34 import org.neo4j.kernel.EmbeddedGraphDatabase;
35 import org.neo4j.server.database.Database;
36 import org.neo4j.server.database.DatabaseBlockedException;
37
38 public class GraphDbHelper
39 {
40 private final Database database;
41
42 public GraphDbHelper( Database database )
43 {
44 this.database = database;
45 }
46
47 public int getNumberOfNodes() throws DatabaseBlockedException
48 {
49 return numberOfEntitiesFor( Node.class );
50 }
51
52 public int getNumberOfRelationships() throws DatabaseBlockedException
53 {
54 return numberOfEntitiesFor( Relationship.class );
55 }
56
57 private int numberOfEntitiesFor( Class<? extends PropertyContainer> type ) throws DatabaseBlockedException
58 {
59 return (int) ((EmbeddedGraphDatabase) database.graph).getConfig().getGraphDbModule().getNodeManager().getNumberOfIdsInUse( type );
60 }
61
62 public Map<String, Object> getNodeProperties( long nodeId ) throws DatabaseBlockedException
63 {
64 Transaction tx = database.graph.beginTx();
65 try
66 {
67 Node node = database.graph.getNodeById( nodeId );
68 Map<String, Object> allProperties = new HashMap<String, Object>();
69 for ( String propertyKey : node.getPropertyKeys() )
70 {
71 allProperties.put( propertyKey, node.getProperty( propertyKey ) );
72 }
73 tx.success();
74 return allProperties;
75 } finally
76 {
77 tx.finish();
78 }
79 }
80
81 public void setNodeProperties( long nodeId, Map<String, Object> properties ) throws DatabaseBlockedException
82 {
83 Transaction tx = database.graph.beginTx();
84 try
85 {
86 Node node = database.graph.getNodeById( nodeId );
87 for ( Map.Entry<String, Object> propertyEntry : properties.entrySet() )
88 {
89 node.setProperty( propertyEntry.getKey(), propertyEntry.getValue() );
90 }
91 tx.success();
92 } finally
93 {
94 tx.finish();
95 }
96 }
97
98 public long createNode() throws DatabaseBlockedException
99 {
100 Transaction tx = database.graph.beginTx();
101 try
102 {
103 Node node = database.graph.createNode();
104 tx.success();
105 return node.getId();
106 } finally
107 {
108 tx.finish();
109 }
110 }
111
112 public long createNode( Map<String, Object> properties ) throws DatabaseBlockedException
113 {
114 Transaction tx = database.graph.beginTx();
115 try
116 {
117 Node node = database.graph.createNode();
118 for ( Map.Entry<String, Object> entry : properties.entrySet() )
119 {
120 node.setProperty( entry.getKey(), entry.getValue() );
121 }
122 tx.success();
123 return node.getId();
124 } finally
125 {
126 tx.finish();
127 }
128 }
129
130 public void deleteNode( long id ) throws DatabaseBlockedException
131 {
132 Transaction tx = database.graph.beginTx();
133 try
134 {
135 Node node = database.graph.getNodeById( id );
136 node.delete();
137 tx.success();
138 } finally
139 {
140 tx.finish();
141 }
142 }
143
144 public long createRelationship( String type, long startNodeId, long endNodeId ) throws DatabaseBlockedException
145 {
146 Transaction tx = database.graph.beginTx();
147 try
148 {
149 Node startNode = database.graph.getNodeById( startNodeId );
150 Node endNode = database.graph.getNodeById( endNodeId );
151 Relationship relationship = startNode.createRelationshipTo( endNode, DynamicRelationshipType.withName( type ) );
152 tx.success();
153 return relationship.getId();
154 } finally
155 {
156 tx.finish();
157 }
158 }
159
160 public long createRelationship( String type ) throws DatabaseBlockedException
161 {
162 Transaction tx = database.graph.beginTx();
163 try
164 {
165 Node startNode = database.graph.createNode();
166 Node endNode = database.graph.createNode();
167 Relationship relationship = startNode.createRelationshipTo( endNode, DynamicRelationshipType.withName( type ) );
168 tx.success();
169 return relationship.getId();
170 } finally
171 {
172 tx.finish();
173 }
174 }
175
176 public void setRelationshipProperties( long relationshipId, Map<String, Object> properties ) throws DatabaseBlockedException
177 {
178 Transaction tx = database.graph.beginTx();
179 try
180 {
181 Relationship relationship = database.graph.getRelationshipById( relationshipId );
182 for ( Map.Entry<String, Object> propertyEntry : properties.entrySet() )
183 {
184 relationship.setProperty( propertyEntry.getKey(), propertyEntry.getValue() );
185 }
186 tx.success();
187 } finally
188 {
189 tx.finish();
190 }
191 }
192
193 public Map<String, Object> getRelationshipProperties( long relationshipId ) throws DatabaseBlockedException
194 {
195 Transaction tx = database.graph.beginTx();
196 try
197 {
198 Relationship relationship = database.graph.getRelationshipById( relationshipId );
199 Map<String, Object> allProperties = new HashMap<String, Object>();
200 for ( String propertyKey : relationship.getPropertyKeys() )
201 {
202 allProperties.put( propertyKey, relationship.getProperty( propertyKey ) );
203 }
204 tx.success();
205 return allProperties;
206 } finally
207 {
208 tx.finish();
209 }
210 }
211
212 public Relationship getRelationship( long relationshipId ) throws DatabaseBlockedException
213 {
214 Transaction tx = database.graph.beginTx();
215 try
216 {
217 Relationship relationship = database.graph.getRelationshipById( relationshipId );
218 tx.success();
219 return relationship;
220 } finally
221 {
222 tx.finish();
223 }
224 }
225
226 public void addNodeToIndex( String indexName, String key, Object value, long id ) throws DatabaseBlockedException
227 {
228 Index<Node> index = database.getNodeIndex( indexName );
229 Transaction tx = database.graph.beginTx();
230 try
231 {
232 index.add( database.graph.getNodeById( id ), key, value );
233 tx.success();
234 } finally
235 {
236 tx.finish();
237 }
238 }
239
240
241 public Collection<Long> queryIndexedNodes( String indexName, String key, Object value ) throws DatabaseBlockedException
242 {
243
244 Index<Node> index = database.getNodeIndex( indexName );
245 Transaction tx = database.graph.beginTx();
246 try
247 {
248 Collection<Long> result = new ArrayList<Long>();
249 for ( Node node : index.query( key, value ) )
250 {
251 result.add( node.getId() );
252 }
253 tx.success();
254 return result;
255 } finally
256 {
257 tx.finish();
258 }
259 }
260 public Collection<Long> getIndexedNodes( String indexName, String key, Object value ) throws DatabaseBlockedException
261 {
262
263 Index<Node> index = database.getNodeIndex( indexName );
264 Transaction tx = database.graph.beginTx();
265 try
266 {
267 Collection<Long> result = new ArrayList<Long>();
268 for ( Node node : index.get( key, value ) )
269 {
270 result.add( node.getId() );
271 }
272 tx.success();
273 return result;
274 } finally
275 {
276 tx.finish();
277 }
278 }
279
280 public Collection<Long> getIndexedRelationships( String indexName, String key, Object value ) throws DatabaseBlockedException
281 {
282
283 Index<Relationship> index = database.getRelationshipIndex( indexName );
284 Transaction tx = database.graph.beginTx();
285 try
286 {
287 Collection<Long> result = new ArrayList<Long>();
288 for ( Relationship relationship : index.get( key, value ) )
289 {
290 result.add( relationship.getId() );
291 }
292 tx.success();
293 return result;
294 } finally
295 {
296 tx.finish();
297 }
298 }
299
300 public void addRelationshipToIndex( String indexName, String key, String value, long relationshipId )
301 {
302 Index<Relationship> index = database.getRelationshipIndex( indexName );
303 Transaction tx = database.graph.beginTx();
304 try
305 {
306 index.add( database.graph.getRelationshipById( relationshipId ), key, value );
307 tx.success();
308 } finally
309 {
310 tx.finish();
311 }
312 }
313
314 public String[] getNodeIndexes()
315 {
316 return database.getIndexManager().nodeIndexNames();
317 }
318
319 public Index<Node> getNodeIndex( String indexName )
320 {
321 return database.getIndexManager().forNodes( indexName );
322 }
323
324 public Index<Node> createNodeFullTextIndex( String named )
325 {
326 return database.getIndexManager().forNodes( named, MapUtil.stringMap( "provider", "lucene", "type", "fulltext" ) );
327 }
328
329 public Index<Node> createNodeIndex( String named )
330 {
331 return database.getIndexManager().forNodes( named );
332 }
333
334 public String[] getRelationshipIndexes()
335 {
336 return database.getIndexManager().relationshipIndexNames();
337 }
338
339 public long getReferenceNode()
340 {
341 return database.graph.getReferenceNode().getId();
342 }
343
344 public Index<Relationship> getRelationshipIndex( String indexName )
345 {
346 return database.getIndexManager().forRelationships( indexName );
347 }
348
349 public Index<Relationship> createRelationshipFullTextIndex( String named )
350 {
351 return database.getIndexManager().forRelationships( named, MapUtil.stringMap( "provider", "lucene", "type", "fulltext" ) );
352 }
353
354 public Index<Relationship> createRelationshipIndex( String named )
355 {
356 return database.getIndexManager().forRelationships( named );
357 }
358 }