From e5fad313f77b1a2e8beb3c5436ce5aac037415e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Borgstr=C3=B6m?= Date: Tue, 26 Oct 2010 21:25:25 +0200 Subject: [PATCH] Store cleanup --- dedupestore/archiver.py | 5 ++- dedupestore/cache.py | 2 +- dedupestore/{bandstore.py => store.py} | 43 +++++++++++++++++--------- dedupestore/test.py | 4 +-- 4 files changed, 33 insertions(+), 21 deletions(-) rename dedupestore/{bandstore.py => store.py} (87%) diff --git a/dedupestore/archiver.py b/dedupestore/archiver.py index bae34d68f..0980b15a9 100644 --- a/dedupestore/archiver.py +++ b/dedupestore/archiver.py @@ -4,7 +4,7 @@ import sys from .archive import Archive -from .bandstore import BandStore +from .store import Store from .cache import Cache from .crypto import CryptoManager, KeyChain from .helpers import location_validator, pretty_size, LevelFilter @@ -13,8 +13,7 @@ class Archiver(object): def open_store(self, location): - store = BandStore(location.path) - return store + return Store(location.path) def exit_code_from_logger(self): return 1 if self.level_filter.count.get('ERROR') else 0 diff --git a/dedupestore/cache.py b/dedupestore/cache.py index ed957a67f..47172cc26 100644 --- a/dedupestore/cache.py +++ b/dedupestore/cache.py @@ -13,7 +13,7 @@ class Cache(object): def __init__(self, store, crypto): self.store = store self.path = os.path.join(os.path.expanduser('~'), '.dedupestore', 'cache', - '%s.cache' % self.store.uuid) + '%s.cache' % self.store.id.encode('hex')) self.tid = -1 self.open() if self.tid != self.store.tid: diff --git a/dedupestore/bandstore.py b/dedupestore/store.py similarity index 87% rename from dedupestore/bandstore.py rename to dedupestore/store.py index a8a1620bd..eb78dc38b 100644 --- a/dedupestore/bandstore.py +++ b/dedupestore/store.py @@ -3,13 +3,12 @@ import shutil import unittest import sqlite3 -import uuid import fcntl Binary = sqlite3.Binary -class BandStore(object): +class Store(object): """ """ @@ -45,6 +44,15 @@ def open(self, path): self.cursor = self.cnx.cursor() self._begin() + def get_option(self, key): + return self.cursor.execute('SELECT value FROM system WHERE key=?', (key,)) \ + .fetchone()[0] + + def set_option(self, key, value): + return self.cursor.execute('UPDATE system SET value=? WHERE key=?', + (value, key)) + + def _begin(self): if self.read_fd: self.read_fd.close() @@ -52,10 +60,12 @@ def _begin(self): if self.write_fd: self.write_fd.close() self.write_fd = None - row = self.cursor.execute('SELECT uuid, tid, nextband, version, ' - 'bandlimit FROM system').fetchone() - self.uuid, self.tid, self.nextband, version, self.bandlimit = row - assert version == 1 + self.version = self.get_option('version') + self.id = self.get_option('id').decode('hex') + self.tid = self.get_option('tid') + self.nextband = self.get_option('nextband') + self.bandlimit = self.get_option('bandlimit') + assert self.version == 1 self.state = self.OPEN self.read_band = None self.write_band = None @@ -72,10 +82,13 @@ def create(self, path): cnx.execute('CREATE TABLE objects(ns BINARY NOT NULL, id BINARY NOT NULL, ' 'band NOT NULL, offset NOT NULL, size NOT NULL)') cnx.execute('CREATE UNIQUE INDEX objects_pk ON objects(ns, id)') - cnx.execute('CREATE TABLE system(uuid NOT NULL, tid NOT NULL, ' - 'nextband NOT NULL, version NOT NULL, bandlimit NOT NULL)') - cnx.execute('INSERT INTO system VALUES(?,?,?,?,?)', - (uuid.uuid1().hex, 0, 0, 1, self.BAND_LIMIT)) + cnx.execute('CREATE TABLE system(key UNIQUE NOT NULL, value)') + cnx.executemany('INSERT INTO system VALUES(?, ?)', + (('id', os.urandom(32).encode('hex')), + ('version', 1), + ('tid', 0), + ('nextband', 0), + ('bandlimit', self.BAND_LIMIT))) cnx.commit() def close(self): @@ -102,8 +115,8 @@ def delete_bands(self): self.cursor.execute('UPDATE objects SET band=?, offset=?, size=? ' 'WHERE ns=? AND id=?', (band, offset, size, Binary(o[0]), Binary(o[1]))) - self.cursor.execute('UPDATE system SET tid=tid+1, nextband=?', - (self.nextband,)) + self.set_option('tid', self.tid + 1) + self.set_option('nextband', self.nextband) self.cnx.commit() for b in self.to_delete: os.unlink(self.band_filename(b)) @@ -195,11 +208,11 @@ def list(self, ns, prefix='', marker=None, max_keys=1000000): yield str(row[0]) -class BandStoreTestCase(unittest.TestCase): +class StoreTestCase(unittest.TestCase): def setUp(self): self.tmppath = tempfile.mkdtemp() - self.store = BandStore(os.path.join(self.tmppath, 'store')) + self.store = Store(os.path.join(self.tmppath, 'store')) def tearDown(self): shutil.rmtree(self.tmppath) @@ -249,7 +262,7 @@ def test_list(self): def suite(): - return unittest.TestLoader().loadTestsFromTestCase(BandStoreTestCase) + return unittest.TestLoader().loadTestsFromTestCase(StoreTestCase) if __name__ == '__main__': unittest.main() diff --git a/dedupestore/test.py b/dedupestore/test.py index 52e6fbea4..030885bb9 100644 --- a/dedupestore/test.py +++ b/dedupestore/test.py @@ -4,7 +4,7 @@ import unittest from .archiver import Archiver -from . import bandstore +from . import store class Test(unittest.TestCase): @@ -63,7 +63,7 @@ def test_symlinks(self): def suite(): suite = unittest.TestSuite() suite.addTest(unittest.TestLoader().loadTestsFromTestCase(Test)) - suite.addTest(bandstore.suite()) + suite.addTest(store.suite()) return suite if __name__ == '__main__':