Merge: Neo4j: bug fixes and improvments
authorJean Privat <jean@pryen.org>
Mon, 21 Jul 2014 14:44:32 +0000 (10:44 -0400)
committerJean Privat <jean@pryen.org>
Mon, 21 Jul 2014 14:44:32 +0000 (10:44 -0400)
Changes:
* avoid node duplication by force loading nodes from local store
* better handling of big graphes through separation of nodes and edges loading
* try to speed things up with nodes data preloading from `nodes_with_labels` response

Pull-Request: #597
Reviewed-by: Jean Privat <jean@pryen.org>

lib/neo4j/neo4j.nit

index 9b7ad1c..6b3f0f3 100644 (file)
@@ -249,13 +249,12 @@ class Neo4jClient
        fun nodes_with_label(lbl: String): Array[NeoNode] do
                var res = get("{base_url}/db/data/label/{lbl}/nodes")
                var nodes = new Array[NeoNode]
-               var batch = new NeoBatch(self)
-               for obj in res.as(JsonArray) do
-                       var node = new NeoNode.from_json(self, obj.as(JsonObject))
-                       batch.load_node(node)
+               for json in res.as(JsonArray) do
+                       var obj = json.as(JsonObject)
+                       var node = load_node(obj["self"].to_s)
+                       node.internal_properties = obj["data"].as(JsonObject)
                        nodes.add node
                end
-               batch.execute
                return nodes
        end
 
@@ -690,7 +689,7 @@ class NeoEdge
        fun from: NeoNode do return internal_from or else load_from
 
        private fun load_from: NeoNode do
-               var node = new NeoNode.from_neo(neo, internal_from_url.to_s)
+               var node = neo.load_node(internal_from_url.to_s)
                internal_from = node
                return node
        end
@@ -699,7 +698,7 @@ class NeoEdge
        fun to: NeoNode do return internal_to or else load_to
 
        private fun load_to: NeoNode do
-               var node = new NeoNode.from_neo(neo, internal_to_url.to_s)
+               var node = neo.load_node(internal_to_url.to_s)
                internal_to = node
                return node
        end
@@ -768,13 +767,6 @@ class NeoBatch
 
        # Load a node in batch mode also load labels, data and edges
        fun load_node(node: NeoNode) do
-               load_node_data(node)
-               load_node_labels(node)
-               load_node_out_edges(node)
-       end
-
-       # Load data into node
-       private fun load_node_data(node: NeoNode) do
                var job = new_job(node)
                job.action = load_node_data_action
                job.method = "GET"
@@ -783,11 +775,7 @@ class NeoBatch
                else
                        job.to = "\{{node.batch_id.to_s}\}"
                end
-       end
-
-       # Load labels into node
-       private fun load_node_labels(node: NeoNode) do
-               var job = new_job(node)
+               job = new_job(node)
                job.action = load_node_labels_action
                job.method = "GET"
                if node.id != null then
@@ -797,9 +785,17 @@ class NeoBatch
                end
        end
 
-       # Load out edges into node
-       private fun load_node_out_edges(node: NeoNode) do
+       # Load in and out edges into node
+       fun load_node_edges(node: NeoNode) do
                var job = new_job(node)
+               job.action = load_node_in_edges_action
+               job.method = "GET"
+               if node.id != null then
+                       job.to = "/node/{node.id.to_s}/relationships/in"
+               else
+                       job.to = "\{{node.batch_id.to_s}\}/relationships/in"
+               end
+               job = new_job(node)
                job.action = load_node_out_edges_action
                job.method = "GET"
                if node.id != null then
@@ -825,7 +821,7 @@ class NeoBatch
                job.to = "\{{node.batch_id.to_s}\}/labels"
                job.body = new JsonArray.from(node.labels)
                # add edges
-               save_edges(node.out_edges)
+               #save_edges(node.out_edges)
        end
 
        # Create multiple nodes
@@ -898,12 +894,19 @@ class NeoBatch
                                var labels = new Array[String]
                                for l in res["body"].as(JsonArray) do labels.add l.to_s
                                node.internal_labels = labels
+                       else if job.action == load_node_in_edges_action then
+                               var node = job.entity.as(NeoNode)
+                               var edges = res["body"].as(JsonArray)
+                               node.internal_in_edges = new List[NeoEdge]
+                               for edge in edges do
+                                       node.internal_in_edges.add client.load_edge(edge.as(JsonObject)["self"].to_s)
+                               end
                        else if job.action == load_node_out_edges_action then
                                var node = job.entity.as(NeoNode)
                                var edges = res["body"].as(JsonArray)
                                node.internal_out_edges = new List[NeoEdge]
                                for edge in edges do
-                                       node.internal_out_edges.add new NeoEdge.from_json(client, edge.as(JsonObject))
+                                       node.internal_out_edges.add client.load_edge(edge.as(JsonObject)["self"].to_s)
                                end
                        end
                end
@@ -917,7 +920,8 @@ class NeoBatch
        private fun create_edge_action: Int do return 2
        private fun load_node_data_action: Int do return 3
        private fun load_node_labels_action: Int do return 4
-       private fun load_node_out_edges_action: Int do return 5
+       private fun load_node_in_edges_action: Int do return 5
+       private fun load_node_out_edges_action: Int do return 6
 end
 
 # A job that can be executed in a `NeoBatch`