Changeset View
Changeset View
Standalone View
Standalone View
swh/vault/cache.py
Show All 13 Lines | class VaultCache: | ||||
This implementation computes sha1('<bundle_type>:<swhid>') as the | This implementation computes sha1('<bundle_type>:<swhid>') as the | ||||
internal identifiers used in the underlying objstorage. | internal identifiers used in the underlying objstorage. | ||||
""" | """ | ||||
def __init__(self, **objstorage): | def __init__(self, **objstorage): | ||||
self.objstorage = get_objstorage(**objstorage) | self.objstorage = get_objstorage(**objstorage) | ||||
def add(self, bundle_type, swhid: CoreSWHID, content): | def add(self, bundle_type, swhid: CoreSWHID, content) -> None: | ||||
sid = self._get_internal_id(bundle_type, swhid) | sid = self._get_internal_id(bundle_type, swhid) | ||||
return self.objstorage.add(content, sid) | self.objstorage.add(content, sid) | ||||
def get(self, bundle_type, swhid: CoreSWHID): | def get(self, bundle_type, swhid: CoreSWHID) -> bytes: | ||||
sid = self._get_internal_id(bundle_type, swhid) | sid = self._get_internal_id(bundle_type, swhid) | ||||
return self.objstorage.get(hashutil.hash_to_bytes(sid)) | return self.objstorage.get(hashutil.hash_to_bytes(sid)) | ||||
def delete(self, bundle_type, swhid: CoreSWHID): | def delete(self, bundle_type, swhid: CoreSWHID): | ||||
sid = self._get_internal_id(bundle_type, swhid) | sid = self._get_internal_id(bundle_type, swhid) | ||||
return self.objstorage.delete(hashutil.hash_to_bytes(sid)) | return self.objstorage.delete(hashutil.hash_to_bytes(sid)) | ||||
def is_cached(self, bundle_type, swhid: CoreSWHID): | def is_cached(self, bundle_type, swhid: CoreSWHID) -> bool: | ||||
sid = self._get_internal_id(bundle_type, swhid) | sid = self._get_internal_id(bundle_type, swhid) | ||||
return hashutil.hash_to_bytes(sid) in self.objstorage | return hashutil.hash_to_bytes(sid) in self.objstorage | ||||
def _get_internal_id(self, bundle_type, swhid: CoreSWHID): | def _get_internal_id(self, bundle_type, swhid: CoreSWHID): | ||||
return compute_hash("{}:{}".format(bundle_type, swhid).encode()) | return compute_hash("{}:{}".format(bundle_type, swhid).encode()) |