borg/dedupestore/cache.py

105 lines
3.0 KiB
Python
Raw Normal View History

2010-03-06 17:25:35 +00:00
import hashlib
2010-10-20 19:08:46 +00:00
import logging
import msgpack
2010-10-20 19:08:46 +00:00
import os
2010-10-20 17:59:15 +00:00
import zlib
2010-03-06 17:25:35 +00:00
from .helpers import pack, unpack
2010-10-19 19:08:42 +00:00
NS_ARCHIVES = 'A'
NS_CHUNKS = 'C'
NS_CINDEX = 'I'
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 = msgpack.unpackb(zlib.decompress(data))
2010-10-20 19:08:46 +00:00
version = data.get('version')
if version != 1:
logging.error('Unsupported cache version %r' % version)
return
if data['store'] != 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.tid = data['tid']
def init(self):
"""Initializes cache by fetching and reading all archive indicies
"""
logging.info('Initializing cache...')
2010-03-06 17:25:35 +00:00
self.chunkmap = {}
self.tid = self.store.tid
if self.store.tid == 0:
return
for id in list(self.store.list(NS_CINDEX)):
cindex = unpack(self.store.get(NS_CINDEX, id))
for id, size in cindex['chunks']:
2010-10-20 18:28:29 +00:00
try:
count, size = self.chunkmap[id]
self.chunkmap[id] = count + 1, size
except KeyError:
self.chunkmap[id] = 1, size
self.save()
2010-03-06 17:25:35 +00:00
def save(self):
assert self.store.state == self.store.OPEN
2010-10-20 19:08:46 +00:00
data = {'version': 1,
'store': self.store.uuid,
'chunkmap': self.chunkmap,
'tid': self.store.tid,
}
2010-03-06 17:25:35 +00:00
cachedir = os.path.dirname(self.path)
if not os.path.exists(cachedir):
os.makedirs(cachedir)
with open(self.path, 'wb') as fd:
data = zlib.compress(msgpack.packb(data))
2010-10-15 20:18:22 +00:00
id = hashlib.sha256(data).digest()
fd.write(id + data)
2010-03-06 17:25:35 +00:00
2010-10-20 18:28:29 +00:00
def add_chunk(self, id, data):
if self.seen_chunk(id):
return self.chunk_incref(id)
_, data = pack(data)
csize = len(data)
self.store.put(NS_CHUNKS, id, data)
2010-10-20 19:08:46 +00:00
self.chunkmap[id] = (1, csize)
return csize
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)
2010-10-20 18:28:29 +00:00
return 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)