neo4j-core 0.0.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/Gemfile +27 -0
  2. data/README.rdoc +27 -0
  3. data/config/neo4j/config.yml +102 -0
  4. data/lib/db/active_tx_log +1 -0
  5. data/lib/db/index/lucene-store.db +0 -0
  6. data/lib/db/index/lucene.log.1 +0 -0
  7. data/lib/db/index/lucene.log.active +0 -0
  8. data/lib/db/lock +0 -0
  9. data/lib/db/messages.log +530 -0
  10. data/lib/db/neostore +0 -0
  11. data/lib/db/neostore.id +0 -0
  12. data/lib/db/neostore.nodestore.db +0 -0
  13. data/lib/db/neostore.nodestore.db.id +0 -0
  14. data/lib/db/neostore.propertystore.db +0 -0
  15. data/lib/db/neostore.propertystore.db.arrays +0 -0
  16. data/lib/db/neostore.propertystore.db.arrays.id +0 -0
  17. data/lib/db/neostore.propertystore.db.id +0 -0
  18. data/lib/db/neostore.propertystore.db.index +0 -0
  19. data/lib/db/neostore.propertystore.db.index.id +0 -0
  20. data/lib/db/neostore.propertystore.db.index.keys +0 -0
  21. data/lib/db/neostore.propertystore.db.index.keys.id +0 -0
  22. data/lib/db/neostore.propertystore.db.strings +0 -0
  23. data/lib/db/neostore.propertystore.db.strings.id +0 -0
  24. data/lib/db/neostore.relationshipstore.db +0 -0
  25. data/lib/db/neostore.relationshipstore.db.id +0 -0
  26. data/lib/db/neostore.relationshiptypestore.db +0 -0
  27. data/lib/db/neostore.relationshiptypestore.db.id +0 -0
  28. data/lib/db/neostore.relationshiptypestore.db.names +0 -0
  29. data/lib/db/neostore.relationshiptypestore.db.names.id +0 -0
  30. data/lib/db/nioneo_logical.log.2 +0 -0
  31. data/lib/db/nioneo_logical.log.active +0 -0
  32. data/lib/db/tm_tx_log.1 +0 -0
  33. data/lib/neo4j/config.rb +139 -0
  34. data/lib/neo4j/cypher.rb +156 -0
  35. data/lib/neo4j/neo4j.rb +244 -0
  36. data/lib/neo4j/neo4j.rb~ +214 -0
  37. data/lib/neo4j/node.rb +39 -0
  38. data/lib/neo4j/relationship.rb +61 -0
  39. data/lib/neo4j/transaction.rb +86 -0
  40. data/lib/neo4j/type_converters/type_converters.rb +287 -0
  41. data/lib/neo4j-core/cypher/cypher.rb +867 -0
  42. data/lib/neo4j-core/cypher/result_wrapper.rb +39 -0
  43. data/lib/neo4j-core/database.rb +191 -0
  44. data/lib/neo4j-core/equal/equal.rb +23 -0
  45. data/lib/neo4j-core/event_handler.rb +265 -0
  46. data/lib/neo4j-core/index/class_methods.rb +117 -0
  47. data/lib/neo4j-core/index/index.rb +36 -0
  48. data/lib/neo4j-core/index/index_config.rb +112 -0
  49. data/lib/neo4j-core/index/indexer.rb +243 -0
  50. data/lib/neo4j-core/index/indexer_registry.rb +55 -0
  51. data/lib/neo4j-core/index/lucene_query.rb +264 -0
  52. data/lib/neo4j-core/lazy_map.rb +21 -0
  53. data/lib/neo4j-core/node/class_methods.rb +77 -0
  54. data/lib/neo4j-core/node/node.rb +47 -0
  55. data/lib/neo4j-core/property/property.rb +94 -0
  56. data/lib/neo4j-core/relationship/class_methods.rb +80 -0
  57. data/lib/neo4j-core/relationship/relationship.rb +97 -0
  58. data/lib/neo4j-core/relationship_set.rb +61 -0
  59. data/lib/neo4j-core/rels/rels.rb +147 -0
  60. data/lib/neo4j-core/rels/traverser.rb +99 -0
  61. data/lib/neo4j-core/to_java.rb +51 -0
  62. data/lib/neo4j-core/traversal/evaluator.rb +36 -0
  63. data/lib/neo4j-core/traversal/filter_predicate.rb +30 -0
  64. data/lib/neo4j-core/traversal/prune_evaluator.rb +20 -0
  65. data/lib/neo4j-core/traversal/rel_expander.rb +35 -0
  66. data/lib/neo4j-core/traversal/traversal.rb +130 -0
  67. data/lib/neo4j-core/traversal/traverser.rb +295 -0
  68. data/lib/neo4j-core/version.rb +5 -0
  69. data/lib/neo4j-core.rb +64 -0
  70. data/neo4j-core.gemspec +31 -0
  71. metadata +145 -0
@@ -0,0 +1,99 @@
1
+ module Neo4j
2
+ module Core
3
+ module Rels
4
+
5
+ # Traverse relationships of depth one from one node.
6
+ # This object is returned from the Neo4j::Node#rels method.
7
+ # @see Neo4j::Core::Node#rels
8
+ class Traverser
9
+ include Enumerable
10
+ include Neo4j::Core::ToJava
11
+
12
+ attr_reader :node
13
+ attr_reader :dir
14
+ attr_reader :types
15
+
16
+ # Called from Neo4j::Core::Node#rels
17
+ def initialize(node, types, dir = :both)
18
+ @node = node
19
+ @types = types
20
+ @dir = dir
21
+ end
22
+
23
+ def to_s
24
+ "#{self.class} [types: #{@types.join(',')} dir:#{@dir}]"
25
+ end
26
+
27
+ # Implements the Ruby Enumerable mixin
28
+ def each
29
+ iter = iterator
30
+ while (iter.has_next())
31
+ rel = iter.next
32
+ yield rel.wrapper if match_to_other?(rel)
33
+ end
34
+ end
35
+
36
+ # @return [true,false] if there are no relationships of specified dir and type(s)
37
+ def empty?
38
+ first == nil
39
+ end
40
+
41
+ # @return The Java Iterator
42
+ def iterator
43
+ @node._rels(@dir, *@types)
44
+ end
45
+
46
+ # @return [true,false] true if it match the specified other node
47
+ # @see #to_other
48
+ def match_to_other?(rel)
49
+ if @to_other.nil?
50
+ true
51
+ elsif @dir == :outgoing
52
+ rel._end_node == @to_other
53
+ elsif @dir == :incoming
54
+ rel._start_node == @to_other
55
+ else
56
+ rel._start_node == @to_other || rel._end_node == @to_other
57
+ end
58
+ end
59
+
60
+ # Specifies that we only want relationship to the given node
61
+ # @param [Neo4j::Node] to_other a node or an object that implements the Neo4j::Core::Equal mixin
62
+ # @return self
63
+ def to_other(to_other)
64
+ @to_other = to_other
65
+ self
66
+ end
67
+
68
+ # Deletes all the relationships
69
+ def del
70
+ each { |rel| rel.del }
71
+ end
72
+
73
+
74
+ # Specifies that we want both incoming and outgoing direction
75
+ # @return self
76
+ def both
77
+ @dir = :both
78
+ self
79
+ end
80
+
81
+ # Specifies that we only want incoming relationships
82
+ # @return self
83
+ def incoming
84
+ @dir = :incoming
85
+ self
86
+ end
87
+
88
+ # Specifies that only outgoing relationships is wanted.
89
+ # @return self
90
+ def outgoing
91
+ @dir = :outgoing
92
+ self
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,51 @@
1
+ module Neo4j
2
+ module Core
3
+ # A Utility class for translating Ruby object to Neo4j Java types
4
+ # @private
5
+ module ToJava
6
+ def type_to_java(type)
7
+ Java::OrgNeo4jGraphdb::DynamicRelationshipType.withName(type.to_s)
8
+ end
9
+
10
+ module_function :type_to_java
11
+
12
+ def types_to_java(types)
13
+ types.inject([]) { |result, type| result << type_to_java(type) }.to_java(Java::OrgNeo4jGraphdb::RelationshipType)
14
+ end
15
+
16
+ module_function :types_to_java
17
+
18
+
19
+ def dir_from_java(dir)
20
+ case dir
21
+ when Java::OrgNeo4jGraphdb::Direction::OUTGOING then
22
+ :outgoing
23
+ when Java::OrgNeo4jGraphdb::Direction::BOTH then
24
+ :both
25
+ when Java::OrgNeo4jGraphdb::Direction::INCOMING then
26
+ :incoming
27
+ else
28
+ raise "unknown direction '#{dir} / #{dir.class}'"
29
+ end
30
+ end
31
+
32
+ module_function :dir_from_java
33
+
34
+ def dir_to_java(dir)
35
+ case dir
36
+ when :outgoing then
37
+ Java::OrgNeo4jGraphdb::Direction::OUTGOING
38
+ when :both then
39
+ Java::OrgNeo4jGraphdb::Direction::BOTH
40
+ when :incoming then
41
+ Java::OrgNeo4jGraphdb::Direction::INCOMING
42
+ else
43
+ raise "unknown direction '#{dir}', expects argument: outgoing, :incoming or :both"
44
+ end
45
+ end
46
+
47
+ module_function :dir_to_java
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,36 @@
1
+ module Neo4j
2
+ module Core
3
+ module Traversal
4
+
5
+ # Implements the Neo4j Evaluator Java interface, only used internally.
6
+ # @private
7
+ class Evaluator
8
+ include Java::OrgNeo4jGraphdbTraversal::Evaluator
9
+
10
+ def initialize(&eval_block)
11
+ @eval_block = eval_block
12
+ end
13
+
14
+ # Implements the Java Interface:
15
+ # evaluate(Path path)
16
+ # Evaluates a Path and returns an Evaluation containing information about whether or not to include it in the traversal result, i.e return it from the Traverser.
17
+ def evaluate(path)
18
+ ret = @eval_block.call(path)
19
+ case ret
20
+ when :exclude_and_continue then
21
+ Java::OrgNeo4jGraphdbTraversal::Evaluation::EXCLUDE_AND_CONTINUE
22
+ when :exclude_and_prune then
23
+ Java::OrgNeo4jGraphdbTraversal::Evaluation::EXCLUDE_AND_PRUNE
24
+ when :include_and_continue then
25
+ Java::OrgNeo4jGraphdbTraversal::Evaluation::INCLUDE_AND_CONTINUE
26
+ when :include_and_prune then
27
+ Java::OrgNeo4jGraphdbTraversal::Evaluation::INCLUDE_AND_PRUNE
28
+ else
29
+ raise "Got #{ret}, only accept :exclude_and_continue,:exclude_and_prune,:include_and_continue and :include_and_prune"
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,30 @@
1
+ module Neo4j
2
+ module Core
3
+ module Traversal
4
+ # Implements the Neo4j Predicate Java interface, only used internally.
5
+ # @private
6
+ class FilterPredicate
7
+ include Java::OrgNeo4jHelpers::Predicate
8
+
9
+ def initialize
10
+ @procs = []
11
+ end
12
+
13
+ def add(proc)
14
+ @procs << proc
15
+ end
16
+
17
+ def include_start_node
18
+ @include_start_node = true
19
+ end
20
+
21
+ def accept(path)
22
+ return false if @include_start_node && path.length == 0
23
+ # find the first filter which returns false
24
+ # if not found then we will accept this path
25
+ @procs.find { |p| !p.call(path) }.nil?
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,20 @@
1
+ module Neo4j
2
+ module Core
3
+
4
+ # Implements the Neo4j PruneEvaluator Java interface, only used internally.
5
+ # @private
6
+ module Traversal
7
+ class PruneEvaluator
8
+ include Java::OrgNeo4jGraphdbTraversal::PruneEvaluator
9
+
10
+ def initialize(proc)
11
+ @proc = proc
12
+ end
13
+
14
+ def prune_after(path)
15
+ @proc.call(path)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,35 @@
1
+ module Neo4j
2
+ module Core
3
+ module Traversal
4
+ # Implements the Neo4j RelationshipExpander Java interface, only used internally.
5
+ # @private
6
+ class RelExpander
7
+ include Java::OrgNeo4jGraphdb::RelationshipExpander
8
+
9
+ attr_accessor :reversed
10
+
11
+ def initialize(&block)
12
+ @block = block
13
+ @reverse = false
14
+ end
15
+
16
+ def self.create_pair(&block)
17
+ normal = RelExpander.new(&block)
18
+ reversed = RelExpander.new(&block)
19
+ normal.reversed = reversed
20
+ reversed.reversed = normal
21
+ reversed.reverse!
22
+ normal
23
+ end
24
+
25
+ def expand(node)
26
+ @block.arity == 1 ? @block.call(node) : @block.call(node, @reverse)
27
+ end
28
+
29
+ def reverse!
30
+ @reverse = true
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,130 @@
1
+ require 'neo4j-core/traversal/filter_predicate'
2
+ require 'neo4j-core/traversal/prune_evaluator'
3
+ require 'neo4j-core/traversal/rel_expander'
4
+ require 'neo4j-core/traversal/traverser'
5
+
6
+ module Neo4j
7
+
8
+ module Core
9
+ # Contains methods that are mixin for Neo4j::Node
10
+ # The builder pattern is used to construct traversals (all methods returns Neo4j::Traversal::Traverser)
11
+ # @see http://github.com/andreasronge/neo4j/wiki/traverser
12
+ # @see http://docs.neo4j.org/chunked/stable/tutorial-traversal-java-api.html
13
+ module Traversal
14
+ include ToJava
15
+
16
+ # A more powerful alternative of #outgoing, #incoming and #both method.
17
+ # You can use this method for example to only traverse nodes based on properties on the relationships
18
+ #
19
+ # @example traverse all relationships with a property of age > 5
20
+ # some_node.expand { |n| n._rels.find_all { |r| r[:age] > 5 } }.depth(:all).to_a
21
+ #
22
+ # @yield [node] Used to find out which relationship should be included in the traversal
23
+ # @yieldparam [Neo4j::Node] node the current node from which we can decide which relationship should be traversed
24
+ # @yieldreturn [Enumerable<Neo4j::Relationship>] which relationships should be traversed
25
+ # @return [Neo4j::Core::Traversal::Traverser] a traverser object which can be used to further describe the traversal
26
+ def expand(&expander)
27
+ Traverser.new(self).expander(&expander)
28
+ end
29
+
30
+
31
+ # Returns the outgoing nodes for this node.
32
+ #
33
+ # @example Find all my friends (nodes of depth 1 of type <tt>friends</tt>)
34
+ # me.outgoing(:friends).each {|friend| puts friend.name}
35
+ #
36
+ # @example A possible faster way, avoid loading wrapper Ruby classes, instead use raw java neo4j node objects
37
+ # me.outgoing(:friends).raw.each {|friend| puts friend[:name]}
38
+ #
39
+ # @example Find all my friends and their friends (nodes of depth 1 of type <tt>friends</tt>)
40
+ # me.outgoing(:friends).depth(2).each {|friend| puts friend[:name]}
41
+ #
42
+ # @example Find all my friends and include my self in the result
43
+ # me.outgoing(:friends).depth(4).include_start_node.each {...}
44
+ #
45
+ # @example Find all my friends friends friends, etc. at any depth
46
+ # me.outgoing(:friends).depth(:all).each {...}
47
+ #
48
+ # @example Find all my friends friends but do not include my friends (only depth == 2)
49
+ # me.outgoing(:friends).depth(2).filter{|path| path.length == 2}
50
+ #
51
+ # @example Find all my friends but 'cut off' some parts of the traversal path
52
+ # me.outgoing(:friends).depth(42).prune(|path| an_expression_using_path_returning_true_false }
53
+ #
54
+ # @example Find all my friends and work colleges
55
+ # me.outgoing(:friends).outgoing(:work).each {...}
56
+ #
57
+ # Of course all the methods <tt>outgoing</tt>, <tt>incoming</tt>, <tt>both</tt>, <tt>depth</tt>, <tt>include_start_node</tt>, <tt>filter</tt>, and <tt>prune</tt>, <tt>eval_paths</tt>, <tt>unique</tt> can be combined.
58
+ #
59
+ # @see http://github.com/andreasronge/neo4j/wiki/traverser
60
+ # @param [String, Symbol] type the relationship type
61
+ # @return (see #expand)
62
+ def outgoing(type)
63
+ Traverser.new(self, :outgoing, type)
64
+ end
65
+
66
+
67
+ # Returns the incoming nodes of given type(s).
68
+ #
69
+ # @param [String, Symbol] type the relationship type
70
+ # @see #outgoing
71
+ # @see http://github.com/andreasronge/neo4j/wiki/traverser
72
+ # @return (see #expand)
73
+ def incoming(type)
74
+ Traverser.new(self, :incoming, type)
75
+ end
76
+
77
+ # Returns both incoming and outgoing nodes of given types(s)
78
+ #
79
+ # If a type is not given then it will return all types of relationships.
80
+ #
81
+ # @see #outgoing
82
+ # @return (see #expand)
83
+ def both(type=nil)
84
+ Traverser.new(self, :both, type)
85
+ end
86
+
87
+
88
+ # Traverse using a block. The block is expected to return one of the following values:
89
+ # * <tt>:exclude_and_continue</tt>
90
+ # * <tt>:exclude_and_prune</tt>
91
+ # * <tt>:include_and_continue</tt>
92
+ # * <tt>:include_and_prune</tt>
93
+ # This value decides if it should continue to traverse and if it should include the node in the traversal result.
94
+ # The block will receive a path argument.
95
+ #
96
+ # @example
97
+ # @pet0.eval_paths {|path| path.end_node == @principal1 ? :include_and_prune : :exclude_and_continue }.unique(:node_path).depth(:all)
98
+ #
99
+ # ==== See also
100
+ #
101
+ # * How to use - http://neo4j.rubyforge.org/guides/traverser.html
102
+ # * the path parameter - http://api.neo4j.org/1.4/org/neo4j/graphdb/Path.html
103
+ # * the #unique method - if paths should be visit more the once, etc...
104
+ #
105
+ # @return (see #expand)
106
+ def eval_paths(&eval_block)
107
+ Traverser.new(self).eval_paths(&eval_block)
108
+ end
109
+
110
+ # Sets uniqueness of nodes or relationships to visit during a traversals.
111
+ #
112
+ # Allowed values
113
+ # * <tt>:node_global</tt> A node cannot be traversed more than once (default)
114
+ # * <tt>:node_path</tt> For each returned node there 's a unique path from the start node to it.
115
+ # * <tt>:node_recent</tt> This is like :node_global, but only guarantees uniqueness among the most recent visited nodes, with a configurable count.
116
+ # * <tt>:none</tt> No restriction (the user will have to manage it).
117
+ # * <tt>:rel_global</tt> A relationship cannot be traversed more than once, whereas nodes can.
118
+ # * <tt>:rel_path</tt> No restriction (the user will have to manage it).
119
+ # * <tt>:rel_recent</tt> Same as for :node_recent, but for relationships.
120
+ #
121
+ # @param (see Neo4j::Core::Traversal::Traverser#unique)
122
+ # @see example in #eval_paths
123
+ # @see http://docs.neo4j.org/chunked/stable/tutorial-traversal-java-api.html#_uniqueness
124
+ # @return (see #expand)
125
+ def unique(u)
126
+ Traverser.new(self).unique(u)
127
+ end
128
+ end
129
+ end
130
+ end