Exemplo n.º 1
0
    async def create(self, block: types.Block) -> types.Block:
        """
        Creates a new object in the datastore and controller.
        """
        await self.wait_for_sync()
        # sid is required for new blocks
        sid = block.pop('id')
        validate_sid(sid)

        try:
            placeholder = object()
            self._store[sid, placeholder] = 'PLACEHOLDER'
        except twinkeydict.TwinKeyError as ex:
            raise exceptions.ExistingId(ex) from ex

        try:
            created = await self._spark.create_object(block)

        finally:
            del self._store[sid, placeholder]

        self._store.rename((created['id'], None), (sid, None))
        created['id'] = sid

        block_cache.set(self.app, created)
        await self.publish(changed=[created])
        return created
Exemplo n.º 2
0
    async def write_block(self, block: Block) -> Block:
        """
        Write to a pre-existing block on the controller.

        Args:
            block (Block):
                A complete block. Either sid or nid may be omitted,
                but all data should be present. No attempt is made
                to merge new and existing data.

        Returns:
            Block:
                The desired block, as present on the controller after writing.
        """
        async with self._execute('Write block'):
            block = self._to_firmware_block(block)
            block = await self._cmder.write_block(block)
            block = self._to_block(block)
            block_cache.set(self.app, block)
            return block
Exemplo n.º 3
0
    async def read_block(self, block: BlockIdentity) -> Block:
        """
        Read block on controller.

        Args:
            block (BlockIdentity):
                An object containing at least a block sid or nid.
                It is valid for `block` to be a complete block,
                but all fields except the id and type will be ignored.

        Returns:
            Block:
                The desired block, as present on the controller.
        """
        async with self._execute('Read block'):
            block = self._to_firmware_block_identity(block)
            block = await self._cmder.read_block(block)
            block = self._to_block(block)
            block_cache.set(self.app, block)
            return block
Exemplo n.º 4
0
    async def create_block(self, block: Block) -> Block:
        """
        Create a new block on the controller.
        sid must be set, but nid will be auto-assigned by the controller
        if not set or 0.
        The block will not be created if either sid or nid is already in use.

        Args:
            block (Block):
                A complete block.

        Returns:
            Block:
                The desired block, as present on the controller after creation.
        """

        async with self._execute('Create block'):
            desired_sid = block.id
            self._validate_sid(desired_sid)
            block = self._to_firmware_block(block, find_nid=False)

            # Avoid race conditions for the desired sid
            # Claim it with a placeholder until the spark create call returns
            placeholder_nid = object()
            self._store[desired_sid, placeholder_nid] = 'PLACEHOLDER'

            try:
                block = await self._cmder.create_block(block)
            finally:
                del self._store[desired_sid, placeholder_nid]

            # The placeholder is always removed - add real entry if create was ok
            self._store[desired_sid, block.nid] = dict()

            block = self._to_block(block)
            block_cache.set(self.app, block)
            return block
Exemplo n.º 5
0
 async def read(self, ids: types.BlockIds) -> types.Block:
     await self.wait_for_sync()
     block = await self._spark.read_object(ids)
     block_cache.set(self.app, block)
     return block
Exemplo n.º 6
0
 async def write(self, block: types.Block) -> types.Block:
     await self.wait_for_sync()
     block = await self._spark.write_object(block)
     block_cache.set(self.app, block)
     await self.publish(changed=[block])
     return block
 async def read(self, ids: dict) -> dict:
     await self.wait_for_sync()
     block = await self._spark.read_object(ids)
     block_cache.set(self.app, block)
     return block