neo4j 2.0.0.alpha.5-java → 2.0.0.alpha.6-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. data/CHANGELOG +12 -0
  2. data/Gemfile +0 -4
  3. data/README.rdoc +106 -62
  4. data/lib/neo4j.rb +7 -33
  5. data/lib/neo4j/performance.rb +43 -0
  6. data/lib/neo4j/rails/accept_id.rb +19 -18
  7. data/lib/neo4j/rails/attributes.rb +366 -120
  8. data/lib/neo4j/rails/finders.rb +41 -15
  9. data/lib/neo4j/rails/has_n.rb +203 -0
  10. data/lib/neo4j/rails/identity.rb +25 -0
  11. data/lib/neo4j/rails/model.rb +65 -242
  12. data/lib/neo4j/rails/nested_attributes.rb +108 -0
  13. data/lib/neo4j/rails/node_persistance.rb +56 -0
  14. data/lib/neo4j/rails/observer.rb +0 -2
  15. data/lib/neo4j/rails/persistence.rb +32 -154
  16. data/lib/neo4j/rails/rack_middleware.rb +26 -2
  17. data/lib/neo4j/rails/rails.rb +9 -6
  18. data/lib/neo4j/rails/railtie.rb +1 -2
  19. data/lib/neo4j/rails/relationship.rb +18 -125
  20. data/lib/neo4j/rails/relationship_persistence.rb +107 -0
  21. data/lib/neo4j/rails/relationships/node_dsl.rb +72 -44
  22. data/lib/neo4j/rails/relationships/relationships.rb +187 -59
  23. data/lib/neo4j/rails/relationships/rels_dsl.rb +18 -17
  24. data/lib/neo4j/rails/relationships/storage.rb +19 -17
  25. data/lib/neo4j/rails/timestamps.rb +53 -51
  26. data/lib/neo4j/rails/transaction.rb +9 -1
  27. data/lib/neo4j/rails/validations/uniqueness.rb +1 -1
  28. data/lib/neo4j/rails/versioning/versioning.rb +2 -2
  29. data/lib/neo4j/version.rb +1 -1
  30. data/lib/orm_adapter/adapters/neo4j.rb +47 -46
  31. data/neo4j.gemspec +1 -1
  32. metadata +10 -69
  33. data/lib/neo4j/algo/algo.rb +0 -294
  34. data/lib/neo4j/batch/batch.rb +0 -4
  35. data/lib/neo4j/batch/indexer.rb +0 -109
  36. data/lib/neo4j/batch/inserter.rb +0 -179
  37. data/lib/neo4j/batch/rule_inserter.rb +0 -24
  38. data/lib/neo4j/batch/rule_node.rb +0 -72
  39. data/lib/neo4j/config.rb +0 -177
  40. data/lib/neo4j/core_ext/class/inheritable_attributes.rb +0 -12
  41. data/lib/neo4j/core_ext/class/rewrite_inheritable_attributes.rb +0 -170
  42. data/lib/neo4j/database.rb +0 -158
  43. data/lib/neo4j/equal.rb +0 -21
  44. data/lib/neo4j/event_handler.rb +0 -263
  45. data/lib/neo4j/has_list/class_methods.rb +0 -11
  46. data/lib/neo4j/has_list/has_list.rb +0 -3
  47. data/lib/neo4j/has_list/mapping.rb +0 -133
  48. data/lib/neo4j/has_n/class_methods.rb +0 -119
  49. data/lib/neo4j/has_n/decl_relationship_dsl.rb +0 -246
  50. data/lib/neo4j/has_n/has_n.rb +0 -3
  51. data/lib/neo4j/has_n/mapping.rb +0 -98
  52. data/lib/neo4j/identity_map.rb +0 -140
  53. data/lib/neo4j/index/class_methods.rb +0 -108
  54. data/lib/neo4j/index/index.rb +0 -39
  55. data/lib/neo4j/index/indexer.rb +0 -341
  56. data/lib/neo4j/index/indexer_registry.rb +0 -68
  57. data/lib/neo4j/index/lucene_query.rb +0 -256
  58. data/lib/neo4j/load.rb +0 -25
  59. data/lib/neo4j/migrations/class_methods.rb +0 -110
  60. data/lib/neo4j/migrations/extensions.rb +0 -58
  61. data/lib/neo4j/migrations/lazy_node_mixin.rb +0 -41
  62. data/lib/neo4j/migrations/migration.rb +0 -112
  63. data/lib/neo4j/migrations/migrations.rb +0 -6
  64. data/lib/neo4j/migrations/node_mixin.rb +0 -80
  65. data/lib/neo4j/migrations/ref_node_wrapper.rb +0 -32
  66. data/lib/neo4j/model.rb +0 -4
  67. data/lib/neo4j/neo4j.rb +0 -216
  68. data/lib/neo4j/node.rb +0 -270
  69. data/lib/neo4j/node_mixin/class_methods.rb +0 -51
  70. data/lib/neo4j/node_mixin/node_mixin.rb +0 -141
  71. data/lib/neo4j/paginated.rb +0 -23
  72. data/lib/neo4j/property/class_methods.rb +0 -79
  73. data/lib/neo4j/property/property.rb +0 -111
  74. data/lib/neo4j/rails/mapping/property.rb +0 -183
  75. data/lib/neo4j/rails/rel_persistence.rb +0 -237
  76. data/lib/neo4j/relationship.rb +0 -239
  77. data/lib/neo4j/relationship_mixin/class_methods.rb +0 -36
  78. data/lib/neo4j/relationship_mixin/relationship_mixin.rb +0 -142
  79. data/lib/neo4j/relationship_set.rb +0 -58
  80. data/lib/neo4j/rels/rels.rb +0 -110
  81. data/lib/neo4j/rels/traverser.rb +0 -102
  82. data/lib/neo4j/rule/class_methods.rb +0 -201
  83. data/lib/neo4j/rule/event_listener.rb +0 -66
  84. data/lib/neo4j/rule/functions/count.rb +0 -43
  85. data/lib/neo4j/rule/functions/function.rb +0 -74
  86. data/lib/neo4j/rule/functions/functions.rb +0 -3
  87. data/lib/neo4j/rule/functions/sum.rb +0 -29
  88. data/lib/neo4j/rule/rule.rb +0 -150
  89. data/lib/neo4j/rule/rule_node.rb +0 -217
  90. data/lib/neo4j/to_java.rb +0 -31
  91. data/lib/neo4j/transaction.rb +0 -73
  92. data/lib/neo4j/traversal/filter_predicate.rb +0 -25
  93. data/lib/neo4j/traversal/prune_evaluator.rb +0 -14
  94. data/lib/neo4j/traversal/rel_expander.rb +0 -31
  95. data/lib/neo4j/traversal/traversal.rb +0 -141
  96. data/lib/neo4j/traversal/traverser.rb +0 -284
  97. data/lib/neo4j/type_converters/type_converters.rb +0 -288
@@ -1,68 +0,0 @@
1
- module Neo4j
2
- module Index
3
- class IndexerRegistry #:nodoc:
4
- class << self
5
-
6
- def delete_all_indexes
7
- @@indexers.values.each {|i| i.delete_index_type}
8
- end
9
-
10
- def create_for(this_clazz, using_other_clazz, type)
11
- @@indexers ||= {}
12
- index = Indexer.new(this_clazz, type)
13
- index.inherit_fields_from(@@indexers[using_other_clazz.to_s]) if @@indexers[using_other_clazz.to_s]
14
- @@indexers[this_clazz.to_s] = index
15
- end
16
-
17
- def find_by_class(classname)
18
- @@indexers[classname]
19
- end
20
-
21
- def on_node_deleted(node, old_props, deleted_relationship_set, deleted_identity_map)
22
- indexer = find_by_class(old_props['_classname'] || node.class.to_s)
23
- indexer && indexer.remove_index_on_fields(node, old_props, deleted_relationship_set)
24
- end
25
-
26
- def on_property_changed(node, field, old_val, new_val)
27
- classname = node['_classname'] || node.class.to_s
28
- indexer = find_by_class(classname)
29
-
30
- indexer && indexer.update_index_on(node, field, old_val, new_val)
31
- end
32
-
33
- def on_rel_property_changed(rel, field, old_val, new_val)
34
- # works exactly like for nodes
35
- on_property_changed(rel, field, old_val, new_val)
36
- end
37
-
38
- def on_relationship_created(rel,created_identity_map)
39
- end_node = rel._end_node
40
- # if end_node was created in this transaction then it will be handled in on_property_changed
41
- created = created_identity_map.get(end_node.neo_id)
42
- unless created
43
- indexer = find_by_class(end_node['_classname'])
44
- indexer && indexer.update_on_new_relationship(rel)
45
- end
46
- end
47
-
48
- def on_relationship_deleted(rel, old_props, deleted_relationship_set, deleted_identity_map)
49
- on_node_deleted(rel, old_props, deleted_relationship_set, deleted_identity_map)
50
- # if only the relationship has been deleted then we have to remove the index
51
- # if both the relationship and the node has been deleted then the index will be removed in the
52
- # on_node_deleted callback
53
- end_node = rel._end_node
54
- deleted = deleted_identity_map.get(end_node.neo_id)
55
- unless deleted
56
- indexer = find_by_class(end_node['_classname'])
57
- indexer && indexer.update_on_deleted_relationship(rel)
58
- end
59
- end
60
-
61
- def on_neo4j_shutdown(*)
62
- @@indexers.each_value {|indexer| indexer.on_neo4j_shutdown}
63
- end
64
- end
65
- end
66
- Neo4j.unstarted_db.event_handler.add(IndexerRegistry) unless Neo4j.read_only?
67
- end
68
- end
@@ -1,256 +0,0 @@
1
- module Neo4j
2
- module Index
3
- # == LuceneQuery
4
- #
5
- # This object is returned when you call the #find method on the Node, Relationship.
6
- # The actual query is not executed until the first item is requested.
7
- #
8
- # You can perform a query in many different ways:
9
- #
10
- # ==== By Hash
11
- #
12
- # Example:
13
- # Person.find(:name => 'foo', :age => 3)
14
- #
15
- # ==== By Range
16
- #
17
- # Example:
18
- # Person.find(:age).between(15,35)
19
- #
20
- # ==== By Lucene Query Syntax
21
- #
22
- # Example
23
- # Car.find('wheels:"4" AND colour: "blue")
24
- #
25
- # For more information about the syntax see http://lucene.apache.org/java/3_0_2/queryparsersyntax.html
26
- #
27
- # ==== By Compound Queries
28
- #
29
- # You can combine several queries by <tt>AND</tt>ing those together.
30
- #
31
- # Example:
32
- # Vehicle.find(:weight).between(5.0, 100000.0).and(:name).between('a', 'd')
33
- #
34
- # === See Also
35
- # * Neo4j::Index::Indexer#index
36
- # * Neo4j::Index::Indexer#find - which returns an LuceneQuery
37
- #
38
- class LuceneQuery
39
- include Enumerable
40
- attr_accessor :left_and_query, :left_or_query, :right_not_query
41
-
42
- def initialize(index, decl_props, query, params={})
43
- @index = index
44
- @query = query
45
- @decl_props = decl_props
46
- @params = params
47
-
48
- if params.include?(:sort)
49
- @order = {}
50
- params[:sort].each_pair { |k, v| @order[k] = (v == :desc) }
51
- end
52
- end
53
-
54
- # Implements the Ruby +Enumerable+ interface
55
- def each
56
- hits.each { |x| yield x.wrapper }
57
- end
58
-
59
- # Close hits
60
- #
61
- # Closes the underlying search result. This method should be called whenever you've got what you wanted from the result and won't use it anymore.
62
- # It's necessary to call it so that underlying indexes can dispose of allocated resources for this search result.
63
- # You can however skip to call this method if you loop through the whole result, then close() will be called automatically.
64
- # Even if you loop through the entire result and then call this method it will silently ignore any consequtive call (for convenience).
65
- #
66
- # This must be done according to the Neo4j Java Documentation:
67
- def close
68
- @hits.close if @hits
69
- @hits = nil
70
- end
71
-
72
- # True if there is no search hits.
73
- def empty?
74
- hits.size == 0
75
- end
76
-
77
- # returns the n'th search item
78
- # Does simply loop all search items till the n'th is found.
79
- #
80
- def [](index)
81
- i = 0
82
- each{|x| return x if i == index; i += 1}
83
- nil # out of index
84
- end
85
-
86
- # Returns the number of search hits
87
- def size
88
- hits.size
89
- end
90
-
91
- def hits #:nodoc:
92
- close
93
- @hits = perform_query
94
- end
95
-
96
- # Performs a range query
97
- # Notice that if you don't specify a type when declaring a property a String range query will be performed.
98
- #
99
- def between(lower, upper, lower_incusive=false, upper_inclusive=false)
100
- raise "Expected a symbol. Syntax for range queries example: index(:weight).between(a,b)" unless Symbol === @query
101
- raise "Can't only do range queries on Neo4j::NodeMixin, Neo4j::Model, Neo4j::RelationshipMixin" unless @decl_props
102
- # check that we perform a range query on the same values as we have declared with the property :key, :type => ...
103
- type = @decl_props[@query] && @decl_props[@query][:type]
104
- raise "find(#{@query}).between(#{lower}, #{upper}): #{lower} not a #{type}" if type && !type === lower.class
105
- raise "find(#{@query}).between(#{lower}, #{upper}): #{upper} not a #{type}" if type && !type === upper.class
106
-
107
- # Make it possible to convert those values
108
- @query = range_query(@query, lower, upper, lower_incusive, upper_inclusive)
109
- self
110
- end
111
-
112
- def range_query(field, lower, upper, lower_incusive, upper_inclusive)
113
- lower = TypeConverters.convert(lower)
114
- upper = TypeConverters.convert(upper)
115
-
116
- case lower
117
- when Fixnum
118
- org.apache.lucene.search.NumericRangeQuery.new_long_range(field.to_s, lower, upper, lower_incusive, upper_inclusive)
119
- when Float
120
- org.apache.lucene.search.NumericRangeQuery.new_double_range(field.to_s, lower, upper, lower_incusive, upper_inclusive)
121
- else
122
- org.apache.lucene.search.TermRangeQuery.new(field.to_s, lower, upper, lower_incusive, upper_inclusive)
123
- end
124
- end
125
-
126
-
127
- # Create a compound lucene query.
128
- #
129
- # ==== Parameters
130
- # query2 :: the query that should be AND together
131
- #
132
- # ==== Example
133
- #
134
- # Person.find(:name=>'kalle').and(:age => 3)
135
- #
136
- def and(query2)
137
- LuceneQuery.new(@index, @decl_props, query2).tap { |new_query| new_query.left_and_query = self }
138
- end
139
-
140
- # Create an OR lucene query.
141
- #
142
- # ==== Parameters
143
- # query2 :: the query that should be OR together
144
- #
145
- # ==== Example
146
- #
147
- # Person.find(:name=>'kalle').or(:age => 3)
148
- #
149
- def or(query2)
150
- LuceneQuery.new(@index, @decl_props, query2).tap { |new_query| new_query.left_or_query = self }
151
- end
152
-
153
- # Create a NOT lucene query.
154
- #
155
- # ==== Parameters
156
- # query2 :: the query that should exclude matching results
157
- #
158
- # ==== Example
159
- #
160
- # Person.find(:age => 3).not(:name=>'kalle')
161
- #
162
- def not(query2)
163
- LuceneQuery.new(@index, @decl_props, query2).tap { |new_query| new_query.right_not_query = self }
164
- end
165
-
166
-
167
- # Sort descending the given fields.
168
- def desc(*fields)
169
- @order = fields.inject(@order || {}) { |memo, field| memo[field] = true; memo }
170
- self
171
- end
172
-
173
- # Sort ascending the given fields.
174
- def asc(*fields)
175
- @order = fields.inject(@order || {}) { |memo, field| memo[field] = false; memo }
176
- self
177
- end
178
-
179
- def build_and_query(query) #:nodoc:
180
- build_composite_query(@left_and_query.build_query, query, org.apache.lucene.search.BooleanClause::Occur::MUST)
181
- end
182
-
183
- def build_or_query(query) #:nodoc:
184
- build_composite_query(@left_or_query.build_query, query, org.apache.lucene.search.BooleanClause::Occur::SHOULD)
185
- end
186
-
187
- def build_not_query(query) #:nodoc:
188
- right_query = @right_not_query.build_query
189
- composite_query = org.apache.lucene.search.BooleanQuery.new
190
- composite_query.add(query, org.apache.lucene.search.BooleanClause::Occur::MUST_NOT)
191
- composite_query.add(right_query, org.apache.lucene.search.BooleanClause::Occur::MUST)
192
- composite_query
193
- end
194
-
195
- def build_composite_query(left_query, right_query, opeartor) #:nodoc:
196
- composite_query = org.apache.lucene.search.BooleanQuery.new
197
- composite_query.add(left_query, opeartor)
198
- composite_query.add(right_query, opeartor)
199
- composite_query
200
- end
201
-
202
- def build_sort_query(query) #:nodoc:
203
- java_sort_fields = @order.keys.inject([]) do |memo, field|
204
- decl_type = @decl_props && @decl_props[field] && @decl_props[field][:type]
205
- type = case
206
- when Float == decl_type
207
- org.apache.lucene.search.SortField::DOUBLE
208
- when Fixnum == decl_type || DateTime == decl_type || Date == decl_type || Time == decl_type
209
- org.apache.lucene.search.SortField::LONG
210
- else
211
- org.apache.lucene.search.SortField::STRING
212
- end
213
- memo << org.apache.lucene.search.SortField.new(field.to_s, type, @order[field])
214
- end
215
- sort = org.apache.lucene.search.Sort.new(*java_sort_fields)
216
- org.neo4j.index.lucene.QueryContext.new(query).sort(sort)
217
- end
218
-
219
- def build_hash_query(query) #:nodoc:
220
- and_query = org.apache.lucene.search.BooleanQuery.new
221
-
222
- query.each_pair do |key, value|
223
- type = @decl_props && @decl_props[key.to_sym] && @decl_props[key.to_sym][:type]
224
- if !type.nil? && type != String
225
- if Range === value
226
- and_query.add(range_query(key, value.first, value.last, true, !value.exclude_end?), org.apache.lucene.search.BooleanClause::Occur::MUST)
227
- else
228
- and_query.add(range_query(key, value, value, true, true), org.apache.lucene.search.BooleanClause::Occur::MUST)
229
- end
230
- else
231
- conv_value = type ? TypeConverters.convert(value) : value.to_s
232
- term = org.apache.lucene.index.Term.new(key.to_s, conv_value)
233
- term_query = org.apache.lucene.search.TermQuery.new(term)
234
- and_query.add(term_query, org.apache.lucene.search.BooleanClause::Occur::MUST)
235
- end
236
- end
237
- and_query
238
- end
239
-
240
- def build_query #:nodoc:
241
- query = @query
242
- query = build_hash_query(query) if Hash === query
243
- query = build_and_query(query) if @left_and_query
244
- query = build_or_query(query) if @left_or_query
245
- query = build_not_query(query) if @right_not_query
246
- query = build_sort_query(query) if @order
247
- query
248
- end
249
-
250
- def perform_query #:nodoc:
251
- @index.query(build_query)
252
- end
253
- end
254
- end
255
- end
256
-
data/lib/neo4j/load.rb DELETED
@@ -1,25 +0,0 @@
1
- module Neo4j
2
-
3
- # === Mixin responsible for loading Ruby wrappers for Neo4j Nodes and Relationship.
4
- #
5
- module Load
6
- def wrapper(entity) # :nodoc:
7
- return entity unless entity.property?(:_classname)
8
- existing_instance = Neo4j::IdentityMap.get(entity)
9
- return existing_instance if existing_instance
10
- new_instance = to_class(entity[:_classname]).load_wrapper(entity)
11
- Neo4j::IdentityMap.add(entity, new_instance)
12
- new_instance
13
- end
14
-
15
- def to_class(class_name) # :nodoc:
16
- class_name.split("::").inject(Kernel) {|container, name| container.const_get(name.to_s) }
17
- end
18
-
19
- # Checks if the given entity (node/relationship) or entity id (#neo_id) exists in the database.
20
- def exist?(entity_or_entity_id, db = Neo4j.started_db)
21
- id = entity_or_entity_id.kind_of?(Fixnum) ? entity_or_entity_id : entity_or_entity_id.id
22
- _load(id, db) != nil
23
- end
24
- end
25
- end
@@ -1,110 +0,0 @@
1
- module Neo4j
2
- module Migrations
3
- module ClassMethods
4
-
5
- # Sets the the version we request to migrate to
6
- # If not set will migrate to the highest possible migration
7
- def migrate_to=(version)
8
- @migrate_to = version
9
- end
10
-
11
- def migrate_to
12
- @migrate_to
13
- end
14
-
15
- def latest_migration
16
- migrations.keys.sort.reverse[0]
17
- end
18
-
19
- # contains all the migrations defined with the #migration DSL method
20
- def migrations
21
- @migrations ||= {}
22
- end
23
-
24
- # Remote all migration and set migrate_to = nil
25
- # Does not change the version of nodes.
26
- def reset_migrations!
27
- @migrations = nil
28
- @migrate_to = nil
29
- end
30
-
31
-
32
- # Specifies a migration to be performed.
33
- # Updates the migrate_to variable so that it will migrate to the latest migration.
34
- #
35
- # === Example
36
- #
37
- # In the following example the up and down method will be evaluated in the context of a Person node.
38
- #
39
- # Person.migration 1, :my_first_migration do
40
- # up { ... }
41
- # down { ... }
42
- # end
43
- #
44
- # See the Neo4j::Migrations::Migration which the DSL is evaluated in.
45
- #
46
- def migration(version, name, &block)
47
- migration = Migration.new(version, name)
48
- migration.instance_eval(&block)
49
- migrations[version] = migration
50
- self.migrate_to = latest_migration
51
- end
52
-
53
-
54
- def _migrate!(context, meta_node, version=nil) #:nodoc:
55
- # set the version we want to migrate to if provided
56
- self.migrate_to = version if version
57
-
58
- # requested to migrate to a version ?
59
- return if self.migrate_to.nil?
60
-
61
- # which version are we on now ?
62
- current_version = meta_node[:_db_version] || 0
63
-
64
- # do we need to migrate ?
65
- return if current_version == self.migrate_to
66
-
67
- # ok, so we are running some migrations
68
- if Neo4j::Config['migration_thread']
69
- Thread.new { _upgrade_or_downgrade(current_version, context, meta_node) }
70
- else
71
- _upgrade_or_downgrade(current_version, context, meta_node)
72
- end
73
- end
74
-
75
- def _upgrade_or_downgrade(current_version, context, meta_node) #:nodoc:
76
- if (current_version < self.migrate_to)
77
- upgrade((current_version+1).upto(self.migrate_to).collect { |ver| migrations[ver] }, context, meta_node)
78
- else
79
- downgrade(current_version.downto(self.migrate_to+1).collect { |ver| migrations[ver] }, context, meta_node)
80
- end
81
-
82
- end
83
-
84
-
85
- # Running the up method on the given migrations.
86
- #
87
- # === Parameters
88
- # migrations :: an enumerable of Migration objects
89
- def upgrade(migrations, context, meta_node)
90
- migrations.each do |m|
91
- Neo4j.logger.info "Running upgrade: #{m}"
92
- m.execute_up(context, meta_node)
93
- end
94
- end
95
-
96
- # Running the down method on the given migrations.
97
- #
98
- # === Parameters
99
- # migrations:: an enumerable of Migration objects
100
- def downgrade(migrations, context, meta_node)
101
- migrations.each do |m|
102
- Neo4j.logger.info "Running downgrade: #{m}"
103
- m.execute_down(context, meta_node)
104
- end
105
- end
106
-
107
- end
108
- end
109
-
110
- end