swh.objstorage package

Submodules

swh.objstorage.cli module

swh.objstorage.cli.main()[source]

swh.objstorage.exc module

exception swh.objstorage.exc.Error[source]

Bases: Exception

__str__()[source]

Return str(self).

__module__ = 'swh.objstorage.exc'
__weakref__

list of weak references to the object (if defined)

exception swh.objstorage.exc.ObjNotFoundError[source]

Bases: swh.objstorage.exc.Error

__str__()[source]

Return str(self).

__module__ = 'swh.objstorage.exc'
exception swh.objstorage.exc.ObjStorageAPIError[source]

Bases: Exception

Specific internal exception of an object storage (mainly connection).

__str__()[source]

Return str(self).

__module__ = 'swh.objstorage.exc'
__weakref__

list of weak references to the object (if defined)

swh.objstorage.objstorage module

swh.objstorage.objstorage.compute_hash(content)[source]

Compute the content’s hash.

Parameters:
  • content (bytes) – The raw content to hash
  • hash_name (str) – Hash’s name (default to ID_HASH_ALGO)
Returns:

The ID_HASH_ALGO for the content

class swh.objstorage.objstorage.NullCompressor[source]

Bases: object

compress(data)[source]
flush()[source]
__dict__ = mappingproxy({'__weakref__': <attribute '__weakref__' of 'NullCompressor' objects>, '__doc__': None, '__module__': 'swh.objstorage.objstorage', 'compress': <function NullCompressor.compress>, '__dict__': <attribute '__dict__' of 'NullCompressor' objects>, 'flush': <function NullCompressor.flush>})
__module__ = 'swh.objstorage.objstorage'
__weakref__

list of weak references to the object (if defined)

class swh.objstorage.objstorage.ObjStorage(*, allow_delete=False, **kwargs)[source]

Bases: object

High-level API to manipulate the Software Heritage object storage.

Conceptually, the object storage offers the following methods:

  • check_config() check if the object storage is properly configured
  • __contains__() check if an object is present, by object id
  • add() add a new object, returning an object id
  • restore() same as add() but erase an already existed content
  • get() retrieve the content of an object, by object id
  • check() check the integrity of an object, by object id
  • delete() remove an object

And some management methods:

  • get_random() get random object id of existing contents (used for the
    content integrity checker).

Some of the methods have available streaming equivalents:

  • add_stream() same as add() but with a chunked iterator
  • restore_stream() same as add_stream() but erase already existing content
  • get_stream() same as get() but returns a chunked iterator

Each implementation of this interface can have a different behavior and its own way to store the contents.

__init__(*, allow_delete=False, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

check_config(*, check_write)[source]

Check whether the object storage is properly configured.

Parameters:
  • check_write (bool) – if True, check if writes to the object storage
  • succeed. (can) –
Returns:

True if the configuration check worked, an exception if it didn’t.

__contains__(obj_id, *args, **kwargs)[source]

Indicate if the given object is present in the storage.

Parameters:obj_id (bytes) – object identifier.
Returns:True if and only if the object is present in the current object storage.
add(content, obj_id=None, check_presence=True, *args, **kwargs)[source]

Add a new object to the object storage.

Parameters:
  • content (bytes) – object’s raw content to add in storage.
  • obj_id (bytes) – checksum of [bytes] using [ID_HASH_ALGO] algorithm. When given, obj_id will be trusted to match the bytes. If missing, obj_id will be computed on the fly.
  • check_presence (bool) – indicate if the presence of the content should be verified before adding the file.
Returns:

the id (bytes) of the object into the storage.

add_batch(contents, check_presence=True)[source]

Add a batch of new objects to the object storage.

Parameters:contents (dict) – mapping from obj_id to object contents
Returns:the number of objects added to the storage
restore(content, obj_id=None, *args, **kwargs)[source]

Restore a content that have been corrupted.

This function is identical to add but does not check if the object id is already in the file system. The default implementation provided by the current class is suitable for most cases.

Parameters:
  • content (bytes) – object’s raw content to add in storage
  • obj_id (bytes) – checksum of bytes as computed by ID_HASH_ALGO. When given, obj_id will be trusted to match bytes. If missing, obj_id will be computed on the fly.
get(obj_id, *args, **kwargs)[source]

Retrieve the content of a given object.

Parameters:obj_id (bytes) – object id.
Returns:the content of the requested object as bytes.
Raises:ObjNotFoundError – if the requested object is missing.
get_batch(obj_ids, *args, **kwargs)[source]

Retrieve objects’ raw content in bulk from storage.

Note: This function does have a default implementation in ObjStorage that is suitable for most cases.

For object storages that needs to do the minimal number of requests possible (ex: remote object storages), that method can be overridden to perform a more efficient operation.

Parameters:([bytes] (obj_ids) – list of object ids.
Returns:list of resulting contents, or None if the content could not be retrieved. Do not raise any exception as a fail for one content will not cancel the whole request.
check(obj_id, *args, **kwargs)[source]

Perform an integrity check for a given object.

Verify that the file object is in place and that the gziped content matches the object id.

Parameters:

obj_id (bytes) – object identifier.

Raises:
  • ObjNotFoundError – if the requested object is missing.
  • Error – if the request object is corrupted.
delete(obj_id, *args, **kwargs)[source]

Delete an object.

Parameters:obj_id (bytes) – object identifier.
Raises:ObjNotFoundError – if the requested object is missing.
get_random(batch_size, *args, **kwargs)[source]

Get random ids of existing contents.

This method is used in order to get random ids to perform content integrity verifications on random contents.

Parameters:batch_size (int) – Number of ids that will be given
Yields:An iterable of ids (bytes) of contents that are in the current object storage.
add_stream(content_iter, obj_id, check_presence=True)[source]

Add a new object to the object storage using streaming.

This function is identical to add() except it takes a generator that yields the chunked content instead of the whole content at once.

Parameters:
  • content (bytes) – chunked generator that yields the object’s raw content to add in storage.
  • obj_id (bytes) – object identifier
  • check_presence (bool) – indicate if the presence of the content should be verified before adding the file.
Returns:

the id (bytes) of the object into the storage.

restore_stream(content_iter, obj_id=None)[source]

Restore a content that have been corrupted using streaming.

This function is identical to restore() except it takes a generator that yields the chunked content instead of the whole content at once. The default implementation provided by the current class is suitable for most cases.

Parameters:
  • content (bytes) – chunked generator that yields the object’s raw content to add in storage.
  • obj_id (bytes) – object identifier
get_stream(obj_id, chunk_size=2097152)[source]

Retrieve the content of a given object as a chunked iterator.

Parameters:obj_id (bytes) – object id.
Returns:the content of the requested object as bytes.
Raises:ObjNotFoundError – if the requested object is missing.
list_content(last_obj_id=None, limit=10000)[source]

Generates known object ids.

Parameters:
  • last_obj_id (bytes) – object id from which to iterate from (excluded).
  • limit (int) – max number of object ids to generate.
Generates:
obj_id (bytes): object ids.
__abstractmethods__ = frozenset({'get', 'check_config', 'check', 'delete', '__contains__', 'add'})
__dict__ = mappingproxy({'delete': <function ObjStorage.delete>, 'get_batch': <function ObjStorage.get_batch>, 'add_stream': <function ObjStorage.add_stream>, 'get_stream': <function ObjStorage.get_stream>, '_abc_registry': <_weakrefset.WeakSet object>, '_abc_cache': <_weakrefset.WeakSet object>, 'check': <function ObjStorage.check>, '_abc_negative_cache_version': 42, '__abstractmethods__': frozenset({'get', 'check_config', 'check', 'delete', '__contains__', 'add'}), 'list_content': <function ObjStorage.list_content>, 'get': <function ObjStorage.get>, 'check_config': <function ObjStorage.check_config>, '__doc__': ' High-level API to manipulate the Software Heritage object storage.\n\n Conceptually, the object storage offers the following methods:\n\n - check_config() check if the object storage is properly configured\n - __contains__() check if an object is present, by object id\n - add() add a new object, returning an object id\n - restore() same as add() but erase an already existed content\n - get() retrieve the content of an object, by object id\n - check() check the integrity of an object, by object id\n - delete() remove an object\n\n And some management methods:\n\n - get_random() get random object id of existing contents (used for the\n content integrity checker).\n\n Some of the methods have available streaming equivalents:\n\n - add_stream() same as add() but with a chunked iterator\n - restore_stream() same as add_stream() but erase already existing content\n - get_stream() same as get() but returns a chunked iterator\n\n Each implementation of this interface can have a different behavior and\n its own way to store the contents.\n ', '__module__': 'swh.objstorage.objstorage', '__init__': <function ObjStorage.__init__>, 'get_random': <function ObjStorage.get_random>, '__dict__': <attribute '__dict__' of 'ObjStorage' objects>, 'add': <function ObjStorage.add>, 'restore_stream': <function ObjStorage.restore_stream>, 'restore': <function ObjStorage.restore>, '__weakref__': <attribute '__weakref__' of 'ObjStorage' objects>, '__contains__': <function ObjStorage.__contains__>, 'add_batch': <function ObjStorage.add_batch>, '_abc_negative_cache': <_weakrefset.WeakSet object>})
__module__ = 'swh.objstorage.objstorage'
__weakref__

list of weak references to the object (if defined)

_abc_cache = <_weakrefset.WeakSet object>
_abc_negative_cache = <_weakrefset.WeakSet object>
_abc_negative_cache_version = 42
_abc_registry = <_weakrefset.WeakSet object>

Module contents

swh.objstorage.get_objstorage(cls, args)[source]

Create an ObjStorage using the given implementation class.

Parameters:
  • 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.

class swh.objstorage.ObjStorage(*, allow_delete=False, **kwargs)[source]

Bases: object

High-level API to manipulate the Software Heritage object storage.

Conceptually, the object storage offers the following methods:

  • check_config() check if the object storage is properly configured
  • __contains__() check if an object is present, by object id
  • add() add a new object, returning an object id
  • restore() same as add() but erase an already existed content
  • get() retrieve the content of an object, by object id
  • check() check the integrity of an object, by object id
  • delete() remove an object

And some management methods:

  • get_random() get random object id of existing contents (used for the
    content integrity checker).

Some of the methods have available streaming equivalents:

  • add_stream() same as add() but with a chunked iterator
  • restore_stream() same as add_stream() but erase already existing content
  • get_stream() same as get() but returns a chunked iterator

Each implementation of this interface can have a different behavior and its own way to store the contents.

__init__(*, allow_delete=False, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

check_config(*, check_write)[source]

Check whether the object storage is properly configured.

Parameters:
  • check_write (bool) – if True, check if writes to the object storage
  • succeed. (can) –
Returns:

True if the configuration check worked, an exception if it didn’t.

__contains__(obj_id, *args, **kwargs)[source]

Indicate if the given object is present in the storage.

Parameters:obj_id (bytes) – object identifier.
Returns:True if and only if the object is present in the current object storage.
add(content, obj_id=None, check_presence=True, *args, **kwargs)[source]

Add a new object to the object storage.

Parameters:
  • content (bytes) – object’s raw content to add in storage.
  • obj_id (bytes) – checksum of [bytes] using [ID_HASH_ALGO] algorithm. When given, obj_id will be trusted to match the bytes. If missing, obj_id will be computed on the fly.
  • check_presence (bool) – indicate if the presence of the content should be verified before adding the file.
Returns:

the id (bytes) of the object into the storage.

add_batch(contents, check_presence=True)[source]

Add a batch of new objects to the object storage.

Parameters:contents (dict) – mapping from obj_id to object contents
Returns:the number of objects added to the storage
restore(content, obj_id=None, *args, **kwargs)[source]

Restore a content that have been corrupted.

This function is identical to add but does not check if the object id is already in the file system. The default implementation provided by the current class is suitable for most cases.

Parameters:
  • content (bytes) – object’s raw content to add in storage
  • obj_id (bytes) – checksum of bytes as computed by ID_HASH_ALGO. When given, obj_id will be trusted to match bytes. If missing, obj_id will be computed on the fly.
get(obj_id, *args, **kwargs)[source]

Retrieve the content of a given object.

Parameters:obj_id (bytes) – object id.
Returns:the content of the requested object as bytes.
Raises:ObjNotFoundError – if the requested object is missing.
get_batch(obj_ids, *args, **kwargs)[source]

Retrieve objects’ raw content in bulk from storage.

Note: This function does have a default implementation in ObjStorage that is suitable for most cases.

For object storages that needs to do the minimal number of requests possible (ex: remote object storages), that method can be overridden to perform a more efficient operation.

Parameters:([bytes] (obj_ids) – list of object ids.
Returns:list of resulting contents, or None if the content could not be retrieved. Do not raise any exception as a fail for one content will not cancel the whole request.
check(obj_id, *args, **kwargs)[source]

Perform an integrity check for a given object.

Verify that the file object is in place and that the gziped content matches the object id.

Parameters:

obj_id (bytes) – object identifier.

Raises:
  • ObjNotFoundError – if the requested object is missing.
  • Error – if the request object is corrupted.
delete(obj_id, *args, **kwargs)[source]

Delete an object.

Parameters:obj_id (bytes) – object identifier.
Raises:ObjNotFoundError – if the requested object is missing.
get_random(batch_size, *args, **kwargs)[source]

Get random ids of existing contents.

This method is used in order to get random ids to perform content integrity verifications on random contents.

Parameters:batch_size (int) – Number of ids that will be given
Yields:An iterable of ids (bytes) of contents that are in the current object storage.
add_stream(content_iter, obj_id, check_presence=True)[source]

Add a new object to the object storage using streaming.

This function is identical to add() except it takes a generator that yields the chunked content instead of the whole content at once.

Parameters:
  • content (bytes) – chunked generator that yields the object’s raw content to add in storage.
  • obj_id (bytes) – object identifier
  • check_presence (bool) – indicate if the presence of the content should be verified before adding the file.
Returns:

the id (bytes) of the object into the storage.

restore_stream(content_iter, obj_id=None)[source]

Restore a content that have been corrupted using streaming.

This function is identical to restore() except it takes a generator that yields the chunked content instead of the whole content at once. The default implementation provided by the current class is suitable for most cases.

Parameters:
  • content (bytes) – chunked generator that yields the object’s raw content to add in storage.
  • obj_id (bytes) – object identifier
get_stream(obj_id, chunk_size=2097152)[source]

Retrieve the content of a given object as a chunked iterator.

Parameters:obj_id (bytes) – object id.
Returns:the content of the requested object as bytes.
Raises:ObjNotFoundError – if the requested object is missing.
list_content(last_obj_id=None, limit=10000)[source]

Generates known object ids.

Parameters:
  • last_obj_id (bytes) – object id from which to iterate from (excluded).
  • limit (int) – max number of object ids to generate.
Generates:
obj_id (bytes): object ids.
__abstractmethods__ = frozenset({'get', 'check_config', 'check', 'delete', '__contains__', 'add'})
__dict__ = mappingproxy({'delete': <function ObjStorage.delete>, 'get_batch': <function ObjStorage.get_batch>, 'add_stream': <function ObjStorage.add_stream>, 'get_stream': <function ObjStorage.get_stream>, '_abc_registry': <_weakrefset.WeakSet object>, '_abc_cache': <_weakrefset.WeakSet object>, 'check': <function ObjStorage.check>, '_abc_negative_cache_version': 42, '__abstractmethods__': frozenset({'get', 'check_config', 'check', 'delete', '__contains__', 'add'}), 'list_content': <function ObjStorage.list_content>, 'get': <function ObjStorage.get>, 'check_config': <function ObjStorage.check_config>, '__doc__': ' High-level API to manipulate the Software Heritage object storage.\n\n Conceptually, the object storage offers the following methods:\n\n - check_config() check if the object storage is properly configured\n - __contains__() check if an object is present, by object id\n - add() add a new object, returning an object id\n - restore() same as add() but erase an already existed content\n - get() retrieve the content of an object, by object id\n - check() check the integrity of an object, by object id\n - delete() remove an object\n\n And some management methods:\n\n - get_random() get random object id of existing contents (used for the\n content integrity checker).\n\n Some of the methods have available streaming equivalents:\n\n - add_stream() same as add() but with a chunked iterator\n - restore_stream() same as add_stream() but erase already existing content\n - get_stream() same as get() but returns a chunked iterator\n\n Each implementation of this interface can have a different behavior and\n its own way to store the contents.\n ', '__module__': 'swh.objstorage.objstorage', '__init__': <function ObjStorage.__init__>, 'get_random': <function ObjStorage.get_random>, '__dict__': <attribute '__dict__' of 'ObjStorage' objects>, 'add': <function ObjStorage.add>, 'restore_stream': <function ObjStorage.restore_stream>, 'restore': <function ObjStorage.restore>, '__weakref__': <attribute '__weakref__' of 'ObjStorage' objects>, '__contains__': <function ObjStorage.__contains__>, 'add_batch': <function ObjStorage.add_batch>, '_abc_negative_cache': <_weakrefset.WeakSet object>})
__module__ = 'swh.objstorage.objstorage'
__weakref__

list of weak references to the object (if defined)

_abc_cache = <_weakrefset.WeakSet object>
_abc_negative_cache = <_weakrefset.WeakSet object>
_abc_negative_cache_version = 42
_abc_registry = <_weakrefset.WeakSet object>