Skip to content

Commit

Permalink
Merge pull request #93 from ruprict/get_or_create_on_index
Browse files Browse the repository at this point in the history
feature: Add unique to add to index
  • Loading branch information
maxdemarzi committed May 7, 2013
2 parents b452748 + 4deb7e2 commit 589bd7b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pkg/*
log/*
.rvmrc
Gemfile.lock
*.swp
6 changes: 3 additions & 3 deletions lib/neography/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,4 +49,4 @@ def find(*args)
end

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

Expand Down
5 changes: 3 additions & 2 deletions lib/neography/rest/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,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,
Expand Down
6 changes: 3 additions & 3 deletions lib/neography/rest/indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)}",
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions lib/neography/rest/node_indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
7 changes: 7 additions & 0 deletions spec/integration/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/rest/node_indexes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 589bd7b

Please sign in to comment.