summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2016-03-13 15:02:48 +0200
committerLars Wirzenius <liw@liw.fi>2016-03-13 15:52:23 +0200
commit6e3e2de88c5a0ade97ddbfd325583bddc2bbea74 (patch)
tree514c56863131735ea00799fd66a6b1877b05eeef
parentf4e308bd1882875bcdd6e347e27f6795233b8af6 (diff)
downloadobnam-6e3e2de88c5a0ade97ddbfd325583bddc2bbea74.tar.gz
Add support for well-known objcts in blob stores
-rw-r--r--obnamlib/bag.py6
-rw-r--r--obnamlib/bag_store.py3
-rw-r--r--obnamlib/bag_store_tests.py6
-rw-r--r--obnamlib/bag_tests.py15
-rw-r--r--obnamlib/blob_store.py13
-rw-r--r--obnamlib/blob_store_tests.py26
6 files changed, 65 insertions, 4 deletions
diff --git a/obnamlib/bag.py b/obnamlib/bag.py
index cf176b1b..87bec277 100644
--- a/obnamlib/bag.py
+++ b/obnamlib/bag.py
@@ -1,4 +1,4 @@
-# Copyright 2015 Lars Wirzenius
+# Copyright 2015-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
@@ -30,6 +30,7 @@ class Bag(object):
return self._bag_id
def set_id(self, bag_id):
+ assert isinstance(bag_id, (int, long, str))
self._bag_id = bag_id
def append(self, blob):
@@ -55,6 +56,9 @@ class BagIdNotSetError(obnamlib.ObnamError):
def make_object_id(bag_id, object_index):
+ if isinstance(bag_id, str):
+ assert object_index == 0
+ return '%s.%d' % (bag_id, object_index)
return '%016x.%d' % (bag_id, object_index)
diff --git a/obnamlib/bag_store.py b/obnamlib/bag_store.py
index 0e1fe448..2246f232 100644
--- a/obnamlib/bag_store.py
+++ b/obnamlib/bag_store.py
@@ -32,6 +32,9 @@ class BagStore(object):
self._id_inventor.set_filename_maker(self._make_bag_filename)
def _make_bag_filename(self, bag_id):
+ if isinstance(bag_id, str):
+ return os.path.join(self._dirname, '%s.bag' % bag_id)
+
basename = '%016x' % bag_id
return os.path.join(
self._dirname,
diff --git a/obnamlib/bag_store_tests.py b/obnamlib/bag_store_tests.py
index aa6e6740..49290d09 100644
--- a/obnamlib/bag_store_tests.py
+++ b/obnamlib/bag_store_tests.py
@@ -74,3 +74,9 @@ class BagStoreTests(unittest.TestCase):
self.store.put_bag(self.bag)
self.store.remove_bag(self.bag.get_id())
self.assertEqual(list(self.store.get_bag_ids()), [])
+
+ def test_puts_bag_with_nonnumeric_id(self):
+ self.bag.set_id('well-known')
+ self.store.put_bag(self.bag)
+ returned = self.store.get_bag('well-known')
+ self.assertEqualBags(returned, self.bag)
diff --git a/obnamlib/bag_tests.py b/obnamlib/bag_tests.py
index 1465fc24..9bbffefe 100644
--- a/obnamlib/bag_tests.py
+++ b/obnamlib/bag_tests.py
@@ -1,4 +1,4 @@
-# Copyright 2015 Lars Wirzenius
+# Copyright 2015-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
@@ -27,11 +27,16 @@ class BagTests(unittest.TestCase):
bag = obnamlib.Bag()
self.assertEqual(bag.get_id(), None)
- def test_sets_bag_id(self):
+ def test_sets_bag_id_to_integer(self):
bag = obnamlib.Bag()
bag.set_id(123)
self.assertEqual(bag.get_id(), 123)
+ def test_sets_bag_id_to_string(self):
+ bag = obnamlib.Bag()
+ bag.set_id('well-known')
+ self.assertEqual(bag.get_id(), 'well-known')
+
def test_is_empty_initially(self):
bag = obnamlib.Bag()
self.assertEqual(len(bag), 0)
@@ -61,6 +66,12 @@ class BagTests(unittest.TestCase):
object_id = bag.append('foo')
self.assertEqual(object_id, obnamlib.make_object_id(1, 0))
+ def test_appending_returns_object_id_when_bag_id_is_a_string(self):
+ bag = obnamlib.Bag()
+ bag.set_id('well-known')
+ object_id = bag.append('foo')
+ self.assertEqual(object_id, obnamlib.make_object_id('well-known', 0))
+
class ObjectIdTests(unittest.TestCase):
diff --git a/obnamlib/blob_store.py b/obnamlib/blob_store.py
index 8373fd32..219e585a 100644
--- a/obnamlib/blob_store.py
+++ b/obnamlib/blob_store.py
@@ -64,6 +64,19 @@ class BlobStore(object):
bag.set_id(self._bag_store.reserve_bag_id())
return bag
+ def get_well_known_blob(self, well_known_name):
+ if self._bag_store.has_bag(well_known_name):
+ bag = self._bag_store.get_bag(well_known_name)
+ if len(bag) > 0:
+ return bag[0]
+ return None
+
+ def put_well_known_blob(self, well_known_name, blob):
+ bag = obnamlib.Bag()
+ bag.set_id(well_known_name)
+ bag.append(blob)
+ self._bag_store.put_bag(bag)
+
def flush(self):
if self._bag is not None:
self._bag_store.put_bag(self._bag)
diff --git a/obnamlib/blob_store_tests.py b/obnamlib/blob_store_tests.py
index 079c932b..fed20d14 100644
--- a/obnamlib/blob_store_tests.py
+++ b/obnamlib/blob_store_tests.py
@@ -1,4 +1,4 @@
-# Copyright 2015 Lars Wirzenius
+# Copyright 2015-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
@@ -98,6 +98,30 @@ class BlobStoreTests(unittest.TestCase):
retrieved = blob_store.get_blob(blob_id)
self.assertEqual(blob, retrieved)
+ def test_returns_None_if_well_known_blog_does_not_exist(self):
+ bag_store = DummyBagStore()
+ well_known_id = 'bobby'
+
+ blob_store = obnamlib.BlobStore()
+ blob_store.set_bag_store(bag_store)
+ retrieved = blob_store.get_well_known_blob(well_known_id)
+ self.assertEqual(retrieved, None)
+
+ def test_puts_well_known_blob(self):
+ bag_store = DummyBagStore()
+ well_known_blob = 'Bob'
+ well_known_id = 'bobby'
+
+ blob_store = obnamlib.BlobStore()
+ blob_store.set_bag_store(bag_store)
+ blob_store.set_max_bag_size(len(well_known_blob) + 1)
+ returned_id = blob_store.put_well_known_blob(
+ well_known_id, well_known_blob)
+ self.assertEqual(returned_id, None)
+
+ retrieved = blob_store.get_well_known_blob(well_known_id)
+ self.assertEqual(well_known_blob, retrieved)
+
class DummyBagStore(object):