diff options
Diffstat (limited to 'service')
-rw-r--r-- | service/pixelated/adapter/tag_index.py | 18 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mailbox_test.py | 3 | ||||
-rw-r--r-- | service/test/adapter/tag_index_test.py | 5 |
3 files changed, 13 insertions, 13 deletions
diff --git a/service/pixelated/adapter/tag_index.py b/service/pixelated/adapter/tag_index.py index 3cdeb1d8..357f7513 100644 --- a/service/pixelated/adapter/tag_index.py +++ b/service/pixelated/adapter/tag_index.py @@ -24,14 +24,17 @@ class TagIndex: Manages an index for mail's tags using a file storage. """ + __db_instances = dict() + def __init__(self, db_path): self.db_path = db_path - self.db = dbm.open(db_path, 'c') - atexit.register(self.close_db) + if db_path not in TagIndex.__db_instances: + TagIndex.__db_instances[db_path] = dbm.open(db_path, 'c') + self.db = TagIndex.__db_instances[db_path] + atexit.register(self._close_db) def set(self, tag): self.db[tag.name] = tag.as_json_string() - self._flush() def add(self, tag): if tag.name not in self.db: @@ -46,7 +49,6 @@ class TagIndex: def remove(self, tag_name): if tag_name in self.db: del self.db[tag_name] - self._flush() def empty(self): return len(self.db.keys()) == 0 @@ -54,9 +56,7 @@ class TagIndex: def values(self): return set(self.get(key) for key in self.db.keys()) - def close_db(self): + def _close_db(self): self.db.close() - - def _flush(self): - self.close_db() - self.db = dbm.open(self.db_path, 'c') + if self.db_path in TagIndex.__db_instances: + del TagIndex.__db_instances[self.db_path] diff --git a/service/test/adapter/pixelated_mailbox_test.py b/service/test/adapter/pixelated_mailbox_test.py index 7e8e9d3c..72e6141d 100644 --- a/service/test/adapter/pixelated_mailbox_test.py +++ b/service/test/adapter/pixelated_mailbox_test.py @@ -28,6 +28,7 @@ class TestPixelatedMailbox(unittest.TestCase): self.db_file_path = '/tmp/test_tags' def tearDown(self): + TagIndex(self.db_file_path)._close_db() os.remove(self.db_file_path + '.db') def test_special_tags_always_exists(self): @@ -38,7 +39,6 @@ class TestPixelatedMailbox(unittest.TestCase): tag_index = TagIndex(self.db_file_path) tag_index.set(Tag('one_tag')) tag_index.set(Tag('two_tag')) - tag_index.close_db() mailbox = PixelatedMailbox(test_helper.leap_mailbox(), self.db_file_path) expected_tags = set([Tag('one_tag'), Tag('two_tag')]).union(mailbox.SPECIAL_TAGS) self.assertEquals(expected_tags, mailbox.all_tags()) @@ -48,7 +48,6 @@ class TestPixelatedMailbox(unittest.TestCase): tag = Tag('one_tag') tag.increment(12) tag_index.set(tag) - tag_index.close_db() mailbox = PixelatedMailbox(test_helper.leap_mailbox(), self.db_file_path) self.assertEquals(0, mailbox.tag_index.get('inbox').total) self.assertEquals(1, mailbox.tag_index.get('one_tag').total) diff --git a/service/test/adapter/tag_index_test.py b/service/test/adapter/tag_index_test.py index 515c9571..21564fc5 100644 --- a/service/test/adapter/tag_index_test.py +++ b/service/test/adapter/tag_index_test.py @@ -16,6 +16,7 @@ # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. import unittest import os +import uuid from pixelated.adapter.tag_index import TagIndex from pixelated.adapter.tag import Tag @@ -24,11 +25,11 @@ from pixelated.adapter.tag import Tag class TestTagIndex(unittest.TestCase): def setUp(self): - self.db_path = '/tmp/database_test_tag_index' + self.db_path = '/tmp/test_tag_index_' + str(uuid.uuid4()) self.tag_index = TagIndex(self.db_path) def tearDown(self): - self.tag_index.close_db() + self.tag_index._close_db() os.remove(self.db_path + '.db') def test_get_and_set_works(self): |