version 0.6.9
[nit.git] / lib / neo4j / neo4j.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Neo4j connector through its JSON REST API using curl.
16 #
17 # For ease of use and testing this module provide a wrapper to the `neo4j` command:
18 #
19 # # Start the Neo4j server
20 # var srv = new Neo4jServer
21 # assert srv.start_quiet
22 #
23 # In order to connect to Neo4j you need a connector:
24 #
25 # # Create new Neo4j client
26 # var client = new Neo4jClient("http://localhost:7474")
27 # assert client.is_ok
28 #
29 # The fundamental units that form a graph are nodes and relationships.
30 #
31 # Nodes are used to represent entities stored in base:
32 #
33 # # Create a disconnected node
34 # var andres = new NeoNode
35 # andres["name"] = "Andres"
36 # # Connect the node to Neo4j
37 # client.save_node(andres)
38 # assert andres.is_linked
39 # #
40 # # Create a second node
41 # var kate = new NeoNode
42 # kate["name"] = "Kate"
43 # client.save_node(kate)
44 # assert kate.is_linked
45 #
46 # Relationships between nodes are a key part of a graph database.
47 # They allow for finding related data. Just like nodes, relationships can have properties.
48 #
49 # # Create a relationship
50 # var loves = new NeoEdge(andres, "LOVES", kate)
51 # client.save_edge(loves)
52 # assert loves.is_linked
53 #
54 # Nodes can also be loaded fron Neo4j:
55 #
56 # # Get a node from DB and explore edges
57 # var url = andres.url.to_s
58 # var from = client.load_node(url)
59 # assert from["name"].to_s == "Andres"
60 # var to = from.out_nodes("LOVES").first # follow the first LOVES relationship
61 # assert to["name"].to_s == "Kate"
62 #
63 # For more details, see http://docs.neo4j.org/chunked/milestone/rest-api.html
64 module neo4j
65
66 import curl_json
67
68 # Handles Neo4j server start and stop command
69 #
70 # `neo4j` binary must be in `PATH` in order to work
71 class Neo4jServer
72
73 # Start the local Neo4j server instance
74 fun start: Bool do
75 sys.system("neo4j start console")
76 return true
77 end
78
79 # Like `start` but redirect the console output to `/dev/null`
80 fun start_quiet: Bool do
81 sys.system("neo4j start console > /dev/null")
82 return true
83 end
84
85 # Stop the local Neo4j server instance
86 fun stop: Bool do
87 sys.system("neo4j stop")
88 return true
89 end
90
91 # Like `stop` but redirect the console output to `/dev/null`
92 fun stop_quiet: Bool do
93 sys.system("neo4j stop > /dev/null")
94 return true
95 end
96 end
97
98 # `Neo4jClient` is needed to communicate through the REST API
99 #
100 # var client = new Neo4jClient("http://localhost:7474")
101 # assert client.is_ok
102 class Neo4jClient
103
104 # Neo4j REST services baseurl
105 var base_url: String
106 # REST service to get node data
107 private var node_url: String
108 # REST service to batch
109 private var batch_url: String
110 # REST service to send cypher requests
111 private var cypher_url: String
112
113 private var curl = new Curl
114
115 init(base_url: String) do
116 self.base_url = base_url
117 var root = service_root
118 if not root isa JsonObject then
119 print "Neo4jClientError: cannot connect to server at {base_url}"
120 abort
121 end
122 self.node_url = root["node"].to_s
123 self.batch_url = root["batch"].to_s
124 self.cypher_url = root["cypher"].to_s
125 end
126
127 fun service_root: Jsonable do return get("{base_url}/db/data")
128
129 # Is the connection with the Neo4j server ok?
130 fun is_ok: Bool do return service_root isa JsonObject
131
132 # Empty the graph
133 fun clear_graph do
134 cypher(new CypherQuery.from_string("MATCH (n) OPTIONAL MATCH n-[r]-() DELETE r, n"))
135 end
136
137 # Last errors
138 var errors = new Array[String]
139
140 # Nodes view stored locally
141 private var local_nodes = new HashMap[String, nullable NeoNode]
142
143 # Save the node in base
144 #
145 # var client = new Neo4jClient("http://localhost:7474")
146 # #
147 # # Create a node
148 # var andres = new NeoNode
149 # andres["name"] = "Andres"
150 # client.save_node(andres)
151 # assert andres.is_linked
152 #
153 # Once linked, nodes cannot be created twice:
154 #
155 # var oldurl = andres.url
156 # client.save_node(andres) # do nothing
157 # assert andres.url == oldurl
158 fun save_node(node: NeoNode): Bool do
159 if node.is_linked then return true
160 node.neo = self
161 var batch = new NeoBatch(self)
162 batch.save_node(node)
163 # batch.create_edges(node.out_edges)
164 var errors = batch.execute
165 if not errors.is_empty then
166 errors.add_all errors
167 return false
168 end
169 local_nodes[node.url.to_s] = node
170 return true
171 end
172
173 # Load a node from base
174 # Data, labels and edges will be loaded lazily.
175 fun load_node(url: String): NeoNode do
176 if local_nodes.has_key(url) then
177 var node = local_nodes[url]
178 if node != null then return node
179 end
180 var node = new NeoNode.from_neo(self, url)
181 local_nodes[url] = node
182 return node
183 end
184
185 # Remove the entity from base
186 fun delete_node(node: NeoNode): Bool do
187 if not node.is_linked then return false
188 var url = node.url.to_s
189 delete(url)
190 local_nodes[url] = null
191 node.url = null
192 return true
193 end
194
195 # Edges view stored locally
196 private var local_edges = new HashMap[String, nullable NeoEdge]
197
198 # Save the edge in base
199 # From and to nodes will be created.
200 #
201 # var client = new Neo4jClient("http://localhost:7474")
202 # #
203 # var andres = new NeoNode
204 # var kate = new NeoNode
205 # var edge = new NeoEdge(andres, "LOVES", kate)
206 # client.save_edge(edge)
207 # assert andres.is_linked
208 # assert kate.is_linked
209 # assert edge.is_linked
210 fun save_edge(edge: NeoEdge): Bool do
211 if edge.is_linked then return true
212 edge.neo = self
213 edge.from.out_edges.add edge
214 edge.to.in_edges.add edge
215 var batch = new NeoBatch(self)
216 batch.save_edge(edge)
217 var errors = batch.execute
218 if not errors.is_empty then
219 errors.add_all errors
220 return false
221 end
222 local_edges[edge.url.to_s] = edge
223 return true
224 end
225
226 # Load a edge from base
227 # Data will be loaded lazily.
228 fun load_edge(url: String): NeoEdge do
229 if local_edges.has_key(url) then
230 var node = local_edges[url]
231 if node != null then return node
232 end
233 var edge = new NeoEdge.from_neo(self, url)
234 local_edges[url] = edge
235 return edge
236 end
237
238 # Remove the edge from base
239 fun delete_edge(edge: NeoEdge): Bool do
240 if not edge.is_linked then return false
241 var url = edge.url.to_s
242 delete(url)
243 local_edges[url] = null
244 edge.url = null
245 return true
246 end
247
248 # Retrieve all nodes with specified `lbl`
249 #
250 # var client = new Neo4jClient("http://localhost:7474")
251 # #
252 # var andres = new NeoNode
253 # andres.labels.add_all(["Human", "Male"])
254 # client.save_node(andres)
255 # var kate = new NeoNode
256 # kate.labels.add_all(["Human", "Female"])
257 # client.save_node(kate)
258 # #
259 # var nodes = client.nodes_with_label("Human")
260 # assert nodes.has(andres)
261 # assert nodes.has(kate)
262 fun nodes_with_label(lbl: String): Array[NeoNode] do
263 var res = get("{base_url}/db/data/label/{lbl}/nodes")
264 var nodes = new Array[NeoNode]
265 for json in res.as(JsonArray) do
266 var obj = json.as(JsonObject)
267 var node = load_node(obj["self"].to_s)
268 node.internal_properties = obj["data"].as(JsonObject)
269 nodes.add node
270 end
271 return nodes
272 end
273
274 # Retrieve nodes belonging to all the specified `labels`.
275 #
276 # var client = new Neo4jClient("http://localhost:7474")
277 # #
278 # var andres = new NeoNode
279 # andres.labels.add_all(["Human", "Male"])
280 # client.save_node(andres)
281 # var kate = new NeoNode
282 # kate.labels.add_all(["Human", "Female"])
283 # client.save_node(kate)
284 # #
285 # var nodes = client.nodes_with_labels(["Human", "Male"])
286 # assert nodes.has(andres)
287 # assert not nodes.has(kate)
288 fun nodes_with_labels(labels: Array[String]): Array[NeoNode] do
289 assert not labels.is_empty
290 var res = cypher(new CypherQuery.from_string("MATCH (n:{labels.join(":")}) RETURN n"))
291 var nodes = new Array[NeoNode]
292 for json in res.as(JsonObject)["data"].as(JsonArray) do
293 var obj = json.as(JsonArray).first.as(JsonObject)
294 var node = load_node(obj["self"].to_s)
295 node.internal_properties = obj["data"].as(JsonObject)
296 nodes.add node
297 end
298 return nodes
299 end
300
301 # Perform a `CypherQuery`
302 # see: CypherQuery
303 fun cypher(query: CypherQuery): Jsonable do
304 return post("{cypher_url}", query.to_json)
305 end
306
307 # GET JSON data from `url`
308 fun get(url: String): Jsonable do
309 var request = new JsonGET(url, curl)
310 var response = request.execute
311 return parse_response(response)
312 end
313
314 # POST `params` to `url`
315 fun post(url: String, params: Jsonable): Jsonable do
316 var request = new JsonPOST(url, curl)
317 request.data = params
318 var response = request.execute
319 return parse_response(response)
320 end
321
322 # PUT `params` at `url`
323 fun put(url: String, params: Jsonable): Jsonable do
324 var request = new JsonPUT(url, curl)
325 request.data = params
326 var response = request.execute
327 return parse_response(response)
328 end
329
330 # DELETE `url`
331 fun delete(url: String): Jsonable do
332 var request = new JsonDELETE(url, curl)
333 var response = request.execute
334 return parse_response(response)
335 end
336
337 # Parse the cURL `response` as a JSON string
338 private fun parse_response(response: CurlResponse): Jsonable do
339 if response isa CurlResponseSuccess then
340 if response.body_str.is_empty then
341 return new JsonObject
342 else
343 var str = response.body_str
344 var res = str.to_jsonable
345 if res == null then
346 # empty response wrap it in empty object
347 return new JsonObject
348 else if res isa JsonObject and res.has_key("exception") then
349 var error = "Neo4jError::{res["exception"] or else "null"}"
350 var msg = ""
351 if res.has_key("message") then
352 msg = res["message"].to_s
353 end
354 return new JsonError(error, msg.to_json)
355 else
356 return res
357 end
358 end
359 else if response isa CurlResponseFailed then
360 return new JsonError("Curl error", "{response.error_msg} ({response.error_code})")
361 else
362 return new JsonError("Curl error", "Unexpected response '{response}'")
363 end
364 end
365 end
366
367 # A Cypher query for Neo4j REST API
368 #
369 # The Neo4j REST API allows querying with Cypher.
370 # The results are returned as a list of string headers (columns), and a data part,
371 # consisting of a list of all rows, every row consisting of a list of REST representations
372 # of the field value - Node, Relationship, Path or any simple value like String.
373 #
374 # Example:
375 #
376 # var client = new Neo4jClient("http://localhost:7474")
377 # var query = new CypherQuery
378 # query.nmatch("(n)-[r:LOVES]->(m)")
379 # query.nwhere("n.name=\"Andres\"")
380 # query.nreturn("m.name")
381 # var res = client.cypher(query).as(JsonObject)
382 # assert res["data"].as(JsonArray).first.as(JsonArray).first == "Kate"
383 #
384 # For more details, see: http://docs.neo4j.org/chunked/milestone/rest-api-cypher.html
385 class CypherQuery
386 # Query string to perform
387 private var query: String = ""
388
389 # `params` to embed in the query like in prepared statements
390 var params = new JsonObject
391
392 init do end
393
394 # init the query from a query string
395 init from_string(query: String) do
396 self.query = query
397 end
398
399 # init the query with parameters
400 init with_params(params: JsonObject) do
401 self.params = params
402 end
403
404 # Add a `CREATE` statement to the query
405 fun ncreate(query: String): CypherQuery do
406 self.query = "{self.query}CREATE {query} "
407 return self
408 end
409
410 # Add a `START` statement to the query
411 fun nstart(query: String): CypherQuery do
412 self.query = "{self.query}START {query} "
413 return self
414 end
415
416 # Add a `MATCH` statement to the query
417 fun nmatch(query: String): CypherQuery do
418 self.query = "{self.query}MATCH {query} "
419 return self
420 end
421
422 # Add a `WHERE` statement to the query
423 fun nwhere(query: String): CypherQuery do
424 self.query = "{self.query}WHERE {query} "
425 return self
426 end
427
428 # Add a `AND` statement to the query
429 fun nand(query: String): CypherQuery do
430 self.query = "{self.query}AND {query} "
431 return self
432 end
433
434 # Add a `RETURN` statement to the query
435 fun nreturn(query: String): CypherQuery do
436 self.query = "{self.query}RETURN {query} "
437 return self
438 end
439
440 # Translate the query to JSON
441 fun to_json: JsonObject do
442 var obj = new JsonObject
443 obj["query"] = query
444 if not params.is_empty then
445 obj["params"] = params
446 end
447 return obj
448 end
449
450 redef fun to_s do return to_json.to_s
451 end
452
453 # The fundamental units that form a graph are nodes and relationships.
454 #
455 # Entities can have two states:
456 #
457 # * linked: the NeoEntity references an existing node or edge in Neo4j
458 # * unlinked: the NeoEntity is not yet created in Neo4j
459 #
460 # If the entity is initialized unlinked from neo4j:
461 #
462 # # Create a disconnected node
463 # var andres = new NeoNode
464 # andres["name"] = "Andres"
465 # # At this point, the node is not linked
466 # assert not andres.is_linked
467 #
468 # Then we can link the entity to the base:
469 #
470 # # Init client
471 # var client = new Neo4jClient("http://localhost:7474")
472 # client.save_node(andres)
473 # # The node is now linked
474 # assert andres.is_linked
475 #
476 # Entities can also be loaded from Neo4j:
477 #
478 # # Get a node from Neo4j
479 # var url = andres.url.to_s
480 # var node = client.load_node(url)
481 # assert node.is_linked
482 #
483 # When working in connected mode, all reading operations are executed lazily on the base:
484 #
485 # # Get the node `name` property
486 # assert node["name"] == "Andres" # loaded lazily from base
487 abstract class NeoEntity
488 # Neo4j client connector
489 private var neo: Neo4jClient is noinit
490
491 # Entity unique URL in Neo4j REST API
492 var url: nullable String = null
493
494 # Temp id used in batch mode to update the entity
495 private var batch_id: nullable Int = null
496
497 # Load the entity from base
498 private init from_neo(neo: Neo4jClient, url: String) do
499 self.neo = neo
500 self.url = url
501 end
502
503 # Init entity from JSON representation
504 private init from_json(neo: Neo4jClient, obj: JsonObject) do
505 self.neo = neo
506 self.url = obj["self"].to_s
507 self.internal_properties = obj["data"].as(JsonObject)
508 end
509
510 # Create a empty (and not-connected) entity
511 init do
512 self.internal_properties = new JsonObject
513 end
514
515 # Is the entity linked to a Neo4j database?
516 fun is_linked: Bool do return url != null
517
518 # In Neo4j, both nodes and relationships can contain properties.
519 # Properties are key-value pairs where the key is a string.
520 # Property values are JSON formatted.
521 #
522 # Properties are loaded lazily
523 fun properties: JsonObject do return internal_properties or else load_properties
524
525 private var internal_properties: nullable JsonObject = null
526
527 private fun load_properties: JsonObject do
528 var obj = neo.get("{url.to_s}/properties").as(JsonObject)
529 internal_properties = obj
530 return obj
531 end
532
533 # Get the entity `id` if connected to base
534 fun id: nullable Int do
535 if url == null then return null
536 return url.split("/").last.to_i
537 end
538
539 # Get the entity property at `key`
540 fun [](key: String): nullable Jsonable do
541 if not properties.has_key(key) then return null
542 return properties[key]
543 end
544
545 # Set the entity property `value` at `key`
546 fun []=(key: String, value: nullable Jsonable) do properties[key] = value
547
548 # Is the property `key` set?
549 fun has_key(key: String): Bool do return properties.has_key(key)
550
551 # Translate `self` to JSON
552 fun to_json: JsonObject do return properties
553 end
554
555 # Nodes are used to represent entities stored in base.
556 # Apart from properties and relationships (edges),
557 # nodes can also be labeled with zero or more labels.
558 #
559 # A label is a `String` that is used to group nodes into sets.
560 # All nodes labeled with the same label belongs to the same set.
561 # A node may be labeled with any number of labels, including none,
562 # making labels an optional addition to the graph.
563 #
564 # Creating new nodes:
565 #
566 # var client = new Neo4jClient("http://localhost:7474")
567 # #
568 # var andres = new NeoNode
569 # andres.labels.add "Person"
570 # andres["name"] = "Andres"
571 # andres["age"] = 22
572 # client.save_node(andres)
573 # assert andres.is_linked
574 #
575 # Get nodes from Neo4j:
576 #
577 # var url = andres.url.to_s
578 # var node = client.load_node(url)
579 # assert node["name"] == "Andres"
580 # assert node["age"].to_s.to_i == 22
581 class NeoNode
582 super NeoEntity
583
584 private var internal_labels: nullable Array[String] = null
585 private var internal_in_edges: nullable List[NeoEdge] = null
586 private var internal_out_edges: nullable List[NeoEdge] = null
587
588 init do
589 super
590 self.internal_labels = new Array[String]
591 self.internal_in_edges = new List[NeoEdge]
592 self.internal_out_edges = new List[NeoEdge]
593 end
594
595 redef fun to_s do
596 var tpl = new FlatBuffer
597 tpl.append "\{"
598 tpl.append "labels: [{labels.join(", ")}],"
599 tpl.append "data: {to_json}"
600 tpl.append "\}"
601 return tpl.write_to_string
602 end
603
604 # A label is a `String` that is used to group nodes into sets.
605 # A node may be labeled with any number of labels, including none.
606 # All nodes labeled with the same label belongs to the same set.
607 #
608 # Many database queries can work with these sets instead of the whole graph,
609 # making queries easier to write and more efficient.
610 #
611 # Labels are loaded lazily
612 fun labels: Array[String] do return internal_labels or else load_labels
613
614 private fun load_labels: Array[String] do
615 var labels = new Array[String]
616 var res = neo.get("{url.to_s}/labels")
617 if res isa JsonArray then
618 for val in res do labels.add val.to_s
619 end
620 internal_labels = labels
621 return labels
622 end
623
624 # Get the list of `NeoEdge` pointing to `self`
625 #
626 # Edges are loaded lazily
627 fun in_edges: List[NeoEdge] do return internal_in_edges or else load_in_edges
628
629 private fun load_in_edges: List[NeoEdge] do
630 var edges = new List[NeoEdge]
631 var res = neo.get("{url.to_s}/relationships/in").as(JsonArray)
632 for obj in res do
633 edges.add(new NeoEdge.from_json(neo, obj.as(JsonObject)))
634 end
635 internal_in_edges = edges
636 return edges
637 end
638
639 # Get the list of `NeoEdge` pointing from `self`
640 #
641 # Edges are loaded lazily
642 fun out_edges: List[NeoEdge] do return internal_out_edges or else load_out_edges
643
644 private fun load_out_edges: List[NeoEdge] do
645 var edges = new List[NeoEdge]
646 var res = neo.get("{url.to_s}/relationships/out")
647 for obj in res.as(JsonArray) do
648 edges.add(new NeoEdge.from_json(neo, obj.as(JsonObject)))
649 end
650 internal_out_edges = edges
651 return edges
652 end
653
654 # Get nodes pointed by `self` following a `rel_type` edge
655 fun out_nodes(rel_type: String): Array[NeoNode] do
656 var res = new Array[NeoNode]
657 for edge in out_edges do
658 if edge.rel_type == rel_type then res.add edge.to
659 end
660 return res
661 end
662
663 # Get nodes pointing to `self` following a `rel_type` edge
664 fun in_nodes(rel_type: String): Array[NeoNode] do
665 var res = new Array[NeoNode]
666 for edge in in_edges do
667 if edge.rel_type == rel_type then res.add edge.from
668 end
669 return res
670 end
671 end
672
673 # A relationship between two nodes.
674 # Relationships between nodes are a key part of a graph database.
675 # They allow for finding related data. Just like nodes, relationships can have properties.
676 #
677 # Create a relationship:
678 #
679 # var client = new Neo4jClient("http://localhost:7474")
680 # # Create nodes
681 # var andres = new NeoNode
682 # andres["name"] = "Andres"
683 # var kate = new NeoNode
684 # kate["name"] = "Kate"
685 # # Create a relationship of type `LOVES`
686 # var loves = new NeoEdge(andres, "LOVES", kate)
687 # client.save_edge(loves)
688 # assert loves.is_linked
689 #
690 # Get an edge from DB:
691 #
692 # var url = loves.url.to_s
693 # var edge = client.load_edge(url)
694 # assert edge.from["name"].to_s == "Andres"
695 # assert edge.to["name"].to_s == "Kate"
696 class NeoEdge
697 super NeoEntity
698
699 private var internal_from: nullable NeoNode
700 private var internal_to: nullable NeoNode
701 private var internal_type: nullable String
702 private var internal_from_url: nullable String
703 private var internal_to_url: nullable String
704
705 init(from: NeoNode, rel_type: String, to: NeoNode) do
706 self.internal_from = from
707 self.internal_to = to
708 self.internal_type = rel_type
709 end
710
711 redef init from_neo(neo, url) do
712 super
713 var obj = neo.get(url).as(JsonObject)
714 self.internal_type = obj["type"].to_s
715 self.internal_from_url = obj["start"].to_s
716 self.internal_to_url = obj["end"].to_s
717 end
718
719 redef init from_json(neo, obj) do
720 super
721 self.internal_type = obj["type"].to_s
722 self.internal_from_url = obj["start"].to_s
723 self.internal_to_url = obj["end"].to_s
724 end
725
726 # Get `from` node
727 fun from: NeoNode do return internal_from or else load_from
728
729 private fun load_from: NeoNode do
730 var node = neo.load_node(internal_from_url.to_s)
731 internal_from = node
732 return node
733 end
734
735 # Get `to` node
736 fun to: NeoNode do return internal_to or else load_to
737
738 private fun load_to: NeoNode do
739 var node = neo.load_node(internal_to_url.to_s)
740 internal_to = node
741 return node
742 end
743
744 # Get edge type
745 fun rel_type: nullable String do return internal_type
746
747 redef fun to_json do
748 var obj = new JsonObject
749 if to.is_linked then
750 obj["to"] = to.url
751 else
752 obj["to"] = "\{{to.batch_id.to_s}\}"
753 end
754 obj["type"] = rel_type
755 obj["data"] = properties
756 return obj
757 end
758 end
759
760 # Batches are used to perform multiple operations on the REST API in one cURL request.
761 # This can significantly improve performance for large insert and update operations.
762 #
763 # see: http://docs.neo4j.org/chunked/milestone/rest-api-batch-ops.html
764 #
765 # This service is transactional.
766 # If any of the operations performed fails (returns a non-2xx HTTP status code),
767 # the transaction will be rolled back and all changes will be undone.
768 #
769 # Example:
770 #
771 # var client = new Neo4jClient("http://localhost:7474")
772 # #
773 # var node1 = new NeoNode
774 # var node2 = new NeoNode
775 # var edge = new NeoEdge(node1, "TO", node2)
776 # #
777 # var batch = new NeoBatch(client)
778 # batch.save_node(node1)
779 # batch.save_node(node2)
780 # batch.save_edge(edge)
781 # batch.execute
782 # #
783 # assert node1.is_linked
784 # assert node2.is_linked
785 # assert edge.is_linked
786 class NeoBatch
787
788 # Neo4j client connector
789 var client: Neo4jClient
790
791 # Jobs to perform in this batch
792 #
793 # The batch service expects an array of job descriptions as input,
794 # each job description describing an action to be performed via the normal server API.
795 var jobs = new HashMap[Int, NeoJob]
796
797 # Append a new job to the batch in JSON Format
798 # see `NeoJob`
799 fun new_job(nentity: NeoEntity): NeoJob do
800 var id = jobs.length
801 var job = new NeoJob(id, nentity)
802 jobs[id] = job
803 return job
804 end
805
806 # Load a node in batch mode also load labels, data and edges
807 fun load_node(node: NeoNode) do
808 var job = new_job(node)
809 job.action = load_node_data_action
810 job.method = "GET"
811 if node.id != null then
812 job.to = "/node/{node.id.to_s}"
813 else
814 job.to = "\{{node.batch_id.to_s}\}"
815 end
816 job = new_job(node)
817 job.action = load_node_labels_action
818 job.method = "GET"
819 if node.id != null then
820 job.to = "/node/{node.id.to_s}/labels"
821 else
822 job.to = "\{{node.batch_id.to_s}\}/labels"
823 end
824 end
825
826 # Load in and out edges into node
827 fun load_node_edges(node: NeoNode) do
828 var job = new_job(node)
829 job.action = load_node_in_edges_action
830 job.method = "GET"
831 if node.id != null then
832 job.to = "/node/{node.id.to_s}/relationships/in"
833 else
834 job.to = "\{{node.batch_id.to_s}\}/relationships/in"
835 end
836 job = new_job(node)
837 job.action = load_node_out_edges_action
838 job.method = "GET"
839 if node.id != null then
840 job.to = "/node/{node.id.to_s}/relationships/out"
841 else
842 job.to = "\{{node.batch_id.to_s}\}/relationships/out"
843 end
844 end
845
846 # Create a `NeoNode` or a `NeoEdge` in batch mode.
847 fun save_entity(nentity: NeoEntity) do
848 if nentity isa NeoNode then
849 save_node(nentity)
850 else if nentity isa NeoEdge then
851 save_edge(nentity)
852 else abort
853 end
854
855 # Create a node in batch mode also create labels and edges
856 fun save_node(node: NeoNode) do
857 if node.id != null or node.batch_id != null then return
858 # create node
859 var job = new_job(node)
860 node.batch_id = job.id
861 job.action = create_node_action
862 job.method = "POST"
863 job.to = "/node"
864 job.body = node.properties
865 # add labels
866 job = new_job(node)
867 job.method = "POST"
868 job.to = "\{{node.batch_id.to_s}\}/labels"
869 job.body = new JsonArray.from(node.labels)
870 # add edges
871 #save_edges(node.out_edges)
872 end
873
874 # Create multiple nodes
875 # also create labels and edges
876 fun save_nodes(nodes: Collection[NeoNode]) do for node in nodes do save_node(node)
877
878 # Create an edge
879 # nodes `edge.from` and `edge.to` will be created if not in base
880 fun save_edge(edge: NeoEdge) do
881 if edge.id != null or edge.batch_id != null then return
882 # create nodes
883 save_node(edge.from)
884 save_node(edge.to)
885 # create edge
886 var job = new_job(edge)
887 edge.batch_id = job.id
888 job.action = create_edge_action
889 job.method = "POST"
890 if edge.from.id != null then
891 job.to = "/node/{edge.from.id.to_s}/relationships"
892 else
893 job.to = "\{{edge.from.batch_id.to_s}\}/relationships"
894 end
895 job.body = edge.to_json
896 end
897
898 # Create multiple edges
899 fun save_edges(edges: Collection[NeoEdge]) do for edge in edges do save_edge(edge)
900
901 # Execute the batch and update local nodes
902 fun execute: List[JsonError] do
903 var request = new JsonPOST(client.batch_url, client.curl)
904 # request.headers["X-Stream"] = "true"
905 var json_jobs = new JsonArray
906 for job in jobs.values do json_jobs.add job.to_json
907 request.data = json_jobs
908 var response = request.execute
909 var res = client.parse_response(response)
910 return finalize_batch(res)
911 end
912
913 # Associate data from response in original nodes and edges
914 private fun finalize_batch(response: Jsonable): List[JsonError] do
915 var errors = new List[JsonError]
916 if not response isa JsonArray then
917 errors.add(new JsonError("Neo4jError", "Unexpected batch response format"))
918 return errors
919 end
920 # print " {res.length} jobs executed"
921 for res in response do
922 if not res isa JsonObject then
923 errors.add(new JsonError("Neo4jError", "Unexpected job format in batch response"))
924 continue
925 end
926 var id = res["id"].as(Int)
927 var job = jobs[id]
928 if job.action == create_node_action then
929 var node = job.entity.as(NeoNode)
930 node.batch_id = null
931 node.url = res["location"].to_s
932 else if job.action == create_edge_action then
933 var edge = job.entity.as(NeoEdge)
934 edge.batch_id = null
935 edge.url = res["location"].to_s
936 else if job.action == load_node_data_action then
937 var node = job.entity.as(NeoNode)
938 node.internal_properties = res["body"].as(JsonObject)["data"].as(JsonObject)
939 else if job.action == load_node_labels_action then
940 var node = job.entity.as(NeoNode)
941 var labels = new Array[String]
942 for l in res["body"].as(JsonArray) do labels.add l.to_s
943 node.internal_labels = labels
944 else if job.action == load_node_in_edges_action then
945 var node = job.entity.as(NeoNode)
946 var edges = res["body"].as(JsonArray)
947 node.internal_in_edges = new List[NeoEdge]
948 for edge in edges do
949 node.internal_in_edges.add client.load_edge(edge.as(JsonObject)["self"].to_s)
950 end
951 else if job.action == load_node_out_edges_action then
952 var node = job.entity.as(NeoNode)
953 var edges = res["body"].as(JsonArray)
954 node.internal_out_edges = new List[NeoEdge]
955 for edge in edges do
956 node.internal_out_edges.add client.load_edge(edge.as(JsonObject)["self"].to_s)
957 end
958 end
959 end
960 return errors
961 end
962
963 # JobActions
964 # TODO replace with enum
965
966 private fun create_node_action: Int do return 1
967 private fun create_edge_action: Int do return 2
968 private fun load_node_data_action: Int do return 3
969 private fun load_node_labels_action: Int do return 4
970 private fun load_node_in_edges_action: Int do return 5
971 private fun load_node_out_edges_action: Int do return 6
972 end
973
974 # A job that can be executed in a `NeoBatch`
975 # This is a representation of a neo job in JSON Format
976 #
977 # Each job description should contain a `to` attribute, with a value relative to the data API root
978 # (so http://localhost:7474/db/data/node becomes just /node), and a `method` attribute containing
979 # HTTP verb to use.
980 #
981 # Optionally you may provide a `body` attribute, and an `id` attribute to help you keep track
982 # of responses, although responses are guaranteed to be returned in the same order the job
983 # descriptions are received.
984 class NeoJob
985 # The job uniq `id`
986 var id: Int
987 # Entity targeted by the job
988 var entity: NeoEntity
989
990 init(id: Int, entity: NeoEntity) do
991 self.id = id
992 self.entity = entity
993 end
994
995 # What kind of action do the job
996 # used to attach responses to original Neo objets
997 private var action: nullable Int = null
998
999 # Job HTTP method: `GET`, `POST`, `PUT`, `DELETE`...
1000 var method: String
1001 # Job service target: `/node`, `/labels` etc...
1002 var to: String
1003 # Body to send with the job service request
1004 var body: nullable Jsonable = null
1005
1006 # JSON formated job
1007 fun to_json: JsonObject do
1008 var job = new JsonObject
1009 job["id"] = id
1010 job["method"] = method
1011 job["to"] = to
1012 if not body == null then
1013 job["body"] = body
1014 end
1015 return job
1016 end
1017 end
1018