Skip to content

Commit

Permalink
Refactor Relationship Auto Indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 27, 2014
1 parent 9af9aeb commit 0e5b349
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 66 deletions.
33 changes: 1 addition & 32 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Rest
include NodeIndexes
include NodeAutoIndexes
include RelationshipIndexes
include RelationshipAutoIndexes
extend Forwardable

attr_reader :connection
Expand All @@ -63,8 +64,6 @@ def initialize(options = ENV['NEO4J_URL'] || {})
@node_traversal ||= NodeTraversal.new(@connection)
@node_paths ||= NodePaths.new(@connection)

@relationship_auto_indexes ||= RelationshipAutoIndexes.new(@connection)

@cypher ||= Cypher.new(@connection)
@gremlin ||= Gremlin.new(@connection)
@extensions ||= Extensions.new(@connection)
Expand Down Expand Up @@ -104,36 +103,6 @@ def get_relationship_end_node(rel)
get_node(rel["end"])
end

# relationship auto indexes

def get_relationship_auto_index(key, value)
@relationship_auto_indexes.get(key, value)
end

def find_relationship_auto_index(key_or_query, value = nil)
@relationship_auto_indexes.find_or_query(key_or_query, value)
end

def get_relationship_auto_index_status
@relationship_auto_indexes.status
end

def set_relationship_auto_index_status(change_to = true)
@relationship_auto_indexes.status = change_to
end

def get_relationship_auto_index_properties
@relationship_auto_indexes.properties
end

def add_relationship_auto_index_property(property)
@relationship_auto_indexes.add_property(property)
end

def remove_relationship_auto_index_property(property)
@relationship_auto_indexes.remove_property(property)
end

# traversal

def traverse(id, return_type, description)
Expand Down
4 changes: 2 additions & 2 deletions lib/neography/rest/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def parse_direction(direction)
end

def encode(value)
CGI.escape(value).gsub("+", "%20")
CGI.escape(value.to_s).gsub("+", "%20")
end

def escape(value)
if value.class == String
"%22"+encode(value)+"%22";
"%22"+encode(value.to_s)+"%22";
else
encode(value.to_s)
end
Expand Down
62 changes: 54 additions & 8 deletions lib/neography/rest/relationship_auto_indexes.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,59 @@
module Neography
class Rest
class RelationshipAutoIndexes < AutoIndexes
extend Neography::Rest::Paths

add_path :key_value, "/index/auto/relationship/:key/:value"
add_path :query_index, "/index/auto/relationship/?query=:query"
add_path :index_status, "/index/auto/relationship/status"
add_path :index_properties, "/index/auto/relationship/properties"
add_path :index_property, "/index/auto/relationship/properties/:property"
module RelationshipAutoIndexes

def get_relationship_auto_index(key, value)
index = @connection.get("/index/auto/relationship/%{key}/%{value}" % {:key => key, :value => encode(value)}) || []
return nil if index.empty?
index
end

def find_relationship_auto_index(key_or_query, value = nil)
if value
index = find_relationship_auto_index_by_value(key_or_query, value)
else
index = query_relationship_auto_index(key_or_query)
end
return nil if index.empty?
index
end

def find_relationship_auto_index_by_value(key, value)
@connection.get("/index/auto/relationship/%{key}/%{value}" % {:key => key, :value => encode(value)}) || []
end

def query_relationship_auto_index(query_expression)
@connection.get("/index/auto/relationship/?query=%{query}" % {:query => query_expression}) || []
end

def get_relationship_auto_index_status
@connection.get("/index/auto/relationship/status")
end

def set_relationship_auto_index_status(value = true)
options = {
:body => value.to_json,
:headers => json_content_type
}
@connection.put("/index/auto/relationship/status", options)
end

def get_relationship_auto_index_properties
@connection.get("/index/auto/relationship/properties")
end

def add_relationship_auto_index_property(property)
options = {
:body => property,
:headers => json_content_type
}
@connection.post("/index/auto/relationship/properties", options)
end

def remove_relationship_auto_index_property(property)
@connection.delete("/index/auto/relationship/properties/%{property}" % {:property => property})
end


end
end
Expand Down
47 changes: 23 additions & 24 deletions spec/unit/rest/relationship_auto_indexes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,61 @@ module Neography
class Rest
describe RelationshipAutoIndexes do

let(:connection) { double }
subject { RelationshipAutoIndexes.new(connection) }
subject { Neography::Rest.new }

it "gets a relationship from an auto index" do
connection.should_receive(:get).with("/index/auto/relationship/some_key/some_value")
subject.get("some_key", "some_value")
subject.connection.should_receive(:get).with("/index/auto/relationship/some_key/some_value")
subject.get_relationship_auto_index("some_key", "some_value")
end

it "returns nil if nothing was found in the auto index" do
connection.stub(:get).and_return(nil)
subject.get("some_key", "some_value").should be_nil
subject.connection.stub(:get).and_return(nil)
subject.get_relationship_auto_index("some_key", "some_value").should be_nil
end

it "finds by key and value if value passed to #find_or_query" do
connection.should_receive(:get).with("/index/auto/relationship/some_key/some_value")
subject.find_or_query("some_key", "some_value")
subject.connection.should_receive(:get).with("/index/auto/relationship/some_key/some_value")
subject.find_relationship_auto_index("some_key", "some_value")
end

it "finds by query if no value passed to #find_or_query" do
connection.should_receive(:get).with("/index/auto/relationship/?query=some_query")
subject.find_or_query("some_query")
subject.connection.should_receive(:get).with("/index/auto/relationship/?query=some_query")
subject.find_relationship_auto_index("some_query")
end

it "finds by key and value" do
connection.should_receive(:get).with("/index/auto/relationship/some_key/some_value")
subject.find("some_key", "some_value")
subject.connection.should_receive(:get).with("/index/auto/relationship/some_key/some_value")
subject.find_relationship_auto_index("some_key", "some_value")
end

it "finds by query" do
connection.should_receive(:get).with("/index/auto/relationship/?query=some_query")
subject.query("some_query")
subject.connection.should_receive(:get).with("/index/auto/relationship/?query=some_query")
subject.find_relationship_auto_index("some_query")
end

it "gets the status" do
connection.should_receive(:get).with("/index/auto/relationship/status")
subject.status
subject.connection.should_receive(:get).with("/index/auto/relationship/status")
subject.get_relationship_auto_index_status
end

it "sets the status" do
connection.should_receive(:put).with("/index/auto/relationship/status", hash_match(:body, '"foo"'))
subject.status = "foo"
subject.connection.should_receive(:put).with("/index/auto/relationship/status", hash_match(:body, '"foo"'))
subject.set_relationship_auto_index_status("foo")
end

it "gets auto index properties" do
connection.should_receive(:get).with("/index/auto/relationship/properties")
subject.properties
subject.connection.should_receive(:get).with("/index/auto/relationship/properties")
subject.get_relationship_auto_index_properties
end

it "adds a property to an auto index" do
connection.should_receive(:post).with("/index/auto/relationship/properties", hash_match(:body, "foo"))
subject.add_property("foo")
subject.connection.should_receive(:post).with("/index/auto/relationship/properties", hash_match(:body, "foo"))
subject.add_relationship_auto_index_property("foo")
end

it "removes a property from an auto index" do
connection.should_receive(:delete).with("/index/auto/relationship/properties/foo")
subject.remove_property("foo")
subject.connection.should_receive(:delete).with("/index/auto/relationship/properties/foo")
subject.remove_relationship_auto_index_property("foo")
end

end
Expand Down

0 comments on commit 0e5b349

Please sign in to comment.