diff --git a/swh/storage/buffer.py b/swh/storage/buffer.py --- a/swh/storage/buffer.py +++ b/swh/storage/buffer.py @@ -1,4 +1,4 @@ -# Copyright (C) 2019 The Software Heritage developers +# Copyright (C) 2019-2020 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 @@ -102,6 +102,17 @@ return summary + def clear(self, object_types: Optional[Iterable[str]] = None) -> None: + """Clear objects from current buffer + + """ + if object_types is None: + object_types = self.object_types + + for object_type in object_types: + q = self._objects[object_type] + q.clear() + def object_add( self, objects: Iterable[BaseModel], *, object_type: str) -> Dict: """Enqueue objects to write to the storage. This checks if the queue's diff --git a/swh/storage/tests/test_buffer.py b/swh/storage/tests/test_buffer.py --- a/swh/storage/tests/test_buffer.py +++ b/swh/storage/tests/test_buffer.py @@ -1,4 +1,4 @@ -# Copyright (C) 2019 The Software Heritage developers +# Copyright (C) 2019-2020 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 @@ -277,3 +277,55 @@ s = storage.flush() assert s == {} + + +def test_buffering_proxy_storage_clear(sample_data): + """Clear operation on buffer + + """ + threshold = 10 + contents = sample_data['content'] + assert len(contents) < threshold + revisions = sample_data['revision'] + assert len(revisions) < threshold + releases = sample_data['release'] + assert len(releases) < threshold + + storage = get_storage_with_buffer_config( + min_batch_size={ + 'content': threshold, + 'revision': threshold, + 'release': threshold, + } + ) + + s = storage.content_add(contents) + assert s == {} + + s = storage.revision_add(revisions) + assert s == {} + + s = storage.release_add(releases) + assert s == {} + + assert len(storage._objects['content']) == len(contents) + assert len(storage._objects['revision']) == len(revisions) + assert len(storage._objects['release']) == len(releases) + + # clear only content from the buffer + s = storage.clear(['content']) + assert s is None + + # specific clear operation on specific object type content only touched + # them + assert len(storage._objects['content']) == 0 + assert len(storage._objects['revision']) == len(revisions) + assert len(storage._objects['release']) == len(releases) + + # clear current buffer from all object types + s = storage.clear() + assert s is None + + assert len(storage._objects['content']) == 0 + assert len(storage._objects['revision']) == 0 + assert len(storage._objects['release']) == 0