Пример #1
0
    async def get(self, oid, ignore_registered=False):
        """Getting a oid from the db"""

        if not ignore_registered:
            obj = self.modified.get(oid, None)
            if obj is not None:
                return obj

        result = HARD_CACHE.get(oid, None)
        if result is None:
            result = await self._cache.get(oid=oid)

        if result is not None:
            obj = reader(result)
            obj._p_jar = self
            return obj

        result = await self._manager._storage.load(self, oid)
        obj = reader(result)
        obj._p_jar = self

        if obj.__immutable_cache__:
            # ttl of zero means we want to provide a hard cache here
            HARD_CACHE[oid] = result
        else:
            if self._cache.max_cache_record_size > len(result['state']):
                await self._cache.set(result, oid=oid)

        return obj
Пример #2
0
 async def items(self, container):
     async for record in self._manager._storage.items(
             self, container._p_oid):
         obj = reader(record)
         obj.__parent__ = container
         obj._p_jar = self
         yield obj.id, obj
Пример #3
0
async def get_object_by_uid(uid: str, txn=None) -> IBaseObject:
    '''
    Get an object from an uid

    :param uid: Object id of object you need to retreive
    :param txn: Database transaction object. Will get current
                transaction is not provided
    '''
    if txn is None:
        from guillotina.transactions import get_transaction
        txn = get_transaction()
    result = txn._manager._hard_cache.get(uid, None)
    if result is None:
        result = await txn._get(uid)

    if result['parent_id'] == TRASHED_ID:
        raise KeyError(uid)

    obj = reader(result)
    obj.__txn__ = txn
    if result['parent_id']:
        parent = await get_object_by_uid(result['parent_id'], txn)
        if parent is not None:
            obj.__parent__ = parent
        else:
            raise KeyError(result['parent_id'])
    return obj
Пример #4
0
async def get_object_by_oid(oid: str, txn=None) -> typing.Optional[IResource]:
    '''
    Get an object from an oid

    :param oid: Object id of object you need to retreive
    :param txn: Database transaction object. Will get current
                transaction is not provided
    '''
    if txn is None:
        from guillotina.transactions import get_transaction
        txn = get_transaction()
    result = txn._manager._hard_cache.get(oid, None)
    if result is None:
        try:
            result = await txn._get(oid)
        except KeyError:
            return None

    if result['parent_id'] == TRASHED_ID:
        return None

    obj = reader(result)
    obj._p_jar = txn
    if result['parent_id']:
        obj.__parent__ = await get_object_by_oid(result['parent_id'], txn)
    return obj
Пример #5
0
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)
Пример #6
0
 async def get_child(self, container, key):
     result = await self._manager._storage.get_child(
         self, container._p_oid, key)
     obj = reader(result)
     obj.__parent__ = container
     obj._p_jar = self
     return obj
Пример #7
0
    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)
Пример #8
0
 async def get_annotation(self, base_obj, id):
     result = await self._get_annotation(base_obj, id)
     if result == _EMPTY:
         raise KeyError(id)
     obj = reader(result)
     obj.__of__ = base_obj._p_oid
     obj._p_jar = self
     return obj
Пример #9
0
 async def get_annotation(self, base_obj, id):
     result = await self._manager._storage.get_annotation(
         self, base_obj._p_oid, id)
     if result is None:
         raise KeyError(id)
     obj = reader(result)
     obj.__of__ = base_obj._p_oid
     obj._p_jar = self
     return obj
Пример #10
0
    async def get_child(self, container, key):
        result = await self._cache.get(container=container, id=key)
        if result is None:
            result = await self._manager._storage.get_child(
                self, container._p_oid, key)
            if result is None:
                return None
            await self._cache.set(result, container=container, id=key)

        obj = reader(result)
        obj.__parent__ = container
        obj._p_jar = self
        return obj
Пример #11
0
    async def get_child(self, container, key):
        result = await self._cache.get(container=container, id=key)
        if result is None:
            result = await self._manager._storage.get_child(
                self, container._p_oid, key)
            if result is None:
                return None
            if self._cache.max_cache_record_size > len(result['state']):
                await self._cache.set(result, container=container, id=key)

        obj = reader(result)
        obj.__parent__ = container
        obj._p_jar = self
        return obj
Пример #12
0
async def run2():
    print("Test deserialize content from db")
    ob = await create_content("TestContent6", id="foobar")
    ob.foobar1 = "1"
    ob.foobar2 = "2"
    ob.foobar6 = "6"
    start = time.time()
    writer = get_adapter(ob, IWriter)
    serialized = writer.serialize()
    for _ in range(ITERATIONS):
        ob = reader({"state": serialized, "zoid": 0, "tid": 0, "id": "foobar"})
    end = time.time()
    assert ob.foobar1 == "1"
    assert ob.foobar6 == "6"
    print(f"Done with {ITERATIONS} in {end - start} seconds")
Пример #13
0
    async def get(self, oid):
        """Getting a oid from the db"""
        obj = self.modified.get(oid, None)
        if obj is not None:
            return obj

        obj = self._cache.get(oid, None)
        if obj is not None:
            return obj

        result = HARD_CACHE.get(oid, None)
        if result is not None:
            obj = reader(result)
            obj._p_jar = self
            return obj

        result = await self._manager._storage.load(self, oid)
        obj = reader(result)
        obj._p_jar = self

        if obj.__cache__ == 0:
            HARD_CACHE[oid] = result

        return obj
Пример #14
0
async def run2():
    print('Test deserialize content from db')
    ob = await create_content('TestContent6', id='foobar')
    ob.foobar1 = '1'
    ob.foobar2 = '2'
    ob.foobar6 = '6'
    start = time.time()
    writer = get_adapter(ob, IWriter)
    serialized = writer.serialize()
    for _ in range(ITERATIONS):
        ob = reader({'state': serialized, 'zoid': 0, 'tid': 0, 'id': 'foobar'})
    end = time.time()
    assert ob.foobar1 == '1'
    assert ob.foobar6 == '6'
    print(f'Done with {ITERATIONS} in {end - start} seconds')
Пример #15
0
async def get_object_by_oid(oid, txn=None):
    '''
    Need to do a reverse lookup of the object to all the parents
    '''
    if txn is None:
        from guillotina.transactions import get_transaction
        txn = get_transaction()
    result = txn._manager._hard_cache.get(oid, None)
    if result is None:
        result = await txn._get(oid)

    obj = reader(result)
    obj._p_jar = txn
    if result['parent_id']:
        obj.__parent__ = await get_object_by_oid(result['parent_id'], txn)
    return obj
Пример #16
0
    async def get(self, oid, ignore_registered=False):
        """Getting a oid from the db"""
        if not ignore_registered:
            obj = self.modified.get(oid, None)
            if obj is not None:
                return obj

        result = HARD_CACHE.get(oid, None)
        if result is None:
            result = await self._get(oid)

        obj = reader(result)
        obj._p_jar = self
        if obj.__immutable_cache__:
            HARD_CACHE[oid] = result

        return obj
Пример #17
0
 async def get_annotation(self, base_obj, id):
     result = await self._cache.get(container=base_obj,
                                    id=id,
                                    variant='annotation')
     if result is None:
         result = await self._manager._storage.get_annotation(
             self, base_obj._p_oid, id)
         if result is None:
             raise KeyError(id)
         await self._cache.set(result,
                               container=base_obj,
                               id=id,
                               variant='annotation')
     obj = reader(result)
     obj.__of__ = base_obj._p_oid
     obj._p_jar = self
     return obj
Пример #18
0
    async def get(self, oid, ignore_registered=False):
        """Getting a oid from the db"""
        if not ignore_registered:
            obj = self.modified.get(oid, None)
            if obj is not None:
                return obj

        result = self._manager._hard_cache.get(oid, None)
        if result is None:
            result = await self._get(oid)

        obj = reader(result)
        obj._p_jar = self
        if obj.__immutable_cache__:
            # ttl of zero means we want to provide a hard cache here
            self._manager._hard_cache[oid] = result

        return obj
Пример #19
0
    async def get_object(self, oid):
        if oid in self.cache:
            return self.cache[oid]

        try:
            result = self.txn._manager._hard_cache.get(oid, None)
        except AttributeError:
            from guillotina.db.transaction import HARD_CACHE  # noqa
            result = HARD_CACHE.get(oid, None)
        if result is None:
            result = await self.txn._cache.get(oid=oid)

        if result is None:
            result = await self.tm._storage.load(self.txn, oid)

        obj = reader(result)
        obj._p_jar = self.txn
        if result['parent_id']:
            obj.__parent__ = await self.get_object(result['parent_id'])
        return obj
Пример #20
0
async def get_object_by_oid(oid: str, txn=None) -> IResource:
    '''
    Get an object from an oid

    :param oid: Object id of object you need to retreive
    :param txn: Database transaction object. Will get current
                transaction is not provided
    '''
    if txn is None:
        from guillotina.transactions import get_transaction
        txn = get_transaction()
    result = txn._manager._hard_cache.get(oid, None)
    if result is None:
        result = await txn._get(oid)

    obj = reader(result)
    obj._p_jar = txn
    if result['parent_id']:
        obj.__parent__ = await get_object_by_oid(result['parent_id'], txn)
    return obj
Пример #21
0
 async def get_annotation(self, base_obj, id):
     result = await self._cache.get(container=base_obj,
                                    id=id,
                                    variant='annotation')
     if result == _EMPTY:
         raise KeyError(id)
     if result is None:
         result = await self._manager._storage.get_annotation(
             self, base_obj._p_oid, id)
         if result is None:
             await self._cache.set(_EMPTY,
                                   container=base_obj,
                                   id=id,
                                   variant='annotation')
             raise KeyError(id)
         if self._cache.max_cache_record_size > len(result['state']):
             await self._cache.set(result,
                                   container=base_obj,
                                   id=id,
                                   variant='annotation')
     obj = reader(result)
     obj.__of__ = base_obj._p_oid
     obj._p_jar = self
     return obj
Пример #22
0
    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)
Пример #23
0
async def load_res(result, txn):
    obj = reader(result)
    obj._p_jar = txn
    if result["parent_id"]:
        obj.__parent__ = await get_object_by_oid(result["parent_id"], txn)
    return obj
Пример #24
0
 def _fill_object(self, item, parent):
     obj = reader(item)
     obj.__parent__ = parent
     obj._p_jar = self
     return obj