def keys( self, path: Union[AimObjectKey, AimObjectPath] = (), level: int = None ) -> Iterator[Union[AimObjectPath, AimObjectKey]]: encoded_path = E.encode_path(path) walker = self.container.walk(encoded_path) path = None while True: try: if path is None: path = next(walker) else: path = walker.send(path) except StopIteration: return path = E.decode_path(path) path = path[:(1 if level is None else level)] if level is None: yield path[0] else: yield path p = E.encode_path(path) assert p.endswith(b'\xfe') path = p[:-1] + b'\xff'
def first( self, path: Union[AimObjectKey, AimObjectPath] = () ) -> Tuple[AimObjectKey, AimObject]: if isinstance(path, (int, str)): path = (path,) prefix = E.encode_path(path) p = E.decode_path(self.container.view(prefix).next_key()) return p[0]
def __delitem__( self, path: Union[AimObjectKey, AimObjectPath] ): if path == Ellipsis: path = () if not isinstance(path, (tuple, list)): path = (path,) encoded_path = E.encode_path(path) return self.container.delete_range(encoded_path, encoded_path + b'\xff')
def iterlevel( self, path: Union[AimObjectKey, AimObjectPath] = (), level: int = 1 ) -> Iterator[Tuple[ AimObjectPath, AimObject ]]: prefix = E.encode_path(path) it = self.container.items(prefix) for path, value in treeutils.iter_decode_tree(it, level=level): yield path, value
def items( self, path: Union[AimObjectKey, AimObjectPath] = () ) -> Iterator[Tuple[ AimObjectKey, AimObject ]]: prefix = E.encode_path(path) it = self.container.view(prefix).items() for path, value in treeutils.iter_decode_tree(it, level=1): key, = path yield key, value
def last( self, path: Union[AimObjectKey, AimObjectPath] = () ) -> Tuple[AimObjectKey, AimObject]: if isinstance(path, (int, str)): path = (path,) prefix = E.encode_path(path) # assert prefix.endswith(b'\xfe') # prefix = prefix[:-1] + b'\xff' p = E.decode_path(self.container.view(prefix).prev_key()) if not p: raise KeyError return p[0]
def dbs(self): index_prefix = encode_path((self.db_name, "chunks")) index_path = os.path.join(self.db_path, self.db_name, "index") try: index_db = self._get_db(index_prefix, index_path, self._dbs) except Exception: index_db = None logger.warning('No index was detected') # If index exists -- only load those in progress selector = 'progress' if index_db is not None else 'chunks' new_dbs: Dict[bytes, aimrocks.DB] = {} db_dir = os.path.join(self.db_path, self.db_name, selector) for prefix in self._list_dir(db_dir): path = os.path.join(self.db_path, self.db_name, "chunks", prefix) prefix = encode_path((self.db_name, "chunks", prefix)) self._get_db(prefix, path, self._dbs, new_dbs) if index_db is not None: new_dbs[b""] = index_db self._dbs = new_dbs return new_dbs
def collect( self, path: Union[AimObjectKey, AimObjectPath] = (), strict: bool = True ) -> AimObject: if path == Ellipsis: path = () if isinstance(path, (int, str)): path = (path,) prefix = E.encode_path(path) it = self.container.view(prefix).items() try: return treeutils.decode_tree(it, strict=strict) except KeyError: raise KeyError('No key {} is present.'.format(path))
def view( self, path: Union[AimObjectKey, AimObjectPath], resolve: bool = False ): prefix = E.encode_path(path) container_view = self.container.view(prefix) tree_view = ContainerTreeView(container_view) # Okay, but let's decide if we want to initialize a CustomObject view if not resolve: return tree_view flag = decode(container_view.get(b'', default=b'\0')) if isinstance(flag, CustomObjectFlagType): return CustomObject._aim_decode(flag.aim_name, tree_view) return tree_view
def set( self, path: Union[AimObjectKey, AimObjectPath], value: AimObject, strict: bool = True ): if path == Ellipsis: path = () if not isinstance(path, (tuple, list)): path = (path,) batch = self.container.batch() encoded_path = E.encode_path(path) self.container.delete_range(encoded_path, encoded_path + b'\xff', store_batch=batch) for key, val in treeutils.encode_tree(value, strict=strict): self.container.set(encoded_path + key, val, store_batch=batch) self.container.commit(batch)
def generate_resource_path(prefix_view: 'PrefixView', additional_path: 'AimObjectPath') -> str: prefix_path = decode_path(prefix_view.prefix) encoded_path = encode_path((*prefix_path, *additional_path)) return encoded_path.hex()
def view( self, prefix: Union[bytes, Tuple[Union[int, str], ...]]) -> 'ContainerView': if not isinstance(prefix, bytes): prefix = E.encode_path(prefix) return PrefixView(prefix=prefix, container=self)
def make_array( self, path: Union[AimObjectKey, AimObjectPath] = () ): prefix = E.encode_path(path) self.container[prefix] = E.encode(ArrayFlag)