2010-03-06 17:25:35 +00:00
|
|
|
import cPickle
|
|
|
|
import hashlib
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import zlib
|
|
|
|
|
2010-05-10 20:35:46 +00:00
|
|
|
NS_ARCHIVES = 'ARCHIVES'
|
|
|
|
NS_CHUNKS = 'CHUNKS'
|
2010-03-06 17:25:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Cache(object):
|
|
|
|
"""Client Side cache
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, store):
|
|
|
|
self.store = store
|
2010-10-13 20:07:55 +00:00
|
|
|
self.path = os.path.join(os.path.expanduser('~'), '.dedupestore', 'cache',
|
2010-03-06 17:25:35 +00:00
|
|
|
'%s.cache' % self.store.uuid)
|
|
|
|
self.tid = -1
|
|
|
|
self.open()
|
|
|
|
if self.tid != self.store.tid:
|
|
|
|
self.init()
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
if not os.path.exists(self.path):
|
|
|
|
return
|
|
|
|
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
|
|
|
|
self.chunkmap = data['chunkmap']
|
|
|
|
self.archives = data['archives']
|
|
|
|
self.tid = data['tid']
|
|
|
|
print 'done'
|
|
|
|
|
|
|
|
def init(self):
|
|
|
|
"""Initializes cache by fetching and reading all archive indicies
|
|
|
|
"""
|
|
|
|
self.chunkmap = {}
|
|
|
|
self.archives = []
|
|
|
|
self.tid = self.store.tid
|
|
|
|
if self.store.tid == 0:
|
|
|
|
return
|
|
|
|
print 'Recreating cache...'
|
2010-10-15 17:33:15 +00:00
|
|
|
for id in list(self.store.list(NS_ARCHIVES)):
|
2010-03-06 17:25:35 +00:00
|
|
|
archive = cPickle.loads(zlib.decompress(self.store.get(NS_ARCHIVES, id)))
|
|
|
|
self.archives.append(archive['name'])
|
2010-10-15 17:33:15 +00:00
|
|
|
for id, csize, osize in archive['chunks']:
|
2010-03-15 20:23:34 +00:00
|
|
|
if self.seen_chunk(id):
|
|
|
|
self.chunk_incref(id)
|
|
|
|
else:
|
2010-10-13 20:07:55 +00:00
|
|
|
self.init_chunk(id, csize, osize)
|
2010-03-06 17:25:35 +00:00
|
|
|
print 'done'
|
|
|
|
|
|
|
|
def save(self):
|
2010-05-10 20:35:46 +00:00
|
|
|
assert self.store.state == self.store.OPEN
|
2010-03-06 17:25:35 +00:00
|
|
|
print 'saving cache'
|
2010-04-18 20:08:12 +00:00
|
|
|
data = {'uuid': self.store.uuid,
|
2010-10-13 20:07:55 +00:00
|
|
|
'chunkmap': self.chunkmap,
|
2010-03-06 17:25:35 +00:00
|
|
|
'tid': self.store.tid, 'archives': self.archives}
|
|
|
|
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'
|
|
|
|
|
|
|
|
def add_chunk(self, data):
|
2010-03-15 20:23:34 +00:00
|
|
|
osize = len(data)
|
2010-03-06 17:25:35 +00:00
|
|
|
data = zlib.compress(data)
|
2010-03-15 20:23:34 +00:00
|
|
|
id = hashlib.sha1(data).digest()
|
|
|
|
if self.seen_chunk(id):
|
|
|
|
return self.chunk_incref(id)
|
|
|
|
csize = len(data)
|
|
|
|
self.store.put(NS_CHUNKS, id, data)
|
2010-10-13 20:07:55 +00:00
|
|
|
return self.init_chunk(id, csize, osize)
|
2010-03-15 20:23:34 +00:00
|
|
|
|
2010-10-13 20:07:55 +00:00
|
|
|
def init_chunk(self, id, csize, osize):
|
|
|
|
self.chunkmap[id] = (1, csize, osize)
|
|
|
|
return id, csize, osize
|
2010-03-06 17:25:35 +00:00
|
|
|
|
2010-03-15 20:23:34 +00:00
|
|
|
def seen_chunk(self, id):
|
2010-10-13 20:07:55 +00:00
|
|
|
count, csize, osize = self.chunkmap.get(id, (0, 0, 0))
|
2010-04-18 20:34:21 +00:00
|
|
|
return count
|
2010-03-06 17:25:35 +00:00
|
|
|
|
|
|
|
def chunk_incref(self, id):
|
2010-10-13 20:07:55 +00:00
|
|
|
count, csize, osize = self.chunkmap[id]
|
|
|
|
self.chunkmap[id] = (count + 1, csize, osize)
|
|
|
|
return id, csize, osize
|
2010-03-06 17:25:35 +00:00
|
|
|
|
|
|
|
def chunk_decref(self, id):
|
2010-10-13 20:07:55 +00:00
|
|
|
count, csize, osize = self.chunkmap[id]
|
2010-03-15 20:23:34 +00:00
|
|
|
if count == 1:
|
2010-03-06 17:25:35 +00:00
|
|
|
del self.chunkmap[id]
|
|
|
|
self.store.delete(NS_CHUNKS, id)
|
2010-03-15 20:23:34 +00:00
|
|
|
else:
|
2010-10-13 20:07:55 +00:00
|
|
|
self.chunkmap[id] = (count - 1, csize, osize)
|
|
|
|
|
|
|
|
|