Permalink
Browse files

Found one bug with Neo4j::Rails::Model.outgoing/incoming/rels

  • Loading branch information...
andreasronge committed Apr 6, 2011
1 parent 661fc79 commit 09ef389c9ce4cce9809d5db81ebf24f8a1b83a49
@@ -153,6 +153,7 @@ def update
def create
node = Neo4j::Node.new
@_java_node = node
+ puts "CREATE NODE #{neo_id}/#{self.object_id} caller #{caller.inspect}"
init_on_create
clear_changes
clear_relationships
@@ -156,6 +156,7 @@ def create()
return false unless end_node.save
end
@_java_rel = Neo4j::Relationship.new(type, start_node, end_node)
+ puts "CREATE REL #{self}, caller #{caller.inspect}"
init_on_create
clear_changes
true
@@ -26,27 +26,20 @@ def other_node(node) # TODO - compare neo_id instead ?
alias_method :get_other_node, :other_node # so it looks like the java version
def to_s
- "id: #{self.object_id} start_node: #{start_node} end_node: #{end_node} type:#{@type}"
+ neoid = _java_rel.nil? ? "not persisted" : _java_rel.neo_id
+ "objid: #{self.object_id} neo_id: #{neoid} start_node: #{start_node} end_node: #{end_node} type:#{@type}"
end
def id
_java_rel.nil? || neo_id.nil? ? nil : neo_id.to_s
end
def start_node
- if persisted?
- _java_rel.start_node
- else
- @start_node
- end
+ @start_node ||= _java_rel && _java_rel.start_node
end
def end_node
- if persisted?
- _java_rel.end_node
- else
- @end_node
- end
+ @end_node ||= _java_rel && _java_rel.start_node
end
def del
@@ -56,8 +56,10 @@ def create_relationship_to(other_node, rel_type) #:nodoc:
def incoming(rel_type)
storage = _create_or_get_storage(rel_type)
if persisted? && !storage.modified?
+ puts "incoming Neo4j::Traversal::Traverser"
Neo4j::Traversal::Traverser.new(self).incoming(rel_type)
else
+ puts "NodesDSL #{persisted?} #{storage}"
NodesDSL.new(storage, :incoming)
end
end
@@ -86,6 +88,10 @@ def add_incoming_rel(rel_type, rel) #:nodoc:
def rm_incoming_rel(rel_type, rel) #:nodoc:
_create_or_get_storage(rel_type).rm_incoming_rel(rel)
end
+
+ def rm_outgoing_rel(rel_type, rel) #:nodoc:
+ _create_or_get_storage(rel_type).rm_outgoing_rel(rel)
+ end
end
end
end
@@ -8,15 +8,18 @@ class Storage #:nodoc:
attr_reader :dsl, :node, :rel_type
def initialize(node, rel_type, rel_class)
- @rel_type = rel_type.to_sym
- @node = node
- @rel_class = rel_class || Neo4j::Rails::Relationship
+ @rel_type = rel_type.to_sym
+ @node = node
+ @rel_class = rel_class || Neo4j::Rails::Relationship
@outgoing_rels = []
@incoming_rels = []
+
+ puts "Created #{self.to_s}"
end
def to_s #:nodoc:
- "#{self.class} #{object_id} rel_type: #{@rel_type} outgoing #{@outgoing_rels.size} incoming #{@incoming_rels.size}"
+ node_id = @node._java_node ? "#{@node.neo_id}/#{@node.object_id}" : "not persisted #{@node.object_id}"
+ "Storage #{object_id} node_id: #{node_id} rel_type: #{@rel_type} outgoing #{@outgoing_rels.size} incoming #{@incoming_rels.size}"
end
def modified?
@@ -27,7 +30,7 @@ def modified?
def size(dir)
counter = 0
# count persisted relationship
- @node._java_node && @node._java_node.getRelationships(java_rel_type, dir_to_java(dir)).each {|*| counter += 1 }
+ @node._java_node && @node._java_node.getRelationships(java_rel_type, dir_to_java(dir)).each { |*| counter += 1 }
# count relationship which has not yet been persisted
counter += relationships(dir).size
counter
@@ -108,29 +111,37 @@ def del_rel(rel)
def create_relationship_to(to, dir)
if dir == :outgoing
rel = @rel_class.new(@rel_type, @node, to, self)
- # puts "create outgoing rel_class #{@rel_class} rel = #{rel} from #{@node} to #{to} of type #{@rel_type}"
+ puts "create outgoing rel_class #{@rel_class} rel = #{rel} from #{@node} to #{to} of type #{@rel_type}"
to.class != Neo4j::Node && to.add_incoming_rel(@rel_type, rel)
add_outgoing_rel(rel)
else
- # puts "create incoming rel #{@rel_class} from #{to} to #{@node} of type #{@rel_type}"
+ puts "create incoming rel #{@rel_class} from #{to} to #{@node} of type #{@rel_type}"
rel = @rel_class.new(@rel_type, to, @node, self)
@node.class != Neo4j::Node && to.add_outgoing_rel(@rel_type, rel)
add_incoming_rel(rel)
end
end
-
+
def add_incoming_rel(rel)
@incoming_rels << rel
+ puts "add_incoming_rel #{@incoming_rels.size} for #{@node} on #{self.to_s}"
end
def add_outgoing_rel(rel)
@outgoing_rels << rel
+ puts "add_outgoing_rel #{@outgoing_rels.size} for #{@node} on #{self.to_s}"
end
def rm_incoming_rel(rel)
@incoming_rels.delete(rel)
+ puts "rm_incoming_rel #{self.to_s}"
end
-
+
+ def rm_outgoing_rel(rel)
+ @outgoing_rels.delete(rel)
+ puts "rm_outgoing_rel #{rel} #{self.to_s}"
+ end
+
def valid?(context, validated_nodes)
return true if validated_nodes.include?(@node)
all_valid = true
@@ -142,11 +153,11 @@ def valid?(context, validated_nodes)
validated_nodes << start_node << end_node
if !end_node.valid?(context, validated_nodes)
- all_valid = false
+ all_valid = false
start_node.errors[@rel_type.to_sym] ||= []
start_node.errors[@rel_type.to_sym] << end_node.errors.clone
elsif !start_node.valid?(context, validated_nodes)
- all_valid = false
+ all_valid = false
end_node.errors[@rel_type.to_sym] ||= []
end_node.errors[@rel_type.to_sym] << start_node.errors.clone
end
@@ -155,6 +166,7 @@ def valid?(context, validated_nodes)
end
def persist
+ puts "PERSIST"
success = true
@outgoing_rels.each do |rel|
success = rel.save
@@ -163,12 +175,14 @@ def persist
if success
@outgoing_rels.each do |rel|
+ puts "RM OUT #{rel} from #{rel.end_node.neo_id} (#{rel.start_node.neo_id})"
rel.end_node.rm_incoming_rel(@rel_type.to_sym, rel)
end
@outgoing_rels.clear
@incoming_rels.each do |rel|
- success = rel.start_node.persisted? || rel.start_node.save
+ puts "RM IN #{rel} from #{rel.start_node.neo_id}"
+ rel.start_node.rm_outgoing_rel(@rel_type.to_sym, rel)
break unless success
end
success
@@ -140,6 +140,7 @@
describe "#all_paths(a,b)" do
context "no paths" do
it "#outgoing(:friends).first.nodes returns the nodes in the path" do
+ pending "does not work - BUG"
a = Neo4j::Node.new
b = Neo4j::Node.new
new_tx
@@ -38,6 +38,31 @@ class RelationshipWithNoProperty < Neo4j::Rails::Relationship
a.outgoing(:friends).should include(b, c)
end
+ it "should find the relationship using #incoming method" do
+ clazz = create_model
+ a = clazz.create
+ b = clazz.create
+ c = clazz.create
+ a.outgoing(:qwe) << b
+ a.outgoing(:qwe) << c
+ a.outgoing(:qwe).size.should == 2
+ b.incoming(:qwe).size.should == 1
+ b.outgoing(:qwe).size.should == 0
+ c.incoming(:qwe).size.should == 1
+ c.outgoing(:qwe).size.should == 0
+ b.incoming(:qwe).should include(a)
+ c.incoming(:qwe).should include(a)
+ a.save
+ a.outgoing(:qwe).size.should == 2
+ puts "GET INCOMIG b = #{b.neo_id}"
+ b._java_node.incoming(:qwe).size.should == 1
+ b.incoming(:qwe).size.should == 1
+ b.outgoing(:qwe).size.should == 0
+ c.incoming(:qwe).size.should == 1
+ c.outgoing(:qwe).size.should == 0
+ b.incoming(:qwe).should include(a)
+ c.incoming(:qwe).should include(a)
+ end
it "should find the relationship using #has_n method when created with outgoing method" do
clazz = create_model

0 comments on commit 09ef389

Please sign in to comment.