]> git.jsancho.org Git - mojodb.git/blobdiff - collection.py
Savepoints to protect integrity of documents
[mojodb.git] / collection.py
index 3c93b8b3192f909f1d31b01100fa5072e8fb7e01..804135379d089cf4ebc5f0e6f0f8b3a1a8981f42 100644 (file)
@@ -19,9 +19,8 @@
 #
 ##############################################################################
 
-import msgpack
 from cursor import Cursor
-import uuid
+from objectid import ObjectId
 
 class Collection(object):
     def __init__(self, database, table_name):
@@ -64,7 +63,7 @@ class Collection(object):
         else:
             docs = doc_or_docs
         for doc in docs:
-            doc_id = uuid.uuid4().hex
+            doc_id = str(ObjectId())
             if not '_id' in doc:
                 doc['_id'] = doc_id
             self._insert_document(doc_id, doc)
@@ -76,17 +75,23 @@ class Collection(object):
 
     def _insert_document(self, doc_id, doc):
         fields = self._get_fields()
-        for f in doc:
-            if not f in fields:
-                self._create_field(f)
-            table_f = '%s$%s' % (self.table_name, f)
-            self._insert_field(doc_id, table_f, f, doc[f])
+        self.database.connection.savepoint("insert_document")
+        try:
+            for f in doc:
+                if not f in fields:
+                    self._create_field(f)
+                table_f = '%s$%s' % (self.table_name, f)
+                self._insert_field(doc_id, table_f, f, doc[f])
+            self.database.connection.commit_savepoint("insert_document")
+        except:
+            self.database.connection.rollback_savepoint("insert_document")
+            raise
 
     def _insert_field(self, doc_id, field_table, field_name, field_value):
         values = {
             'id': doc_id,
             'name': field_name,
-            'value': msgpack.dumps(field_value),
+            'value': self.database.connection.serializer.dumps(field_value),
             }
         if type(field_value) in (int, float):
             values['number'] = field_value