1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Neo4j connector through its JSON REST API using curl.
17 # For ease of use and testing this module provide a wrapper to the `neo4j` command:
19 # # Start the Neo4j server
20 # var srv = new Neo4jServer
21 # assert srv.start_quiet
23 # In order to connect to Neo4j you need a connector:
25 # # Create new Neo4j client
26 # var client = new Neo4jClient("http://localhost:7474")
29 # The fundamental units that form a graph are nodes and relationships.
31 # Nodes are used to represent entities stored in base:
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
40 # # Create a second node
41 # var kate = new NeoNode
42 # kate["name"] = "Kate"
43 # client.save_node(kate)
44 # assert kate.is_linked
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.
49 # # Create a relationship
50 # var loves = new NeoEdge(andres, "LOVES", kate)
51 # client.save_edge(loves)
52 # assert loves.is_linked
54 # Nodes can also be loaded fron Neo4j:
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"
63 # For more details, see http://docs.neo4j.org/chunked/milestone/rest-api.html
68 # Handles Neo4j server start and stop command
70 # `neo4j` binary must be in `PATH` in order to work
73 # Start the local Neo4j server instance
75 sys
.system
("neo4j start console")
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")
85 # Stop the local Neo4j server instance
87 sys
.system
("neo4j stop")
91 # Like `stop` but redirect the console output to `/dev/null`
92 fun stop_quiet
: Bool do
93 sys
.system
("neo4j stop > /dev/null")
98 # `Neo4jClient` is needed to communicate through the REST API
100 # var client = new Neo4jClient("http://localhost:7474")
101 # assert client.is_ok
104 # Neo4j REST services baseurl
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
113 private var curl
= new Curl
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}"
122 self.node_url
= root
["node"].to_s
123 self.batch_url
= root
["batch"].to_s
124 self.cypher_url
= root
["cypher"].to_s
127 fun service_root
: Jsonable do return get
("{base_url}/db/data")
129 # Is the connection with the Neo4j server ok?
130 fun is_ok
: Bool do return service_root
isa JsonObject
134 cypher
(new CypherQuery.from_string
("MATCH (n) OPTIONAL MATCH n-[r]-() DELETE r, n"))
138 var errors
= new Array[String]
140 # Nodes view stored locally
141 private var local_nodes
= new HashMap[String, nullable NeoNode]
143 # Save the node in base
145 # var client = new Neo4jClient("http://localhost:7474")
148 # var andres = new NeoNode
149 # andres["name"] = "Andres"
150 # client.save_node(andres)
151 # assert andres.is_linked
153 # Once linked, nodes cannot be created twice:
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
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
169 local_nodes
[node
.url
.to_s
] = node
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
180 var node
= new NeoNode.from_neo
(self, url
)
181 local_nodes
[url
] = node
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
190 local_nodes
[url
] = null
195 # Edges view stored locally
196 private var local_edges
= new HashMap[String, nullable NeoEdge]
198 # Save the edge in base
199 # From and to nodes will be created.
201 # var client = new Neo4jClient("http://localhost:7474")
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
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
222 local_edges
[edge
.url
.to_s
] = edge
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
233 var edge
= new NeoEdge.from_neo
(self, url
)
234 local_edges
[url
] = edge
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
243 local_edges
[url
] = null
248 # Retrieve all nodes with specified `lbl`
250 # var client = new Neo4jClient("http://localhost:7474")
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)
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)
274 # Retrieve nodes belonging to all the specified `labels`.
276 # var client = new Neo4jClient("http://localhost:7474")
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)
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)
301 # Perform a `CypherQuery`
303 fun cypher
(query
: CypherQuery): Jsonable do
304 return post
("{cypher_url}", query
.to_json
)
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
)
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
)
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
)
331 fun delete
(url
: String): Jsonable do
332 var request
= new JsonDELETE(url
, curl
)
333 var response
= request
.execute
334 return parse_response
(response
)
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
343 var str
= response
.body_str
344 var res
= str
.to_jsonable
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"}"
351 if res
.has_key
("message") then
352 msg
= res
["message"].to_s
354 return new JsonError(error
, msg
.to_json
)
359 else if response
isa CurlResponseFailed then
360 return new JsonError("Curl error", "{response.error_msg} ({response.error_code})")
362 return new JsonError("Curl error", "Unexpected response '{response}'")
367 # A Cypher query for Neo4j REST API
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.
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"
384 # For more details, see: http://docs.neo4j.org/chunked/milestone/rest-api-cypher.html
386 # Query string to perform
387 private var query
: String
389 # `params` to embed in the query like in prepared statements
390 var params
= new JsonObject
396 # init the query from a query string
397 init from_string
(query
: String) do
401 # init the query with parameters
402 init with_params
(params
: JsonObject) do
406 # Add a `CREATE` statement to the query
407 fun ncreate
(query
: String): CypherQuery do
408 self.query
= "{self.query}CREATE {query} "
412 # Add a `START` statement to the query
413 fun nstart
(query
: String): CypherQuery do
414 self.query
= "{self.query}START {query} "
418 # Add a `MATCH` statement to the query
419 fun nmatch
(query
: String): CypherQuery do
420 self.query
= "{self.query}MATCH {query} "
424 # Add a `WHERE` statement to the query
425 fun nwhere
(query
: String): CypherQuery do
426 self.query
= "{self.query}WHERE {query} "
430 # Add a `AND` statement to the query
431 fun nand
(query
: String): CypherQuery do
432 self.query
= "{self.query}AND {query} "
436 # Add a `RETURN` statement to the query
437 fun nreturn
(query
: String): CypherQuery do
438 self.query
= "{self.query}RETURN {query} "
442 # Translate the query to JSON
443 fun to_json
: JsonObject do
444 var obj
= new JsonObject
446 if not params
.is_empty
then
447 obj
["params"] = params
452 redef fun to_s
do return to_json
.to_s
455 # The fundamental units that form a graph are nodes and relationships.
457 # Entities can have two states:
459 # * linked: the NeoEntity references an existing node or edge in Neo4j
460 # * unlinked: the NeoEntity is not yet created in Neo4j
462 # If the entity is initialized unlinked from neo4j:
464 # # Create a disconnected node
465 # var andres = new NeoNode
466 # andres["name"] = "Andres"
467 # # At this point, the node is not linked
468 # assert not andres.is_linked
470 # Then we can link the entity to the base:
473 # var client = new Neo4jClient("http://localhost:7474")
474 # client.save_node(andres)
475 # # The node is now linked
476 # assert andres.is_linked
478 # Entities can also be loaded from Neo4j:
480 # # Get a node from Neo4j
481 # var url = andres.url.to_s
482 # var node = client.load_node(url)
483 # assert node.is_linked
485 # When working in connected mode, all reading operations are executed lazily on the base:
487 # # Get the node `name` property
488 # assert node["name"] == "Andres" # loaded lazily from base
489 abstract class NeoEntity
490 # Neo4j client connector
491 private var neo
: Neo4jClient
493 # Entity unique URL in Neo4j REST API
494 var url
: nullable String
496 # Temp id used in batch mode to update the entity
497 private var batch_id
: nullable Int = null
499 # Load the entity from base
500 private init from_neo
(neo
: Neo4jClient, url
: String) do
505 # Init entity from JSON representation
506 private init from_json
(neo
: Neo4jClient, obj
: JsonObject) do
508 self.url
= obj
["self"].to_s
509 self.internal_properties
= obj
["data"].as(JsonObject)
512 # Create a empty (and not-connected) entity
514 self.internal_properties
= new JsonObject
517 # Is the entity linked to a Neo4j database?
518 fun is_linked
: Bool do return url
!= null
520 # In Neo4j, both nodes and relationships can contain properties.
521 # Properties are key-value pairs where the key is a string.
522 # Property values are JSON formatted.
524 # Properties are loaded lazily
525 fun properties
: JsonObject do return internal_properties
or else load_properties
527 private var internal_properties
: nullable JsonObject = null
529 private fun load_properties
: JsonObject do
530 var obj
= neo
.get
("{url.to_s}/properties").as(JsonObject)
531 internal_properties
= obj
535 # Get the entity `id` if connected to base
536 fun id
: nullable Int do
537 if url
== null then return null
538 return url
.split
("/").last
.to_i
541 # Get the entity property at `key`
542 fun [](key
: String): nullable Jsonable do
543 if not properties
.has_key
(key
) then return null
544 return properties
[key
]
547 # Set the entity property `value` at `key`
548 fun []=(key
: String, value
: nullable Jsonable) do properties
[key
] = value
550 # Is the property `key` set?
551 fun has_key
(key
: String): Bool do return properties
.has_key
(key
)
553 # Translate `self` to JSON
554 fun to_json
: JsonObject do return properties
557 # Nodes are used to represent entities stored in base.
558 # Apart from properties and relationships (edges),
559 # nodes can also be labeled with zero or more labels.
561 # A label is a `String` that is used to group nodes into sets.
562 # All nodes labeled with the same label belongs to the same set.
563 # A node may be labeled with any number of labels, including none,
564 # making labels an optional addition to the graph.
566 # Creating new nodes:
568 # var client = new Neo4jClient("http://localhost:7474")
570 # var andres = new NeoNode
571 # andres.labels.add "Person"
572 # andres["name"] = "Andres"
574 # client.save_node(andres)
575 # assert andres.is_linked
577 # Get nodes from Neo4j:
579 # var url = andres.url.to_s
580 # var node = client.load_node(url)
581 # assert node["name"] == "Andres"
582 # assert node["age"].to_s.to_i == 22
586 private var internal_labels
: nullable Array[String] = null
587 private var internal_in_edges
: nullable List[NeoEdge] = null
588 private var internal_out_edges
: nullable List[NeoEdge] = null
592 self.internal_labels
= new Array[String]
593 self.internal_in_edges
= new List[NeoEdge]
594 self.internal_out_edges
= new List[NeoEdge]
598 var tpl
= new FlatBuffer
600 tpl
.append
"labels: [{labels.join(", ")}],"
601 tpl
.append
"data: {to_json}"
603 return tpl
.write_to_string
606 # A label is a `String` that is used to group nodes into sets.
607 # A node may be labeled with any number of labels, including none.
608 # All nodes labeled with the same label belongs to the same set.
610 # Many database queries can work with these sets instead of the whole graph,
611 # making queries easier to write and more efficient.
613 # Labels are loaded lazily
614 fun labels
: Array[String] do return internal_labels
or else load_labels
616 private fun load_labels
: Array[String] do
617 var labels
= new Array[String]
618 var res
= neo
.get
("{url.to_s}/labels")
619 if res
isa JsonArray then
620 for val
in res
do labels
.add val
.to_s
622 internal_labels
= labels
626 # Get the list of `NeoEdge` pointing to `self`
628 # Edges are loaded lazily
629 fun in_edges
: List[NeoEdge] do return internal_in_edges
or else load_in_edges
631 private fun load_in_edges
: List[NeoEdge] do
632 var edges
= new List[NeoEdge]
633 var res
= neo
.get
("{url.to_s}/relationships/in").as(JsonArray)
635 edges
.add
(new NeoEdge.from_json
(neo
, obj
.as(JsonObject)))
637 internal_in_edges
= edges
641 # Get the list of `NeoEdge` pointing from `self`
643 # Edges are loaded lazily
644 fun out_edges
: List[NeoEdge] do return internal_out_edges
or else load_out_edges
646 private fun load_out_edges
: List[NeoEdge] do
647 var edges
= new List[NeoEdge]
648 var res
= neo
.get
("{url.to_s}/relationships/out")
649 for obj
in res
.as(JsonArray) do
650 edges
.add
(new NeoEdge.from_json
(neo
, obj
.as(JsonObject)))
652 internal_out_edges
= edges
656 # Get nodes pointed by `self` following a `rel_type` edge
657 fun out_nodes
(rel_type
: String): Array[NeoNode] do
658 var res
= new Array[NeoNode]
659 for edge
in out_edges
do
660 if edge
.rel_type
== rel_type
then res
.add edge
.to
665 # Get nodes pointing to `self` following a `rel_type` edge
666 fun in_nodes
(rel_type
: String): Array[NeoNode] do
667 var res
= new Array[NeoNode]
668 for edge
in in_edges
do
669 if edge
.rel_type
== rel_type
then res
.add edge
.from
675 # A relationship between two nodes.
676 # Relationships between nodes are a key part of a graph database.
677 # They allow for finding related data. Just like nodes, relationships can have properties.
679 # Create a relationship:
681 # var client = new Neo4jClient("http://localhost:7474")
683 # var andres = new NeoNode
684 # andres["name"] = "Andres"
685 # var kate = new NeoNode
686 # kate["name"] = "Kate"
687 # # Create a relationship of type `LOVES`
688 # var loves = new NeoEdge(andres, "LOVES", kate)
689 # client.save_edge(loves)
690 # assert loves.is_linked
692 # Get an edge from DB:
694 # var url = loves.url.to_s
695 # var edge = client.load_edge(url)
696 # assert edge.from["name"].to_s == "Andres"
697 # assert edge.to["name"].to_s == "Kate"
701 private var internal_from
: nullable NeoNode
702 private var internal_to
: nullable NeoNode
703 private var internal_type
: nullable String
704 private var internal_from_url
: nullable String
705 private var internal_to_url
: nullable String
707 init(from
: NeoNode, rel_type
: String, to
: NeoNode) do
708 self.internal_from
= from
709 self.internal_to
= to
710 self.internal_type
= rel_type
713 redef init from_neo
(neo
, url
) do
715 var obj
= neo
.get
(url
).as(JsonObject)
716 self.internal_type
= obj
["type"].to_s
717 self.internal_from_url
= obj
["start"].to_s
718 self.internal_to_url
= obj
["end"].to_s
721 redef init from_json
(neo
, obj
) do
723 self.internal_type
= obj
["type"].to_s
724 self.internal_from_url
= obj
["start"].to_s
725 self.internal_to_url
= obj
["end"].to_s
729 fun from
: NeoNode do return internal_from
or else load_from
731 private fun load_from
: NeoNode do
732 var node
= neo
.load_node
(internal_from_url
.to_s
)
738 fun to
: NeoNode do return internal_to
or else load_to
740 private fun load_to
: NeoNode do
741 var node
= neo
.load_node
(internal_to_url
.to_s
)
747 fun rel_type
: nullable String do return internal_type
750 var obj
= new JsonObject
754 obj
["to"] = "\{{to.batch_id.to_s}\}"
756 obj
["type"] = rel_type
757 obj
["data"] = properties
762 # Batches are used to perform multiple operations on the REST API in one cURL request.
763 # This can significantly improve performance for large insert and update operations.
765 # see: http://docs.neo4j.org/chunked/milestone/rest-api-batch-ops.html
767 # This service is transactional.
768 # If any of the operations performed fails (returns a non-2xx HTTP status code),
769 # the transaction will be rolled back and all changes will be undone.
773 # var client = new Neo4jClient("http://localhost:7474")
775 # var node1 = new NeoNode
776 # var node2 = new NeoNode
777 # var edge = new NeoEdge(node1, "TO", node2)
779 # var batch = new NeoBatch(client)
780 # batch.save_node(node1)
781 # batch.save_node(node2)
782 # batch.save_edge(edge)
785 # assert node1.is_linked
786 # assert node2.is_linked
787 # assert edge.is_linked
790 # Neo4j client connector
791 var client
: Neo4jClient
793 # Jobs to perform in this batch
795 # The batch service expects an array of job descriptions as input,
796 # each job description describing an action to be performed via the normal server API.
797 var jobs
= new HashMap[Int, NeoJob]
799 # Append a new job to the batch in JSON Format
801 fun new_job
(nentity
: NeoEntity): NeoJob do
803 var job
= new NeoJob(id
, nentity
)
808 # Load a node in batch mode also load labels, data and edges
809 fun load_node
(node
: NeoNode) do
810 var job
= new_job
(node
)
811 job
.action
= load_node_data_action
813 if node
.id
!= null then
814 job
.to
= "/node/{node.id.to_s}"
816 job
.to
= "\{{node.batch_id.to_s}\}"
819 job
.action
= load_node_labels_action
821 if node
.id
!= null then
822 job
.to
= "/node/{node.id.to_s}/labels"
824 job
.to
= "\{{node.batch_id.to_s}\}/labels"
828 # Load in and out edges into node
829 fun load_node_edges
(node
: NeoNode) do
830 var job
= new_job
(node
)
831 job
.action
= load_node_in_edges_action
833 if node
.id
!= null then
834 job
.to
= "/node/{node.id.to_s}/relationships/in"
836 job
.to
= "\{{node.batch_id.to_s}\}/relationships/in"
839 job
.action
= load_node_out_edges_action
841 if node
.id
!= null then
842 job
.to
= "/node/{node.id.to_s}/relationships/out"
844 job
.to
= "\{{node.batch_id.to_s}\}/relationships/out"
848 # Create a `NeoNode` or a `NeoEdge` in batch mode.
849 fun save_entity
(nentity
: NeoEntity) do
850 if nentity
isa NeoNode then
852 else if nentity
isa NeoEdge then
857 # Create a node in batch mode also create labels and edges
858 fun save_node
(node
: NeoNode) do
859 if node
.id
!= null or node
.batch_id
!= null then return
861 var job
= new_job
(node
)
862 node
.batch_id
= job
.id
863 job
.action
= create_node_action
866 job
.body
= node
.properties
870 job
.to
= "\{{node.batch_id.to_s}\}/labels"
871 job
.body
= new JsonArray.from
(node
.labels
)
873 #save_edges(node.out_edges)
876 # Create multiple nodes
877 # also create labels and edges
878 fun save_nodes
(nodes
: Collection[NeoNode]) do for node
in nodes
do save_node
(node
)
881 # nodes `edge.from` and `edge.to` will be created if not in base
882 fun save_edge
(edge
: NeoEdge) do
883 if edge
.id
!= null or edge
.batch_id
!= null then return
888 var job
= new_job
(edge
)
889 edge
.batch_id
= job
.id
890 job
.action
= create_edge_action
892 if edge
.from
.id
!= null then
893 job
.to
= "/node/{edge.from.id.to_s}/relationships"
895 job
.to
= "\{{edge.from.batch_id.to_s}\}/relationships"
897 job
.body
= edge
.to_json
900 # Create multiple edges
901 fun save_edges
(edges
: Collection[NeoEdge]) do for edge
in edges
do save_edge
(edge
)
903 # Execute the batch and update local nodes
904 fun execute
: List[JsonError] do
905 var request
= new JsonPOST(client
.batch_url
, client
.curl
)
906 # request.headers["X-Stream"] = "true"
907 var json_jobs
= new JsonArray
908 for job
in jobs
.values
do json_jobs
.add job
.to_json
909 request
.data
= json_jobs
910 var response
= request
.execute
911 var res
= client
.parse_response
(response
)
912 return finalize_batch
(res
)
915 # Associate data from response in original nodes and edges
916 private fun finalize_batch
(response
: Jsonable): List[JsonError] do
917 var errors
= new List[JsonError]
918 if not response
isa JsonArray then
919 errors
.add
(new JsonError("Neo4jError", "Unexpected batch response format"))
922 # print " {res.length} jobs executed"
923 for res
in response
do
924 if not res
isa JsonObject then
925 errors
.add
(new JsonError("Neo4jError", "Unexpected job format in batch response"))
928 var id
= res
["id"].as(Int)
930 if job
.action
== create_node_action
then
931 var node
= job
.entity
.as(NeoNode)
933 node
.url
= res
["location"].to_s
934 else if job
.action
== create_edge_action
then
935 var edge
= job
.entity
.as(NeoEdge)
937 edge
.url
= res
["location"].to_s
938 else if job
.action
== load_node_data_action
then
939 var node
= job
.entity
.as(NeoNode)
940 node
.internal_properties
= res
["body"].as(JsonObject)["data"].as(JsonObject)
941 else if job
.action
== load_node_labels_action
then
942 var node
= job
.entity
.as(NeoNode)
943 var labels
= new Array[String]
944 for l
in res
["body"].as(JsonArray) do labels
.add l
.to_s
945 node
.internal_labels
= labels
946 else if job
.action
== load_node_in_edges_action
then
947 var node
= job
.entity
.as(NeoNode)
948 var edges
= res
["body"].as(JsonArray)
949 node
.internal_in_edges
= new List[NeoEdge]
951 node
.internal_in_edges
.add client
.load_edge
(edge
.as(JsonObject)["self"].to_s
)
953 else if job
.action
== load_node_out_edges_action
then
954 var node
= job
.entity
.as(NeoNode)
955 var edges
= res
["body"].as(JsonArray)
956 node
.internal_out_edges
= new List[NeoEdge]
958 node
.internal_out_edges
.add client
.load_edge
(edge
.as(JsonObject)["self"].to_s
)
966 # TODO replace with enum
968 private fun create_node_action
: Int do return 1
969 private fun create_edge_action
: Int do return 2
970 private fun load_node_data_action
: Int do return 3
971 private fun load_node_labels_action
: Int do return 4
972 private fun load_node_in_edges_action
: Int do return 5
973 private fun load_node_out_edges_action
: Int do return 6
976 # A job that can be executed in a `NeoBatch`
977 # This is a representation of a neo job in JSON Format
979 # Each job description should contain a `to` attribute, with a value relative to the data API root
980 # (so http://localhost:7474/db/data/node becomes just /node), and a `method` attribute containing
983 # Optionally you may provide a `body` attribute, and an `id` attribute to help you keep track
984 # of responses, although responses are guaranteed to be returned in the same order the job
985 # descriptions are received.
989 # Entity targeted by the job
990 var entity
: NeoEntity
992 init(id
: Int, entity
: NeoEntity) do
997 # What kind of action do the job
998 # used to attach responses to original Neo objets
999 private var action
: nullable Int = null
1001 # Job HTTP method: `GET`, `POST`, `PUT`, `DELETE`...
1003 # Job service target: `/node`, `/labels` etc...
1005 # Body to send with the job service request
1006 var body
: nullable Jsonable = null
1009 fun to_json
: JsonObject do
1010 var job
= new JsonObject
1012 job
["method"] = method
1014 if not body
== null then