Skip to content

Commit

Permalink
Refactor Relationship Indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 27, 2014
1 parent c2b5fb3 commit 9af9aeb
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 203 deletions.
43 changes: 2 additions & 41 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require 'neography/rest/helpers'
require 'neography/rest/paths'

require 'neography/rest/indexes'
require 'neography/rest/auto_indexes'
require 'neography/rest/schema_indexes'

Expand Down Expand Up @@ -51,6 +50,7 @@ class Rest
include OtherNodeRelationships
include NodeIndexes
include NodeAutoIndexes
include RelationshipIndexes
extend Forwardable

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

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

@cypher ||= Cypher.new(@connection)
Expand Down Expand Up @@ -103,45 +102,7 @@ def get_relationship_start_node(rel)

def get_relationship_end_node(rel)
get_node(rel["end"])
end

# relationship indexes

def list_relationship_indexes
@relationship_indexes.list
end

def create_relationship_index(name, type = "exact", provider = "lucene")
@relationship_indexes.create(name, type, provider)
end

def create_relationship_auto_index(type = "exact", provider = "lucene")
@relationship_indexes.create_auto(type, provider)
end

def create_unique_relationship(index, key, value, type, from, to, props = nil)
@relationship_indexes.create_unique(index, key, value, type, from, to, props)
end

def add_relationship_to_index(index, key, value, id)
@relationship_indexes.add(index, key, value, id)
end

def remove_relationship_from_index(index, id_or_key, id_or_value = nil, id = nil)
@relationship_indexes.remove(index, id_or_key, id_or_value, id)
end

def get_relationship_index(index, key, value)
@relationship_indexes.get(index, key, value)
end

def find_relationship_index(index, key_or_query, value = nil)
@relationship_indexes.find(index, key_or_query, value)
end

def drop_relationship_index(index)
@relationship_indexes.drop(index)
end
end

# relationship auto indexes

Expand Down
6 changes: 3 additions & 3 deletions lib/neography/rest/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def create_relationship(type, from, to, data = nil)
# RelationshipIndexes

def create_unique_relationship(index, key, value, type, from, to, props = nil)
post RelationshipIndexes.unique_path(:index => index) do
post "/index/relationship/%{index}?unique" % {:index => index} do
{
:key => key,
:value => value,
Expand All @@ -172,7 +172,7 @@ def create_unique_relationship(index, key, value, type, from, to, props = nil)
end

def add_relationship_to_index(index, key, value, id)
post RelationshipIndexes.base_path(:index => index) do
post "/index/relationship/%{index}" % {:index => index} do
{
:uri => build_relationship_uri(id),
:key => key,
Expand All @@ -182,7 +182,7 @@ def add_relationship_to_index(index, key, value, id)
end

def get_relationship_index(index, key, value)
get RelationshipIndexes.key_value_path(:index => index, :key => key, :value => value)
get "/index/relationship/%{index}/%{key}/%{value}" % {:index => index, :key => key, :value => encode(value)}
end

def remove_relationship_from_index(index, key_or_id, value_or_id = nil, id = nil)
Expand Down
102 changes: 0 additions & 102 deletions lib/neography/rest/indexes.rb

This file was deleted.

118 changes: 104 additions & 14 deletions lib/neography/rest/relationship_indexes.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,99 @@
module Neography
class Rest
class RelationshipIndexes < Indexes
extend Neography::Rest::Paths
module RelationshipIndexes
include Neography::Rest::Helpers

add_path :all, "/index/relationship"
add_path :base, "/index/relationship/:index"
add_path :unique, "/index/relationship/:index?unique"
add_path :id, "/index/relationship/:index/:id"
add_path :key, "/index/relationship/:index/:key/:id"
add_path :value, "/index/relationship/:index/:key/:value/:id"
add_path :key_value, "/index/relationship/:index/:key/:value"
add_path :query, "/index/relationship/:index?query=:query"
def list_relationship_indexes
@connection.get("/index/relationship")
end

def create_relationship_index(name, type = "exact", provider = "lucene", extra_config = nil)
config = {
:type => type,
:provider => provider
}
config.merge!(extra_config) unless extra_config.nil?
options = {
:body => (
{ :name => name,
:config => config
}
).to_json,
:headers => json_content_type
}
@connection.post("/index/relationship", options)
end

def create_relationship_auto_index(type = "exact", provider = "lucene")
create_relationship_index("relationship_auto_index", type, provider)
end

def initialize(connection)
super(connection, :relationship)
def add_relationship_to_index(index, key, value, id, unique = false)
options = {
:body => (
{ :uri => @connection.configuration + "/relationship/#{get_id(id)}",
:key => key,
:value => value
}
).to_json,
:headers => json_content_type
}
path = unique ? "/index/relationship/%{index}?unique" % {:index => index} : "/index/relationship/%{index}" % {:index => index}
@connection.post(path, options)
end

def create_unique(index, key, value, type, from, to, props = nil)
def get_relationship_index(index, key, value)
index = @connection.get("/index/relationship/%{index}/%{key}/%{value}" % {:index => index, :key => key, :value => encode(value)}) || []
return nil if index.empty?
index
end

def find_relationship_index(index, key_or_query, value = nil)
if value
index = find_relationship_index_by_key_value(index, key_or_query, value)
else
index = find_relationship_index_by_query(index, key_or_query)
end
return nil if index.empty?
index
end

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

def find_relationship_index_by_query(index, query)
@connection.get("/index/relationship/%{index}?query=%{query}" % {:index => index, :query => encode(query)}) || []
end

# Mimick original neography API in Rest class.
def remove_relationship_from_index(index, id_or_key, id_or_value = nil, id = nil)
if id
remove_relationship_index_by_value(index, id, id_or_key, id_or_value)
elsif id_or_value
remove_relationship_index_by_key(index, id_or_value, id_or_key)
else
remove_relationship_index_by_id(index, id_or_key)
end
end

def remove_relationship_index_by_id(index, id)
@connection.delete("/index/relationship/%{index}/%{id}" % {:index => index, :id => get_id(id)})
end

def remove_relationship_index_by_key(index, id, key)
@connection.delete("/index/relationship/%{index}/%{key}/%{id}" % {:index => index, :id => get_id(id), :key => key})
end

def remove_relationship_index_by_value(index, id, key, value)
@connection.delete("/index/relationship/%{index}/%{key}/%{value}/%{id}" % {:index => index, :id => get_id(id), :key => key, :value => encode(value)})
end

def drop_relationship_index(index)
@connection.delete("/index/relationship/%{index}" % {:index => index})
end

def create_unique_relationship(index, key, value, type, from, to, props = nil)
body = {
:key => key,
:value => value,
Expand All @@ -28,7 +104,21 @@ def create_unique(index, key, value, type, from, to, props = nil)
}
options = { :body => body.to_json, :headers => json_content_type }

@connection.post(unique_path(:index => index), options)
@connection.post("/index/relationship/%{index}?unique" % {:index => index}, options)
end

def get_or_create_unique_relationship(index, key, value, properties = {})
options = {
:body => (
{ :properties => properties,
:key => key,
:value => value
}
).to_json,
:headers => json_content_type
}
@connection.post("/index/relationship/%{index}?uniqueness=%{function}" % {:index => index, :function => 'get_or_create'}, options)

end

end
Expand Down
Loading

0 comments on commit 9af9aeb

Please sign in to comment.