Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/objstorage.py
# Copyright (C) 2015-2020 The Software Heritage developers | # Copyright (C) 2015-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import abc | import abc | ||||
import bz2 | import bz2 | ||||
from itertools import dropwhile, islice | from itertools import dropwhile, islice | ||||
import lzma | import lzma | ||||
from typing import Callable, Dict, Iterator, List, Optional | from typing import Callable, Dict, Iterator, List, Optional | ||||
import zlib | import zlib | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from .constants import DEFAULT_LIMIT, ID_HASH_ALGO | from .constants import DEFAULT_LIMIT, ID_HASH_ALGO | ||||
from .exc import ObjNotFoundError | from .exc import ObjNotFoundError | ||||
from .interface import ObjId, ObjStorageInterface | from .interface import CompositeObjId, ObjId, ObjStorageInterface | ||||
def objid_to_default_hex(obj_id: ObjId) -> str: | |||||
ardumont: Please add a docstring so one can immediately see why we use a specific implementation over the… | |||||
if isinstance(obj_id, bytes): | |||||
return hashutil.hash_to_hex(obj_id) | |||||
elif isinstance(obj_id, str): | |||||
return obj_id | |||||
else: | |||||
return hashutil.hash_to_hex(obj_id[ID_HASH_ALGO]) | |||||
def compute_hash(content, algo=ID_HASH_ALGO): | def compute_hash(content, algo=ID_HASH_ALGO): | ||||
"""Compute the content's hash. | """Compute the content's hash. | ||||
Args: | Args: | ||||
content (bytes): The raw content to hash | content (bytes): The raw content to hash | ||||
hash_name (str): Hash's name (default to ID_HASH_ALGO) | hash_name (str): Hash's name (default to ID_HASH_ALGO) | ||||
▲ Show 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | class ObjStorage(metaclass=abc.ABCMeta): | ||||
def delete(self, obj_id: ObjId): | def delete(self, obj_id: ObjId): | ||||
if not self.allow_delete: | if not self.allow_delete: | ||||
raise PermissionError("Delete is not allowed.") | raise PermissionError("Delete is not allowed.") | ||||
def list_content( | def list_content( | ||||
self: ObjStorageInterface, | self: ObjStorageInterface, | ||||
last_obj_id: Optional[ObjId] = None, | last_obj_id: Optional[ObjId] = None, | ||||
limit: int = DEFAULT_LIMIT, | limit: int = DEFAULT_LIMIT, | ||||
) -> Iterator[ObjId]: | ) -> Iterator[CompositeObjId]: | ||||
it = iter(self) | it = iter(self) | ||||
if last_obj_id is not None: | if last_obj_id: | ||||
it = dropwhile(last_obj_id.__ge__, it) | last_obj_id_hex = objid_to_default_hex(last_obj_id) | ||||
it = dropwhile(lambda x: objid_to_default_hex(x) <= last_obj_id_hex, it) | |||||
return islice(it, limit) | return islice(it, limit) |
Please add a docstring so one can immediately see why we use a specific implementation over the default swh.model.hashutil.bytes_to_hex.