Skip to content

Commit

Permalink
Merging upstream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
David Pitman committed Jan 8, 2012
2 parents 51d5c35 + eb03523 commit ebda23d
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 39 deletions.
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
PATH
remote: .
specs:
neography (0.0.18)
neography (0.0.20)
crack (= 0.1.8)
httparty (= 0.8.1)
json
os
rake (>= 0.8.7)
rubyzip

GEM
Expand Down
33 changes: 32 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ in order to access the functionality.
=== Dependencies

for use:
os
rake
json
httparty

Expand Down Expand Up @@ -133,8 +135,10 @@ To Use:
@neo.get_relationship_index(index, key, value) # exact query of the relationship index with the given key/value pair
@neo.find_relationship_index(index, key, value) # advanced query of the relationship index with the given key/value pair
@neo.find_relationship_index(index, query) # advanced query of the relationship index with the given query
@neo.execute_script("g.v(0)") # sends a Groovy script(through the Gremlin plugin)
@neo.execute_script("g.v(0)") # sends a Groovy script (through the Gremlin plugin)
@neo.execute_script("g.v(id)", {:id => 3}) # sends a parameterized Groovy script (optimized for repeated calls)
@neo.execute_query("start n=node(0) return n") # sends a Cypher query (through the Cypher plugin)
@neo.execute_query("start n=node(id) return n", {:id => 3}) # sends a parameterized Cypher query (optimized for repeated calls)

@neo.get_path(node1, node2, relationships, depth=4, algorithm="shortestPath") # finds the shortest path between two nodes
@neo.get_paths(node1, node2, relationships, depth=3, algorithm="allPaths") # finds all paths between two nodes
Expand All @@ -158,6 +162,33 @@ To Use:

Please see the specs for more examples.

Batch (in progress):

@neo.batch [:get_node, node1], [:get_node, node2] # Gets two nodes in a batch
@neo.batch [:create_node, {"name" => "Max"}],
[:create_node, {"name" => "Marc"}] # Creates two nodes in a batch
@neo.batch [:set_node_property, node1, {"name" => "Tom"}],
[:set_node_property, node2, {"name" => "Jerry"}] # Sets the property of two nodes
@neo.batch [:get_relationship, rel1],
[:get_relationship, rel2] # Gets two relationships in a batch
@neo.batch [:create_relationship, "friends",
node1, node2, {:since => "high school"}],
[:create_relationship, "friends",
node1, node3, {:since => "college"}] # Creates two relationships in a batch
@neo.batch [:create_node, {"name" => "Max"}],
[:create_node, {"name" => "Marc"}], # Creates two nodes and index them
[:add_node_to_index, "test_node_index", key, value, "{0}"],
[:add_node_to_index, "test_node_index", key, value, "{1}"],
[:create_relationship, "friends", # and create a relationship for those
"{0}", "{1}", {:since => "college"}], # newly created nodes
[:add_relationship_to_index,
"test_relationship_index", key, value, "{4}"] # and index the new relationship

@neo.batch *[[:create_node, {"name" => "Max"}],
[:create_node, {"name" => "Marc"}]] # Use the Splat (*) with Arrays of Arrays

See http://docs.neo4j.org/chunked/milestone/rest-api-batch-ops.html for Neo4j Batch operations documentation.

Experimental:

nodes = @neo.create_nodes(5) # Create 5 empty nodes
Expand Down
46 changes: 42 additions & 4 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ def create_node_index(name, type = "exact", provider = "lucene")

def add_node_to_index(index, key, value, id)
options = { :body => ({:uri => self.configuration + "/node/#{get_id(id)}", :key => key, :value => value }).to_json, :headers => {'Content-Type' => 'application/json'} }
#post("/index/node/#{index}/#{key}/#{value}", options)
post("/index/node/#{index}", options)
end

Expand Down Expand Up @@ -354,19 +353,58 @@ def get_paths(from, to, relationships, depth=1, algorithm="allPaths")
paths = post("/node/#{get_id(from)}/paths", options) || Array.new
end

def execute_query(query)
def execute_query_old(query)
options = { :body => {:query => query}.to_json, :headers => {'Content-Type' => 'application/json'} }
result = post("/ext/CypherPlugin/graphdb/execute_query", options)
end

def execute_query(query, params = {})
options = { :body => {:query => query, :params => params}.to_json, :headers => {'Content-Type' => 'application/json'} }
result = post("/ext/CypherPlugin/graphdb/execute_query", options)
end

def execute_script(script)
options = { :body => "script=" + CGI::escape(script), :headers => {'Content-Type' => 'application/x-www-form-urlencoded'} }
def execute_script(script, params = {})
options = { :body => {:script => script, :params => params}.to_json , :headers => {'Content-Type' => 'application/json'} }
result = post("/ext/GremlinPlugin/graphdb/execute_script", options)
result == "null" ? nil : result
end

def batch(*args)
batch = []
Array(args).each_with_index do |c,i|
batch << {:id => i}.merge(get_batch(c))
end
options = { :body => batch.to_json, :headers => {'Content-Type' => 'application/json'} }
post("/batch", options)
end

private

def get_batch(args)
case args[0]
when :get_node
{:method => "GET", :to => "/node/#{get_id(args[1])}"}
when :create_node
{:method => "POST", :to => "/node/", :body => args[1]}
when :set_node_property
{:method => "PUT", :to => "/node/#{get_id(args[1])}/properties/#{args[2].keys.first}", :body => args[2].values.first}
when :reset_node_properties
{:method => "PUT", :to => "/node/#{get_id(args[1])}/properties", :body => args[2]}
when :get_relationship
{:method => "GET", :to => "/relationship/#{get_id(args[1])}"}
when :create_relationship
{:method => "POST", :to => (args[2].is_a?(String) && args[2].start_with?("{") ? "" : "/node/") + "#{get_id(args[2])}/relationships", :body => {:to => (args[3].is_a?(String) && args[3].start_with?("{") ? "" : "/node/") + "#{get_id(args[3])}", :type => args[1], :data => args[4] } }
when :set_relationship_property
{:method => "PUT", :to => "/relationship/#{get_id(args[1])}/properties/#{args[2].keys.first}", :body => args[2].values.first}
when :reset_relationship_properties
{:method => "PUT", :to => "/relationship/#{get_id(args[1])}/properties", :body => args[2]}
when :add_node_to_index
{:method => "POST", :to => "/index/node/#{args[1]}", :body => {:uri => (args[4].is_a?(String) && args[4].start_with?("{") ? "" : "/node/") + "#{get_id(args[4])}", :key => args[2], :value => args[3] } }
when :add_relationship_to_index
{:method => "POST", :to => "/index/relationship/#{args[1]}", :body => {:uri => (args[4].is_a?(String) && args[4].start_with?("{") ? "" : "/relationship/") + "#{get_id(args[4])}", :key => args[2], :value => args[3] } }
end
end

def evaluate_response(response)
code = response.code
body = response.body
Expand Down
2 changes: 1 addition & 1 deletion lib/neography/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Neography
VERSION = "0.0.18"
VERSION = "0.0.20"
end
1 change: 1 addition & 0 deletions neography.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "rake", "~> 0.8.7"
s.add_dependency "crack", "0.1.8"
s.add_dependency "httparty", "0.8.1"
s.add_dependency "rake", ">= 0.8.7"
s.add_dependency "json"
s.add_dependency "os"
s.add_dependency "rubyzip"
Expand Down
205 changes: 173 additions & 32 deletions spec/integration/rest_batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,214 @@

describe "simple batch" do
it "can get a single node" do
pending
new_node = @neo.create_node
new_node[:id] = new_node["self"].split('/').last
batch_result = @neo.batch [:get_node, new_node]
batch_result.first.should_not be_nil
batch_result.first.should have_key("id")
batch_result.first.should have_key("body")
batch_result.first.should have_key("from")
batch_result.first["body"]["self"].split('/').last.should == new_node[:id]
end

it "can get multiple nodes" do
pending
end
node1 = @neo.create_node
node1[:id] = node1["self"].split('/').last
node2 = @neo.create_node
node2[:id] = node2["self"].split('/').last

it "can create a single node" do
pending
end
batch_result = @neo.batch [:get_node, node1], [:get_node, node2]
batch_result.first.should_not be_nil
batch_result.first.should have_key("id")
batch_result.first.should have_key("body")
batch_result.first.should have_key("from")
batch_result.first["body"]["self"].split('/').last.should == node1[:id]
batch_result.last.should have_key("id")
batch_result.last.should have_key("body")
batch_result.last.should have_key("from")
batch_result.last["body"]["self"].split('/').last.should == node2[:id]

it "can create multiple nodes" do
pending
end

it "can update a single node" do
pending
it "can create a single node" do
batch_result = @neo.batch [:create_node, {"name" => "Max"}]
batch_result.first["body"]["data"]["name"].should == "Max"
end

it "can update multiple nodes" do
pending
it "can create multiple nodes" do
batch_result = @neo.batch [:create_node, {"name" => "Max"}], [:create_node, {"name" => "Marc"}]
batch_result.first["body"]["data"]["name"].should == "Max"
batch_result.last["body"]["data"]["name"].should == "Marc"
end

it "can create multiple nodes given an *array" do
batch_result = @neo.batch *[[:create_node, {"name" => "Max"}], [:create_node, {"name" => "Marc"}]]
batch_result.first["body"]["data"]["name"].should == "Max"
batch_result.last["body"]["data"]["name"].should == "Marc"
end


it "can update a property of a node" do
new_node = @neo.create_node("name" => "Max")
batch_result = @neo.batch [:set_node_property, new_node, {"name" => "Marc"}]
batch_result.first.should have_key("id")
batch_result.first.should have_key("from")
existing_node = @neo.get_node(new_node)
existing_node["data"]["name"].should == "Marc"
end

it "can update a property of multiple nodes" do
node1 = @neo.create_node("name" => "Max")
node2 = @neo.create_node("name" => "Marc")
batch_result = @neo.batch [:set_node_property, node1, {"name" => "Tom"}], [:set_node_property, node2, {"name" => "Jerry"}]
batch_result.first.should have_key("id")
batch_result.first.should have_key("from")
batch_result.last.should have_key("id")
batch_result.last.should have_key("from")
existing_node = @neo.get_node(node1)
existing_node["data"]["name"].should == "Tom"
existing_node = @neo.get_node(node2)
existing_node["data"]["name"].should == "Jerry"
end

it "can reset the properties of a node" do
new_node = @neo.create_node("name" => "Max", "weight" => 200)
batch_result = @neo.batch [:reset_node_properties, new_node, {"name" => "Marc"}]
batch_result.first.should have_key("id")
batch_result.first.should have_key("from")
existing_node = @neo.get_node(new_node)
existing_node["data"]["name"].should == "Marc"
existing_node["data"]["weight"].should be_nil
end

it "can reset the properties of multiple nodes" do
node1 = @neo.create_node("name" => "Max", "weight" => 200)
node2 = @neo.create_node("name" => "Marc", "weight" => 180)
batch_result = @neo.batch [:reset_node_properties, node1, {"name" => "Tom"}], [:reset_node_properties, node2, {"name" => "Jerry"}]
batch_result.first.should have_key("id")
batch_result.first.should have_key("from")
batch_result.last.should have_key("id")
batch_result.last.should have_key("from")
existing_node = @neo.get_node(node1)
existing_node["data"]["name"].should == "Tom"
existing_node["data"]["weight"].should be_nil
existing_node = @neo.get_node(node2)
existing_node["data"]["name"].should == "Jerry"
existing_node["data"]["weight"].should be_nil
end

it "can get a single relationship" do
pending
node1 = @neo.create_node
node2 = @neo.create_node
new_relationship = @neo.create_relationship("friends", node1, node2)
batch_result = @neo.batch [:get_relationship, new_relationship]
batch_result.first["body"]["type"].should == "friends"
batch_result.first["body"]["start"].split('/').last.should == node1["self"].split('/').last
batch_result.first["body"]["end"].split('/').last.should == node2["self"].split('/').last
batch_result.first["body"]["self"].should == new_relationship["self"]
end

it "can get multiple relationships" do
pending
end

it "can create a single relationship" do
pending
end

it "can create multiple relationships" do
pending
node1 = @neo.create_node
node2 = @neo.create_node
batch_result = @neo.batch [:create_relationship, "friends", node1, node2, {:since => "high school"}]
batch_result.first["body"]["type"].should == "friends"
batch_result.first["body"]["data"]["since"].should == "high school"
batch_result.first["body"]["start"].split('/').last.should == node1["self"].split('/').last
batch_result.first["body"]["end"].split('/').last.should == node2["self"].split('/').last
end

it "can update a single relationship" do
pending
end

it "can update multiple relationships" do
pending
node1 = @neo.create_node
node2 = @neo.create_node
new_relationship = @neo.create_relationship("friends", node1, node2, {:since => "high school"})
batch_result = @neo.batch [:set_relationship_property, new_relationship, {:since => "college"}]
batch_result.first.should have_key("id")
batch_result.first.should have_key("from")
existing_relationship = @neo.get_relationship(new_relationship)
existing_relationship["type"].should == "friends"
existing_relationship["data"]["since"].should == "college"
existing_relationship["start"].split('/').last.should == node1["self"].split('/').last
existing_relationship["end"].split('/').last.should == node2["self"].split('/').last
existing_relationship["self"].should == new_relationship["self"]
end

it "can add a node to an index" do
pending
new_node = @neo.create_node
key = generate_text(6)
value = generate_text
new_index = @neo.get_node_index("test_node_index", key, value)
batch_result = @neo.batch [:add_node_to_index, "test_node_index", key, value, new_node]
batch_result.first.should have_key("id")
batch_result.first.should have_key("from")
existing_index = @neo.find_node_index("test_node_index", key, value)
existing_index.should_not be_nil
existing_index.first["self"].should == new_node["self"]
@neo.remove_node_from_index("test_node_index", key, value, new_node)
end
end

describe "referenced batch" do
it "can create a relationship from two newly created nodes" do
pending
batch_result = @neo.batch [:create_node, {"name" => "Max"}], [:create_node, {"name" => "Marc"}], [:create_relationship, "friends", "{0}", "{1}", {:since => "high school"}]
batch_result.first["body"]["data"]["name"].should == "Max"
batch_result[1]["body"]["data"]["name"].should == "Marc"
batch_result.last["body"]["type"].should == "friends"
batch_result.last["body"]["data"]["since"].should == "high school"
batch_result.last["body"]["start"].split('/').last.should == batch_result.first["body"]["self"].split('/').last
batch_result.last["body"]["end"].split('/').last.should == batch_result[1]["body"]["self"].split('/').last
end

it "can create a relationship from an existing node and a newly created node" do
pending
node1 = @neo.create_node("name" => "Max", "weight" => 200)
batch_result = @neo.batch [:create_node, {"name" => "Marc"}], [:create_relationship, "friends", "{0}", node1, {:since => "high school"}]
batch_result.first["body"]["data"]["name"].should == "Marc"
batch_result.last["body"]["type"].should == "friends"
batch_result.last["body"]["data"]["since"].should == "high school"
batch_result.last["body"]["start"].split('/').last.should == batch_result.first["body"]["self"].split('/').last
batch_result.last["body"]["end"].split('/').last.should == node1["self"].split('/').last
end

it "can add a newly created node to an index" do
pending
key = generate_text(6)
value = generate_text
new_index = @neo.get_node_index("test_node_index", key, value)
batch_result = @neo.batch [:create_node, {"name" => "Max"}], [:add_node_to_index, "test_node_index", key, value, "{0}"]
batch_result.first.should have_key("id")
batch_result.first.should have_key("from")
existing_index = @neo.find_node_index("test_node_index", key, value)
existing_index.should_not be_nil
existing_index.first["self"].should == batch_result.first["body"]["self"]
@neo.remove_node_from_index("test_node_index", key, value, batch_result.first["body"]["self"].split('/').last)
end

it "can add a newly created relationship to an index" do
pending
key = generate_text(6)
value = generate_text
node1 = @neo.create_node
node2 = @neo.create_node
batch_result = @neo.batch [:create_relationship, "friends", node1, node2, {:since => "high school"}], [:add_relationship_to_index, "test_relationship_index", key, value, "{0}"]
batch_result.first["body"]["type"].should == "friends"
batch_result.first["body"]["data"]["since"].should == "high school"
batch_result.first["body"]["start"].split('/').last.should == node1["self"].split('/').last
batch_result.first["body"]["end"].split('/').last.should == node2["self"].split('/').last
existing_index = @neo.find_relationship_index("test_relationship_index", key, value)
existing_index.should_not be_nil
existing_index.first["self"].should == batch_result.first["body"]["self"]

end

it "can kitchen sink" do
key = generate_text(6)
value = generate_text

batch_result = @neo.batch [:create_node, {"name" => "Max"}],
[:create_node, {"name" => "Marc"}],
[:add_node_to_index, "test_node_index", key, value, "{0}"]
[:add_node_to_index, "test_node_index", key, value, "{1}"]
[:create_relationship, "friends", "{0}", "{1}", {:since => "college"}]
[:add_relationship_to_index, "test_relationship_index", key, value, "{4}"]
batch_result.should_not be_nil
end
end

Expand Down
Loading

0 comments on commit ebda23d

Please sign in to comment.