From 60126b6330f37d333b729ed1c9455b502cf8dca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Borgstr=C3=B6m?= Date: Mon, 1 Mar 2010 23:02:40 +0100 Subject: [PATCH] Some code refactoring. --- dedupestore/archiver.py | 40 +++++++++++++++++++-------------------- dedupestore/chunkifier.py | 4 ++-- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/dedupestore/archiver.py b/dedupestore/archiver.py index 8d82d1dfb..a88867c69 100644 --- a/dedupestore/archiver.py +++ b/dedupestore/archiver.py @@ -17,30 +17,32 @@ NS_CHUNKS = 'CHUNKS' class Cache(object): """Client Side cache """ - def __init__(self, path, store): + def __init__(self, store): self.store = store - self.path = path + self.path = os.path.join(os.path.expanduser('~'), '.dedupestore', 'cache', + '%s.cache' % self.store.uuid) self.tid = -1 self.open() if self.tid != self.store.tid: - print self.tid, self.store.tid - self.create() + self.init() def open(self): - if self.store.tid == -1: + if not os.path.exists(self.path): return - filename = os.path.join(self.path, '%s.cache' % self.store.uuid) - if not os.path.exists(filename): + print 'Loading cache: ', self.path, '...' + data = cPickle.loads(zlib.decompress(open(self.path, 'rb').read())) + if data['uuid'] != self.store.uuid: + print >> sys.stderr, 'Cache UUID mismatch' return - print 'Loading cache: ', filename, '...' - data = cPickle.loads(zlib.decompress(open(filename, 'rb').read())) self.chunkmap = data['chunkmap'] self.summap = data['summap'] self.archives = data['archives'] self.tid = data['tid'] print 'done' - def create(self): + def init(self): + """Initializes cache by fetching and reading all archive indicies + """ self.summap = {} self.chunkmap = {} self.archives = [] @@ -60,11 +62,14 @@ class Cache(object): def save(self): assert self.store.state == Store.OPEN print 'saving cache' - data = {'chunkmap': self.chunkmap, 'summap': self.summap, + data = {'uuid': self.store.uuid, + 'chunkmap': self.chunkmap, 'summap': self.summap, 'tid': self.store.tid, 'archives': self.archives} - filename = os.path.join(self.path, '%s.cache' % self.store.uuid) - print 'Saving cache as:', filename - with open(filename, 'wb') as fd: + print 'Saving cache as:', self.path + cachedir = os.path.dirname(self.path) + if not os.path.exists(cachedir): + os.makedirs(cachedir) + with open(self.path, 'wb') as fd: fd.write(zlib.compress(cPickle.dumps(data))) print 'done' @@ -221,8 +226,6 @@ class Archiver(object): def run(self): parser = OptionParser() - parser.add_option("-C", "--cache", dest="cache", - help="cache directory to use", metavar="CACHE") parser.add_option("-s", "--store", dest="store", help="path to dedupe store", metavar="STORE") parser.add_option("-c", "--create", dest="create_archive", @@ -243,10 +246,7 @@ class Archiver(object): self.store = Store(options.store) else: parser.error('No store path specified') - if options.cache: - self.cache = Cache(options.cache, self.store) - else: - parser.error('No cache path specified') + self.cache = Cache(self.store) if options.list_archives: self.list_archives() elif options.list_archive: diff --git a/dedupestore/chunkifier.py b/dedupestore/chunkifier.py index cf51bc2ad..30e5f4e95 100644 --- a/dedupestore/chunkifier.py +++ b/dedupestore/chunkifier.py @@ -73,6 +73,8 @@ class ChunkifyIter(object): else: self.done = True return self.data[self.i:] + elif o == self.chunk_size: + return self.data[self.i-self.chunk_size:self.i] elif self.sum in self.chunks: if o > 0: chunk = self.data[self.i - o:self.i] @@ -81,8 +83,6 @@ class ChunkifyIter(object): self.i += self.chunk_size self.full_sum = True return chunk - elif o == self.chunk_size: - return self.data[self.i-self.chunk_size:self.i] else: self.i += 1 o += 1