[Checkins] SVN: mongopersist/trunk/ - Feature: ``ConflictError`` has now a much more meaningful API. Instead of

Stephen Richter cvs-admin at zope.org
Thu Mar 29 17:25:36 UTC 2012


Log message for revision 124796:
  - Feature: ``ConflictError`` has now a much more meaningful API. Instead of
    just referencing the object and different serials, it now actual has the
    original, current and new state documents.
  
  

Changed:
  U   mongopersist/trunk/CHANGES.txt
  U   mongopersist/trunk/src/mongopersist/README.txt
  U   mongopersist/trunk/src/mongopersist/datamanager.py
  U   mongopersist/trunk/src/mongopersist/interfaces.py
  U   mongopersist/trunk/src/mongopersist/tests/test_datamanager.py

-=-
Modified: mongopersist/trunk/CHANGES.txt
===================================================================
--- mongopersist/trunk/CHANGES.txt	2012-03-29 17:00:02 UTC (rev 124795)
+++ mongopersist/trunk/CHANGES.txt	2012-03-29 17:25:32 UTC (rev 124796)
@@ -5,6 +5,10 @@
 0.7.0 (2012-03-??)
 ------------------
 
+- Feature: ``ConflictError`` has now a much more meaningful API. Instead of
+  just referencing the object and different serials, it now actual has the
+  original, current and new state documents.
+
 - Feature: Conflicts are now detected while aborting a transaction. The
   implemented policy will not reset the document state, if a conflict is
   detected.

Modified: mongopersist/trunk/src/mongopersist/README.txt
===================================================================
--- mongopersist/trunk/src/mongopersist/README.txt	2012-03-29 17:00:02 UTC (rev 124795)
+++ mongopersist/trunk/src/mongopersist/README.txt	2012-03-29 17:25:32 UTC (rev 124796)
@@ -592,6 +592,10 @@
   Traceback (most recent call last):
   ...
   ConflictError: database conflict error
-      (oid ..., class Person, start serial 2, current serial 3)
+      (oid DBRef(u'__main__.Person',
+                 ObjectId('4e7ddf12e138237403000000'),
+                 u'mongopersist_test'),
+       class Person,
+       orig serial 2, cur serial 3, new serial 3)
 
   >>> transaction.abort()

Modified: mongopersist/trunk/src/mongopersist/datamanager.py
===================================================================
--- mongopersist/trunk/src/mongopersist/datamanager.py	2012-03-29 17:00:02 UTC (rev 124795)
+++ mongopersist/trunk/src/mongopersist/datamanager.py	2012-03-29 17:25:32 UTC (rev 124796)
@@ -31,10 +31,8 @@
 
 LOG = logging.getLogger(__name__)
 
-def create_conflict_error(obj, new_doc):
-    return interfaces.ConflictError(
-        None, obj,
-        (new_doc.get('_py_serial', 0), serialize.u64(obj._p_serial)))
+def create_conflict_error(obj, orig_doc, cur_doc, new_doc):
+    return interfaces.ConflictError(None, obj, orig_doc, cur_doc, new_doc)
 
 def process_spec(collection, spec):
     try:
@@ -224,7 +222,10 @@
             return None if can_raise else False
         if new_doc.get('_py_serial', 0) != serialize.u64(obj._p_serial):
             if can_raise:
-                raise self.conflict_error_factory(obj, new_doc)
+                orig_doc = self._original_states.get(obj._p_oid)
+                cur_doc = coll.find_one(obj._p_oid.id)
+                raise self.conflict_error_factory(
+                    obj, orig_doc, cur_doc, new_doc)
             else:
                 return True
         return None if can_raise else False

Modified: mongopersist/trunk/src/mongopersist/interfaces.py
===================================================================
--- mongopersist/trunk/src/mongopersist/interfaces.py	2012-03-29 17:00:02 UTC (rev 124795)
+++ mongopersist/trunk/src/mongopersist/interfaces.py	2012-03-29 17:25:32 UTC (rev 124796)
@@ -27,25 +27,33 @@
 
 class ConflictError(transaction.interfaces.TransientError):
 
-    def __init__(self, message=None, object=None, serials=None):
+    def __init__(self, message=None, object=None,
+                 orig_state=None, cur_state=None, new_state=None):
         self.message = message or "database conflict error"
         self.object = object
-        self.serials = serials
+        self.orig_state = orig_state
+        self.cur_state = cur_state
+        self.new_state = new_state
 
     @property
-    def new_serial(self):
-        return self.serials[0]
+    def orig_serial(self):
+        return self.orig_state.get('_py_serial') if self.orig_state else None
 
     @property
-    def old_serial(self):
-        return self.serials[1]
+    def cur_serial(self):
+        return self.cur_state.get('_py_serial') if self.cur_state else None
 
+    @property
+    def new_serial(self):
+        return self.new_state.get('_py_serial') if self.new_state else None
+
     def __str__(self):
         extras = [
             'oid %s' %self.object._p_oid,
             'class %s' %self.object.__class__.__name__,
-            'start serial %s' %self.old_serial,
-            'current serial %s' %self.new_serial]
+            'orig serial %s' %self.orig_serial,
+            'cur serial %s' %self.cur_serial,
+            'new serial %s' %self.new_serial]
         return "%s (%s)" % (self.message, ", ".join(extras))
 
     def __repr__(self):

Modified: mongopersist/trunk/src/mongopersist/tests/test_datamanager.py
===================================================================
--- mongopersist/trunk/src/mongopersist/tests/test_datamanager.py	2012-03-29 17:00:02 UTC (rev 124795)
+++ mongopersist/trunk/src/mongopersist/tests/test_datamanager.py	2012-03-29 17:25:32 UTC (rev 124796)
@@ -32,11 +32,11 @@
     Simple helper function to create a conflict error.
 
      >>> foo = Foo()
-     >>> foo._p_serial = '\x00\x00\x00\x00\x00\x00\x00\x01'
 
-     >>> datamanager.create_conflict_error(foo, {'_py_serial': 3})
+     >>> datamanager.create_conflict_error(
+     ...     foo, {'_py_serial': 1}, {'_py_serial': 2}, {'_py_serial': 3})
      ConflictError: database conflict error
-                    (oid None, class Foo, start serial 1, current serial 3)
+         (oid None, class Foo, orig serial 1, cur serial 2, new serial 3)
     """
 
 def doctest_Root():
@@ -282,9 +282,10 @@
         ...
         ConflictError: database conflict error
             (oid DBRef('mongopersist.tests.test_datamanager.Foo',
-                       ObjectId('4f5bfcaf37a08e2849000000'),
+                       ObjectId('4e7ddf12e138237403000000'),
                        'mongopersist_test'),
-             class Foo, start serial 1, current serial 2)
+             class Foo,
+             orig serial 1, cur serial 2, new serial 2)
     """
 
 def doctest_MongoDataManager_insert():



More information about the checkins mailing list