Skip to content

Commit

Permalink
batch chunked
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Apr 11, 2013
1 parent f800179 commit f0bc117
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 25 deletions.
20 changes: 19 additions & 1 deletion lib/neography/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ def post(path, options={})
evaluate_response(@client.post(configuration + path, merge_options(options)[:body], merge_options(options)[:headers]))
end

def post_chunked(path, options={})
authenticate(configuration + path)
result = ""
response = @client.post(configuration + path, merge_options(options)[:body], merge_options(options)[:headers]) do |chunk|
result << chunk
end
evaluate_chunk_response(response, result)
end

def put(path, options={})
authenticate(configuration + path)
evaluate_response(@client.put(configuration + path, merge_options(options)[:body], merge_options(options)[:headers]))
Expand Down Expand Up @@ -97,9 +106,18 @@ def save_local_configuration(config)
end
end

def evaluate_chunk_response(response, result)
code = response.code
return_result(code, result)
end

def evaluate_response(response)
code = response.code
body = response.body
return_result(code, body)
end

def return_result(code, body)
case code
when 200
@logger.debug "OK" if @log_enabled
Expand All @@ -115,7 +133,7 @@ def evaluate_response(response)
when 400..500
handle_4xx_500_response(code, body)
nil
end
end
end

def handle_4xx_500_response(code, body)
Expand Down
7 changes: 5 additions & 2 deletions lib/neography/rest/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ def batch(accept_header, *args)
:body => batch.to_json,
:headers => json_content_type.merge(accept_header)
}

@connection.post(batch_path, options)
if accept_header.empty?
@connection.post(batch_path, options)
else
@connection.post_chunked(batch_path, options)
end
end

def get_batch(args)
Expand Down
17 changes: 17 additions & 0 deletions spec/integration/performance_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe Neography::Rest, :slow => true do
before(:each) do
@neo = Neography::Rest.new
end

describe "can perform" do
it "is fast" do
Benchmark.bm do |x|
x.report(" 100 Times") { 100.times { @neo.create_node } }
x.report(" 500 Times") { 500.times { @neo.create_node } }
x.report("1000 Times") { 1000.times { @neo.create_node } }
end
end
end
end
2 changes: 1 addition & 1 deletion spec/integration/rest_batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,5 +452,5 @@
end

end

end
32 changes: 32 additions & 0 deletions spec/integration/rest_batch_streaming_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

describe Neography::Rest do
before(:each) do
@neo = Neography::Rest.new
end

describe "streaming" do

it "can send a 1000 item batch" do
commands = []
1000.times do |x|
commands << [:create_node, {"name" => "Max " + x.to_s}]
end
batch_result = @neo.batch *commands
batch_result.first["body"]["data"]["name"].should == "Max 0"
batch_result.last["body"]["data"]["name"].should == "Max 999"
end

it "can send a 10000 item batch" do
commands = []
10000.times do |x|
commands << [:get_node, 0]
end
batch_result = @neo.batch *commands
batch_result.first["body"]["self"].split('/').last.should == "0"
batch_result.last["body"]["self"].split('/').last.should == "0"
end

end

end
42 changes: 21 additions & 21 deletions spec/unit/rest/batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Rest
{ "id" => 1, "method" => "GET", "to" => "/node/bar" }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:get_node, "foo"], [:get_node, "bar"]
end

Expand All @@ -23,7 +23,7 @@ class Rest
{ "id" => 1, "method" => "POST", "to" => "/node", "body" => { "baz" => "qux" } }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:create_node, { "foo" => "bar" }], [:create_node, { "baz" => "qux" }]
end

Expand All @@ -33,7 +33,7 @@ class Rest
{ "id" => 1, "method" => "DELETE", "to" => "/node/bar" }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:delete_node, "foo"], [:delete_node, "bar"]
end

Expand All @@ -43,7 +43,7 @@ class Rest
{ "id" => 1, "method" => "POST", "to" => "/index/node/quux?unique", "body" => { "key" => "corge", "value" => "grault", "properties" => "garply" } }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:create_unique_node, "foo", "bar", "baz", "qux" ],
[:create_unique_node, "quux", "corge", "grault", "garply"]
end
Expand All @@ -54,7 +54,7 @@ class Rest
{ "id" => 1, "method" => "POST", "to" => "/index/node/quux", "body" => { "uri" => "{0}", "key" => "corge", "value" => "grault" } }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:add_node_to_index, "foo", "bar", "baz", "qux" ],
[:add_node_to_index, "quux", "corge", "grault", "{0}"]
end
Expand All @@ -65,7 +65,7 @@ class Rest
{ "id" => 1, "method" => "GET", "to" => "/index/node/qux/quux/corge" }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:get_node_index, "foo", "bar", "baz" ],
[:get_node_index, "qux", "quux", "corge" ]
end
Expand All @@ -77,7 +77,7 @@ class Rest
{ "id" => 2, "method" => "DELETE", "to" => "/index/node/index3/key3/value3/id3" }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:remove_node_from_index, "index1", "id1", ],
[:remove_node_from_index, "index2", "key2", "id2" ],
[:remove_node_from_index, "index3", "key3", "value3", "id3" ]
Expand All @@ -89,7 +89,7 @@ class Rest
{ "id" => 1, "method" => "PUT", "to" => "/node/index2/properties/key2", "body" => "value2" }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:set_node_property, "index1", { "key1" => "value1" } ],
[:set_node_property, "index2", { "key2" => "value2" } ]
end
Expand All @@ -100,7 +100,7 @@ class Rest
{ "id" => 1, "method" => "PUT", "to" => "/node/index2/properties", "body" => { "key2" => "value2" } }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:reset_node_properties, "index1", { "key1" => "value1" } ],
[:reset_node_properties, "index2", { "key2" => "value2" } ]
end
Expand All @@ -111,7 +111,7 @@ class Rest
{ "id" => 1, "method" => "GET", "to" => "/node/id2/relationships/all" }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:get_node_relationships, "id1", "direction1" ],
[:get_node_relationships, "id2" ]
end
Expand All @@ -122,7 +122,7 @@ class Rest
{ "id" => 1, "method" => "GET", "to" => "/relationship/bar" }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:get_relationship, "foo"], [:get_relationship, "bar"]
end

Expand All @@ -132,7 +132,7 @@ class Rest
{ "id" => 1, "method" => "POST", "to" => "{0}/relationships", "body" => { "to" => "{1}", "type" => "type2", "data" => "data2" } }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:create_relationship, "type1", "from1", "to1", "data1" ],
[:create_relationship, "type2", "{0}", "{1}", "data2" ]
end
Expand All @@ -143,7 +143,7 @@ class Rest
{ "id" => 1, "method" => "DELETE", "to" => "/relationship/bar" }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:delete_relationship, "foo"], [:delete_relationship, "bar"]
end

Expand All @@ -153,7 +153,7 @@ class Rest
{ "id" => 1, "method" => "POST", "to" => "/index/relationship/index2?unique", "body" => { "key" => "key2", "value" => "value2", "type" => "type2", "start" => "{0}", "end" => "{1}" } }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:create_unique_relationship, "index1", "key1", "value1", "type1", "node1", "node2" ],
[:create_unique_relationship, "index2", "key2", "value2", "type2", "{0}", "{1}" ]
end
Expand All @@ -164,7 +164,7 @@ class Rest
{ "id" => 1, "method" => "POST", "to" => "/index/relationship/index2", "body" => { "uri" => "{0}", "key" => "key2", "value" => "value2" } }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:add_relationship_to_index, "index1", "key1", "value1", "rel1" ],
[:add_relationship_to_index, "index2", "key2", "value2", "{0}"]
end
Expand All @@ -175,7 +175,7 @@ class Rest
{ "id" => 1, "method" => "GET", "to" => "/index/relationship/qux/quux/corge" }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:get_relationship_index, "foo", "bar", "baz" ],
[:get_relationship_index, "qux", "quux", "corge" ]
end
Expand All @@ -187,7 +187,7 @@ class Rest
{ "id" => 2, "method" => "DELETE", "to" => "/index/relationship/index3/key3/value3/id3" }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:remove_relationship_from_index, "index1", "id1", ],
[:remove_relationship_from_index, "index2", "key2", "id2" ],
[:remove_relationship_from_index, "index3", "key3", "value3", "id3" ]
Expand All @@ -199,7 +199,7 @@ class Rest
{ "id" => 1, "method" => "PUT", "to" => "/relationship/index2/properties/key2", "body" => "value2" }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:set_relationship_property, "index1", { "key1" => "value1" } ],
[:set_relationship_property, "index2", { "key2" => "value2" } ]
end
Expand All @@ -210,7 +210,7 @@ class Rest
{ "id" => 1, "method" => "PUT", "to" => "{0}/properties", "body" => { "key2" => "value2" } }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:reset_relationship_properties, "index1", { "key1" => "value1" } ],
[:reset_relationship_properties, "{0}", { "key2" => "value2" } ]
end
Expand All @@ -221,7 +221,7 @@ class Rest
{ "id" => 1, "method" => "POST", "to" => "/gremlin", "body" => { "script" => "script2", "params" => "params2" } }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:execute_script, "script1", "params1"],
[:execute_script, "script2", "params2"]
end
Expand All @@ -232,7 +232,7 @@ class Rest
{ "id" => 1, "method" => "POST", "to" => "/cypher", "body" => { "query" => "query2" } }
]

connection.should_receive(:post).with("/batch", json_match(:body, expected_body))
connection.should_receive(:post_chunked).with("/batch", json_match(:body, expected_body))
subject.execute [:execute_query, "query1", "params1"],
[:execute_query, "query2" ]
end
Expand Down

0 comments on commit f0bc117

Please sign in to comment.