Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/cloud/objstorage_cloud.py
# Copyright (C) 2016 The Software Heritage developers | # Copyright (C) 2016 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import abc | import abc | ||||
from ..objstorage import ObjStorage, ID_HASH_ALGO | from ..objstorage import ObjStorage, compute_hash | ||||
from ..exc import ObjNotFoundError, Error | from ..exc import ObjNotFoundError, Error | ||||
from swh.core import hashutil | from swh.core import hashutil | ||||
from libcloud.storage import providers | from libcloud.storage import providers | ||||
from libcloud.storage.types import Provider, ObjectDoesNotExistError | from libcloud.storage.types import Provider, ObjectDoesNotExistError | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | def __len__(self): | ||||
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, obj_id=None, check_presence=True): | def add(self, content, obj_id=None, check_presence=True): | ||||
if obj_id is None: | if obj_id is None: | ||||
# Checksum is missing, compute it on the fly. | # Checksum is missing, compute it on the fly. | ||||
h = hashutil._new_hash(ID_HASH_ALGO, len(bytes)) | obj_id = compute_hash(content) | ||||
h.update(bytes) | |||||
obj_id = h.digest() | |||||
if check_presence and obj_id in self: | if check_presence and obj_id in self: | ||||
return obj_id | return obj_id | ||||
self._put_object(content, obj_id) | self._put_object(content, obj_id) | ||||
return obj_id | return obj_id | ||||
def restore(self, content, obj_id=None): | def restore(self, content, obj_id=None): | ||||
return self.add(content, obj_id, chech_presence=False) | return self.add(content, obj_id, chech_presence=False) | ||||
def get(self, obj_id): | def get(self, obj_id): | ||||
return bytes(self._get_object(obj_id).as_stream()) | return bytes(self._get_object(obj_id).as_stream()) | ||||
def check(self, obj_id): | def check(self, obj_id): | ||||
# Check that the file exists, as _get_object raises ObjNotFoundError | # Check that the file exists, as _get_object raises ObjNotFoundError | ||||
self._get_object(obj_id) | self._get_object(obj_id) | ||||
# Check the content integrity | # Check the content integrity | ||||
obj_content = self.get(obj_id) | obj_content = self.get(obj_id) | ||||
# TODO factorize the hash computation. | content_obj_id = compute_hash(obj_content) | ||||
h = hashutil._new_hash(ID_HASH_ALGO, len(obj_content)) | |||||
h.update(obj_content) | |||||
content_obj_id = h.digest() | |||||
if content_obj_id != obj_id: | if content_obj_id != obj_id: | ||||
raise Error(obj_id) | raise Error(obj_id) | ||||
def _get_object(self, obj_id): | def _get_object(self, obj_id): | ||||
""" Get a Libcloud wrapper for an object pointer. | """ Get a Libcloud wrapper for an object pointer. | ||||
This wrapper does not retrieve the content of the object direclty. | This wrapper does not retrieve the content of the object direclty. | ||||
""" | """ | ||||
Show All 29 Lines |