Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/backends/seaweedfs/objstorage.py
Show First 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | def __len__(self): | ||||
performance applies. | performance applies. | ||||
Returns: | Returns: | ||||
number of objects contained in the storage. | number of objects contained in the storage. | ||||
""" | """ | ||||
return sum(1 for i in self) | return sum(1 for i in self) | ||||
def add(self, content: bytes, obj_id: ObjId, check_presence: bool = True) -> ObjId: | def add(self, content: bytes, obj_id: ObjId, check_presence: bool = True) -> None: | ||||
if check_presence and obj_id in self: | if check_presence and obj_id in self: | ||||
return obj_id | return | ||||
def compressor(data): | def compressor(data): | ||||
comp = compressors[self.compression]() | comp = compressors[self.compression]() | ||||
yield comp.compress(data) | yield comp.compress(data) | ||||
yield comp.flush() | yield comp.flush() | ||||
assert isinstance( | assert isinstance( | ||||
content, bytes | content, bytes | ||||
), "list of content chunks is not supported anymore" | ), "list of content chunks is not supported anymore" | ||||
self.wf.put(io.BytesIO(b"".join(compressor(content))), self._path(obj_id)) | self.wf.put(io.BytesIO(b"".join(compressor(content))), self._path(obj_id)) | ||||
return obj_id | |||||
def restore(self, content: bytes, obj_id: ObjId): | def restore(self, content: bytes, obj_id: ObjId) -> None: | ||||
return self.add(content, obj_id, check_presence=False) | return self.add(content, obj_id, check_presence=False) | ||||
def get(self, obj_id: ObjId) -> bytes: | def get(self, obj_id: ObjId) -> bytes: | ||||
try: | try: | ||||
obj = self.wf.get(self._path(obj_id)) | obj = self.wf.get(self._path(obj_id)) | ||||
except Exception: | except Exception: | ||||
raise ObjNotFoundError(obj_id) | raise ObjNotFoundError(obj_id) | ||||
▲ Show 20 Lines • Show All 56 Lines • Show Last 20 Lines |