Skip to content

Commit

Permalink
Merge pull request maxdemarzi#53 from kamranjon/batch
Browse files Browse the repository at this point in the history
Adding query support (neo4j-cypher does not yet support)
  • Loading branch information
kamranjon committed Oct 24, 2013
2 parents 47244cd + 7d02653 commit 2627090
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PATH
deja (0.1)
activemodel (~> 4.0.0)
activesupport (~> 4.0.0)
neo4j-cypher (~> 1.0.0)
neo4j-cypher (~> 1.0.1)
neography (~> 1.1.3)
oj (~> 2.1.4)

Expand Down Expand Up @@ -32,14 +32,14 @@ GEM
method_source (0.8.2)
minitest (4.7.5)
multi_json (1.7.9)
neo4j-cypher (1.0.0)
neo4j-cypher (1.0.1)
neography (1.1.4)
httpclient (>= 2.3.3)
json (>= 1.7.7)
multi_json (>= 1.3.2)
os (>= 0.9.6)
rubyzip (~> 0.9.7)
oj (2.1.4)
oj (2.1.7)
os (0.9.6)
pry (0.9.12.2)
coderay (~> 1.0.5)
Expand Down
32 changes: 22 additions & 10 deletions lib/deja/bridge.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
module Deja
class Bridge
class << self
def is_index?(id_or_index)
id_or_index.is_a?(Hash)
end

def cypher(&block)
Neo4j::Cypher.query(&block)
end

def is_index?(id)
id.is_a?(Hash) && id[:index] && id[:key] && id[:value]
end

def is_query?(id)
id.is_a?(Hash) && id[:index] && id[:query]
end

## these methods take a cypher block context as an argument,
## it allows us to treat nodes/rels the same regardless of index or id
def node(id, context, return_root = true)
if return_root
is_index?(id) ? context.lookup(id[:index], id[:key], id[:value]).ret : context.node(id).ret
return context.lookup(id[:index], id[:key], id[:value]).ret if is_index?(id)
return context.query(id[:index], id[:query]).ret if is_query?(id)
context.node(id).ret
else
is_index?(id) ? context.lookup(id[:index], id[:key], id[:value]) : context.node(id)
return context.lookup(id[:index], id[:key], id[:value]) if is_index?(id)
return context.query(id[:index], id[:query]) if is_query?(id)
context.node(id)
end
end

def rel(id, context, return_root = true)
if return_root
is_index?(id) ? context.lookup_rel(id[:index], id[:key], id[:value]).ret : context.rel(id).ret
return context.lookup_rel(id[:index], id[:key], id[:value]).ret if is_index?(id)
return context.query_rel(id[:index], id[:query]).ret if is_query?(id)
context.rel(id).ret
else
is_index?(id) ? context.lookup_rel(id[:index], id[:key], id[:value]) : context.rel(id)
return context.lookup_rel(id[:index], id[:key], id[:value]) if is_index?(id)
return context.query_rel(id[:index], id[:query]) if is_query?(id)
context.rel(id)
end
end

Expand Down Expand Up @@ -74,7 +86,7 @@ def update_node(id, attributes)
attributes.each do |key, value|
n[key] = value
end
end
end.ret
end
end

Expand All @@ -96,7 +108,7 @@ def update_relationship(id, attributes = {})
attributes.each do |key, value|
r[key] = value
end
end
end.ret
end
end

Expand Down
12 changes: 10 additions & 2 deletions spec/bridge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@
end

describe ".is_index" do
context "given a hash" do
context "given a hash with correct structure" do
it "should return true" do
Deja::Bridge.is_index?({}).should be_true
Deja::Bridge.is_index?({:index => '', :key => '', :value => ''}).should be_true
end
end
end

describe ".is_query" do
context "given a hash with correct structure" do
it "should return true" do
Deja::Bridge.is_query?({:index => '', :query => ''}).should be_true
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,34 @@ class InvestedIn < Relationship; end
end
end
end

describe "auto indexing of nodes" do
before :each do
Deja.get_node_auto_index_properties.each do |index_property|
Deja.remove_node_auto_index_property(index_property)
end
Deja.set_node_auto_index_status(false)
end

context "creating a node after auto index is set to true" do
it "should add the node to the auto index" do
Deja.set_node_auto_index_status(true)
Deja.add_node_auto_index_property('name')
@first_node.save()
@grabit = Deja::Node.find({:index => 'node_auto_index', :key => 'name', :value => @first_node.name}, :include => :none)
@grabit.id.should eq(@first_node.id)
end
end

context "creating a node after index is created on two properties" do
it "should allow queries across two keys" do
Deja.set_node_auto_index_status(true)
Deja.add_node_auto_index_property('name')
Deja.add_node_auto_index_property('type')
@first_node.save()
#@grabit = Deja::Node.find({:index => 'node_auto_index', :query => "name:#{@first_node.name}"}, :include => :none)
#@grabit.id.should eq(@first_node.id)
end
end
end
end

0 comments on commit 2627090

Please sign in to comment.