neo4j-core 2.0.0.alpha.1-java → 2.0.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/Gemfile +2 -2
  2. data/README.rdoc +161 -13
  3. data/config/neo4j/config.yml +1 -1
  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.active +0 -0
  7. data/lib/db/messages.log +2299 -0
  8. data/lib/db/neostore +0 -0
  9. data/lib/db/neostore.id +0 -0
  10. data/lib/db/neostore.nodestore.db +0 -0
  11. data/lib/db/neostore.nodestore.db.id +0 -0
  12. data/lib/db/neostore.propertystore.db +0 -0
  13. data/lib/db/neostore.propertystore.db.arrays +0 -0
  14. data/lib/db/neostore.propertystore.db.arrays.id +0 -0
  15. data/lib/db/neostore.propertystore.db.id +0 -0
  16. data/lib/db/neostore.propertystore.db.index +0 -0
  17. data/lib/db/neostore.propertystore.db.index.id +0 -0
  18. data/lib/db/neostore.propertystore.db.index.keys +0 -0
  19. data/lib/db/neostore.propertystore.db.index.keys.id +0 -0
  20. data/lib/db/neostore.propertystore.db.strings +0 -0
  21. data/lib/db/neostore.propertystore.db.strings.id +0 -0
  22. data/lib/db/neostore.relationshipstore.db +0 -0
  23. data/lib/db/neostore.relationshipstore.db.id +0 -0
  24. data/lib/db/neostore.relationshiptypestore.db +0 -0
  25. data/lib/db/neostore.relationshiptypestore.db.id +0 -0
  26. data/lib/db/neostore.relationshiptypestore.db.names +0 -0
  27. data/lib/db/neostore.relationshiptypestore.db.names.id +0 -0
  28. data/lib/db/nioneo_logical.log.active +0 -0
  29. data/lib/db/tm_tx_log.1 +0 -0
  30. data/lib/neo4j-core.rb +20 -3
  31. data/lib/neo4j-core/cypher/cypher.rb +1033 -0
  32. data/lib/neo4j-core/cypher/result_wrapper.rb +48 -0
  33. data/lib/neo4j-core/database.rb +4 -5
  34. data/lib/neo4j-core/event_handler.rb +1 -1
  35. data/lib/neo4j-core/hash_with_indifferent_access.rb +165 -0
  36. data/lib/neo4j-core/index/class_methods.rb +27 -41
  37. data/lib/neo4j-core/index/index.rb +3 -4
  38. data/lib/neo4j-core/index/index_config.rb +30 -23
  39. data/lib/neo4j-core/index/indexer.rb +65 -53
  40. data/lib/neo4j-core/index/indexer_registry.rb +2 -2
  41. data/lib/neo4j-core/index/lucene_query.rb +53 -42
  42. data/lib/neo4j-core/index/unique_factory.rb +54 -0
  43. data/lib/neo4j-core/node/class_methods.rb +4 -4
  44. data/lib/neo4j-core/node/node.rb +1 -8
  45. data/lib/neo4j-core/property/java.rb +59 -0
  46. data/lib/neo4j-core/property/property.rb +1 -3
  47. data/lib/neo4j-core/relationship/relationship.rb +8 -10
  48. data/lib/neo4j-core/rels/rels.rb +9 -4
  49. data/lib/neo4j-core/rels/traverser.rb +13 -27
  50. data/lib/neo4j-core/traversal/prune_evaluator.rb +2 -2
  51. data/lib/neo4j-core/traversal/traverser.rb +122 -27
  52. data/lib/neo4j-core/version.rb +1 -1
  53. data/lib/neo4j-core/wrapper/class_methods.rb +22 -0
  54. data/lib/neo4j-core/wrapper/wrapper.rb +20 -0
  55. data/lib/neo4j/algo.rb +300 -0
  56. data/lib/neo4j/config.rb +3 -6
  57. data/lib/neo4j/cypher.rb +180 -0
  58. data/lib/neo4j/neo4j.rb +51 -23
  59. data/lib/neo4j/node.rb +27 -0
  60. data/lib/neo4j/relationship.rb +25 -0
  61. data/lib/test.rb~ +27 -0
  62. data/neo4j-core.gemspec +2 -2
  63. metadata +44 -11
  64. data/lib/neo4j-core/type_converters/type_converters.rb +0 -287
  65. data/lib/neo4j-core/version.rb~ +0 -3
  66. data/lib/test.rb +0 -27
@@ -5,9 +5,7 @@ module Neo4j
5
5
  # @return [Hash] all properties plus the id of the node with the key <tt>_neo_id</tt>
6
6
  def props
7
7
  ret = {"_neo_id" => neo_id}
8
- iter = get_property_keys.iterator
9
- while (iter.hasNext) do
10
- key = iter.next
8
+ property_keys.each do |key|
11
9
  ret[key] = get_property(key)
12
10
  end
13
11
  ret
@@ -3,16 +3,19 @@ module Neo4j
3
3
  module Relationship
4
4
 
5
5
  # Same as Java::OrgNeo4jGraphdb::Relationship#getEndNode
6
+ # @see http://api.neo4j.org/1.6.1/org/neo4j/graphdb/Relationship.html#getEndNode()
6
7
  def _end_node
7
8
  get_end_node
8
9
  end
9
10
 
10
11
  # Same as Java::OrgNeo4jGraphdb::Relationship#getStartNode
12
+ # @see http://api.neo4j.org/1.6.1/org/neo4j/graphdb/Relationship.html#getStartNode()
11
13
  def _start_node
12
14
  get_start_node
13
15
  end
14
16
 
15
17
  # Same as Java::OrgNeo4jGraphdb::Relationship#getOtherNode
18
+ # @see http://api.neo4j.org/1.6.1/org/neo4j/graphdb/Relationship.html#getOtherNode()
16
19
  def _other_node(node)
17
20
  get_other_node(node)
18
21
  end
@@ -49,6 +52,7 @@ module Neo4j
49
52
  #
50
53
  # @param [Neo4j::Node] node the node that we don't want to return
51
54
  # @return [Neo4j::Node] the other node wrapper
55
+ # @see #_other_node
52
56
  def other_node(node)
53
57
  _other_node(node._java_node).wrapper
54
58
  end
@@ -56,7 +60,7 @@ module Neo4j
56
60
 
57
61
  # same as #_java_rel
58
62
  # Used so that we have same method for both relationship and nodes
59
- def wrapped_entity
63
+ def _java_entity
60
64
  self
61
65
  end
62
66
 
@@ -70,21 +74,15 @@ module Neo4j
70
74
  Neo4j::Relationship.exist?(self)
71
75
  end
72
76
 
73
- # Loads the wrapper using the #wrapper class method if it exists, otherwise return self.
74
- def wrapper
75
- self.class.respond_to?(:wrapper) ? self.class.wrapper(node) : self
76
- end
77
-
78
-
79
77
  # Returns the relationship name
80
78
  #
81
79
  # @example
82
80
  # a = Neo4j::Node.new
83
81
  # a.outgoing(:friends) << Neo4j::Node.new
84
- # a.rels.first.rel_type # => 'friends'
85
- # @return [String] the type of the relationship
82
+ # a.rels.first.rel_type # => :friends
83
+ # @return [Symbol] the type of the relationship
86
84
  def rel_type
87
- getType().name()
85
+ getType().name().to_sym
88
86
  end
89
87
 
90
88
  def class
@@ -2,6 +2,8 @@ module Neo4j
2
2
  module Core
3
3
  # Contains methods for traversing relationship object of depth one from one node.
4
4
  module Rels
5
+
6
+
5
7
  # Returns the only node of a given type and direction that is attached to this node, or nil.
6
8
  # This is a convenience method that is used in the commonly occuring situation where a node has exactly zero or one relationships of a given type and direction to another node.
7
9
  # Typically this invariant is maintained by the rest of the code: if at any time more than one such relationships exist, it is a fatal error that should generate an exception.
@@ -76,7 +78,9 @@ module Neo4j
76
78
  #
77
79
  # @see Neo4j::Core::Node#wrapper #wrapper - The method used wrap to the node in a Ruby object if the node was found
78
80
  # @see Neo4j::Relationship#rel_type
79
- def rels(dir, *types)
81
+ # @raise an exception if the first parameter is not <tt>:both</tt>, <tt>;outgoing</tt> or <tt>:incoming</tt>
82
+ def rels(dir=:both, *types)
83
+ raise "Illegal argument, first argument must be :both, :incoming or :outgoing, got #{dir.inspect}" unless [:incoming, :outgoing, :both].include?(dir)
80
84
  Neo4j::Core::Rels::Traverser.new(self, types, dir)
81
85
  end
82
86
 
@@ -114,14 +118,15 @@ module Neo4j
114
118
  # @return [Enumerable] of Neo4j::Relationship objects
115
119
  def _rels(dir=:both, *types)
116
120
  if types.size > 1
117
- get_relationships(ToJava.dir_to_java(dir), ToJava.types_to_java(types))
121
+ get_relationships(ToJava.dir_to_java(dir), ToJava.types_to_java(types)).iterator
118
122
  elsif types.size == 1
119
- get_relationships(ToJava.type_to_java(types[0]), ToJava.dir_to_java(dir))
123
+ get_relationships(ToJava.type_to_java(types[0]), ToJava.dir_to_java(dir)).iterator
120
124
  else
121
- get_relationships(ToJava.dir_to_java(dir))
125
+ get_relationships(ToJava.dir_to_java(dir)).iterator
122
126
  end
123
127
  end
124
128
 
129
+
125
130
  # Check if the given relationship exists
126
131
  # Returns true if there are one or more relationships from this node to other nodes
127
132
  # with the given relationship.
@@ -26,10 +26,8 @@ module Neo4j
26
26
 
27
27
  # Implements the Ruby Enumerable mixin
28
28
  def each
29
- iter = iterator
30
- while (iter.has_next())
31
- rel = iter.next
32
- yield rel.wrapper if match_to_other?(rel)
29
+ iterator.each do |rel|
30
+ yield rel.wrapper if match_between?(rel)
33
31
  end
34
32
  end
35
33
 
@@ -43,42 +41,30 @@ module Neo4j
43
41
  @node._rels(@dir, *@types)
44
42
  end
45
43
 
46
- # @return [Fixnum] the size of all matched relationship, also check if it #to_other node
47
- # @see #to_other
48
- def size
49
- c = 0
50
- iter = iterator
51
- while (iter.has_next())
52
- rel = iter.next
53
- next unless match_to_other?(rel)
54
- c += 1
55
- end
56
- c
57
- end
58
-
59
-
60
44
  # @return [true,false] true if it match the specified other node
61
- # @see #to_other
62
- def match_to_other?(rel)
63
- if @to_other.nil?
45
+ # @see #between
46
+ def match_between?(rel)
47
+ if @between.nil?
64
48
  true
65
49
  elsif @dir == :outgoing
66
- rel._end_node == @to_other
50
+ rel._end_node == @between
67
51
  elsif @dir == :incoming
68
- rel._start_node == @to_other
52
+ rel._start_node == @between
69
53
  else
70
- rel._start_node == @to_other || rel._end_node == @to_other
54
+ rel._start_node == @between || rel._end_node == @between
71
55
  end
72
56
  end
73
57
 
74
58
  # Specifies that we only want relationship to the given node
75
- # @param [Neo4j::Node] to_other a node or an object that implements the Neo4j::Core::Equal mixin
59
+ # @param [Neo4j::Node] between a node or an object that implements the Neo4j::Core::Equal mixin
76
60
  # @return self
77
- def to_other(to_other)
78
- @to_other = to_other
61
+ def between(between)
62
+ @between = between
79
63
  self
80
64
  end
81
65
 
66
+ alias_method :to_other, :between
67
+
82
68
  # Deletes all the relationships
83
69
  def del
84
70
  each { |rel| rel.del }
@@ -1,9 +1,9 @@
1
1
  module Neo4j
2
2
  module Core
3
3
 
4
- # Implements the Neo4j PruneEvaluator Java interface, only used internally.
5
- # @private
6
4
  module Traversal
5
+ # Implements the Neo4j PruneEvaluator Java interface, only used internally.
6
+ # @private
7
7
  class PruneEvaluator
8
8
  include Java::OrgNeo4jGraphdbTraversal::PruneEvaluator
9
9
 
@@ -2,6 +2,54 @@ module Neo4j
2
2
  module Core
3
3
  module Traversal
4
4
 
5
+ class CypherQuery
6
+ include Enumerable
7
+ attr_accessor :query, :return_variable
8
+
9
+ def initialize(start_id, dir, types, query_hash=nil, &block)
10
+ this = self
11
+
12
+ rel_type = ":#{types.map{|x| "`#{x}`"}.join('|')}"
13
+
14
+ @query = Neo4j::Cypher.new do
15
+ default_ret = node(:default_ret)
16
+ n = node(start_id)
17
+ case dir
18
+ when :outgoing then
19
+ n > rel_type > default_ret
20
+ when :incoming then
21
+ n < rel_type < default_ret
22
+ when :both then
23
+ n - rel_type - default_ret
24
+ end
25
+
26
+ # where statement
27
+ ret_maybe = block && self.instance_exec(default_ret, &block)
28
+ ret = ret_maybe.respond_to?(:var_name) ? ret_maybe : default_ret
29
+ if query_hash
30
+ expr = []
31
+ query_hash.each{|pair| expr << (ret[pair[0]] == pair[1])}.to_a
32
+ expr.each_with_index do |obj, i|
33
+ Neo4j::Core::Cypher::ExprOp.new(obj, expr[i+1], "and") if i < expr.size - 1
34
+ end
35
+ end
36
+
37
+ this.return_variable = ret.var_name.to_sym
38
+ ret
39
+ end.to_s
40
+ end
41
+
42
+ def to_s
43
+ @query
44
+ end
45
+
46
+ def each
47
+ Neo4j._query(query).each do |r|
48
+ yield r[return_variable]
49
+ end
50
+ end
51
+ end
52
+
5
53
  # By using this class you can both specify traversals and create new relationships.
6
54
  # This object is return from the Neo4j::Core::Traversal methods.
7
55
  # @see Neo4j::Core::Traversal#outgoing
@@ -17,14 +65,27 @@ module Neo4j
17
65
  if type.nil?
18
66
  raise "Traversing all relationship in direction #{dir.inspect} not supported, only :both supported" unless dir == :both
19
67
  @td = Java::OrgNeo4jKernelImplTraversal::TraversalDescriptionImpl.new.breadth_first()
68
+ elsif (dir == :both)
69
+ both(type)
70
+ elsif (dir == :incoming)
71
+ incoming(type)
72
+ elsif (dir == :outgoing)
73
+ outgoing(type)
20
74
  else
21
- @type = type_to_java(type)
22
- @dir = dir_to_java(dir)
23
- @td = Java::OrgNeo4jKernelImplTraversal::TraversalDescriptionImpl.new.breadth_first().relationships(@type, @dir)
75
+ raise "Illegal direction #{dir.inspect}, expected :outgoing, :incoming or :both"
24
76
  end
25
77
  end
26
78
 
27
79
 
80
+ def query(query_hash = nil, &block)
81
+ # only one direction is supported
82
+ rel_types = [@outgoing_rel_types, @incoming_rel_types, @both_rel_types].find_all { |x| !x.nil? }
83
+ raise "Only one direction is allowed, outgoing:#{@outgoing_rel_types}, incoming:#{@incoming_rel_types}, @both:#{@both_rel_types}" if rel_types.count != 1
84
+ start_id = @from.neo_id
85
+ dir = (@outgoing_rel_types && :outgoing) || (@incoming_rel_types && :incoming) || (@both_rel_types && :both)
86
+ CypherQuery.new(start_id, dir, rel_types.first, query_hash, &block)
87
+ end
88
+
28
89
  # Sets traversing depth first.
29
90
  #
30
91
  # The <tt>pre_or_post</tt> parameter parameter can have two values: :pre or :post
@@ -112,11 +173,19 @@ module Neo4j
112
173
  end
113
174
 
114
175
  def to_s
115
- "NodeTraverser [from: #{@from.neo_id} depth: #{@depth} type: #{@type} dir:#{@dir}"
176
+ "NodeTraverser [from: #{@from.neo_id} depth: #{@depth}"
116
177
  end
117
178
 
118
179
 
119
180
  # Creates a new relationship between given node and self
181
+ # It can create more then one relationship
182
+ #
183
+ # @example One outgoing relationships
184
+ # node.outgoing(:foo) << other_node
185
+ #
186
+ # @example Two outgoing relationships
187
+ # node.outgoing(:foo).outgoing(:bar) << other_node
188
+ #
120
189
  # @param [Neo4j::Node] other_node the node to which we want to create a relationship
121
190
  # @return (see #new)
122
191
  def <<(other_node)
@@ -130,25 +199,49 @@ module Neo4j
130
199
  end
131
200
 
132
201
  # Creates a new relationship between self and given node.
202
+ # It can create more then one relationship
203
+ # This method is used by the <tt><<</tt> operator.
204
+ #
205
+ # @example create one relationship
206
+ # node.outgoing(:bar).new(other_node, rel_props)
207
+ #
208
+ # @example two relationships
209
+ # node.outgoing(:bar).outgoing(:foo).new(other_node, rel_props)
210
+ #
211
+ # @example both incoming and outgoing - two relationships
212
+ # node.both(:bar).new(other_node, rel_props)
213
+ #
214
+ # @see #<<
133
215
  # @param [Hash] props properties of new relationship
134
216
  # @return [Neo4j::Relationship] the created relationship
135
217
  def new(other_node, props = {})
136
- case @dir
137
- when Java::OrgNeo4jGraphdb::Direction::OUTGOING
138
- @from.create_relationship_to(other_node, @type).update(props)
139
- when Java::OrgNeo4jGraphdb::Direction::INCOMING
140
- other_node.create_relationship_to(@from, @type).update(props)
141
- else
142
- raise "Only allowed to create outgoing or incoming relationships (not #@dir)"
143
- end
218
+ @outgoing_rel_types && @outgoing_rel_types.each { |type| _new_out(other_node, type, props) }
219
+ @incoming_rel_types && @incoming_rel_types.each { |type| _new_in(other_node, type, props) }
220
+ @both_rel_types && @both_rel_types.each { |type| _new_both(other_node, type, props) }
221
+ end
222
+
223
+ # @private
224
+ def _new_out(other_node, type, props)
225
+ @from.create_relationship_to(other_node, type_to_java(type)).update(props)
226
+ end
227
+
228
+ # @private
229
+ def _new_in(other_node, type, props)
230
+ other_node.create_relationship_to(@from, type_to_java(type)).update(props)
231
+ end
232
+
233
+ # @private
234
+ def _new_both(other_node, type, props)
235
+ _new_out(other_node, type, props)
236
+ _new_in(other_node, type, props)
144
237
  end
145
238
 
146
239
  # @param (see Neo4j::Core::Traversal#both)
147
240
  # @see Neo4j::Core::Traversal#both
148
241
  def both(type)
149
- @type = type_to_java(type) if type
150
- @dir = dir_to_java(:both)
151
- @td = @td.relationships(type_to_java(type), @dir)
242
+ @both_rel_types ||= []
243
+ @both_rel_types << type
244
+ _add_rel(:both, type)
152
245
  self
153
246
  end
154
247
 
@@ -165,9 +258,9 @@ module Neo4j
165
258
  # @return self
166
259
  # @see Neo4j::Core::Traversal#outgoing
167
260
  def outgoing(type)
168
- @type = type_to_java(type) if type
169
- @dir = dir_to_java(:outgoing)
170
- @td = @td.relationships(type_to_java(type), @dir)
261
+ @outgoing_rel_types ||= []
262
+ @outgoing_rel_types << type
263
+ _add_rel(:outgoing, type)
171
264
  self
172
265
  end
173
266
 
@@ -176,12 +269,19 @@ module Neo4j
176
269
  # @return self
177
270
  # @see Neo4j::Core::Traversal#incoming
178
271
  def incoming(type)
179
- @type = type_to_java(type) if type
180
- @dir = dir_to_java(:incoming)
181
- @td = @td.relationships(type_to_java(type), @dir)
272
+ @incoming_rel_types ||= []
273
+ @incoming_rel_types << type
274
+ _add_rel(:incoming, type)
182
275
  self
183
276
  end
184
277
 
278
+ # @private
279
+ def _add_rel(dir, type)
280
+ t = type_to_java(type)
281
+ d = dir_to_java(dir)
282
+ @td = @td ? @td.relationships(t, d) : Java::OrgNeo4jKernelImplTraversal::TraversalDescriptionImpl.new.breadth_first().relationships(t, d)
283
+ end
284
+
185
285
  # Cuts of of parts of the traversal.
186
286
  # @yield [path]
187
287
  # @yieldparam [Java::OrgNeo4jGraphdb::Path] path the path which can be used to filter nodes
@@ -228,12 +328,6 @@ module Neo4j
228
328
  self
229
329
  end
230
330
 
231
- #def size
232
- # s = 0
233
- # iterator.each { |_| s += 1 }
234
- # s
235
- #end
236
-
237
331
  # @param [Fixnum] index the n'th node that will be return from the traversal
238
332
  def [](index)
239
333
  each_with_index { |node, i| break node if index == i }
@@ -295,6 +389,7 @@ module Neo4j
295
389
  end
296
390
 
297
391
  end
392
+
298
393
  end
299
394
  end
300
395
  end
@@ -1,5 +1,5 @@
1
1
  module Neo4j
2
2
  module Core
3
- VERSION = "2.0.0.alpha.1"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
@@ -0,0 +1,22 @@
1
+ module Neo4j
2
+ module Core
3
+ module Wrapper
4
+ module ClassMethods
5
+
6
+ # Tries to load a wrapper for this node if possible
7
+ # @see #wrapper_proc=
8
+ def wrapper(entity)
9
+ @_wrapper_proc ? @_wrapper_proc.call(entity) : entity
10
+ end
11
+
12
+ # Sets the procs to be used to load wrappers
13
+ # @see #wrapper
14
+ def wrapper_proc=(proc)
15
+ @_wrapper_proc = proc
16
+ end
17
+
18
+
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ module Neo4j
2
+ module Core
3
+ # Can be used to define your own wrapper class around nodes and relationships
4
+ module Wrapper
5
+
6
+ # @return [self, Object] return self or a wrapper Ruby object
7
+ # @see Neo4j::Node::ClassMethods#wrapper
8
+ def wrapper
9
+ self.class.wrapper(self)
10
+ end
11
+
12
+ # This can be implemented by a wrapper to returned the underlying java node or relationship.
13
+ # You can override this method in your own wrapper class.
14
+ # @return self
15
+ def _java_entity
16
+ self
17
+ end
18
+ end
19
+ end
20
+ end