perobs 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50292da8f34e68427887d224c56dc2070242fc27
4
- data.tar.gz: f3f41eca90b32ebefeeae05eaf655e99130a3af8
3
+ metadata.gz: 231bcc1e34113cad62eefa5bdac4b84d18937638
4
+ data.tar.gz: dfe932b0e210297a4a24a5a2c076d52db04c5ff6
5
5
  SHA512:
6
- metadata.gz: 2ed683b2e8ae0cd9a6bc4531a8f70f6d2f0d98dc5f3f4947ea64f24d715e3c5d050317d6c29ab481820f25cb302e4d8c1e67ca5cf8048c0e1354d732489aef94
7
- data.tar.gz: 2dce8dff389bc974126a88e9fdd5d042f5071e13c6560a2eeab63b05ae536ac209dfa4cddbcccac0e4a2332d46c82ea27894cc0e3a7de1c32b016f515920fbd6
6
+ metadata.gz: 433b39b2568538a634e53adc0bf6ae2ac8f4f8d6a24178b406d5b6de92981d141f6a29ce6a344388eedc5b1c0632e02d4bef6c2996c18650b7805501de29718a
7
+ data.tar.gz: 78904802a71bb2b958f79eebb70c945d5506267bf83c1e921de8ec4478a4e88e5fcfe9bf5409beecda8dd7fb4ea6358a366e3640dbd0965365a387f9b324d4e0
@@ -83,20 +83,6 @@ module PEROBS
83
83
  end
84
84
  end
85
85
 
86
- # Generate a new unique ID. It uses random numbers between 0 and 2**64 -
87
- # 1.
88
- # @return [Fixnum or Bignum]
89
- def new_id
90
- begin
91
- # Generate a random number. It's recommended to not store more than
92
- # 2**62 objects in the same store.
93
- id = rand(2**64)
94
- # Ensure that we don't have already another object with this ID.
95
- end while include?(id)
96
-
97
- id
98
- end
99
-
100
86
  # Check a config option and adjust it if needed.
101
87
  # @param name [String] Name of the config option.
102
88
  def check_option(name)
data/lib/perobs/Object.rb CHANGED
@@ -182,18 +182,17 @@ module PEROBS
182
182
  end
183
183
 
184
184
  def _set(attr, val)
185
- if val.is_a?(ObjectBase)
185
+ if val.respond_to?(:is_poxreference?)
186
186
  # References to other PEROBS::Objects must be handled somewhat
187
187
  # special.
188
188
  if @store != val.store
189
189
  raise ArgumentError, 'The referenced object is not part of this store'
190
190
  end
191
- unless val.respond_to?(:is_poxreference?)
192
- raise ArgumentError, 'A PEROBS::ObjectBase object escaped! ' +
193
- 'Have you used self() instead of myself() to' +
194
- 'get the reference of the PEROBS object that ' +
195
- 'you are trying to assign here?'
196
- end
191
+ elsif val.is_a?(ObjectBase)
192
+ raise ArgumentError, 'A PEROBS::ObjectBase object escaped! ' +
193
+ 'Have you used self() instead of myself() to' +
194
+ 'get the reference of the PEROBS object that ' +
195
+ 'you are trying to assign here?'
197
196
  end
198
197
  instance_variable_set(('@' + attr.to_s).to_sym, val)
199
198
  # Let the store know that we have a modified object.
@@ -127,7 +127,8 @@ module PEROBS
127
127
  "All PEROBS objects must exclusively be created by calling " +
128
128
  "Store.new(). Never call the object constructor directly."
129
129
  end
130
- @_id = @store.db.new_id
130
+ @_id = @store._new_id
131
+ @store._register_in_memory(self, @_id)
131
132
  ObjectSpace.define_finalizer(self, ObjectBase._finalize(@store, @_id))
132
133
  @_stash_map = nil
133
134
  # Allocate a proxy object for this object. User code should only operate
@@ -146,8 +147,6 @@ module PEROBS
146
147
  proc { store._collect(id) }
147
148
  end
148
149
 
149
- public
150
-
151
150
  # This method can be overloaded by derived classes to do some massaging on
152
151
  # the data after it has been restored from the database. This could either
153
152
  # be some sanity check or code to migrate the object from one version to
@@ -226,7 +225,7 @@ module PEROBS
226
225
  }
227
226
  @_stash_map = [] unless @_stash_map
228
227
  # Get a new ID to store this version of the object.
229
- @_stash_map[level] = stash_id = @store.db.new_id
228
+ @_stash_map[level] = stash_id = @store._new_id
230
229
  @store.db.put_object(db_obj, stash_id)
231
230
  end
232
231
 
@@ -236,6 +235,8 @@ module PEROBS
236
235
  # Unregister the object with the old ID from the write cache to prevent
237
236
  # cache corruption. The objects are index by ID in the cache.
238
237
  @store.cache.unwrite(self)
238
+ @store._collect(@_id)
239
+ @store._register_in_memory(self, id)
239
240
  @_id = id
240
241
  end
241
242
 
data/lib/perobs/Store.rb CHANGED
@@ -173,11 +173,6 @@ module PEROBS
173
173
  @object_creation_in_progress = false
174
174
  # If a specific object ID was requested we need to set it now.
175
175
  obj._change_id(id) if id
176
- # Add the new object to the in-memory list. We only store a weak
177
- # reference to the object so it can be garbage collected. When this
178
- # happens the object finalizer is triggered and calls _forget() to
179
- # remove the object from this hash again.
180
- @in_memory_objects[obj._id] = WeakRef.new(obj)
181
176
  obj
182
177
  end
183
178
 
@@ -349,6 +344,31 @@ module PEROBS
349
344
  def rename_classes(rename_map)
350
345
  @class_map.rename(rename_map)
351
346
  end
347
+ # Internal method. Don't use this outside of this library!
348
+ # Generate a new unique ID that is not used by any other object. It uses
349
+ # random numbers between 0 and 2**64 - 1.
350
+ # @return [Fixnum or Bignum]
351
+ def _new_id
352
+ begin
353
+ # Generate a random number. It's recommended to not store more than
354
+ # 2**62 objects in the same store.
355
+ id = rand(2**64)
356
+ # Ensure that we don't have already another object with this ID.
357
+ end while @in_memory_objects.include?(id) || @db.include?(id)
358
+
359
+ id
360
+ end
361
+
362
+ # Internal method. Don't use this outside of this library!
363
+ # Add the new object to the in-memory list. We only store a weak
364
+ # reference to the object so it can be garbage collected. When this
365
+ # happens the object finalizer is triggered and calls _forget() to
366
+ # remove the object from this hash again.
367
+ # @param obj [BasicObject] Object to register
368
+ # @param id [Fixnum or Bignum] object ID
369
+ def _register_in_memory(obj, id)
370
+ @in_memory_objects[id] = WeakRef.new(obj)
371
+ end
352
372
 
353
373
  # Remove the object from the in-memory list. This is an internal method
354
374
  # and should never be called from user code.
@@ -1,4 +1,4 @@
1
1
  module PEROBS
2
2
  # The version number
3
- VERSION = "2.1.0"
3
+ VERSION = "2.1.1"
4
4
  end
data/test/Store_spec.rb CHANGED
@@ -55,6 +55,27 @@ class PersonN < PEROBS::Object
55
55
 
56
56
  end
57
57
 
58
+ class O0 < PEROBS::Object
59
+
60
+ po_attr :r
61
+
62
+ def initialize(store)
63
+ super
64
+ r = @store.new(O1, myself)
65
+ end
66
+
67
+ end
68
+ class O1 < PEROBS::Object
69
+
70
+ po_attr :p
71
+
72
+ def initialize(store, p = nil)
73
+ super(store)
74
+ parent = p
75
+ end
76
+
77
+ end
78
+
58
79
  describe PEROBS::Store do
59
80
 
60
81
  before(:all) do
@@ -398,6 +419,7 @@ describe PEROBS::Store do
398
419
 
399
420
  it 'should track in-memory objects properly' do
400
421
  @store = PEROBS::Store.new(@db_file)
422
+ expect(@store.statistics[:in_memory_objects]).to eq(1)
401
423
  @store['person'] = @store.new(Person)
402
424
  # We have the root hash and the Person object.
403
425
  expect(@store.statistics[:in_memory_objects]).to eq(2)
@@ -407,6 +429,15 @@ describe PEROBS::Store do
407
429
  expect(@store.statistics[:in_memory_objects]).to eq(1)
408
430
  end
409
431
 
432
+ it 'should handle nested constructors' do
433
+ @store = PEROBS::Store.new(@db_file)
434
+ @store['r'] = @store.new(O0)
435
+ @store.sync
436
+ expect(@store.check).to eq(0)
437
+ @store = PEROBS::Store.new(@db_file)
438
+ expect(@store.check).to eq(0)
439
+ end
440
+
410
441
  it 'should survive a real world usage test' do
411
442
  options = { :engine => PEROBS::BTreeDB, :dir_bits => 4 }
412
443
  @store = PEROBS::Store.new(@db_file, options)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perobs
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Schlaeger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-09 00:00:00.000000000 Z
11
+ date: 2016-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler