Skip to content

Commit

Permalink
adding more batching functions, now with referencing
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Jan 5, 2012
1 parent 92ce780 commit 8f15c03
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
6 changes: 4 additions & 2 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
54 changes: 50 additions & 4 deletions spec/integration/rest_batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8f15c03

Please sign in to comment.