Browse Source

Store cleanup

Jonas Borgström 14 years ago
parent
commit
e5fad313f7
4 changed files with 33 additions and 21 deletions
  1. 2 3
      dedupestore/archiver.py
  2. 1 1
      dedupestore/cache.py
  3. 28 15
      dedupestore/store.py
  4. 2 2
      dedupestore/test.py

+ 2 - 3
dedupestore/archiver.py

@@ -4,7 +4,7 @@ import os
 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 @@ from .helpers import location_validator, pretty_size, LevelFilter
 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

+ 1 - 1
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:

+ 28 - 15
dedupestore/bandstore.py → dedupestore/store.py

@@ -3,13 +3,12 @@ import tempfile
 import shutil
 import unittest
 import sqlite3
-import uuid
 import fcntl
 
 Binary = sqlite3.Binary
 
 
-class BandStore(object):
+class Store(object):
     """
     """
 
@@ -45,6 +44,15 @@ class BandStore(object):
         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 @@ class BandStore(object):
         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 @@ class BandStore(object):
         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 @@ class BandStore(object):
                 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 @@ class BandStore(object):
             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 @@ class BandStoreTestCase(unittest.TestCase):
 
 
 def suite():
-    return unittest.TestLoader().loadTestsFromTestCase(BandStoreTestCase)
+    return unittest.TestLoader().loadTestsFromTestCase(StoreTestCase)
 
 if __name__ == '__main__':
     unittest.main()

+ 2 - 2
dedupestore/test.py

@@ -4,7 +4,7 @@ import tempfile
 import unittest
 
 from .archiver import Archiver
-from . import bandstore
+from . import store
 
 
 class Test(unittest.TestCase):
@@ -63,7 +63,7 @@ class Test(unittest.TestCase):
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.TestLoader().loadTestsFromTestCase(Test))
-    suite.addTest(bandstore.suite())
+    suite.addTest(store.suite())
     return suite
 
 if __name__ == '__main__':