From 4deb7e27ae8ab6294bd6694ef48ec334a64af985 Mon Sep 17 00:00:00 2001 From: Glenn Goodrich Date: Tue, 30 Apr 2013 14:35:04 -0400 Subject: [PATCH] feature: Add unique to add to index --- .gitignore | 1 + lib/neography/index.rb | 6 +++--- lib/neography/rest.rb | 4 ++-- lib/neography/rest/batch.rb | 5 +++-- lib/neography/rest/indexes.rb | 6 +++--- lib/neography/rest/node_indexes.rb | 15 +++++++++++++++ spec/integration/index_spec.rb | 7 +++++++ spec/unit/rest/node_indexes_spec.rb | 10 ++++++++++ 8 files changed, 44 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 49257ca..65494ed 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ pkg/* log/* .rvmrc Gemfile.lock +*.swp diff --git a/lib/neography/index.rb b/lib/neography/index.rb index a683773..ca50698 100644 --- a/lib/neography/index.rb +++ b/lib/neography/index.rb @@ -5,9 +5,9 @@ def self.included(base) base.extend(ClassMethods) end - def add_to_index(index, key, value) + def add_to_index(index, key, value, unique = false) if self.is_a? Neography::Node - self.neo_server.add_node_to_index(index, key, value, self.neo_id) + self.neo_server.add_node_to_index(index, key, value, self.neo_id, unique) else self.neo_server.add_relationship_to_index(index, key, value, self.neo_id) end @@ -49,4 +49,4 @@ def find(*args) end end -end \ No newline at end of file +end diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 95e6ce5..598265f 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -190,8 +190,8 @@ def create_unique_node(index, key, value, props={}) @node_indexes.create_unique(index, key, value, props) end - def add_node_to_index(index, key, value, id) - @node_indexes.add(index, key, value, id) + def add_node_to_index(index, key, value, id, unique=false) + @node_indexes.add(index, key, value, id, unique) end alias_method :add_to_index, :add_node_to_index diff --git a/lib/neography/rest/batch.rb b/lib/neography/rest/batch.rb index f798dad..1b8edf9 100644 --- a/lib/neography/rest/batch.rb +++ b/lib/neography/rest/batch.rb @@ -69,8 +69,9 @@ def create_unique_node(index, key, value, properties) end end - def add_node_to_index(index, key, value, id) - post NodeIndexes.base_path(:index => index) do + def add_node_to_index(index, key, value, id, unique = false) + path = unique ? NodeIndexes.unique_path(:index => index) : NodeIndexes.base_path(:index => index) + post path do { :uri => build_node_uri(id), :key => key, diff --git a/lib/neography/rest/indexes.rb b/lib/neography/rest/indexes.rb index 6f74d67..3f35137 100644 --- a/lib/neography/rest/indexes.rb +++ b/lib/neography/rest/indexes.rb @@ -31,7 +31,7 @@ def create_auto(type = "exact", provider = "lucene") create("#{@index_type}_auto_index", type, provider) end - def add(index, key, value, id) + def add(index, key, value, id, unique = false) options = { :body => ( { :uri => @connection.configuration + "/#{@index_type}/#{get_id(id)}", @@ -41,8 +41,8 @@ def add(index, key, value, id) ).to_json, :headers => json_content_type } - - @connection.post(base_path(:index => index), options) + path = unique ? unique_path(:index => index) : base_path(:index => index) + @connection.post(path, options) end def get(index, key, value) diff --git a/lib/neography/rest/node_indexes.rb b/lib/neography/rest/node_indexes.rb index e1bf49f..6484d40 100644 --- a/lib/neography/rest/node_indexes.rb +++ b/lib/neography/rest/node_indexes.rb @@ -7,6 +7,7 @@ class NodeIndexes < Indexes add_path :all, "/index/node" add_path :base, "/index/node/:index" add_path :unique, "/index/node/:index?unique" + add_path :uniqueness, "/index/node/:index?uniqueness=:function" add_path :id, "/index/node/:index/:id" add_path :key, "/index/node/:index/:key/:id" add_path :value, "/index/node/:index/:key/:value/:id" @@ -30,6 +31,20 @@ def create_unique(index, key, value, properties = {}) @connection.post(unique_path(:index => index), options) end + def get_or_create_unique(index, key, value, properties = {}) + options = { + :body => ( + { :properties => properties, + :key => key, + :value => value + } + ).to_json, + :headers => json_content_type + } + @connection.post(uniqueness_path(:index => index, :function => 'get_or_create'), options) + + end + end end end diff --git a/spec/integration/index_spec.rb b/spec/integration/index_spec.rb index c751c90..7177e3a 100644 --- a/spec/integration/index_spec.rb +++ b/spec/integration/index_spec.rb @@ -9,6 +9,13 @@ new_node.add_to_index("node_test_index", key, value) end + it "can add a node to an index uniquely" do + new_node = Neography::Node.create + key = generate_text(6) + value = generate_text + new_node.add_to_index("node_test_index", key, value, true) + end + it "can add a relationship to an index" do node1 = Neography::Node.create node2 = Neography::Node.create diff --git a/spec/unit/rest/node_indexes_spec.rb b/spec/unit/rest/node_indexes_spec.rb index 2b5cc23..28503f7 100644 --- a/spec/unit/rest/node_indexes_spec.rb +++ b/spec/unit/rest/node_indexes_spec.rb @@ -51,6 +51,16 @@ class Rest subject.create_unique("some_index", "key", "value", "properties") end + it "gets or creates a unique node in an index" do + expected_body = { + "properties" => "properties", + "key" => "key", + "value" => "value" + } + connection.should_receive(:post).with("/index/node/some_index?uniqueness=get_or_create", json_match(:body, expected_body)) + subject.get_or_create_unique("some_index", "key", "value", "properties") + end + it "adds a node to an index" do expected_body = { "uri" => "http://configuration/node/42",