Changeset View
Changeset View
Standalone View
Standalone View
swh/vault/cache.py
Show All 25 Lines | class VaultCache(): | ||||
def add(self, obj_type, obj_id, content): | def add(self, obj_type, obj_id, content): | ||||
storage = self._get_storage(obj_type) | storage = self._get_storage(obj_type) | ||||
return storage.add(content, obj_id) | return storage.add(content, obj_id) | ||||
def get(self, obj_type, obj_id): | def get(self, obj_type, obj_id): | ||||
storage = self._get_storage(obj_type) | storage = self._get_storage(obj_type) | ||||
return storage.get(hashutil.hash_to_bytes(obj_id)) | return storage.get(hashutil.hash_to_bytes(obj_id)) | ||||
def delete(self, obj_type, obj_id): | |||||
storage = self._get_storage(obj_type) | |||||
return storage.delete(hashutil.hash_to_bytes(obj_id)) | |||||
def add_stream(self, obj_type, obj_id, content_iter): | def add_stream(self, obj_type, obj_id, content_iter): | ||||
storage = self._get_storage(obj_type) | storage = self._get_storage(obj_type) | ||||
return storage.add_stream(content_iter, obj_id) | return storage.add_stream(content_iter, obj_id) | ||||
def get_stream(self, obj_type, obj_id): | def get_stream(self, obj_type, obj_id): | ||||
storage = self._get_storage(obj_type) | storage = self._get_storage(obj_type) | ||||
return storage.get_stream(hashutil.hash_to_bytes(obj_id)) | return storage.get_stream(hashutil.hash_to_bytes(obj_id)) | ||||
def is_cached(self, obj_type, obj_id): | def is_cached(self, obj_type, obj_id): | ||||
storage = self._get_storage(obj_type) | storage = self._get_storage(obj_type) | ||||
return hashutil.hash_to_bytes(obj_id) in storage | return hashutil.hash_to_bytes(obj_id) in storage | ||||
def ls(self, obj_type): | def ls(self, obj_type): | ||||
storage = self._get_storage(obj_type) | storage = self._get_storage(obj_type) | ||||
yield from storage | yield from storage | ||||
def _get_storage(self, obj_type): | def _get_storage(self, obj_type): | ||||
"""Get the storage that corresponds to the object type""" | """Get the storage that corresponds to the object type""" | ||||
if obj_type not in self.storages: | if obj_type not in self.storages: | ||||
fp = os.path.join(self.root, obj_type) | fp = os.path.join(self.root, obj_type) | ||||
if not os.path.isdir(fp): | if not os.path.isdir(fp): | ||||
os.makedirs(fp, DIR_MODE, exist_ok=True) | os.makedirs(fp, DIR_MODE, exist_ok=True) | ||||
self.storages[obj_type] = get_objstorage( | conf = {'root': fp, 'slicing': '0:1/0:5', 'allow_delete': True} | ||||
'pathslicing', {'root': fp, 'slicing': '0:1/0:5'}) | self.storages[obj_type] = get_objstorage('pathslicing', conf) | ||||
return self.storages[obj_type] | return self.storages[obj_type] |