Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/backends/azure.py
Show First 20 Lines • Show All 200 Lines • ▼ Show 20 Lines | def __len__(self): | ||||
"""Compute the number of objects in the current object storage. | """Compute the number of objects in the current object storage. | ||||
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: | ||||
"""Add an obj in storage if it's not there already.""" | """Add an obj in storage if it's not there already.""" | ||||
if check_presence and obj_id in self: | if check_presence and obj_id in self: | ||||
return obj_id | return | ||||
hex_obj_id = self._internal_id(obj_id) | hex_obj_id = self._internal_id(obj_id) | ||||
# Send the compressed content | # Send the compressed content | ||||
compressor = compressors[self.compression]() | compressor = compressors[self.compression]() | ||||
data = compressor.compress(content) | data = compressor.compress(content) | ||||
data += compressor.flush() | data += compressor.flush() | ||||
client = self.get_blob_client(hex_obj_id) | client = self.get_blob_client(hex_obj_id) | ||||
try: | try: | ||||
client.upload_blob(data=data, length=len(data)) | client.upload_blob(data=data, length=len(data)) | ||||
except ResourceExistsError: | except ResourceExistsError: | ||||
# There's a race condition between check_presence and upload_blob, | # There's a race condition between check_presence and upload_blob, | ||||
# that we can't get rid of as the azure api doesn't allow atomic | # that we can't get rid of as the azure api doesn't allow atomic | ||||
# replaces or renaming a blob. As the restore operation explicitly | # replaces or renaming a blob. As the restore operation explicitly | ||||
# removes the blob, it should be safe to just ignore the error. | # removes the blob, it should be safe to just ignore the error. | ||||
pass | pass | ||||
return obj_id | def restore(self, content: bytes, obj_id: ObjId) -> None: | ||||
def restore(self, content: bytes, obj_id: ObjId): | |||||
"""Restore a content.""" | """Restore a content.""" | ||||
if obj_id in self: | if obj_id in self: | ||||
self.delete(obj_id) | self.delete(obj_id) | ||||
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: | ||||
"""retrieve blob's content if found.""" | """retrieve blob's content if found.""" | ||||
▲ Show 20 Lines • Show All 171 Lines • Show Last 20 Lines |