Persist cache info.

This commit is contained in:
Jonas Borgström 2010-02-22 21:29:31 +01:00
parent 246e3fa00e
commit 1d76aab21e
2 changed files with 33 additions and 6 deletions

View File

@ -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()

View File

@ -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))