1- //
2- // Copyright 2012 James Thornton (http://jamesthornton.com)
3- // BSD License (see LICENSE for details)
4- //
1+ // NOTE: Converting all index values to strings because that's what Neo4j does
2+ // anyway, and when using the JSON type system, Rexster doesn't have a way to
3+ // specify types in the URL for index lookups. This keeps the code consistent.
54
6- // TODO: This will error for property values that are lists.
7- // See https://groups.google.com/forum/#!topic/neo4j/sjH2f5dulTQ
5+ // using closures for clarity
86
9- // Model - Vertex
7+ // TODO: Make this support multiple indices, e.g. an autoindex and a normal index
8+
9+
10+ // Model Proxy - Vertex
1011
1112def create_indexed_vertex (data ,index_name ,keys ) {
12- neo4j = g. getRawGraph()
13- manager = neo4j. index()
14- g. setMaxBufferSize(0 )
15- g. startTransaction()
16- try {
17- index = manager. forNodes(index_name)
18- vertex = neo4j. createNode()
13+ def createIndexedVertex = {
14+ vertex = g. addVertex()
15+ index = g. idx(index_name)
1916 for (entry in data. entrySet()) {
2017 if (entry. value == null ) continue ;
2118 vertex. setProperty(entry. key,entry. value)
2219 if (keys == null || keys. contains(entry. key))
23- index. add(vertex, entry. key,String . valueOf(entry. value))
20+ index. put( entry. key,String . valueOf(entry. value),vertex )
2421 }
25- g. stopTransaction(TransactionalGraph.Conclusion . SUCCESS )
2622 return vertex
27- } catch (e) {
28- g. stopTransaction(TransactionalGraph.Conclusion . FAILURE )
29- return e
3023 }
24+ def transaction = { final Closure closure ->
25+ try {
26+ results = closure();
27+ g. commit();
28+ return results;
29+ } catch (e) {
30+ g. rollback();
31+ throw e;
32+ }
33+ }
34+ return transaction(createIndexedVertex);
3135}
3236
3337
3438def update_indexed_vertex (_id , data , index_name , keys ) {
35- vertex = g. getRawGraph(). getNodeById(_id)
36- manager = g. getRawGraph(). index()
37- g. setMaxBufferSize(0 )
38- g. startTransaction()
39- try {
40- index = manager. forNodes(index_name)
41- index. remove(vertex)
42- for (String key in vertex. getPropertyKeys())
43- vertex. removeProperty(key)
39+ def updateIndexedVertex = {
40+ vertex = g. v(_id);
41+ index = g. idx(index_name);
42+ // remove vertex from index
43+ for (String key in vertex. getPropertyKeys()) {
44+ if (keys == null || keys. contains(key)) {
45+ value = vertex. getProperty(key);
46+ index. remove(key, String . valueOf(value), vertex);
47+ }
48+ }
49+ ElementHelper . removeProperties([vertex]);
50+ ElementHelper . setProperties(vertex,data);
51+ // add vertex to index
4452 for (entry in data. entrySet()) {
4553 if (entry. value == null ) continue ;
46- vertex. setProperty(entry. key,entry. value)
4754 if (keys == null || keys. contains(entry. key))
48- index. add(vertex,entry. key,String . valueOf(entry. value))
55+ index. put(entry. key,String . valueOf(entry. value),vertex);
56+ }
57+ return vertex;
58+ }
59+ def transaction = { final Closure closure ->
60+ try {
61+ results = closure();
62+ g. commit();
63+ return results;
64+ } catch (e) {
65+ g. rollback();
66+ throw e;
4967 }
50- g. stopTransaction(TransactionalGraph.Conclusion . SUCCESS )
51- return vertex
52- } catch (e) {
53- g. stopTransaction(TransactionalGraph.Conclusion . FAILURE )
54- return e
5568 }
69+ return transaction(updateIndexedVertex);
5670}
5771
58- // Model - Edge
72+
73+ // Model Proxy - Edge
5974
6075def create_indexed_edge (outV ,label ,inV ,data ,index_name ,keys ,label_var ) {
61- import org.neo4j.graphdb.DynamicRelationshipType ;
62- neo4j = g. getRawGraph()
63- manager = neo4j. index()
64- vertex = neo4j. getNodeById(outV)
65- relationshipType = DynamicRelationshipType . withName(label)
66- g. setMaxBufferSize(0 )
67- g. startTransaction()
68- try {
69- index = manager. forRelationships(index_name)
70- edge = vertex. createRelationshipTo(neo4j. getNodeById(inV),relationshipType)
76+ def createIndexedEdge = {
77+ index = g. idx(index_name)
78+ edge = g. addEdge(g. v(outV),g. v(inV),label)
7179 for (entry in data. entrySet()) {
7280 if (entry. value == null ) continue ;
7381 edge. setProperty(entry. key,entry. value)
7482 if (keys == null || keys. contains(entry. key))
75- index. add(edge, entry. key,String . valueOf(entry. value))
83+ index. put( entry. key,String . valueOf(entry. value),edge )
7684 }
77- index. add(edge,label_var,String . valueOf(label))
78- g. stopTransaction(TransactionalGraph.Conclusion . SUCCESS )
85+ index. put(label_var,String . valueOf(label),edge)
7986 return edge
80- } catch (e) {
81- g. stopTransaction(TransactionalGraph.Conclusion . FAILURE )
82- return e
8387 }
88+ def transaction = { final Closure closure ->
89+ try {
90+ results = closure();
91+ g. commit();
92+ return results;
93+ } catch (e) {
94+ g. rollback();
95+ throw e;
96+ }
97+ }
98+ return transaction(createIndexedEdge);
8499}
85100
86101// don't need to update indexed label, it can't change
87102def update_indexed_edge (_id , data , index_name , keys ) {
88- neo4j = g. getRawGraph()
89- manager = neo4j. index()
90- edge = neo4j. getRelationshipById(_id)
91- g. setMaxBufferSize(0 )
92- g. startTransaction()
93- try {
94- index = manager. forRelationships(index_name)
95- index. remove(edge)
96- for (String key in edge. getPropertyKeys())
97- edge. removeProperty(key)
103+ def updateIndexedEdge = {
104+ edge = g. e(_id);
105+ index = g. idx(index_name);
106+ for (String key in edge. getPropertyKeys()) {
107+ if (keys == null || keys. contains(key)) {
108+ value = edge. getProperty(key)
109+ index. remove(key, String . valueOf(value), edge);
110+ }
111+ }
112+ ElementHelper . removeProperties([edge]);
113+ ElementHelper . setProperties(edge,data);
98114 for (entry in data. entrySet()) {
99115 if (entry. value == null ) continue ;
100- edge. setProperty(entry. key,entry. value)
101116 if (keys == null || keys. contains(entry. key))
102- index. add(edge,entry. key,String . valueOf(entry. value))
117+ index. put(entry. key,String . valueOf(entry. value),edge)
118+ return edge;
103119 }
104- g. stopTransaction(TransactionalGraph.Conclusion . SUCCESS )
105- return edge
106- } catch (e) {
107- g. stopTransaction(TransactionalGraph.Conclusion . FAILURE )
108- return e
109120 }
110- }
111-
112- // Indices
113-
114- def get_or_create_vertex_index (index_name , config ) {
115- neo4j = g. getRawGraph()
116- manager = g. getRawGraph(). index()
117- index = manager. forNodes(index_name, config)
118- return index
119- }
120-
121- def get_or_create_edge_index (index_name , config ) {
122- neo4j = g. getRawGraph()
123- manager = g. getRawGraph(). index()
124- index = manager. forRelationships(index_name, config)
125- return index
126- }
127-
128- def query_exact_index (index_name , key , query_string ) {
129- // Neo4jTokens.QUERY_HEADER = "%query%"
130- return g. idx(index_name). get(key, Neo4jTokens . QUERY_HEADER + query_string)
131- }
132-
133- // Metadata
134-
135- def get_metadata (key , default_value ) {
136- neo4j = g. getRawGraph();
137- properties = neo4j. getKernelData(). properties();
138- return properties. getProperty(key, default_value);
139- }
140-
141- def set_metadata (key , value ) {
142- g. setMaxBufferSize(0 )
143- g. startTransaction()
144- try {
145- neo4j = g. getRawGraph();
146- properties = neo4j. getKernelData(). properties();
147- resp = properties. setProperty(key, value);
148- g. stopTransaction(TransactionalGraph.Conclusion . SUCCESS )
149- return resp
150- } catch (e) {
151- g. stopTransaction(TransactionalGraph.Conclusion . FAILURE )
152- return e
121+ def transaction = { final Closure closure ->
122+ try {
123+ results = closure();
124+ g. commit();
125+ return results;
126+ } catch (e) {
127+ g. rollback();
128+ throw e;
129+ }
153130 }
131+ return transaction(updateIndexedEdge);
154132}
155-
156- def remove_metadata (key ) {
157- g. setMaxBufferSize(0 )
158- g. startTransaction()
159- try {
160- neo4j = g. getRawGraph();
161- properties = neo4j. getKernelData(). properties();
162- resp = properties. removeProperty(key);
163- g. stopTransaction(TransactionalGraph.Conclusion . SUCCESS )
164- return resp
165- } catch (e) {
166- g. stopTransaction(TransactionalGraph.Conclusion . FAILURE )
167- return e
168- }
169- }
0 commit comments