diff --git a/swh/objstorage/__init__.py b/swh/objstorage/__init__.py index 594bd07..bc4dd77 100644 --- a/swh/objstorage/__init__.py +++ b/swh/objstorage/__init__.py @@ -1,65 +1,67 @@ # Copyright (C) 2016 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from .objstorage import ObjStorage from .objstorage_pathslicing import PathSlicingObjStorage +from .objstorage_in_memory import InMemoryObjStorage from .api.client import RemoteObjStorage from .multiplexer import MultiplexerObjStorage from .multiplexer.filter import add_filters __all__ = ['get_objstorage', 'ObjStorage'] _STORAGE_CLASSES = { 'pathslicing': PathSlicingObjStorage, 'remote': RemoteObjStorage, + 'in-memory': InMemoryObjStorage, } try: from swh.objstorage.cloud.objstorage_azure import AzureCloudObjStorage _STORAGE_CLASSES['azure-storage'] = AzureCloudObjStorage except ImportError: pass def get_objstorage(cls, args): """ Create an ObjStorage using the given implementation class. Args: cls (str): objstorage class unique key contained in the _STORAGE_CLASSES dict. args (dict): arguments for the required class of objstorage that must match exactly the one in the `__init__` method of the class. Returns: subclass of ObjStorage that match the given `storage_class` argument. Raises: ValueError: if the given storage class is not a valid objstorage key. """ try: return _STORAGE_CLASSES[cls](**args) except KeyError: raise ValueError('Storage class %s does not exist' % cls) def _construct_filtered_objstorage(storage_conf, filters_conf): return add_filters( get_objstorage(**storage_conf), filters_conf ) _STORAGE_CLASSES['filtered'] = _construct_filtered_objstorage def _construct_multiplexer_objstorage(objstorages): storages = [get_objstorage(**conf) for conf in objstorages] return MultiplexerObjStorage(storages) _STORAGE_CLASSES['multiplexer'] = _construct_multiplexer_objstorage diff --git a/swh/objstorage/objstorage_in_memory.py b/swh/objstorage/objstorage_in_memory.py new file mode 100644 index 0000000..c023744 --- /dev/null +++ b/swh/objstorage/objstorage_in_memory.py @@ -0,0 +1,55 @@ +# Copyright (C) 2017 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +from swh.objstorage.exc import ObjNotFoundError +from swh.objstorage import objstorage + + +class InMemoryObjStorage(objstorage.ObjStorage): + """In-Memory objstorage. + + Intended for test purposes. + + """ + state = {} + + def __init__(self, **args): + super().__init__() + + def check_config(self, *, check_write): + return True + + def __contains__(self, obj_id, *args, **kwargs): + return obj_id in self.state + + def add(self, content, obj_id=None, check_presence=True, *args, **kwargs): + if obj_id is None: + obj_id = objstorage.compute_hash(content) + + if check_presence and obj_id in self: + return obj_id + + self.state[obj_id] = content + + return obj_id + + def get(self, obj_id, *args, **kwargs): + if obj_id not in self: + raise ObjNotFoundError(obj_id) + + return self.state[obj_id] + + def check(self, obj_id, *args, **kwargs): + if obj_id not in self: + raise ObjNotFoundError(obj_id) + return True + + def delete(self, obj_id, *args, **kwargs): + super().delete(obj_id) # Check delete permission + if obj_id not in self: + raise ObjNotFoundError(obj_id) + + self.state.pop(obj_id) + return True diff --git a/swh/objstorage/tests/test_objstorage_in_memory.py b/swh/objstorage/tests/test_objstorage_in_memory.py new file mode 100644 index 0000000..ca90422 --- /dev/null +++ b/swh/objstorage/tests/test_objstorage_in_memory.py @@ -0,0 +1,16 @@ +# Copyright (C) 2015-2016 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import unittest + +from swh.objstorage.objstorage_in_memory import InMemoryObjStorage + +from objstorage_testing import ObjStorageTestFixture + + +class TestMultiplexerObjStorage(ObjStorageTestFixture, unittest.TestCase): + def setUp(self): + super().setUp() + self.storage = InMemoryObjStorage()