async def read(self, document: str): data = [] try: self.rev = None self.document = None self._ready_event.clear() if not self.volatile: client = couchdb_client.get_client(self.app) self.rev, data = await client.read(DB_NAME, document, []) self.document = document LOGGER.info( f'{self} Read {len(data)} blocks. Rev = {self.rev}') except asyncio.CancelledError: # pragma: no cover raise except Exception as ex: warnings.warn(f'{self} read error {type(ex).__name__}({ex})') finally: # Clear -> load from database -> merge defaults TwinKeyDict.clear(self) for obj in data: TwinKeyDict.__setitem__(self, obj['keys'], obj['data']) for obj in self._defaults: with suppress(TwinKeyError): if obj['keys'] not in self: self.__setitem__(obj['keys'], obj['data']) self._ready_event.set()
async def read(self, device_id: str): key = BLOCK_STORE_KEY.format(id=device_id) data = [] try: self.key = None self._ready_event.clear() if not self.volatile: resp = await http.session(self.app).post(f'{STORE_URL}/get', json={ 'id': key, 'namespace': NAMESPACE, }) self.key = key data = (await resp.json())['value'].get('data', []) LOGGER.info(f'{self} Read {len(data)} blocks') except asyncio.CancelledError: # pragma: no cover raise except Exception as ex: warnings.warn(f'{self} read error {strex(ex)}') finally: # Clear -> load from database -> merge defaults TwinKeyDict.clear(self) for obj in data: TwinKeyDict.__setitem__(self, obj['keys'], obj['data']) for obj in self._defaults: with suppress(TwinKeyError): if obj['keys'] not in self: self.__setitem__(obj['keys'], obj['data']) self._ready_event.set()
def clear(self): TwinKeyDict.clear(self) for obj in self._defaults: self.__setitem__(obj['keys'], obj['data'])
def set_all(cache: TwinKeyDict, blocks: List[dict]): cache.clear() for block in blocks: cache[block['id'], block['nid']] = block
def delete_all(cache: TwinKeyDict): cache.clear()