From 1d76aab21ea1ab3cb8e1f6de0bca7fe9f0782a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Borgstr=C3=B6m?= Date: Mon, 22 Feb 2010 21:29:31 +0100 Subject: [PATCH] Persist cache info. --- dedupstore/archiver.py | 32 +++++++++++++++++++++++++++++--- dedupstore/repository.py | 7 ++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/dedupstore/archiver.py b/dedupstore/archiver.py index a7f5593d0..aef0e60db 100644 --- a/dedupstore/archiver.py +++ b/dedupstore/archiver.py @@ -13,13 +13,30 @@ class Cache(object): """ def __init__(self, path, repo): self.repo = repo + self.path = path self.chunkmap = {} self.archives = [] - self.open(path) + self.tid = -1 + self.open() + if self.tid != self.repo.tid: + print self.tid, self.repo.tid + self.create() - def open(self, path): + def open(self): if self.repo.tid == 0: return + filename = os.path.join(self.path, '%s.cache' % self.repo.uuid) + if not os.path.exists(filename): + return + print 'Reading cache: ', filename, '...' + data = cPickle.loads(zlib.decompress(open(filename, 'rb').read())) + self.chunkmap = data['chunkmap'] + self.tid = data['tid'] + self.archives = data['archives'] + print 'done' + + def create(self): + print 'Recreating cache...' for archive in self.repo.listdir('archives'): self.archives.append(archive) data = self.repo.get_file(os.path.join('archives', archive)) @@ -27,11 +44,19 @@ class Cache(object): for item in a['items']: if item['type'] == 'FILE': for c in item['chunks']: - print 'adding chunk', c.encode('hex') self.chunk_incref(c) + self.tid = self.repo.tid + print 'done' def save(self): assert self.repo.state == Repository.OPEN + print 'saving',self.tid, self.repo.tid + data = {'chunkmap': self.chunkmap, 'tid': self.repo.tid, 'archives': self.archives} + filename = os.path.join(self.path, '%s.cache' % self.repo.uuid) + print 'Saving cache as:', filename + with open(filename, 'wb') as fd: + fd.write(zlib.compress(cPickle.dumps(data))) + print 'done' def chunk_filename(self, sha): hex = sha.encode('hex') @@ -78,6 +103,7 @@ class Archiver(object): archive = {'name': name, 'items': items} zdata = zlib.compress(cPickle.dumps(archive)) self.repo.put_file(os.path.join('archives', archive_name), zdata) + self.cache.archives.append(archive_name) print 'Archive file size: %d' % len(zdata) self.repo.commit() self.cache.save() diff --git a/dedupstore/repository.py b/dedupstore/repository.py index f205afab4..9e996d92a 100644 --- a/dedupstore/repository.py +++ b/dedupstore/repository.py @@ -10,7 +10,6 @@ import uuid log = logging.getLogger('') -# FIXME: UUID class Repository(object): """ @@ -103,6 +102,7 @@ class Repository(object): os.rename(os.path.join(self.path, 'txn-commit'), os.path.join(self.path, 'txn-applied')) shutil.rmtree(os.path.join(self.path, 'txn-applied')) + self.tid = tid self.state = Repository.OPEN def rollback(self): @@ -137,12 +137,13 @@ class Repository(object): """ """ self.prepare_txn() - if os.path.exists(os.path.join(self.path, 'txn-active', 'add', path)): + if (path in self.txn_added or + (path not in self.txn_removed and os.path.exists(os.path.join(self.path, 'data', path)))): raise Exception('FileAlreadyExists: %s' % path) if path in self.txn_removed: self.txn_removed.remove(path) if path not in self.txn_added: - self.txn_added.append(path) + self.txn_added.append(path) filename = os.path.join(self.path, 'txn-active', 'add', path) if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename))