borg/dedupestore/cache.py

118 lines
3.6 KiB
Python
Raw Normal View History

2010-03-06 17:25:35 +00:00
import hashlib
import os
import zlib
from avro import io
from cStringIO import StringIO
import cPickle
from dedupestore import archive_schema
2010-03-06 17:25:35 +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
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
2010-10-15 20:18:22 +00:00
data = open(self.path, 'rb').read()
id = data[:32]
data = data[32:]
if hashlib.sha256(data).digest() != id:
raise Exception('Cache hash did not match')
data = cPickle.loads(zlib.decompress(data))
2010-03-06 17:25:35 +00:00
if data['uuid'] != self.store.uuid:
2010-10-15 20:18:22 +00:00
raise Exception('Cache UUID mismatch')
2010-03-06 17:25:35 +00:00
self.chunkmap = data['chunkmap']
self.archives = data['archives']
self.tid = data['tid']
def init(self):
"""Initializes cache by fetching and reading all archive indicies
"""
self.chunkmap = {}
2010-10-15 20:18:22 +00:00
self.archives = {}
2010-03-06 17:25:35 +00:00
self.tid = self.store.tid
if self.store.tid == 0:
return
2010-10-15 17:33:15 +00:00
for id in list(self.store.list(NS_ARCHIVES)):
2010-10-15 20:18:22 +00:00
data = self.store.get(NS_ARCHIVES, id)
if hashlib.sha256(data).digest() != id:
raise Exception('Archive hash did not match')
buffer = StringIO(zlib.decompress(data))
reader = io.DatumReader(archive_schema)
decoder = io.BinaryDecoder(buffer)
archive = reader.read(decoder)
2010-10-15 20:18:22 +00:00
self.archives[archive['name']] = id
2010-10-17 16:05:46 +00:00
for item in archive['items']:
if item['type'] != 'FILE':
continue
for idx in item['chunks']:
chunk = archive['chunks'][idx]
id = chunk['id']
if self.seen_chunk(id):
self.chunk_incref(id)
else:
self.init_chunk(id, chunk['size'])
self.save()
2010-03-06 17:25:35 +00:00
def save(self):
assert self.store.state == self.store.OPEN
2010-04-18 20:08:12 +00:00
data = {'uuid': self.store.uuid,
'chunkmap': self.chunkmap,
2010-03-06 17:25:35 +00:00
'tid': self.store.tid, 'archives': self.archives}
cachedir = os.path.dirname(self.path)
if not os.path.exists(cachedir):
os.makedirs(cachedir)
with open(self.path, 'wb') as fd:
2010-10-15 20:18:22 +00:00
data = zlib.compress(cPickle.dumps(data))
id = hashlib.sha256(data).digest()
fd.write(id + data)
2010-03-06 17:25:35 +00:00
def add_chunk(self, data):
2010-10-15 20:18:22 +00:00
id = hashlib.sha256(data).digest()
if self.seen_chunk(id):
return self.chunk_incref(id)
2010-10-15 20:18:22 +00:00
osize = len(data)
data = zlib.compress(data)
data = hashlib.sha256(data).digest() + data
csize = len(data)
self.store.put(NS_CHUNKS, id, data)
return self.init_chunk(id, csize)
def init_chunk(self, id, size):
self.chunkmap[id] = (1, size)
return id, size
2010-03-06 17:25:35 +00:00
def seen_chunk(self, id):
count, size = self.chunkmap.get(id, (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):
count, size = self.chunkmap[id]
self.chunkmap[id] = (count + 1, size)
return id, size
2010-03-06 17:25:35 +00:00
def chunk_decref(self, id):
count, size = self.chunkmap[id]
if count == 1:
2010-03-06 17:25:35 +00:00
del self.chunkmap[id]
self.store.delete(NS_CHUNKS, id)
else:
self.chunkmap[id] = (count - 1, size)