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
@@ -0,0 +1,108 @@
1
+ module Neo4j
2
+ module Rails
3
+ module NestedAttributes
4
+ extend ActiveSupport::Concern
5
+ extend TxMethods
6
+
7
+ def update_nested_attributes(rel_type, attr, options)
8
+ allow_destroy, reject_if = [options[:allow_destroy], options[:reject_if]] if options
9
+ begin
10
+ # Check if we want to destroy not found nodes (e.g. {..., :_destroy => '1' } ?
11
+ destroy = attr.delete(:_destroy)
12
+ found = _find_node(rel_type, attr[:id]) || Neo4j::Rails::Model.find(attr[:id])
13
+ if allow_destroy && destroy && destroy != '0'
14
+ found.destroy if found
15
+ else
16
+ if not found
17
+ _create_entity(rel_type, attr) #Create new node from scratch
18
+ else
19
+ #Create relationship to existing node in case it doesn't exist already
20
+ _add_relationship(rel_type, found) if (not _has_relationship(rel_type, attr[:id]))
21
+ found.update_attributes(attr)
22
+ end
23
+ end
24
+ end unless reject_if?(reject_if, attr)
25
+ end
26
+ tx_methods :update_nested_attributes
27
+
28
+
29
+ module ClassMethods
30
+ def accepts_nested_attributes_for(*attr_names)
31
+ options = attr_names.pop if attr_names[-1].is_a?(Hash)
32
+
33
+ attr_names.each do |association_name|
34
+ # Do some validation that we have defined the relationships we want to nest
35
+ rel = self._decl_rels[association_name.to_sym]
36
+ raise "No relationship declared with has_n or has_one with type #{association_name}" unless rel
37
+ raise "Can't use accepts_nested_attributes_for(#{association_name}) since it has not defined which class it has a relationship to, use has_n(#{association_name}).to(MyOtherClass)" unless rel.target_class
38
+
39
+ if rel.has_one?
40
+ send(:define_method, "#{association_name}_attributes=") do |attributes|
41
+ update_nested_attributes(association_name.to_sym, attributes, options)
42
+ end
43
+ else
44
+ send(:define_method, "#{association_name}_attributes=") do |attributes|
45
+ if attributes.is_a?(Array)
46
+ attributes.each do |attr|
47
+ update_nested_attributes(association_name.to_sym, attr, options)
48
+ end
49
+ else
50
+ attributes.each_value do |attr|
51
+ update_nested_attributes(association_name.to_sym, attr, options)
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ end
58
+ end
59
+ end
60
+
61
+ protected
62
+
63
+ def _create_entity(rel_type, attr)
64
+ clazz = self.class._decl_rels[rel_type.to_sym].target_class
65
+ _add_relationship(rel_type, clazz.new(attr))
66
+ end
67
+
68
+ def _add_relationship(rel_type, node)
69
+ if respond_to?("#{rel_type}_rel")
70
+ send("#{rel_type}=", node)
71
+ elsif respond_to?("#{rel_type}_rels")
72
+ has_n = send("#{rel_type}")
73
+ has_n << node
74
+ else
75
+ raise "oops #{rel_type}"
76
+ end
77
+ end
78
+
79
+ def _find_node(rel_type, id)
80
+ return nil if id.nil?
81
+ if respond_to?("#{rel_type}_rel")
82
+ send("#{rel_type}")
83
+ elsif respond_to?("#{rel_type}_rels")
84
+ has_n = send("#{rel_type}")
85
+ has_n.find { |n| n.id == id }
86
+ else
87
+ raise "oops #{rel_type}"
88
+ end
89
+ end
90
+
91
+ def _has_relationship(rel_type, id)
92
+ !_find_node(rel_type, id).nil?
93
+ end
94
+
95
+ def reject_if?(proc_or_symbol, attr)
96
+ return false if proc_or_symbol.nil?
97
+ if proc_or_symbol.is_a?(Symbol)
98
+ meth = method(proc_or_symbol)
99
+ meth.arity == 0 ? meth.call : meth.call(attr)
100
+ else
101
+ proc_or_symbol.call(attr)
102
+ end
103
+ end
104
+
105
+
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,56 @@
1
+ module Neo4j
2
+ module Rails
3
+ module NodePersistence
4
+ extend TxMethods
5
+
6
+ def initialize(attributes = nil)
7
+ initialize_relationships
8
+ initialize_attributes(attributes)
9
+ end
10
+
11
+
12
+ def create
13
+ node = Neo4j::Node.new
14
+ init_on_load(node)
15
+ Neo4j::IdentityMap.add(node, self)
16
+ init_on_create
17
+ write_changed_relationships
18
+ clear_relationships
19
+ true
20
+ end
21
+
22
+ def update
23
+ super
24
+ write_changed_relationships
25
+ clear_relationships
26
+ true
27
+ end
28
+
29
+ # Reload the object from the DB
30
+ def reload(options = nil)
31
+ # Can't reload a none persisted node
32
+ return self if new_record?
33
+ clear_changes
34
+ clear_relationships
35
+ clear_composition_cache
36
+ reset_attributes
37
+ unless reload_from_database
38
+ set_deleted_properties
39
+ freeze
40
+ end
41
+ self
42
+ end
43
+
44
+
45
+ def reload_from_database
46
+ Neo4j::IdentityMap.remove_node_by_id(neo_id)
47
+ if reloaded = self.class.load_entity(neo_id)
48
+ send(:attributes=, reloaded.attributes, false)
49
+ end
50
+ reloaded
51
+ end
52
+
53
+
54
+ end
55
+ end
56
+ end
@@ -1,5 +1,3 @@
1
- require 'active_support/core_ext/class/attribute_accessors'
2
-
3
1
  module Neo4j
4
2
  module Rails
5
3
  # Observer classes respond to life cycle callbacks to implement trigger-like
@@ -2,11 +2,8 @@ module Neo4j
2
2
  module Rails
3
3
  module Persistence
4
4
  extend ActiveSupport::Concern
5
+ extend TxMethods
5
6
 
6
- included do
7
- extend TxMethods
8
- tx_methods :destroy, :create, :update, :update_nested_attributes, :delete, :update_attributes, :update_attributes!
9
- end
10
7
 
11
8
  # Persist the object to the database. Validations and Callbacks are included
12
9
  # by default but validation can be disabled by passing :validate => false
@@ -14,6 +11,7 @@ module Neo4j
14
11
  def save(*)
15
12
  create_or_update
16
13
  end
14
+ tx_methods :save
17
15
 
18
16
  # Persist the object to the database. Validations and Callbacks are included
19
17
  # by default but validation can be disabled by passing :validate => false
@@ -26,18 +24,14 @@ module Neo4j
26
24
  end
27
25
  end
28
26
 
29
- # Updates a single attribute and saves the record.
30
- # This is especially useful for boolean flags on existing records. Also note that
31
- #
32
- # * Validation is skipped.
33
- # * Callbacks are invoked.
34
- # * Updates all the attributes that are dirty in this object.
35
- #
36
- def update_attribute(name, value)
37
- respond_to?("#{name}=") ? send("#{name}=", value) : self[name] = value
38
- save(:validate => false)
27
+ def update
28
+ write_changed_attributes
29
+ clear_changes
30
+ true
39
31
  end
40
32
 
33
+
34
+
41
35
  # Removes the node from Neo4j and freezes the object.
42
36
  def destroy
43
37
  delete
@@ -47,39 +41,14 @@ module Neo4j
47
41
  # Same as #destroy but doesn't run destroy callbacks and doesn't freeze
48
42
  # the object
49
43
  def delete
50
- del unless new_record?
44
+ del unless new_record? || destroyed?
51
45
  set_deleted_properties
52
46
  end
47
+ tx_methods :delete
53
48
 
54
49
  # Returns true if the object was destroyed.
55
- def destroyed?()
56
- @_deleted || Neo4j::Node._load(id).nil?
57
- end
58
-
59
- # Updates this resource with all the attributes from the passed-in Hash and requests that the record be saved.
60
- # If saving fails because the resource is invalid then false will be returned.
61
- def update_attributes(attributes)
62
- self.attributes = attributes
63
- save
64
- end
65
-
66
- # Same as #update_attributes, but raises an exception if saving fails.
67
- def update_attributes!(attributes)
68
- self.attributes = attributes
69
- save!
70
- end
71
-
72
- # Reload the object from the DB.
73
- def reload(options = nil)
74
- clear_changes
75
- clear_relationships
76
- clear_composition_cache
77
- reset_attributes
78
- unless reload_from_database
79
- set_deleted_properties
80
- freeze
81
- end
82
- self
50
+ def destroyed?
51
+ @_deleted || (!new_record? && !self.class.load_entity(neo_id))
83
52
  end
84
53
 
85
54
  # Returns if the record is persisted, i.e. it’s not a new record and it was not destroyed
@@ -89,23 +58,38 @@ module Neo4j
89
58
 
90
59
  # Returns true if the record hasn't been saved to Neo4j yet.
91
60
  def new_record?
92
- _java_node.nil?
61
+ _java_entity.nil?
93
62
  end
94
63
 
95
64
  alias :new? :new_record?
96
65
 
97
66
  # Freeze the properties hash.
98
67
  def freeze
99
- @properties.freeze; self
68
+ @_properties.freeze
69
+ self
100
70
  end
101
71
 
102
72
  # Returns +true+ if the properties hash has been frozen.
103
73
  def frozen?
104
- reload
105
- @properties.frozen?
74
+ reload unless new_record?
75
+ @_properties.frozen?
106
76
  end
107
77
 
78
+
108
79
  module ClassMethods
80
+
81
+ def transaction(&block)
82
+ Neo4j::Rails::Transaction.run do |tx|
83
+ block.call(tx)
84
+ end
85
+ end
86
+
87
+ def new(*args, &block)
88
+ instance = orig_new(*args, &block)
89
+ instance.instance_eval(&block) if block
90
+ instance
91
+ end
92
+
109
93
  # Initialize a model and set a bunch of attributes at the same time. Returns
110
94
  # the object whether saved successfully or not.
111
95
  def create(*args)
@@ -143,114 +127,8 @@ module Neo4j
143
127
  end
144
128
  end
145
129
 
146
- def update
147
- write_changed_attributes
148
- write_changed_relationships
149
- clear_changes
150
- clear_relationships
151
- true
152
- end
153
-
154
- def create
155
- node = Neo4j::Node.new
156
- @_java_node = node
157
- Neo4j::IdentityMap.add(node, self)
158
- init_on_create
159
- clear_changes
160
- clear_relationships
161
- true
162
- end
163
-
164
- def init_on_create(*)
165
- self._classname = self.class.to_s
166
- write_default_attributes
167
- write_changed_attributes
168
- write_changed_relationships
169
- end
170
-
171
- def reset_attributes
172
- @properties = {}
173
- end
174
-
175
- def reload_from_database
176
- Neo4j::IdentityMap.remove_node_by_id(id)
177
- if reloaded = self.class.load(id)
178
- clear_relationships
179
- send(:attributes=, reloaded.attributes, false)
180
- end
181
- reloaded
182
- end
183
-
184
130
  def set_deleted_properties
185
- @_deleted = true
186
- @_persisted = false
187
- @_java_node = nil
188
- end
189
-
190
- # Ensure any defaults are stored in the DB
191
- def write_default_attributes
192
- attribute_defaults.each do |attribute, value|
193
- write_attribute(attribute, Neo4j::TypeConverters.convert(value, attribute, self.class, false)) unless changed_attributes.has_key?(attribute) || _java_node.has_property?(attribute)
194
- end
195
- end
196
-
197
- # Write attributes to the Neo4j DB only if they're altered
198
- def write_changed_attributes
199
- @properties.each do |attribute, value|
200
- write_attribute(attribute, value) if changed_attributes.has_key?(attribute)
201
- end
202
- end
203
-
204
- def _create_entity(rel_type, attr)
205
- clazz = self.class._decl_rels[rel_type.to_sym].target_class
206
- _add_relationship(rel_type, clazz.new(attr))
207
- end
208
-
209
- def _add_relationship(rel_type, node)
210
- if respond_to?("#{rel_type}_rel")
211
- send("#{rel_type}=", node)
212
- elsif respond_to?("#{rel_type}_rels")
213
- has_n = send("#{rel_type}")
214
- has_n << node
215
- else
216
- raise "oops #{rel_type}"
217
- end
218
- end
219
-
220
- def _find_node(rel_type, id)
221
- return nil if id.nil?
222
- if respond_to?("#{rel_type}_rel")
223
- send("#{rel_type}")
224
- elsif respond_to?("#{rel_type}_rels")
225
- has_n = send("#{rel_type}")
226
- has_n.find { |n| n.id == id }
227
- else
228
- raise "oops #{rel_type}"
229
- end
230
- end
231
-
232
- def _has_relationship(rel_type, id)
233
- !_find_node(rel_type,id).nil?
234
- end
235
-
236
- def update_nested_attributes(rel_type, attr, options)
237
- allow_destroy, reject_if = [options[:allow_destroy], options[:reject_if]] if options
238
- begin
239
- # Check if we want to destroy not found nodes (e.g. {..., :_destroy => '1' } ?
240
- destroy = attr.delete(:_destroy)
241
- found = _find_node(rel_type, attr[:id]) || Model.find(attr[:id])
242
- if allow_destroy && destroy && destroy != '0'
243
- found.destroy if found
244
- else
245
- if not found
246
- _create_entity(rel_type, attr) #Create new node from scratch
247
- else
248
- #Create relationship to existing node in case it doesn't exist already
249
- _add_relationship(rel_type, found) if (not _has_relationship(rel_type,attr[:id]))
250
- found.update_attributes(attr)
251
- end
252
- end
253
- end unless reject_if?(reject_if, attr)
131
+ @_deleted = true
254
132
  end
255
133
 
256
134
  public
@@ -2,18 +2,42 @@ module Neo4j
2
2
  module Rails
3
3
  # close lucene connections
4
4
  # reset the Neo4j.threadlocal_ref_node (Multitenancy)
5
- class RackMiddleware #:nodoc:
5
+ # clear the identity map
6
+ class Middleware
7
+ class Body #:nodoc:
8
+ def initialize(target, original)
9
+ @target = target
10
+ @original = original
11
+ end
12
+
13
+ def each(&block)
14
+ @target.each(&block)
15
+ end
16
+
17
+ def close
18
+ @target.close if @target.respond_to?(:close)
19
+ ensure
20
+ IdentityMap.enabled = @original
21
+ IdentityMap.clear
22
+ end
23
+ end
24
+
6
25
  def initialize(app)
7
26
  @app = app
8
27
  end
9
28
 
10
29
  def call(env)
11
- @app.call(env)
30
+ enabled = IdentityMap.enabled
31
+ IdentityMap.enabled = Neo4j::Config[:identity_map]
32
+ status, headers, body = @app.call(env)
33
+ [status, headers, Body.new(body, enabled)]
12
34
  ensure
13
35
  Neo4j::Rails::Model.close_lucene_connections
14
36
  Neo4j.threadlocal_ref_node = Neo4j.default_ref_node
15
37
  end
16
38
  end
39
+
17
40
  end
18
41
 
42
+
19
43
  end