Skip to content
This repository has been archived by the owner on May 13, 2020. It is now read-only.

Commit

Permalink
- Bug: don't err on missing _py_serial on older states from mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Groszer committed Apr 6, 2012
1 parent 5f27dc7 commit a0dccbf
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ CHANGES
0.7.1 (2012-04-??)
------------------

- Bug: don't err on missing _py_serial on older states from mongo

- Performance: Switched to ``repoze.lru`` (from ``lru``), which is much
faster.

- Performance: To avoid excessive hash computations, we now use the hash of
the ``DBRef`` references as cache keys.

- Bug: ``ObjectId`` ids are not guarantted to be unique accross
- Bug: ``ObjectId`` ids are not guaranteed to be unique across
collections. Thus they are a bad key for global caches. So we use full
``DBRef`` references instead.

Expand Down
10 changes: 8 additions & 2 deletions src/mongopersist/conflict.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ def is_same(self, obj, orig_state, new_state):
# This should never happen in a real running system.
return False
orig_state = orig_state.copy()
orig_state.pop(self.field_name)
try:
orig_state.pop(self.field_name)
except KeyError:
pass
new_state = new_state.copy()
new_state.pop(self.field_name)
try:
new_state.pop(self.field_name)
except KeyError:
pass
return orig_state == new_state

def resolve(self, obj, orig_doc, cur_doc, new_doc):
Expand Down
46 changes: 46 additions & 0 deletions src/mongopersist/tests/test_datamanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,52 @@ def doctest_MongoDataManager_dump_only_on_real_change():
{u'_id': ObjectId('...'), u'_py_serial': 3, u'name': u'fuh'}
"""

def doctest_MongoDataManager_dump_only_on_real_change_no_py_serial():
r"""MongoDataManager: dump(): dump on real change only.
Quirk: some objects might not have _py_serial in their state
The data manager only writes data when we actually have a difference in
state.
We have to use a serial conflict handler, otherwise it is hard to check
whether data was written.
>>> dm.conflict_handler = conflict.SimpleSerialConflictHandler(dm)
Let's now add an object:
>>> foo = Foo('foo')
>>> foo_ref = dm.insert(foo)
>>> dm.tpc_finish(None)
>>> coll = dm._get_collection_from_object(foo)
>>> state = coll.find_one({})
>>> state
{u'_id': ObjectId('...'), u'_py_serial': 1, u'name': u'foo'}
>>> del state['_py_serial']
>>> coll.save(state)
ObjectId('...')
>>> coll.find_one({})
{u'_id': ObjectId('...'), u'name': u'foo'}
So the original state is in. Let's now modify an object:
>>> foo = dm.load(foo_ref)
>>> foo.name = 'Foo'
>>> foo._p_changed
True
>>> dm.tpc_finish(None)
_py_serial gets added silently, without an exception
>>> coll.find_one({})
{u'_id': ObjectId('...'), u'_py_serial': 1, u'name': u'Foo'}
"""

def doctest_MongoDataManager_flush():
r"""MongoDataManager: flush()
Expand Down

0 comments on commit a0dccbf

Please sign in to comment.