async def real_commit(self): """Commit changes to an object""" await self._manager._storage.precommit(self) for oid, obj in self.added.items(): # Added objects if obj._p_jar is not self and obj._p_jar is not None: raise Exception('Invalid reference to txn') s, l = await self._manager._storage.store(oid, None, IWriter(obj), obj, self) obj._p_serial = s obj._p_oid = oid if obj._p_jar is None: obj._p_jar = self self._to_invalidate.append(oid) for oid, obj in self.modified.items(): # Modified objects if obj._p_jar is not self and obj._p_jar is not None: raise Exception('Invalid reference to txn') # There is no serial serial = getattr(obj, "_p_serial", 0) s, l = await self._manager._storage.store(oid, serial, IWriter(obj), obj, self) obj._p_serial = s if obj._p_jar is None: obj._p_jar = self self._to_invalidate.append(oid) for oid, obj in self.deleted.items(): if obj._p_jar is not self and obj._p_jar is not None: raise Exception('Invalid reference to txn') await self._manager._storage.delete(self, oid) self._to_invalidate.append(oid)
async def initialize(self): """ create root object if necessary """ request = make_mocked_request('POST', '/') request._db_write_enabled = True tm = request._tm = self.get_transaction_manager() txn = await tm.begin() try: await txn._strategy.retrieve_tid() root = await tm._storage.load(txn, ROOT_ID) if root is not None: root = reader(root) root._p_jar = txn if root.__db_id__ is None: root.__db_id__ = self._database_name await tm._storage.store(ROOT_ID, 0, IWriter(root), root, txn) except KeyError: from guillotina.db.db import Root root = Root(self._database_name) await tm._storage.store(ROOT_ID, 0, IWriter(root), root, txn) finally: await tm.commit(txn=txn)
def store(self, ob): writer = IWriter(ob) self._objects[ob._p_oid] = { 'state': writer.serialize(), 'zoid': ob._p_oid, 'tid': 1, 'id': writer.id, 'children': self._objects.get(ob._p_oid, {}).get('children', {}) } if ob.__parent__ and ob.__parent__._p_oid in self._objects: self._objects[ob.__parent__._p_oid]['children'][ob.id] = ob._p_oid
def store(self, oid, old_serial, writer, ob, txn): writer = IWriter(ob) self._objects[ob.__uuid__] = { "state": writer.serialize(), "zoid": ob.__uuid__, "tid": 1, "id": writer.id, "children": self._objects.get(ob.__uuid__, {}).get("children", {}), } if ob.__parent__ and ob.__parent__.__uuid__ in self._objects: self._objects[ob.__parent__.__uuid__]["children"][ob.id] = ob.__uuid__
async def _test_pg_txn(postgres, guillotina_main): """Test a low level transaction""" dsn = "postgres://postgres:@localhost:5432/guillotina" partition_object = "guillotina.db.interfaces.IPartition" aps = APgStorage(dsn=dsn, partition=partition_object, name='db') await aps.initialize() conn = await aps.open() obj = Root() writer = IWriter(obj) tm = TransactionManager(DummyStorage()) txn = Transaction(tm) await aps.tpc_begin(txn, conn) await aps.precommit(txn) await aps.store(ROOT_ID, 0, writer, obj, txn) await aps.tpc_vote(txn) await aps.tpc_finish(txn) await aps.close(conn) tm = TransactionManager(DummyStorage()) txn = Transaction(tm) await aps.tpc_begin(txn, conn) result = await aps.load(txn, ROOT_ID) await aps.abort(txn) await aps.close(txn._db_conn) obj2 = reader(result) assert obj.__name__ == obj2.__name__ await cleanup(aps)
async def _store_object(self, obj, uid, added=False): # There is no serial if added: serial = None else: serial = getattr(obj, "__serial__", None) or 0 writer = IWriter(obj) await self._manager._storage.store(uid, serial, writer, obj, self) obj.__serial__ = self._tid obj.__uuid__ = uid if obj.__txn__ is None: obj.__txn__ = self
async def initialize(self): """ create root object if necessary """ tm = self.get_transaction_manager() txn = await tm.begin() try: await txn._strategy.retrieve_tid() root = await tm._storage.load(txn, ROOT_ID) if root is not None: root = app_settings['object_reader'](root) root.__txn__ = txn if root.__db_id__ is None: root.__db_id__ = self._database_name await tm._storage.store(ROOT_ID, 0, IWriter(root), root, txn) except KeyError: from guillotina.db.db import Root root = Root(self._database_name) await tm._storage.store(ROOT_ID, 0, IWriter(root), root, txn) finally: await tm.commit(txn=txn)
async def _store_object(self, obj, uid, added=False): # Modified objects if obj.__txn__ is not self and obj.__txn__ is not None: raise Exception(f'Invalid reference to txn: {obj}') # There is no serial if added: serial = None else: serial = getattr(obj, "__serial__", None) or 0 writer = IWriter(obj) await self._manager._storage.store(uid, serial, writer, obj, self) obj.__serial__ = self._tid obj.__uuid__ = uid if obj.__txn__ is None: obj.__txn__ = self
async def _process_object(self, obj, data): if data["count"] % 200 == 0: await data["tm"].commit(txn=data["transaction"]) data["transaction"] = await data["tm"].begin() obj.__txn__ = data["transaction"] uuid = obj.__uuid__ writer = IWriter(obj) await self._index(uuid, writer, data["transaction"], data["table_name"]) data["count"] += 1 if IFolder.providedBy(obj): await self._process_folder(obj, data) del obj
async def _store_object(self, obj, oid, added=False): # Modified objects if obj._p_jar is not self and obj._p_jar is not None: raise Exception(f'Invalid reference to txn: {obj}') # There is no serial if added: serial = None else: serial = getattr(obj, "_p_serial", 0) writer = IWriter(obj) await self._manager._storage.store(oid, serial, writer, obj, self) obj._p_serial = self._tid obj._p_oid = oid if obj._p_jar is None: obj._p_jar = self