Example #1
0
class DBDB(object):
    def __init__(self, f):
        self._storage = Storage(f)
        self._tree = BinaryTree(self._storage)

    def _assert_not_closed(self):
        if self._storage.closed:
            raise ValueError("Database closed.")

    def close(self):
        self._storage.close()

    def commit(self):
        self._assert_not_closed()
        self._tree.commit()

    def __getitem__(self, key):
        self._assert_not_closed()
        return self._tree.get(key)

    def __setitem__(self, key, value):
        self._assert_not_closed()
        return self._tree.set(key, value)

    def __delitem__(self, key):
        self._assert_not_closed()
        return self._tree.pop(key)

    def __contains__(self, key):
        try:
            self[key]
        except KeyError:
            return False
        else:
            return True

    def __len__(self):
        return len(self._tree)
Example #2
0
class DBDB(object):
    def __init__(self, f):
        self._storage = Storage(f)
        self._tree = BinaryTree(self._storage)

    def _assert_not_closed(self):
        if self._storage.closed:
            raise ValueError('Database closed.')

    def close(self):
        self._storage.close()

    def commit(self):
        self._assert_not_closed()
        self._tree.commit()

    def __getitem__(self, key):
        self._assert_not_closed()
        return self._tree.get(key)

    def __setitem__(self, key, value):
        self._assert_not_closed()
        return self._tree.set(key, value)

    def __delitem__(self, key):
        self._assert_not_closed()
        return self._tree.pop(key)

    def __contains__(self, key):
        try:
            self[key]
        except KeyError:
            return False
        else:
            return True

    def __len__(self):
        return len(self._tree)
Example #3
0
 def setup(self):
     self.f = tempfile.NamedTemporaryFile()
     self.p = Storage(self.f)
Example #4
0
class TestStorage(object):
    def setup(self):
        self.f = tempfile.NamedTemporaryFile()
        self.p = Storage(self.f)

    def _get_superblock_and_data(self, value):
        superblock = value[:Storage.SUPERBLOCK_SIZE]
        data = value[Storage.SUPERBLOCK_SIZE:]
        return superblock, data

    def _get_f_contents(self):
        self.f.flush()
        with open(self.f.name, 'rb') as f:
            return f.read()

    def test_init_ensures_superblock(self):
        EMPTY_SUPERBLOCK = (b'\x00' * Storage.SUPERBLOCK_SIZE)
        self.f.seek(0, os.SEEK_END)
        value = self._get_f_contents()
        eq_(value, EMPTY_SUPERBLOCK)

    def test_write(self):
        self.p.write(b'ABCDE')
        value = self._get_f_contents()
        superblock, data = self._get_superblock_and_data(value)
        eq_(data, b'\x00\x00\x00\x00\x00\x00\x00\x05ABCDE')

    def test_read(self):
        self.f.seek(Storage.SUPERBLOCK_SIZE)
        self.f.write(b'\x00\x00\x00\x00\x00\x00\x00\x0801234567')
        value = self.p.read(Storage.SUPERBLOCK_SIZE)
        eq_(value, b'01234567')

    def test_commit_root_address(self):
        self.p.commit_root_address(257)
        root_bytes = self._get_f_contents()[:8]
        eq_(root_bytes, b'\x00\x00\x00\x00\x00\x00\x01\x01')

    def test_get_root_address(self):
        self.f.seek(0)
        self.f.write(b'\x00\x00\x00\x00\x00\x00\x02\x02')
        root_address = self.p.get_root_address()
        eq_(root_address, 514)

    def test_workflow(self):
        a1 = self.p.write(b'one')
        a2 = self.p.write(b'two')
        self.p.commit_root_address(a2)
        a3 = self.p.write(b'three')
        eq_(self.p.get_root_address(), a2)
        a4 = self.p.write(b'four')
        self.p.commit_root_address(a4)
        eq_(self.p.read(a1), b'one')
        eq_(self.p.read(a2), b'two')
        eq_(self.p.read(a3), b'three')
        eq_(self.p.read(a4), b'four')
        eq_(self.p.get_root_address(), a4)
Example #5
0
 def __init__(self, f):
     self._storage = Storage(f)
     self._tree = BinaryTree(self._storage)
Example #6
0
 def __init__(self, f):
     self._storage = Storage(f)
     self._tree = BinaryTree(self._storage)
Example #7
0
 def setup(self):
     self.f = tempfile.NamedTemporaryFile()
     self.p = Storage(self.f)
Example #8
0
class TestStorage(object):
    def setup(self):
        self.f = tempfile.NamedTemporaryFile()
        self.p = Storage(self.f)

    def _get_superblock_and_data(self, value):
        superblock = value[: Storage.SUPERBLOCK_SIZE]
        data = value[Storage.SUPERBLOCK_SIZE :]
        return superblock, data

    def _get_f_contents(self):
        self.f.flush()
        with open(self.f.name, "rb") as f:
            return f.read()

    def test_init_ensures_superblock(self):
        EMPTY_SUPERBLOCK = b"\x00" * Storage.SUPERBLOCK_SIZE
        self.f.seek(0, os.SEEK_END)
        value = self._get_f_contents()
        eq_(value, EMPTY_SUPERBLOCK)

    def test_write(self):
        self.p.write(b"ABCDE")
        value = self._get_f_contents()
        superblock, data = self._get_superblock_and_data(value)
        eq_(data, b"\x00\x00\x00\x00\x00\x00\x00\x05ABCDE")

    def test_read(self):
        self.f.seek(Storage.SUPERBLOCK_SIZE)
        self.f.write(b"\x00\x00\x00\x00\x00\x00\x00\x0801234567")
        value = self.p.read(Storage.SUPERBLOCK_SIZE)
        eq_(value, b"01234567")

    def test_commit_root_address(self):
        self.p.commit_root_address(257)
        root_bytes = self._get_f_contents()[:8]
        eq_(root_bytes, b"\x00\x00\x00\x00\x00\x00\x01\x01")

    def test_get_root_address(self):
        self.f.seek(0)
        self.f.write(b"\x00\x00\x00\x00\x00\x00\x02\x02")
        root_address = self.p.get_root_address()
        eq_(root_address, 514)

    def test_workflow(self):
        a1 = self.p.write(b"one")
        a2 = self.p.write(b"two")
        self.p.commit_root_address(a2)
        a3 = self.p.write(b"three")
        eq_(self.p.get_root_address(), a2)
        a4 = self.p.write(b"four")
        self.p.commit_root_address(a4)
        eq_(self.p.read(a1), b"one")
        eq_(self.p.read(a2), b"two")
        eq_(self.p.read(a3), b"three")
        eq_(self.p.read(a4), b"four")
        eq_(self.p.get_root_address(), a4)