From 8f15c033d4ae104043a8f26229722566b9810599 Mon Sep 17 00:00:00 2001 From: Max De Marzi Date: Thu, 5 Jan 2012 03:19:12 +0000 Subject: [PATCH] adding more batching functions, now with referencing --- README.rdoc | 10 ++++++ lib/neography/rest.rb | 6 ++-- spec/integration/rest_batch_spec.rb | 54 ++++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/README.rdoc b/README.rdoc index 45b8fdb..f2cfcbd 100644 --- a/README.rdoc +++ b/README.rdoc @@ -171,6 +171,16 @@ Batch (in progress): node1, node2, {:since => "high school"}] [:create_relationship, "friends", node1, node3, {:since => "college"}] # Creates two relationships in a batch + @neo.batch [:create_node, {"name" => "Max"}], + [:create_node, {"name" => "Marc"}], # Creates two nodes and index them + [:add_node_to_index, "test_node_index", key, value, "{0}"] + [:add_node_to_index, "test_node_index", key, value, "{1}"] + [:create_relationship, "friends", # and create a relationship for those + "{0}", "{1}", {:since => "college"}] # newly created nodes + [:add_relationship_to_index, + "test_relationship_index", key, value, "{4}"] # and index the new relationship + +See http://docs.neo4j.org/chunked/milestone/rest-api-batch-ops.html for Neo4j Batch operations documentation. Experimental: diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index c8ca341..fc660d5 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -376,13 +376,15 @@ def get_batch(args) when :get_relationship {:method => "GET", :to => "/relationship/#{get_id(args[1])}"} when :create_relationship - {:method => "POST", :to => "/node/#{get_id(args[2])}/relationships", :body => {:to => "/node/#{get_id(args[3])}", :type => args[1], :data => args[4] } } + {:method => "POST", :to => (args[2].is_a?(String) && args[2].start_with?("{") ? "" : "/node/") + "#{get_id(args[2])}/relationships", :body => {:to => (args[3].is_a?(String) && args[3].start_with?("{") ? "" : "/node/") + "#{get_id(args[3])}", :type => args[1], :data => args[4] } } when :set_relationship_property {:method => "PUT", :to => "/relationship/#{get_id(args[1])}/properties/#{args[2].keys.first}", :body => args[2].values.first} when :reset_relationship_properties {:method => "PUT", :to => "/relationship/#{get_id(args[1])}/properties", :body => args[2]} when :add_node_to_index - {:method => "POST", :to => "/index/node/#{args[1]}", :body => {:uri => "/node/#{get_id(args[4])}", :key => args[2], :value => args[3] } } + {:method => "POST", :to => "/index/node/#{args[1]}", :body => {:uri => (args[4].is_a?(String) && args[4].start_with?("{") ? "" : "/node/") + "#{get_id(args[4])}", :key => args[2], :value => args[3] } } + when :add_relationship_to_index + {:method => "POST", :to => "/index/relationship/#{args[1]}", :body => {:uri => (args[4].is_a?(String) && args[4].start_with?("{") ? "" : "/relationship/") + "#{get_id(args[4])}", :key => args[2], :value => args[3] } } end end diff --git a/spec/integration/rest_batch_spec.rb b/spec/integration/rest_batch_spec.rb index 639bfbd..e3fb013 100644 --- a/spec/integration/rest_batch_spec.rb +++ b/spec/integration/rest_batch_spec.rb @@ -149,19 +149,65 @@ describe "referenced batch" do it "can create a relationship from two newly created nodes" do - pending + batch_result = @neo.batch [:create_node, {"name" => "Max"}], [:create_node, {"name" => "Marc"}], [:create_relationship, "friends", "{0}", "{1}", {:since => "high school"}] + batch_result.first["body"]["data"]["name"].should == "Max" + batch_result[1]["body"]["data"]["name"].should == "Marc" + batch_result.last["body"]["type"].should == "friends" + batch_result.last["body"]["data"]["since"].should == "high school" + batch_result.last["body"]["start"].split('/').last.should == batch_result.first["body"]["self"].split('/').last + batch_result.last["body"]["end"].split('/').last.should == batch_result[1]["body"]["self"].split('/').last end it "can create a relationship from an existing node and a newly created node" do - pending + node1 = @neo.create_node("name" => "Max", "weight" => 200) + batch_result = @neo.batch [:create_node, {"name" => "Marc"}], [:create_relationship, "friends", "{0}", node1, {:since => "high school"}] + batch_result.first["body"]["data"]["name"].should == "Marc" + batch_result.last["body"]["type"].should == "friends" + batch_result.last["body"]["data"]["since"].should == "high school" + batch_result.last["body"]["start"].split('/').last.should == batch_result.first["body"]["self"].split('/').last + batch_result.last["body"]["end"].split('/').last.should == node1["self"].split('/').last end it "can add a newly created node to an index" do - pending + key = generate_text(6) + value = generate_text + new_index = @neo.get_node_index("test_node_index", key, value) + batch_result = @neo.batch [:create_node, {"name" => "Max"}], [:add_node_to_index, "test_node_index", key, value, "{0}"] + batch_result.first.should have_key("id") + batch_result.first.should have_key("from") + existing_index = @neo.find_node_index("test_node_index", key, value) + existing_index.should_not be_nil + existing_index.first["self"].should == batch_result.first["body"]["self"] + @neo.remove_node_from_index("test_node_index", key, value, batch_result.first["body"]["self"].split('/').last) end it "can add a newly created relationship to an index" do - pending + key = generate_text(6) + value = generate_text + node1 = @neo.create_node + node2 = @neo.create_node + batch_result = @neo.batch [:create_relationship, "friends", node1, node2, {:since => "high school"}], [:add_relationship_to_index, "test_relationship_index", key, value, "{0}"] + batch_result.first["body"]["type"].should == "friends" + batch_result.first["body"]["data"]["since"].should == "high school" + batch_result.first["body"]["start"].split('/').last.should == node1["self"].split('/').last + batch_result.first["body"]["end"].split('/').last.should == node2["self"].split('/').last + existing_index = @neo.find_relationship_index("test_relationship_index", key, value) + existing_index.should_not be_nil + existing_index.first["self"].should == batch_result.first["body"]["self"] + + end + + it "can kitchen sink" do + key = generate_text(6) + value = generate_text + + batch_result = @neo.batch [:create_node, {"name" => "Max"}], + [:create_node, {"name" => "Marc"}], + [:add_node_to_index, "test_node_index", key, value, "{0}"] + [:add_node_to_index, "test_node_index", key, value, "{1}"] + [:create_relationship, "friends", "{0}", "{1}", {:since => "college"}] + [:add_relationship_to_index, "test_relationship_index", key, value, "{4}"] + batch_result.should_not be_nil end end