neo4j 1.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. data/CHANGELOG +141 -0
  2. data/CONTRIBUTORS +15 -0
  3. data/Gemfile +3 -0
  4. data/README.rdoc +2015 -0
  5. data/lib/neo4j.old/batch_inserter.rb +144 -0
  6. data/lib/neo4j.old/config.rb +138 -0
  7. data/lib/neo4j.old/event_handler.rb +73 -0
  8. data/lib/neo4j.old/extensions/activemodel.rb +158 -0
  9. data/lib/neo4j.old/extensions/aggregate.rb +12 -0
  10. data/lib/neo4j.old/extensions/aggregate/aggregate_enum.rb +40 -0
  11. data/lib/neo4j.old/extensions/aggregate/ext/node_mixin.rb +69 -0
  12. data/lib/neo4j.old/extensions/aggregate/node_aggregate.rb +8 -0
  13. data/lib/neo4j.old/extensions/aggregate/node_aggregate_mixin.rb +331 -0
  14. data/lib/neo4j.old/extensions/aggregate/node_aggregator.rb +216 -0
  15. data/lib/neo4j.old/extensions/aggregate/node_group.rb +43 -0
  16. data/lib/neo4j.old/extensions/aggregate/prop_group.rb +30 -0
  17. data/lib/neo4j.old/extensions/aggregate/property_enum.rb +24 -0
  18. data/lib/neo4j.old/extensions/aggregate/props_aggregate.rb +8 -0
  19. data/lib/neo4j.old/extensions/aggregate/props_aggregate_mixin.rb +31 -0
  20. data/lib/neo4j.old/extensions/aggregate/props_aggregator.rb +80 -0
  21. data/lib/neo4j.old/extensions/find_path.rb +117 -0
  22. data/lib/neo4j.old/extensions/graph_algo.rb +1 -0
  23. data/lib/neo4j.old/extensions/graph_algo/all_simple_paths.rb +133 -0
  24. data/lib/neo4j.old/extensions/graph_algo/neo4j-graph-algo-0.3.jar +0 -0
  25. data/lib/neo4j.old/extensions/reindexer.rb +104 -0
  26. data/lib/neo4j.old/extensions/rest.rb +21 -0
  27. data/lib/neo4j.old/extensions/rest/rest.rb +336 -0
  28. data/lib/neo4j.old/extensions/rest/rest_mixin.rb +193 -0
  29. data/lib/neo4j.old/extensions/rest/server.rb +50 -0
  30. data/lib/neo4j.old/extensions/rest/stubs.rb +141 -0
  31. data/lib/neo4j.old/extensions/rest_master.rb +34 -0
  32. data/lib/neo4j.old/extensions/rest_slave.rb +31 -0
  33. data/lib/neo4j.old/extensions/tx_tracker.rb +392 -0
  34. data/lib/neo4j.old/indexer.rb +187 -0
  35. data/lib/neo4j.old/jars.rb +6 -0
  36. data/lib/neo4j.old/jars/geronimo-jta_1.1_spec-1.1.1.jar +0 -0
  37. data/lib/neo4j.old/jars/neo4j-kernel-1.0.jar +0 -0
  38. data/lib/neo4j.old/mixins/java_list_mixin.rb +139 -0
  39. data/lib/neo4j.old/mixins/java_node_mixin.rb +205 -0
  40. data/lib/neo4j.old/mixins/java_property_mixin.rb +169 -0
  41. data/lib/neo4j.old/mixins/java_relationship_mixin.rb +60 -0
  42. data/lib/neo4j.old/mixins/migration_mixin.rb +157 -0
  43. data/lib/neo4j.old/mixins/node_mixin.rb +249 -0
  44. data/lib/neo4j.old/mixins/property_class_methods.rb +265 -0
  45. data/lib/neo4j.old/mixins/rel_class_methods.rb +167 -0
  46. data/lib/neo4j.old/mixins/relationship_mixin.rb +103 -0
  47. data/lib/neo4j.old/neo.rb +247 -0
  48. data/lib/neo4j.old/node.rb +49 -0
  49. data/lib/neo4j.old/reference_node.rb +15 -0
  50. data/lib/neo4j.old/relationship.rb +85 -0
  51. data/lib/neo4j.old/relationships/decl_relationship_dsl.rb +164 -0
  52. data/lib/neo4j.old/relationships/has_list.rb +101 -0
  53. data/lib/neo4j.old/relationships/has_n.rb +129 -0
  54. data/lib/neo4j.old/relationships/node_traverser.rb +138 -0
  55. data/lib/neo4j.old/relationships/relationship_dsl.rb +149 -0
  56. data/lib/neo4j.old/relationships/traversal_position.rb +50 -0
  57. data/lib/neo4j.old/relationships/wrappers.rb +51 -0
  58. data/lib/neo4j.old/search_result.rb +72 -0
  59. data/lib/neo4j.old/transaction.rb +254 -0
  60. data/lib/neo4j.old/version.rb +3 -0
  61. data/lib/neo4j.rb +50 -0
  62. data/lib/neo4j/config.rb +137 -0
  63. data/lib/neo4j/database.rb +43 -0
  64. data/lib/neo4j/equal.rb +22 -0
  65. data/lib/neo4j/event_handler.rb +91 -0
  66. data/lib/neo4j/index.rb +157 -0
  67. data/lib/neo4j/jars/geronimo-jta_1.1_spec-1.1.1.jar +0 -0
  68. data/lib/neo4j/jars/lucene-core-2.9.2.jar +0 -0
  69. data/lib/neo4j/jars/lucene-core-3.0.1.jar +0 -0
  70. data/lib/neo4j/jars/neo4j-index-1.1.jar +0 -0
  71. data/lib/neo4j/jars/neo4j-kernel-1.1.1.jar +0 -0
  72. data/lib/neo4j/jars/neo4j-kernel-1.1.jar +0 -0
  73. data/lib/neo4j/jars/neo4j-lucene-index-0.1-20100916.085626-67.jar +0 -0
  74. data/lib/neo4j/mapping/class_methods/index.rb +21 -0
  75. data/lib/neo4j/mapping/class_methods/property.rb +139 -0
  76. data/lib/neo4j/mapping/class_methods/relationship.rb +96 -0
  77. data/lib/neo4j/mapping/class_methods/rule.rb +135 -0
  78. data/lib/neo4j/mapping/decl_relationship_dsl.rb +151 -0
  79. data/lib/neo4j/mapping/has_n.rb +117 -0
  80. data/lib/neo4j/mapping/node_mixin.rb +70 -0
  81. data/lib/neo4j/neo4j.rb +65 -0
  82. data/lib/neo4j/node.rb +82 -0
  83. data/lib/neo4j/node_mixin.rb +4 -0
  84. data/lib/neo4j/node_relationship.rb +60 -0
  85. data/lib/neo4j/node_traverser.rb +141 -0
  86. data/lib/neo4j/property.rb +72 -0
  87. data/lib/neo4j/rails/lucene_connection_closer.rb +19 -0
  88. data/lib/neo4j/rails/model.rb +210 -0
  89. data/lib/neo4j/rails/railtie.rb +16 -0
  90. data/lib/neo4j/rails/transaction.rb +29 -0
  91. data/lib/neo4j/rails/value.rb +43 -0
  92. data/lib/neo4j/relationship.rb +88 -0
  93. data/lib/neo4j/relationship_traverser.rb +57 -0
  94. data/lib/neo4j/to_java.rb +17 -0
  95. data/lib/neo4j/transaction.rb +69 -0
  96. data/lib/neo4j/version.rb +3 -0
  97. data/neo4j.gemspec +30 -0
  98. metadata +243 -0
@@ -0,0 +1,50 @@
1
+ module Neo4j
2
+ module Relationships
3
+
4
+ # Wrapper for org.neo4j.graphdb.TraversalPosition
5
+ # See Javadoc for org.neo4j.graphdb.TraversalPosition
6
+ # It can be used as a parameter in traversals filter functions.
7
+ #
8
+ # :api: public
9
+ class TraversalPosition
10
+ def initialize(traversal_position, raw = false)
11
+ @traversal_position = traversal_position
12
+ @raw = raw
13
+ end
14
+
15
+ # Return the current node.
16
+ def current_node
17
+ Neo4j.load_node(@traversal_position.currentNode.getId, @raw)
18
+ end
19
+
20
+ # Returns the previous node, may be nil.
21
+ def previous_node
22
+ return nil if @traversal_position.previousNode.nil?
23
+ Neo4j.load_node(@traversal_position.previousNode.getId, @raw)
24
+ end
25
+
26
+ # Return the last relationship traversed, may be nil.
27
+ def last_relationship_traversed
28
+ relationship = @traversal_position.lastRelationshipTraversed()
29
+ return nil if relationship.nil?
30
+ return relationship.wrapper unless @raw
31
+ relationship
32
+ end
33
+
34
+ # Returns the current traversal depth.
35
+ def depth
36
+ @traversal_position.depth
37
+ end
38
+
39
+ # Returns true if the current position is the start node, false otherwise.
40
+ def start_node?
41
+ @traversal_position.isStartNode
42
+ end
43
+
44
+ # Returns the number of nodes returned by traverser so far.
45
+ def returned_nodes_count
46
+ @traversal_position.returnedNodesCount
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,51 @@
1
+ module Neo4j
2
+ module Relationships
3
+
4
+ # Wrapper for org.neo4j.graphdb.ReturnableEvaluator
5
+ #
6
+ # :api: private
7
+ class ReturnableEvaluator #:nodoc:
8
+ include org.neo4j.graphdb.ReturnableEvaluator
9
+
10
+ def initialize(proc, raw = false)
11
+ @proc = proc
12
+ @raw = raw
13
+ end
14
+
15
+ def isReturnableNode( traversal_position )
16
+ # if the Proc takes one argument that we give it the traversal_position
17
+ result = if @proc.arity == 1
18
+ # wrap the traversal_position in the Neo4j.rb TraversalPostion object
19
+ @proc.call TraversalPosition.new(traversal_position, @raw)
20
+ else # otherwise we eval the proc in the context of the current node
21
+ # do not include the start node
22
+ return false if traversal_position.isStartNode()
23
+ eval_context = Neo4j::load_node(traversal_position.currentNode.getId, @raw)
24
+ eval_context.instance_eval(&@proc)
25
+ end
26
+
27
+ # java does not treat nil as false so we need to do it instead
28
+ (result)? true : false
29
+ end
30
+ end
31
+
32
+
33
+ # Wrapper for the neo4j org.neo4j.graphdb.StopEvalutor interface.
34
+ # Used in the Neo4j Traversers.
35
+ #
36
+ # :api: private
37
+ class DepthStopEvaluator #:nodoc:
38
+ include org.neo4j.graphdb.StopEvaluator
39
+
40
+ def initialize(depth)
41
+ @depth = depth
42
+ end
43
+
44
+ def isStopNode(pos)
45
+ pos.depth >= @depth
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,72 @@
1
+ module Neo4j
2
+
3
+ #
4
+ # Holds the search result and performs the query.
5
+ # The query is not performed until a search result item is requested.
6
+ # It is possible that one node is deleted,updated,created after the search is performed.
7
+ #
8
+ class SearchResult
9
+ include Enumerable
10
+
11
+ def initialize(index, query, query_for_nodes = true, &block)
12
+ @query = query
13
+ @query_for_nodes = query_for_nodes # if we are searching for nodes or relationships
14
+ @block = block
15
+ @index = index
16
+ @sort_by_fields = []
17
+ end
18
+
19
+ def hits
20
+ @hits ||= @index.find(query_params, &@block)
21
+ end
22
+
23
+ def query_params
24
+ unless @sort_by_fields.empty?
25
+ return case @query
26
+ when Hash
27
+ @query.merge({:sort_by => @sort_by_fields})
28
+ when String
29
+ [@query, { :sort_by => @sort_by_fields}]
30
+ end
31
+ end
32
+ return @query
33
+ end
34
+
35
+
36
+ # Returns the first item in the search result
37
+ #
38
+ # :api: public
39
+ def first
40
+ return nil if empty?
41
+ self[0]
42
+ end
43
+
44
+ def empty?
45
+ size == 0
46
+ end
47
+
48
+ def each
49
+ hits.each do |doc|
50
+
51
+ node_or_rel = @query_for_nodes ? Neo4j.load_node(doc[:id]) : Neo4j.load_rel(doc[:id])
52
+ # can happen that another thread has deleted it
53
+ raise "lucene found node/rel #{id} but it does not exist in neo" if node_or_rel.nil?
54
+ yield node_or_rel
55
+ end
56
+ end
57
+
58
+ def [](n)
59
+ doc = hits[n]
60
+ Neo4j.load_node(doc[:id])
61
+ end
62
+
63
+ def sort_by(*fields)
64
+ @sort_by_fields += fields
65
+ self
66
+ end
67
+
68
+ def size
69
+ hits.size
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,254 @@
1
+ module Neo4j
2
+
3
+ #
4
+ # Raised when an operation was called without a running transaction.
5
+ #
6
+ class NotInTransactionError < StandardError;
7
+ end
8
+
9
+
10
+ #
11
+ # Raised when an operation was called when an transaction was already running.
12
+ #
13
+ class AlreadyInTransactionError < StandardError;
14
+ end
15
+
16
+
17
+ #
18
+ # Wraps a Neo4j java transaction and lucene transactions.
19
+ # There can only be one transaction per thread.
20
+ #
21
+ class Transaction
22
+ attr_reader :neo_tx
23
+
24
+ @@counter = 0 # just for debugging purpose
25
+
26
+
27
+ # --------------------------------------------------------------------------
28
+ #
29
+ # Class methods
30
+ #
31
+ class << self
32
+
33
+
34
+ # :nodoc:
35
+ # debugging method
36
+ #
37
+ def called
38
+ res = ""
39
+ for i in 2..7 do
40
+ res << /\`([^\']+)\'/.match(caller(i).first)[1]
41
+ res << ', '
42
+ end
43
+ res
44
+ end
45
+
46
+
47
+ def placebo?(tx)
48
+ tx.java_object.java_type == 'org.neo4j.kernel.EmbeddedGraphDbImpl$PlaceboTransaction'
49
+ end
50
+
51
+ # Creates a transaction. If one is already running then a 'placebo' transaction will be created instead.
52
+ # A placebo transactions wraps the real transaction by not allowing the finish method to finish the
53
+ # real transaction.
54
+ #
55
+ def new
56
+ tx = Neo4j.instance.begin_tx
57
+ if running?
58
+ # expects a placebo transaction, check just in case
59
+ raise "Expected placebo transaction since one normal is already running" unless placebo?(tx)
60
+ tx = Transaction.current.create_placebo_tx_if_not_already_exists
61
+ tx
62
+ else
63
+ raise "Expected NOT placebo transaction since no TX is running" if placebo?(tx)
64
+ super(tx)
65
+ end
66
+ end
67
+
68
+ # Runs a block in a Neo4j transaction
69
+ #
70
+ # Most operations on neo requires an transaction. You will get much better performance if
71
+ # one transaction is wrapped around several neo operation instead of running one transaction per
72
+ # neo operation.
73
+ # If one transaction is already running then a 'placebo' transaction will be created.
74
+ # Performing a finish on a placebo transaction will not finish the 'real' transaction.
75
+ #
76
+ # ==== Params
77
+ # @yield the block to be performed in one transaction
78
+ # @yieldparam [Neo4j::Transaction] The transaction
79
+ #
80
+ # ==== Examples
81
+ # include 'neo4j'
82
+ #
83
+ # Neo4j::Transaction.run {
84
+ # node = PersonNode.new
85
+ # }
86
+ #
87
+ # You have also access to transaction object
88
+ #
89
+ # Neo4j::Transaction.run { |t|
90
+ # # something failed
91
+ # t.failure # will cause a rollback
92
+ # }
93
+ #
94
+ #
95
+ # ==== Returns
96
+ # The value of the evaluated provided block
97
+ #
98
+ def run # :yield: transaction
99
+ raise ArgumentError.new("Expected a block to run in Transaction.run") unless block_given?
100
+
101
+ begin
102
+ tx = Neo4j::Transaction.new
103
+ ret = yield tx
104
+ rescue Exception => bang
105
+ # puts "BANG #{bang}"
106
+ # puts bang.backtrace.join("\n")
107
+ tx.failure unless tx.nil?
108
+ raise
109
+ ensure
110
+ tx.finish unless tx.nil?
111
+ end
112
+ ret
113
+ end
114
+
115
+
116
+ # Returns the current running transaction or nil
117
+ #
118
+ def current
119
+ Thread.current[:transaction]
120
+ end
121
+
122
+
123
+ # Returns true if there is a transaction for the current thread
124
+ #
125
+ def running?
126
+ self.current != nil # && self.current.neo_tx != nil
127
+ end
128
+
129
+
130
+ # Returns true if the transaction has been marked for failure/rollback
131
+ #
132
+ def failure?
133
+ current.failure?
134
+ end
135
+
136
+ # Finish the current transaction if it is running.
137
+ #
138
+ # See Neo4j::Transaction#failure
139
+ #
140
+ def failure
141
+ current.failure if running?
142
+ end
143
+
144
+
145
+ # Finish the current transaction if it is running
146
+ def finish
147
+ current.finish if running?
148
+ end
149
+ end
150
+
151
+
152
+ #
153
+ # --------------------------------------------------------------------------
154
+ # Instance methods
155
+ #
156
+
157
+
158
+ def initialize(neo_tx)
159
+ raise AlreadyInTransactionError.new if Transaction.running?
160
+ @neo_tx = neo_tx
161
+ @@counter += 1
162
+ @id = @@counter
163
+ @failure = false
164
+ Thread.current[:transaction] = self
165
+ end
166
+
167
+ def to_s
168
+ "Transaction: placebo: #{placebo?}, #{@id} failure: #{failure?}, running #{Transaction.running?}, lucene: #{Lucene::Transaction.running?}, thread: #{Thread.current.to_s} #{@neo_tx}"
169
+ end
170
+
171
+
172
+ # Returns true if the transaction will rollback
173
+ def failure?
174
+ @failure == true
175
+ end
176
+
177
+
178
+ def placebo?
179
+ false
180
+ end
181
+
182
+ def create_placebo_tx_if_not_already_exists # :nodoc:
183
+ @placebo ||= PlaceboTransaction.new(self)
184
+ end
185
+
186
+
187
+ # Marks this transaction as successful, which means that it will be commited
188
+ # upon invocation of finish() unless failure() has or will be invoked before then.
189
+ def success
190
+ raise NotInTransactionError.new unless Transaction.running?
191
+ @neo_tx.success
192
+ end
193
+
194
+
195
+ # Commits or marks this transaction for rollback, depending on whether
196
+ # success() or failure() has been previously invoked.
197
+ #
198
+ def finish
199
+ return unless Transaction.running?
200
+ Neo4j.event_handler.tx_finished(self) unless failure?
201
+ begin
202
+ @neo_tx.success unless failure?
203
+ @neo_tx.finish
204
+ @neo_tx=nil
205
+ rescue Exception => bang
206
+ raise
207
+ end
208
+
209
+ Thread.current[:transaction] = nil
210
+ if Lucene::Transaction.running?
211
+ # mark lucene transaction for failure if the neo transaction fails
212
+ Lucene::Transaction.current.failure if failure?
213
+ Lucene::Transaction.current.commit
214
+ end
215
+ end
216
+
217
+ # Marks this transaction as failed, which means that it will inexplicably
218
+ # be rolled back upon invocation of finish().
219
+ def failure
220
+ raise NotInTransactionError.new unless Transaction.running?
221
+ @neo_tx.failure
222
+ @failure = true
223
+ $NEO_LOGGER.info{"failure #{self.to_s}"}
224
+ end
225
+
226
+ end
227
+
228
+ #
229
+ # This is returned when trying to create a new transaction while a transaction is already running.
230
+ # This class will do nothing when the finish method is called.
231
+ # Finish will only be called when the 'real' transaction does it.
232
+ #
233
+ class PlaceboTransaction < DelegateClass(Transaction) #:nodoc:
234
+
235
+ def initialize(tx)
236
+ super(tx)
237
+ @tx = tx # store it only for logging purpose
238
+ end
239
+
240
+ def placebo?
241
+ true
242
+ end
243
+
244
+ # Do nothing since Neo4j does not support chained transactions.
245
+ #
246
+ def finish
247
+ end
248
+
249
+ def to_s
250
+ "PLACEBO TX"
251
+ end
252
+ end
253
+
254
+ end
@@ -0,0 +1,3 @@
1
+ module Neo4j
2
+ VERSION = '0.4.4' unless defined?(Neo4j::VERSION)
3
+ end
data/lib/neo4j.rb ADDED
@@ -0,0 +1,50 @@
1
+ include Java
2
+
3
+ require 'enumerator'
4
+ require 'forwardable'
5
+ require 'time'
6
+ require 'date'
7
+
8
+ require 'neo4j/jars/neo4j-kernel-1.1.1.jar'
9
+ #require 'neo4j/jars/neo4j-kernel-1.2-20100916.085228-39.jar'
10
+ require 'neo4j/jars/geronimo-jta_1.1_spec-1.1.1.jar'
11
+ require 'neo4j/jars/lucene-core-3.0.1.jar'
12
+ require 'neo4j/jars/neo4j-lucene-index-0.1-20100916.085626-67.jar'
13
+ require 'neo4j/to_java'
14
+ require 'neo4j/version'
15
+ require 'neo4j/equal'
16
+ require 'neo4j/index'
17
+ require 'neo4j/relationship_traverser'
18
+ require 'neo4j/event_handler'
19
+ require 'neo4j/database'
20
+ require 'neo4j/node_traverser'
21
+ require 'neo4j/property'
22
+ require 'neo4j/transaction'
23
+ require 'neo4j/node_relationship'
24
+ require 'neo4j/relationship'
25
+ require 'neo4j/node'
26
+ require 'neo4j/config'
27
+ require 'neo4j/neo4j'
28
+ require 'neo4j/mapping/class_methods/property'
29
+ require 'neo4j/mapping/class_methods/index'
30
+ require 'neo4j/mapping/class_methods/relationship'
31
+ require 'neo4j/mapping/decl_relationship_dsl'
32
+ require 'neo4j/mapping/has_n'
33
+ require 'neo4j/mapping/node_mixin'
34
+ require 'neo4j/node_mixin'
35
+ require 'neo4j/mapping/class_methods/rule'
36
+
37
+ # rails
38
+ require 'rails/railtie'
39
+ require 'active_model'
40
+ require 'neo4j/rails/transaction'
41
+ require 'neo4j/rails/railtie'
42
+ require 'neo4j/rails/model'
43
+ require 'neo4j/rails/value'
44
+ require 'neo4j/rails/lucene_connection_closer'
45
+
46
+
47
+
48
+ # hmm, looks like Enumerator have been moved in some ruby versions
49
+ Enumerator = Enumerable::Enumerator unless defined? Enumerator
50
+