Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/multiplexer/filter/id_filter.py
# Copyright (C) 2015-2017 The Software Heritage developers | # Copyright (C) 2015-2017 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 re | import re | ||||
from typing import Iterator | |||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.objstorage.exc import ObjNotFoundError | from swh.objstorage.exc import ObjNotFoundError | ||||
from swh.objstorage.interface import CompositeObjId | |||||
from swh.objstorage.multiplexer.filter.filter import ObjStorageFilter | from swh.objstorage.multiplexer.filter.filter import ObjStorageFilter | ||||
class IdObjStorageFilter(ObjStorageFilter, metaclass=abc.ABCMeta): | class IdObjStorageFilter(ObjStorageFilter, metaclass=abc.ABCMeta): | ||||
"""Filter that only allow operations if the object id match a requirement. | """Filter that only allow operations if the object id match a requirement. | ||||
Even for read operations, check before if the id match the requirements. | Even for read operations, check before if the id match the requirements. | ||||
This may prevent for unnecessary disk access. | This may prevent for unnecessary disk access. | ||||
Show All 9 Lines | class IdObjStorageFilter(ObjStorageFilter, metaclass=abc.ABCMeta): | ||||
def __contains__(self, obj_id, *args, **kwargs): | def __contains__(self, obj_id, *args, **kwargs): | ||||
if self.is_valid(obj_id): | if self.is_valid(obj_id): | ||||
return self.storage.__contains__(*args, obj_id=obj_id, **kwargs) | return self.storage.__contains__(*args, obj_id=obj_id, **kwargs) | ||||
return False | return False | ||||
def __len__(self): | def __len__(self): | ||||
return sum(1 for i in [id for id in self.storage if self.is_valid(id)]) | return sum(1 for i in [id for id in self.storage if self.is_valid(id)]) | ||||
def __iter__(self): | def __iter__(self) -> Iterator[CompositeObjId]: | ||||
yield from filter(lambda id: self.is_valid(id), iter(self.storage)) | yield from filter(lambda id: self.is_valid(id), iter(self.storage)) | ||||
def add(self, content, obj_id, check_presence=True, *args, **kwargs): | def add(self, content, obj_id, check_presence=True, *args, **kwargs): | ||||
if self.is_valid(obj_id): | if self.is_valid(obj_id): | ||||
return self.storage.add(content, *args, obj_id=obj_id, **kwargs) | return self.storage.add(content, *args, obj_id=obj_id, **kwargs) | ||||
def restore(self, content, obj_id, *args, **kwargs): | def restore(self, content, obj_id, *args, **kwargs): | ||||
if self.is_valid(obj_id): | if self.is_valid(obj_id): | ||||
Show All 35 Lines |