From c56574ff25951ea637d8d64f3e752a7c0c7e4964 Mon Sep 17 00:00:00 2001 From: Radu Kopetz Date: Tue, 15 Apr 2014 15:51:26 +0200 Subject: [PATCH] add batch_no_streaming method --- ChangeLog | 4 ++++ lib/neography/connection.rb | 16 +++++++++------ lib/neography/rest/batch.rb | 20 ++++++++++++++----- lib/neography/version.rb | 2 +- spec/integration/rest_batch_streaming_spec.rb | 14 +++++++++++-- 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 41805f8..5d18e91 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +v1.4.2 +======= + + v1.3.11 ======= 49e310d Bump to 1.3.11 diff --git a/lib/neography/connection.rb b/lib/neography/connection.rb index b3256e1..f66a5b6 100644 --- a/lib/neography/connection.rb +++ b/lib/neography/connection.rb @@ -35,9 +35,11 @@ def configuration def merge_options(options) merged_options = options.merge!(@authentication) - merged_options[:headers].merge!(@user_agent) if merged_options[:headers] - merged_options[:headers].merge!('X-Stream' => true) if merged_options[:headers] - merged_options[:headers].merge!(@max_execution_time) if merged_options[:headers] + if merged_options[:headers] + merged_options[:headers].merge!(@user_agent) + merged_options[:headers].merge!('X-Stream' => true) unless merged_options[:headers].key?('X-Stream') + merged_options[:headers].merge!(@max_execution_time) + end merged_options end @@ -49,7 +51,9 @@ def merge_options(options) query_path = configuration + path query_body = merge_options(options)[:body] log path, query_body do - evaluate_response(@client.send(action.to_sym, query_path, query_body, merge_options(options)[:headers]), path, query_body) + headers = merge_options(options)[:headers] + evaluate_response(@client.send(action.to_sym, query_path, query_body, headers), + path, query_body, headers['X-Stream'] == true) end end end @@ -127,8 +131,8 @@ def evaluate_chunk_response(response, result) return_result(code, result) end - def evaluate_response(response, path, query_body) - if response.http_header.request_uri.request_uri == "/db/data/batch" + def evaluate_response(response, path, query_body, streaming) + if streaming && response.http_header.request_uri.request_uri == "/db/data/batch" code, body, parsed = handle_batch(response) else code = response.code diff --git a/lib/neography/rest/batch.rb b/lib/neography/rest/batch.rb index b48b099..9d7d062 100644 --- a/lib/neography/rest/batch.rb +++ b/lib/neography/rest/batch.rb @@ -7,18 +7,28 @@ def batch(*args) do_batch(*args) end + def batch_no_streaming(*args) + do_batch_no_streaming(*args) + end + private def do_batch(*args) + @connection.post("/batch", compute_batch_options(*args)) + end + + def do_batch_no_streaming(*args) + options = compute_batch_options(*args) + options[:headers].merge!({ 'X-Stream' => false }) + @connection.post("/batch", options) + end + + def compute_batch_options(*args) batch = [] Array(args).each_with_index do |c, i| batch << {:id => i }.merge(get_batch(c)) end - options = { - :body => batch.to_json, - :headers => json_content_type - } - @connection.post("/batch", options) + {:body => batch.to_json, :headers => json_content_type} end def get_batch(args) diff --git a/lib/neography/version.rb b/lib/neography/version.rb index d658692..6864ef0 100644 --- a/lib/neography/version.rb +++ b/lib/neography/version.rb @@ -1,3 +1,3 @@ module Neography - VERSION = "1.4.1" + VERSION = "1.4.2" end diff --git a/spec/integration/rest_batch_streaming_spec.rb b/spec/integration/rest_batch_streaming_spec.rb index a72a747..a62a101 100644 --- a/spec/integration/rest_batch_streaming_spec.rb +++ b/spec/integration/rest_batch_streaming_spec.rb @@ -15,7 +15,7 @@ batch_result = @neo.batch *commands batch_result.first["body"]["data"]["name"].should == "Max 0" batch_result.last["body"]["data"]["name"].should == "Max 999" - end + end it "can send a 5000 item batch" do commands = [] @@ -25,8 +25,18 @@ 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 + # fails in batch streaming + #it "can send a 20000 item batch" do + # commands = [] + # 20000.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 19999" + #end end end