Skip to content

Commit

Permalink
starting phase 2
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Dec 1, 2010
1 parent 8e66583 commit 7b90ece
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ GEM
fakeweb (1.3.0)
httparty (0.6.1)
crack (= 0.1.8)
json (1.4.6)
json (1.4.6-java)
net-http-spy (0.2.1)
rspec (2.0.1)
Expand All @@ -28,6 +29,7 @@ GEM

PLATFORMS
java
ruby

DEPENDENCIES
fakeweb (~> 1.3.0)
Expand Down
19 changes: 19 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ Experimental:
another_node = @neo.create_node("age" => 31, "name" => "Max")
nodes = @neo.get_nodes([one_set_nodes, another_node]) # Get four nodes

Phase 2 (work in progress):

Now we are returning full objects. The properties of the node or relationship can be accessed directly (node.name).
The Neo4j ID is available by using node.neo_id .

@neo2 = Neography::Rest.new ('http://', '192.168.10.1')

Neography::Node.create # Create an empty node
Neography::Node.create("age" => 31, "name" => "Max") # Create a node with some properties
Neography::Node.create(@neo2, {"age" => 31, "name" => "Max"}) # Create a node on the server defined in @neo2
Neography::Node.create({"age" => 31, "name" => "Max"}, @neo2) # Same as above, but different order

Neography::Node.load(5) # Get a node and its properties by id
Neography::Node.load(existing_node) # Get a node and its properties by Node
Neography::Node.load("http://localhost:7474/db/data/node/2") # Get a node and its properties by String

Neography::Node.load(@neo2, 5) # Get a node on the server defined in @neo2
Neography::Node.load(5, @neo2) # Same as above, but different order


See Neo4j API for:
* {Order}[http://components.neo4j.org/neo4j-examples/1.2.M04/apidocs/org/neo4j/graphdb/Traverser.Order.html]
Expand Down
15 changes: 15 additions & 0 deletions lib/neography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,23 @@ def find_and_require_user_defined_code
require 'httparty'
require 'json'
require 'logger'
require 'ostruct'

require 'neography/config'
require 'neography/rest'
require 'neography/property_container'
require 'neography/node'
require 'neography/relationship'

find_and_require_user_defined_code

module Neography

class << self

def ref_node(this_db = Neography::Rest.new)
this_db.get_root
end

end
end
14 changes: 14 additions & 0 deletions lib/neography/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Neography
class Config
class << self; attr_accessor :protocol, :server, :port, :log_file, :log_enabled, :logger, :max_threads end

@protocol = 'http://'
@server = 'localhost'
@port = 7474
@log_file = 'neography.log'
@log_enabled = false
@logger = Logger.new(@log_file) if @log_enabled
@max_threads = 20

end
end
27 changes: 27 additions & 0 deletions lib/neography/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Neography
class Node < PropertyContainer

class << self
def create(*args)
# the arguments can be an hash of properties to set or a rest instance
props = (args[0].respond_to?(:each_pair) && args[0]) || args[1]
db = (args[0].is_a?(Neography::Rest) && args[0]) || args[1] || Neography::Rest.new
Neography::Node.new(db.create_node(props))
end

def load(*args)
# the first argument can be an hash of properties to set
node = !args[0].is_a?(Neography::Rest) && args[0] || args[1]

# a db instance can be given, it is the first argument or the second
db = (args[0].is_a?(Neography::Rest) && args[0]) || args[1] || Neography::Rest.new
node = db.get_node(node)
node = Neography::Node.new(node) unless node.nil?
node
end

#alias_method :new, :create
end

end
end
17 changes: 17 additions & 0 deletions lib/neography/property_container.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Neography
class PropertyContainer < OpenStruct
attr_reader :neo_id

def initialize(hash=nil)
@table = {}
if hash
@neo_id = hash["self"].split('/').last
for k,v in hash["data"]
@table[k.to_sym] = v
new_ostruct_member(k)
end
end
end

end
end
13 changes: 13 additions & 0 deletions lib/neography/relationship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Neography
class Relationship < PropertyContainer
attr_reader :start, :end, :type

def initialize(hash=nil)
super(hash)
@start = hash["start"].split('/').last
@end = hash["end"].split('/').last
@type = hash["type"]
end

end
end
16 changes: 15 additions & 1 deletion lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ class Rest
include HTTParty
attr_accessor :protocol, :server, :port, :log_file, :log_enabled, :logger, :max_threads

def initialize(protocol='http://', server='localhost', port=7474, log_file='neography.log', log_enabled=false, max_threads=20)
def initialize(protocol=Neography::Config.protocol,
server=Neography::Config.server,
port=Neography::Config.port,
log_file=Neography::Config.log_file,
log_enabled=Neography::Config.log_enabled,
max_threads=Neography::Config.max_threads)
@protocol = protocol
@server = server
@port = port
Expand Down Expand Up @@ -78,6 +83,13 @@ def create_nodes_threaded(nodes)
created_nodes
end

# This is not yet implemented in the REST API
#
# def get_all_node
# puts "get all nodes"
# get("/nodes/")
# end

def get_node(id)
get("/node/#{get_id(id)}")
end
Expand Down Expand Up @@ -286,6 +298,8 @@ def get_id(id)
id["self"].split('/').last
when String
id.split('/').last
when Neography::Node
id.neo_id
else
id
end
Expand Down
10 changes: 10 additions & 0 deletions spec/integration/neography_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe Neography do
describe "ref_node" do
it "can get the reference node" do
root_node = Neography.ref_node
root_node.should have_key("reference_node")
end
end
end
75 changes: 75 additions & 0 deletions spec/integration/node_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe Neography::Node do

describe "create and new" do
it "can create an empty node" do
new_node = Neography::Node.create
new_node.should_not be_nil
end

it "can create a node with one property" do
new_node = Neography::Node.create("name" => "Max")
new_node.name.should == "Max"
end

it "can create a node with more than one property" do
new_node = Neography::Node.create("age" => 31, "name" => "Max")
new_node.name.should == "Max"
new_node.age.should == 31
end

it "can create a node with more than one property not on the default rest server" do
@neo = Neography::Rest.new
new_node = Neography::Node.create({"age" => 31, "name" => "Max"}, @neo)
new_node.name.should == "Max"
new_node.age.should == 31
end

it "can create a node with more than one property not on the default rest server the other way" do
@neo = Neography::Rest.new
new_node = Neography::Node.create(@neo, {"age" => 31, "name" => "Max"})
new_node.name.should == "Max"
new_node.age.should == 31
end
end


describe "get_node" do
it "can get a node that exists" do
new_node = Neography::Node.create
existing_node = Neography::Node.load(new_node)
existing_node.should_not be_nil
existing_node.neo_id.should_not be_nil
existing_node.neo_id.should == new_node.neo_id
end

it "returns nil if it tries to get a node that does not exist" do
new_node = Neography::Node.create
fake_node = new_node.neo_id.to_i + 1000
existing_node = Neography::Node.load(fake_node)
existing_node.should be_nil
end

it "can get a node that exists not on the default rest server" do
@neo = Neography::Rest.new
new_node = Neography::Node.create(@neo)
existing_node = Neography::Node.load(new_node, @neo)
existing_node.should_not be_nil
existing_node.neo_id.should_not be_nil
existing_node.neo_id.should == new_node.neo_id
end

it "can get a node that exists not on the default rest server the other way" do
@neo = Neography::Rest.new
new_node = Neography::Node.create(@neo)
existing_node = Neography::Node.load(@neo, new_node)
existing_node.should_not be_nil
existing_node.neo_id.should_not be_nil
existing_node.neo_id.should == new_node.neo_id
end


end

end
6 changes: 3 additions & 3 deletions spec/integration/rest_bulk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

it "is faster than non-threaded?" do
Benchmark.bm do |x|
x.report("create 100 nodes ") { @not_threaded = @neo.create_nodes(100) }
x.report("create 100 nodes threaded") { @threaded = @neo.create_nodes_threaded(100) }
x.report("create 200 nodes threaded") { @threaded2c = @neo.create_nodes_threaded(200) }
x.report("create 5000 nodes ") { @not_threaded = @neo.create_nodes(5000) }
x.report("create 5000 nodes threaded") { @threaded = @neo.create_nodes_threaded(5000) }
x.report("create 100000 nodes threaded") { @threaded2c = @neo.create_nodes_threaded(10000) }
end

@not_threaded[99].should_not be_nil
Expand Down

0 comments on commit 7b90ece

Please sign in to comment.