From 825ce11e3fade3c9d57bbf358fa39abcaf30ecbe Mon Sep 17 00:00:00 2001 From: benoit Date: Fri, 4 Jul 2014 14:20:50 +0200 Subject: [PATCH] Add cypher batch capability --- keymaker.gemspec | 2 + lib/keymaker.rb | 1 + .../parsers/cypher_response_parser.rb | 6 +++ lib/keymaker/requests/batch_cypher_request.rb | 35 +++++++++++++++++ lib/keymaker/service.rb | 4 ++ .../requests/batch_cypher_request_spec.rb | 39 +++++++++++++++++++ 6 files changed, 87 insertions(+) create mode 100644 lib/keymaker/requests/batch_cypher_request.rb create mode 100644 spec/keymaker/requests/batch_cypher_request_spec.rb diff --git a/keymaker.gemspec b/keymaker.gemspec index e6f3b00..84560f5 100644 --- a/keymaker.gemspec +++ b/keymaker.gemspec @@ -53,6 +53,7 @@ Gem::Specification.new do |s| lib/keymaker/railtie.rb lib/keymaker/request.rb lib/keymaker/requests/add_node_to_index_request.rb + lib/keymaker/requests/batch_cypher_request.rb lib/keymaker/requests/batch_get_nodes_request.rb lib/keymaker/requests/batch_request.rb lib/keymaker/requests/create_node_request.rb @@ -73,6 +74,7 @@ Gem::Specification.new do |s| spec/configuration_spec.rb spec/keymaker/node_spec.rb spec/keymaker/requests/add_node_to_index_request_spec.rb + spec/keymaker/requests/batch_cypher_request_spec.rb spec/keymaker/requests/batch_get_nodes_request_spec.rb spec/keymaker/requests/batch_request_spec.rb spec/keymaker/requests/create_node_request_spec.rb diff --git a/lib/keymaker.rb b/lib/keymaker.rb index 73b4142..9c5f9c0 100644 --- a/lib/keymaker.rb +++ b/lib/keymaker.rb @@ -20,6 +20,7 @@ require 'keymaker/request' require 'keymaker/requests/batch_request' +require 'keymaker/requests/batch_cypher_request' require 'keymaker/requests/batch_get_nodes_request' require 'keymaker/requests/create_node_request' require 'keymaker/requests/delete_node_request' diff --git a/lib/keymaker/parsers/cypher_response_parser.rb b/lib/keymaker/parsers/cypher_response_parser.rb index a5d471b..2ab0ae3 100644 --- a/lib/keymaker/parsers/cypher_response_parser.rb +++ b/lib/keymaker/parsers/cypher_response_parser.rb @@ -11,6 +11,12 @@ def self.parse(response_body) end end + def self.parse_array(response_array) + response_array.map{|response| + parse(response.body) + } + end + def self.translate_response(response_body, result) Hashie::Mash.new(Hash[sanitized_column_names(response_body).zip(result)]) end diff --git a/lib/keymaker/requests/batch_cypher_request.rb b/lib/keymaker/requests/batch_cypher_request.rb new file mode 100644 index 0000000..fdf4f76 --- /dev/null +++ b/lib/keymaker/requests/batch_cypher_request.rb @@ -0,0 +1,35 @@ +module Keymaker + + class BatchCypherRequest < BatchRequest + + attr_accessor :queries + + def initialize(service, queries) + self.config = service.config + self.queries = clean_queries(queries) + self.service = service + self.opts = build_job_descriptions_collection + end + + def build_job_descriptions_collection + [].tap do |batch_jobs| + queries.each_with_index do |query, job_id| + batch_jobs << {id: job_id, to: '/cypher', method: "POST", body: query} + end + end + end + + protected + def clean_queries(queries) + queries.map{ |query| + if query.is_a? String + { query: query} + else + query + end + } + end + + end + +end diff --git a/lib/keymaker/service.rb b/lib/keymaker/service.rb index 1564c72..2020d3e 100644 --- a/lib/keymaker/service.rb +++ b/lib/keymaker/service.rb @@ -84,6 +84,10 @@ def execute_cypher(query, params) Keymaker::CypherResponseParser.parse(response.body) end + def execute_cypher_batch(queries) + response = batch_cypher_request(queries) + Keymaker::CypherResponseParser.parse_array(response.body) + end def execute_script(script, params={}) execute_gremlin_request({script: script, params: params}) end diff --git a/spec/keymaker/requests/batch_cypher_request_spec.rb b/spec/keymaker/requests/batch_cypher_request_spec.rb new file mode 100644 index 0000000..ff0851e --- /dev/null +++ b/spec/keymaker/requests/batch_cypher_request_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' +require 'keymaker' + +describe Keymaker::BatchCypherRequest do + + describe "#build_opts_collection" do + + context "with simple queries" do + + let(:queries) { ['MATCH (n) RETURN COUNT(n)', 'MATCH (n) RETURN n LIMIT 1'] } + let(:batch_cypher_request) do + Keymaker::BatchCypherRequest.new(Keymaker.service, queries) + end + + it "builds the job descriptions collection" do + batch_cypher_request.opts.should == [ + {id: 0, to: "/cypher", method: "POST", body: { query: 'MATCH (n) RETURN COUNT(n)'}}, + {id: 1, to: "/cypher", method: "POST", body: { query: 'MATCH (n) RETURN n LIMIT 1'}}, + ] + end + + end + + context "with parametrized query" do + + let(:queries) { [{query: 'MATCH (n { property: {property} }) RETURN n', :params => { property: 3 }}] } + let(:batch_cypher_request) do + Keymaker::BatchCypherRequest.new(Keymaker.service, queries) + end + + it "builds the job descriptions collection" do + batch_cypher_request.opts.should == [ + {id: 0, to: "/cypher", method: "POST", body: {query: 'MATCH (n { property: {property} }) RETURN n', :params => { property: 3 } }} + ] + end + + end + end +end