From 5bfc5d1667ca2fec3f1cdc0da7ab32ac0e8ef44a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 13 Aug 2016 15:32:53 +0300 Subject: Add LeafStore class for storing CowTree nodes CowTree will be written soon. --- obnamlib/__init__.py | 4 +++- obnamlib/fmt_ga/__init__.py | 1 + obnamlib/fmt_ga/leaf_store.py | 40 +++++++++++++++++++++++++++++++ obnamlib/fmt_ga/leaf_store_tests.py | 47 +++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 obnamlib/fmt_ga/leaf_store.py create mode 100644 obnamlib/fmt_ga/leaf_store_tests.py diff --git a/obnamlib/__init__.py b/obnamlib/__init__.py index ee5f5516..ced359d0 100644 --- a/obnamlib/__init__.py +++ b/obnamlib/__init__.py @@ -185,7 +185,9 @@ from .fmt_ga import ( create_gadirectory_from_dict, GATree, GAChunkStore, - GAChunkIndexes) + GAChunkIndexes, + InMemoryLeafStore, +) # diff --git a/obnamlib/fmt_ga/__init__.py b/obnamlib/fmt_ga/__init__.py index 08875782..a0f9039b 100644 --- a/obnamlib/fmt_ga/__init__.py +++ b/obnamlib/fmt_ga/__init__.py @@ -17,6 +17,7 @@ from .client_list import GAClientList from .chunk_store import GAChunkStore +from .leaf_store import InMemoryLeafStore from .indexes import GAChunkIndexes from .dirobj import GADirectory, GAImmutableError, create_gadirectory_from_dict from .tree import GATree diff --git a/obnamlib/fmt_ga/leaf_store.py b/obnamlib/fmt_ga/leaf_store.py new file mode 100644 index 00000000..5a3a8627 --- /dev/null +++ b/obnamlib/fmt_ga/leaf_store.py @@ -0,0 +1,40 @@ +# Copyright 2016 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# =*= License: GPL-3+ =*= + + +class LeafStoreInterface(object): # pragma: no cover + + def put_leaf(self, leaf): + raise NotImplementedError() + + def get_leaf(self, leaf_id): + raise NotImplementedError() + + +class InMemoryLeafStore(LeafStoreInterface): + + def __init__(self): + self._leaves = {} + self._counter = 0 + + def put_leaf(self, leaf): + self._counter += 1 + self._leaves[self._counter] = leaf + return self._counter + + def get_leaf(self, leaf_id): + return self._leaves.get(leaf_id, None) diff --git a/obnamlib/fmt_ga/leaf_store_tests.py b/obnamlib/fmt_ga/leaf_store_tests.py new file mode 100644 index 00000000..7eb3c8e4 --- /dev/null +++ b/obnamlib/fmt_ga/leaf_store_tests.py @@ -0,0 +1,47 @@ +# Copyright 2016 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# =*= License: GPL-3+ =*= + + +import unittest + +import obnamlib + + +# Pylint doesn't like the LeafStoreTests class, since its methods use +# attributes that aren't defined in the class (they're defined by the +# subclass). However, we know they'll be defined in time and also we +# want the test cases to be defined only once. So we silence pylint's +# no-member warning for this file. +# +# pylint: disable=no-member + +class LeafStoreTests(object): + + def test_roundtrip_works(self): + leaf = {'foo': 'bar'} + leaf_id = self.ls.put_leaf(leaf) + leaf2 = self.ls.get_leaf(leaf_id) + self.assertEqual(leaf, leaf2) + + def test_returns_None_if_leaf_is_missing(self): + self.assertEqual(self.ls.get_leaf(42), None) + + +class InMemoryLeafStoreTests(unittest.TestCase, LeafStoreTests): + + def setUp(self): + self.ls = obnamlib.InMemoryLeafStore() -- cgit v1.2.1