Changeset View
Changeset View
Standalone View
Standalone View
swh/vault/cache.py
Show All 12 Lines | class VaultCache: | ||||
This implementation computes sha1('<bundle_type>:<object_id>') as the | This implementation computes sha1('<bundle_type>:<object_id>') 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, obj_type, obj_id, content): | def add(self, bundle_type, obj_id, content): | ||||
sid = self._get_internal_id(obj_type, obj_id) | sid = self._get_internal_id(bundle_type, obj_id) | ||||
return self.objstorage.add(content, sid) | return self.objstorage.add(content, sid) | ||||
def get(self, obj_type, obj_id): | def get(self, bundle_type, obj_id): | ||||
sid = self._get_internal_id(obj_type, obj_id) | sid = self._get_internal_id(bundle_type, obj_id) | ||||
return self.objstorage.get(hashutil.hash_to_bytes(sid)) | return self.objstorage.get(hashutil.hash_to_bytes(sid)) | ||||
def delete(self, obj_type, obj_id): | def delete(self, bundle_type, obj_id): | ||||
sid = self._get_internal_id(obj_type, obj_id) | sid = self._get_internal_id(bundle_type, obj_id) | ||||
return self.objstorage.delete(hashutil.hash_to_bytes(sid)) | return self.objstorage.delete(hashutil.hash_to_bytes(sid)) | ||||
def add_stream(self, obj_type, obj_id, content_iter): | def add_stream(self, bundle_type, obj_id, content_iter): | ||||
sid = self._get_internal_id(obj_type, obj_id) | sid = self._get_internal_id(bundle_type, obj_id) | ||||
return self.objstorage.add_stream(content_iter, sid) | return self.objstorage.add_stream(content_iter, sid) | ||||
def get_stream(self, obj_type, obj_id): | def get_stream(self, bundle_type, obj_id): | ||||
sid = self._get_internal_id(obj_type, obj_id) | sid = self._get_internal_id(bundle_type, obj_id) | ||||
return self.objstorage.get_stream(hashutil.hash_to_bytes(sid)) | return self.objstorage.get_stream(hashutil.hash_to_bytes(sid)) | ||||
def is_cached(self, obj_type, obj_id): | def is_cached(self, bundle_type, obj_id): | ||||
sid = self._get_internal_id(obj_type, obj_id) | sid = self._get_internal_id(bundle_type, obj_id) | ||||
return hashutil.hash_to_bytes(sid) in self.objstorage | return hashutil.hash_to_bytes(sid) in self.objstorage | ||||
def _get_internal_id(self, obj_type, obj_id): | def _get_internal_id(self, bundle_type, obj_id): | ||||
obj_id = hashutil.hash_to_hex(obj_id) | obj_id = hashutil.hash_to_hex(obj_id) | ||||
return compute_hash("{}:{}".format(obj_type, obj_id).encode()) | return compute_hash("{}:{}".format(bundle_type, obj_id).encode()) |