mirror of
https://github.com/borgbackup/borg.git
synced 2025-02-06 22:43:48 +00:00
Store cleanup
This commit is contained in:
parent
9d20d44b6d
commit
e5fad313f7
4 changed files with 33 additions and 21 deletions
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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()
|
|
@ -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__':
|
||||
|
|
Loading…
Reference in a new issue