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 # In order to connect to Neo4j you need a connector:
19 # # Create new Neo4j client
20 # var client = new Neo4jClient("http://neo4j:7474")
23 # The fundamental units that form a graph are nodes and relationships.
25 # Nodes are used to represent entities stored in base:
27 # # Create a disconnected node
28 # var andres = new NeoNode
29 # andres["name"] = "Andres"
30 # # Connect the node to Neo4j
31 # client.save_node(andres)
32 # assert andres.is_linked
34 # # Create a second node
35 # var kate = new NeoNode
36 # kate["name"] = "Kate"
37 # client.save_node(kate)
38 # assert kate.is_linked
40 # Relationships between nodes are a key part of a graph database.
41 # They allow for finding related data. Just like nodes, relationships can have properties.
43 # # Create a relationship
44 # var loves = new NeoEdge(andres, "LOVES", kate)
45 # client.save_edge(loves)
46 # assert loves.is_linked
48 # Nodes can also be loaded fron Neo4j:
50 # # Get a node from DB and explore edges
51 # var url = andres.url.to_s
52 # var from = client.load_node(url)
53 # assert from["name"].to_s == "Andres"
54 # var to = from.out_nodes("LOVES").first # follow the first LOVES relationship
55 # assert to["name"].to_s == "Kate"
57 # For more details, see http://docs.neo4j.org/chunked/milestone/rest-api.html
63 # `Neo4jClient` is needed to communicate through the REST API
65 # var client = new Neo4jClient("http://neo4j:7474")
69 # Neo4j REST services baseurl
71 # REST service to get node data
72 private var node_url
: String
73 # REST service to batch
74 private var batch_url
: String
75 # REST service to send cypher requests
76 private var cypher_url
: String
78 init(base_url
: String) do
79 self.base_url
= base_url
80 var root
= service_root
81 assert root
isa JsonObject else
82 sys
.stderr
.write
"Neo4jClientError: cannot connect to server at <{base_url}>.\n"
84 self.node_url
= root
["node"].to_s
85 self.batch_url
= root
["batch"].to_s
86 self.cypher_url
= root
["cypher"].to_s
89 fun service_root
: Serializable do return get
(base_url
/ "db/data")
91 # Is the connection with the Neo4j server ok?
92 fun is_ok
: Bool do return service_root
isa JsonObject
96 cypher
(new CypherQuery.from_string
("MATCH (n) OPTIONAL MATCH n-[r]-() DELETE r, n"))
100 var errors
= new Array[String]
102 # Nodes view stored locally
103 private var local_nodes
= new HashMap[String, nullable NeoNode]
105 # Save the node in base
107 # var client = new Neo4jClient("http://neo4j:7474")
110 # var andres = new NeoNode
111 # andres["name"] = "Andres"
112 # client.save_node(andres)
113 # assert andres.is_linked
115 # Once linked, nodes cannot be created twice:
117 # var oldurl = andres.url
118 # client.save_node(andres) # do nothing
119 # assert andres.url == oldurl
120 fun save_node
(node
: NeoNode): Bool do
121 if node
.is_linked
then return true
123 var batch
= new NeoBatch(self)
124 batch
.save_node
(node
)
125 # batch.create_edges(node.out_edges)
126 var errors
= batch
.execute
127 if not errors
.is_empty
then
128 errors
.add_all errors
131 local_nodes
[node
.url
.to_s
] = node
135 # Load a node from base
136 # Data, labels and edges will be loaded lazily.
137 fun load_node
(url
: String): NeoNode do
138 if local_nodes
.has_key
(url
) then
139 var node
= local_nodes
[url
]
140 if node
!= null then return node
142 var node
= new NeoNode.from_neo
(self, url
)
143 local_nodes
[url
] = node
147 # Remove the entity from base
148 fun delete_node
(node
: NeoNode): Bool do
149 if not node
.is_linked
then return false
150 var url
= node
.url
.to_s
152 local_nodes
[url
] = null
157 # Edges view stored locally
158 private var local_edges
= new HashMap[String, nullable NeoEdge]
160 # Save the edge in base
161 # From and to nodes will be created.
163 # var client = new Neo4jClient("http://neo4j:7474")
165 # var andres = new NeoNode
166 # var kate = new NeoNode
167 # var edge = new NeoEdge(andres, "LOVES", kate)
168 # client.save_edge(edge)
169 # assert andres.is_linked
170 # assert kate.is_linked
171 # assert edge.is_linked
172 fun save_edge
(edge
: NeoEdge): Bool do
173 if edge
.is_linked
then return true
175 edge
.from
.out_edges
.add edge
176 edge
.to
.in_edges
.add edge
177 var batch
= new NeoBatch(self)
178 batch
.save_edge
(edge
)
179 var errors
= batch
.execute
180 if not errors
.is_empty
then
181 errors
.add_all errors
184 local_edges
[edge
.url
.to_s
] = edge
188 # Load a edge from base
189 # Data will be loaded lazily.
190 fun load_edge
(url
: String): NeoEdge do
191 if local_edges
.has_key
(url
) then
192 var node
= local_edges
[url
]
193 if node
!= null then return node
195 var edge
= new NeoEdge.from_neo
(self, url
)
196 local_edges
[url
] = edge
200 # Remove the edge from base
201 fun delete_edge
(edge
: NeoEdge): Bool do
202 if not edge
.is_linked
then return false
203 var url
= edge
.url
.to_s
205 local_edges
[url
] = null
210 # Retrieve all nodes with specified `lbl`
212 # var client = new Neo4jClient("http://neo4j:7474")
214 # var andres = new NeoNode
215 # andres.labels.add_all(["Human", "Male"])
216 # client.save_node(andres)
217 # var kate = new NeoNode
218 # kate.labels.add_all(["Human", "Female"])
219 # client.save_node(kate)
221 # var nodes = client.nodes_with_label("Human")
222 # assert nodes.has(andres)
223 # assert nodes.has(kate)
224 fun nodes_with_label
(lbl
: String): Array[NeoNode] do
225 var res
= get
(base_url
/ "db/data/label/{lbl.to_percent_encoding}/nodes")
226 var nodes
= new Array[NeoNode]
227 for json
in res
.as(JsonArray) do
228 var obj
= json
.as(JsonObject)
229 var node
= load_node
(obj
["self"].to_s
)
230 node
.internal_properties
= obj
["data"].as(JsonObject)
236 # Retrieve nodes belonging to all the specified `labels`.
238 # var client = new Neo4jClient("http://neo4j:7474")
240 # var andres = new NeoNode
241 # andres.labels.add_all(["Human", "Male"])
242 # client.save_node(andres)
243 # var kate = new NeoNode
244 # kate.labels.add_all(["Human", "Female"])
245 # client.save_node(kate)
247 # var nodes = client.nodes_with_labels(["Human", "Male"])
248 # assert nodes.has(andres)
249 # assert not nodes.has(kate)
250 fun nodes_with_labels
(labels
: Array[String]): Array[NeoNode] do
251 assert not labels
.is_empty
254 var buffer
= new Buffer
255 buffer
.append
"match (n) where \{label_0\} in labels(n)"
256 for i
in [1..labels
.length
[ do
257 buffer
.append
" and \{label_{i}\} in labels(n)"
259 buffer
.append
" return n"
260 var query
= new CypherQuery.from_string
(buffer
.write_to_string
)
261 for i
in [0..labels
.length
[ do
262 query
.params
["label_{i}"] = labels
[i
]
265 # Retrieve the answer.
266 var res
= cypher
(query
)
267 var nodes
= new Array[NeoNode]
268 for json
in res
.as(JsonObject)["data"].as(JsonArray) do
269 var obj
= json
.as(JsonArray).first
.as(JsonObject)
270 var node
= load_node
(obj
["self"].to_s
)
271 node
.internal_properties
= obj
["data"].as(JsonObject)
277 # Perform a `CypherQuery`
279 fun cypher
(query
: CypherQuery): Serializable do
280 return post
("{cypher_url}", query
.to_rest
)
283 # GET JSON data from `url`
284 fun get
(url
: String): Serializable do
285 var request
= new JsonGET(url
)
286 var response
= request
.execute
287 return parse_response
(response
)
290 # POST `params` to `url`
291 fun post
(url
: String, params
: Serializable): Serializable do
292 var request
= new JsonPOST(url
)
293 request
.json_data
= params
294 var response
= request
.execute
295 return parse_response
(response
)
298 # PUT `params` at `url`
299 fun put
(url
: String, params
: Serializable): Serializable do
300 var request
= new JsonPUT(url
)
301 request
.json_data
= params
302 var response
= request
.execute
303 return parse_response
(response
)
307 fun delete
(url
: String): Serializable do
308 var request
= new JsonDELETE(url
)
309 var response
= request
.execute
310 return parse_response
(response
)
313 # Parse the cURL `response` as a JSON string
314 private fun parse_response
(response
: CurlResponse): Serializable do
315 if response
isa CurlResponseSuccess then
316 var str
= response
.body_str
317 if str
.is_empty
then return new JsonObject
318 var res
= str
.parse_json
319 if res
isa JsonParseError then
320 var e
= new NeoError(res
.to_s
, "JsonParseError")
325 # empty response wrap it in empty object
326 return new JsonObject
327 else if res
isa JsonObject and res
.has_key
("exception") then
328 var error
= "Neo4jError::{res["exception"] or else "null"}"
330 if res
.has_key
("message") then
331 msg
= res
["message"].to_s
333 return new NeoError(msg
, error
)
337 else if response
isa CurlResponseFailed then
338 return new NeoError("{response.error_msg} ({response.error_code})", "CurlError")
340 return new NeoError("Unexpected response \"{response}\
".", "CurlError")
345 # A Cypher query for Neo4j REST API
347 # The Neo4j REST API allows querying with Cypher.
348 # The results are returned as a list of string headers (columns), and a data part,
349 # consisting of a list of all rows, every row consisting of a list of REST representations
350 # of the field value - Node, Relationship, Path or any simple value like String.
354 # var client = new Neo4jClient("http://neo4j:7474")
355 # var query = new CypherQuery
356 # query.nmatch("(n)-[r:LOVES]->(m)")
357 # query.nwhere("n.name=\"Andres\"")
358 # query.nreturn("m.name")
359 # var res = client.cypher(query).as(JsonObject)
360 # assert res["data"].as(JsonArray).first.as(JsonArray).first == "Kate"
362 # For more details, see: http://docs.neo4j.org/chunked/milestone/rest-api-cypher.html
364 # Query string to perform
365 private var query
: String = ""
367 # `params` to embed in the query like in prepared statements
368 var params
= new JsonObject
370 # init the query from a query string
371 init from_string
(query
: String) do
375 # init the query with parameters
376 init with_params
(params
: JsonObject) do
380 # Pass the argument `value` as the parameter `key`.
383 fun []=(key
: String, value
: nullable Serializable) do
387 # Add a `CREATE` statement to the query
388 fun ncreate
(query
: String): CypherQuery do
389 self.query
= "{self.query}CREATE {query} "
393 # Add a `START` statement to the query
394 fun nstart
(query
: String): CypherQuery do
395 self.query
= "{self.query}START {query} "
399 # Add a `MATCH` statement to the query
400 fun nmatch
(query
: String): CypherQuery do
401 self.query
= "{self.query}MATCH {query} "
405 # Add a `WHERE` statement to the query
406 fun nwhere
(query
: String): CypherQuery do
407 self.query
= "{self.query}WHERE {query} "
411 # Add a `AND` statement to the query
412 fun nand
(query
: String): CypherQuery do
413 self.query
= "{self.query}AND {query} "
417 # Add a `RETURN` statement to the query
418 fun nreturn
(query
: String): CypherQuery do
419 self.query
= "{self.query}RETURN {query} "
423 # Pass the argument `value` as the parameter `key`.
428 # var query = (new CypherQuery).
430 # nwhere("n.key = \{key\}").
433 # assert query.params["key"] == "foo"
437 fun set
(key
: String, value
: nullable Serializable): SELF do
442 # Translate the query to the body of a corresponding Neo4j REST request.
443 fun to_rest
: 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_rest
.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://neo4j: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 is noinit
493 # Entity unique URL in Neo4j REST API
494 var url
: nullable String = null
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) is nosuper
do
505 # Init entity from JSON representation
506 private init from_json
(neo
: Neo4jClient, obj
: JsonObject) is nosuper
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 Serializable 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 Serializable) do properties
[key
] = value
550 # Is the property `key` set?
551 fun has_key
(key
: String): Bool do return properties
.has_key
(key
)
554 # Nodes are used to represent entities stored in base.
555 # Apart from properties and relationships (edges),
556 # nodes can also be labeled with zero or more labels.
558 # A label is a `String` that is used to group nodes into sets.
559 # All nodes labeled with the same label belongs to the same set.
560 # A node may be labeled with any number of labels, including none,
561 # making labels an optional addition to the graph.
563 # Creating new nodes:
565 # var client = new Neo4jClient("http://neo4j:7474")
567 # var andres = new NeoNode
568 # andres.labels.add "Person"
569 # andres["name"] = "Andres"
571 # client.save_node(andres)
572 # assert andres.is_linked
574 # Get nodes from Neo4j:
576 # var url = andres.url.to_s
577 # var node = client.load_node(url)
578 # assert node["name"] == "Andres"
579 # assert node["age"].to_s.to_i == 22
583 private var internal_labels
: nullable Array[String] = null
584 private var internal_in_edges
: nullable List[NeoEdge] = null
585 private var internal_out_edges
: nullable List[NeoEdge] = null
589 self.internal_labels
= new Array[String]
590 self.internal_in_edges
= new List[NeoEdge]
591 self.internal_out_edges
= new List[NeoEdge]
595 var tpl
= new FlatBuffer
597 tpl
.append
"labels: [{labels.join(", ")}],"
598 tpl
.append
"data: {properties.to_json}"
600 return tpl
.write_to_string
603 # A label is a `String` that is used to group nodes into sets.
604 # A node may be labeled with any number of labels, including none.
605 # All nodes labeled with the same label belongs to the same set.
607 # Many database queries can work with these sets instead of the whole graph,
608 # making queries easier to write and more efficient.
610 # Labels are loaded lazily
611 fun labels
: Array[String] do return internal_labels
or else load_labels
613 private fun load_labels
: Array[String] do
614 var labels
= new Array[String]
615 var res
= neo
.get
(url
.to_s
/ "labels")
616 if res
isa JsonArray then
617 for val
in res
do labels
.add val
.to_s
619 internal_labels
= labels
623 # Get the list of `NeoEdge` pointing to `self`
625 # Edges are loaded lazily
626 fun in_edges
: List[NeoEdge] do return internal_in_edges
or else load_in_edges
628 private fun load_in_edges
: List[NeoEdge] do
629 var edges
= new List[NeoEdge]
630 var res
= neo
.get
(url
.to_s
/ "relationships/in").as(JsonArray)
632 edges
.add
(new NeoEdge.from_json
(neo
, obj
.as(JsonObject)))
634 internal_in_edges
= edges
638 # Get the list of `NeoEdge` pointing from `self`
640 # Edges are loaded lazily
641 fun out_edges
: List[NeoEdge] do return internal_out_edges
or else load_out_edges
643 private fun load_out_edges
: List[NeoEdge] do
644 var edges
= new List[NeoEdge]
645 var res
= neo
.get
(url
.to_s
/ "relationships/out")
646 for obj
in res
.as(JsonArray) do
647 edges
.add
(new NeoEdge.from_json
(neo
, obj
.as(JsonObject)))
649 internal_out_edges
= edges
653 # Get nodes pointed by `self` following a `rel_type` edge
654 fun out_nodes
(rel_type
: String): Array[NeoNode] do
655 var res
= new Array[NeoNode]
656 for edge
in out_edges
do
657 if edge
.rel_type
== rel_type
then res
.add edge
.to
662 # Get nodes pointing to `self` following a `rel_type` edge
663 fun in_nodes
(rel_type
: String): Array[NeoNode] do
664 var res
= new Array[NeoNode]
665 for edge
in in_edges
do
666 if edge
.rel_type
== rel_type
then res
.add edge
.from
672 # A relationship between two nodes.
673 # Relationships between nodes are a key part of a graph database.
674 # They allow for finding related data. Just like nodes, relationships can have properties.
676 # Create a relationship:
678 # var client = new Neo4jClient("http://neo4j:7474")
680 # var andres = new NeoNode
681 # andres["name"] = "Andres"
682 # var kate = new NeoNode
683 # kate["name"] = "Kate"
684 # # Create a relationship of type `LOVES`
685 # var loves = new NeoEdge(andres, "LOVES", kate)
686 # client.save_edge(loves)
687 # assert loves.is_linked
689 # Get an edge from DB:
691 # var url = loves.url.to_s
692 # var edge = client.load_edge(url)
693 # assert edge.from["name"].to_s == "Andres"
694 # assert edge.to["name"].to_s == "Kate"
698 private var internal_from
: nullable NeoNode
699 private var internal_to
: nullable NeoNode
700 private var internal_type
: nullable String
701 private var internal_from_url
: nullable String
702 private var internal_to_url
: nullable String
704 init(from
: NeoNode, rel_type
: String, to
: NeoNode) do
705 self.internal_from
= from
706 self.internal_to
= to
707 self.internal_type
= rel_type
710 redef init from_neo
(neo
, url
) do
712 var obj
= neo
.get
(url
).as(JsonObject)
713 self.internal_type
= obj
["type"].to_s
714 self.internal_from_url
= obj
["start"].to_s
715 self.internal_to_url
= obj
["end"].to_s
718 redef init from_json
(neo
, obj
) do
720 self.internal_type
= obj
["type"].to_s
721 self.internal_from_url
= obj
["start"].to_s
722 self.internal_to_url
= obj
["end"].to_s
726 fun from
: NeoNode do return internal_from
or else load_from
728 private fun load_from
: NeoNode do
729 var node
= neo
.load_node
(internal_from_url
.to_s
)
735 fun to
: NeoNode do return internal_to
or else load_to
737 private fun load_to
: NeoNode do
738 var node
= neo
.load_node
(internal_to_url
.to_s
)
744 fun rel_type
: nullable String do return internal_type
746 # Get the JSON body of a REST request that create the relationship.
747 private fun to_rest
: JsonObject do
748 var obj
= new JsonObject
752 obj
["to"] = "\{{to.batch_id.to_s}\}"
754 obj
["type"] = rel_type
755 obj
["data"] = properties
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.
763 # see: http://docs.neo4j.org/chunked/milestone/rest-api-batch-ops.html
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.
771 # var client = new Neo4jClient("http://neo4j:7474")
773 # var node1 = new NeoNode
774 # var node2 = new NeoNode
775 # var edge = new NeoEdge(node1, "TO", node2)
777 # var batch = new NeoBatch(client)
778 # batch.save_node(node1)
779 # batch.save_node(node2)
780 # batch.save_edge(edge)
783 # assert node1.is_linked
784 # assert node2.is_linked
785 # assert edge.is_linked
788 # Neo4j client connector
789 var client
: Neo4jClient
791 # Jobs to perform in this batch
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]
797 # Append a new job to the batch in JSON Format
799 fun new_job
(nentity
: NeoEntity): NeoJob do
801 var job
= new NeoJob(id
, nentity
)
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
811 if node
.id
!= null then
812 job
.to
= "/node/{node.id.to_s}"
814 job
.to
= "\{{node.batch_id.to_s}\}"
817 job
.action
= load_node_labels_action
819 if node
.id
!= null then
820 job
.to
= "/node/{node.id.to_s}/labels"
822 job
.to
= "\{{node.batch_id.to_s}\}/labels"
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
831 if node
.id
!= null then
832 job
.to
= "/node/{node.id.to_s}/relationships/in"
834 job
.to
= "\{{node.batch_id.to_s}\}/relationships/in"
837 job
.action
= load_node_out_edges_action
839 if node
.id
!= null then
840 job
.to
= "/node/{node.id.to_s}/relationships/out"
842 job
.to
= "\{{node.batch_id.to_s}\}/relationships/out"
846 # Create a `NeoNode` or a `NeoEdge` in batch mode.
847 fun save_entity
(nentity
: NeoEntity) do
848 if nentity
isa NeoNode then
850 else if nentity
isa NeoEdge then
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
859 var job
= new_job
(node
)
860 node
.batch_id
= job
.id
861 job
.action
= create_node_action
864 job
.body
= node
.properties
868 job
.to
= "\{{node.batch_id.to_s}\}/labels"
869 job
.body
= new JsonArray.from
(node
.labels
)
871 #save_edges(node.out_edges)
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
)
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
886 var job
= new_job
(edge
)
887 edge
.batch_id
= job
.id
888 job
.action
= create_edge_action
890 if edge
.from
.id
!= null then
891 job
.to
= "/node/{edge.from.id.to_s}/relationships"
893 job
.to
= "\{{edge.from.batch_id.to_s}\}/relationships"
895 job
.body
= edge
.to_rest
898 # Create multiple edges
899 fun save_edges
(edges
: Collection[NeoEdge]) do for edge
in edges
do save_edge
(edge
)
901 # Execute the batch and update local nodes
902 fun execute
: List[NeoError] do
903 var request
= new JsonPOST(client
.batch_url
)
904 # request.headers["X-Stream"] = "true"
905 var json_jobs
= new JsonArray
906 for job
in jobs
.values
do json_jobs
.add job
.to_rest
907 request
.json_data
= json_jobs
908 var response
= request
.execute
909 var res
= client
.parse_response
(response
)
910 return finalize_batch
(res
)
913 # Associate data from response in original nodes and edges
914 private fun finalize_batch
(response
: Serializable): List[NeoError] do
915 var errors
= new List[NeoError]
916 if not response
isa JsonArray then
917 errors
.add
(new NeoError("Unexpected batch response format.", "Neo4jError"))
920 # print " {res.length} jobs executed"
921 for res
in response
do
922 if not res
isa JsonObject then
923 errors
.add
(new NeoError("Unexpected job format in batch response.", "Neo4jError"))
926 var id
= res
["id"].as(Int)
928 if job
.action
== create_node_action
then
929 var node
= job
.entity
.as(NeoNode)
931 node
.url
= res
["location"].to_s
932 else if job
.action
== create_edge_action
then
933 var edge
= job
.entity
.as(NeoEdge)
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]
949 node
.internal_in_edges
.add client
.load_edge
(edge
.as(JsonObject)["self"].to_s
)
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]
956 node
.internal_out_edges
.add client
.load_edge
(edge
.as(JsonObject)["self"].to_s
)
964 # TODO replace with enum
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
974 # A job that can be executed in a `NeoBatch`
975 # This is a representation of a neo job in JSON Format
977 # Each job description should contain a `to` attribute, with a value relative to the data API root
978 # (so http://neo4j:7474/db/data/node becomes just /node), and a `method` attribute containing
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.
987 # Entity targeted by the job
988 var entity
: NeoEntity
990 init(id
: Int, entity
: NeoEntity) do
995 # What kind of action do the job
996 # used to attach responses to original Neo objets
997 private var action
: nullable Int = null
999 # Job HTTP method: `GET`, `POST`, `PUT`, `DELETE`...
1001 # Job service target: `/node`, `/labels` etc...
1003 # Body to send with the job service request
1004 var body
: nullable Serializable = null
1007 fun to_rest
: JsonObject do
1008 var job
= new JsonObject
1010 job
["method"] = method
1012 if not body
== null then