From b6b6a9a5c39dd656b7819038bb05892c6d5bd3d1 Mon Sep 17 00:00:00 2001 From: Patrick Maia Date: Mon, 8 Sep 2014 01:35:59 +0000 Subject: #51 - reuses dbm instances between TagIndex instances if the dbm instances are using the same file --- service/pixelated/adapter/tag_index.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'service/pixelated') 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] -- cgit v1.2.3