cache.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import hashlib
  2. import logging
  3. import msgpack
  4. import os
  5. import zlib
  6. from .helpers import pack, unpack
  7. NS_ARCHIVES = 'A'
  8. NS_CHUNKS = 'C'
  9. NS_CINDEX = 'I'
  10. class Cache(object):
  11. """Client Side cache
  12. """
  13. def __init__(self, store):
  14. self.store = store
  15. self.path = os.path.join(os.path.expanduser('~'), '.dedupestore', 'cache',
  16. '%s.cache' % self.store.uuid)
  17. self.tid = -1
  18. self.open()
  19. if self.tid != self.store.tid:
  20. self.init()
  21. def open(self):
  22. if not os.path.exists(self.path):
  23. return
  24. data = open(self.path, 'rb').read()
  25. id = data[:32]
  26. data = data[32:]
  27. if hashlib.sha256(data).digest() != id:
  28. raise Exception('Cache hash did not match')
  29. data = msgpack.unpackb(zlib.decompress(data))
  30. version = data.get('version')
  31. if version != 1:
  32. logging.error('Unsupported cache version %r' % version)
  33. return
  34. if data['store'] != self.store.uuid:
  35. raise Exception('Cache UUID mismatch')
  36. self.chunkmap = data['chunkmap']
  37. self.tid = data['tid']
  38. def init(self):
  39. """Initializes cache by fetching and reading all archive indicies
  40. """
  41. logging.info('Initializing cache...')
  42. self.chunkmap = {}
  43. self.tid = self.store.tid
  44. if self.store.tid == 0:
  45. return
  46. for id in list(self.store.list(NS_CINDEX)):
  47. cindex = unpack(self.store.get(NS_CINDEX, id))
  48. for id, size in cindex['chunks']:
  49. try:
  50. count, size = self.chunkmap[id]
  51. self.chunkmap[id] = count + 1, size
  52. except KeyError:
  53. self.chunkmap[id] = 1, size
  54. self.save()
  55. def save(self):
  56. assert self.store.state == self.store.OPEN
  57. data = {'version': 1,
  58. 'store': self.store.uuid,
  59. 'chunkmap': self.chunkmap,
  60. 'tid': self.store.tid,
  61. }
  62. cachedir = os.path.dirname(self.path)
  63. if not os.path.exists(cachedir):
  64. os.makedirs(cachedir)
  65. with open(self.path, 'wb') as fd:
  66. data = zlib.compress(msgpack.packb(data))
  67. id = hashlib.sha256(data).digest()
  68. fd.write(id + data)
  69. def add_chunk(self, id, data):
  70. if self.seen_chunk(id):
  71. return self.chunk_incref(id)
  72. _, data = pack(data)
  73. csize = len(data)
  74. self.store.put(NS_CHUNKS, id, data)
  75. self.chunkmap[id] = (1, csize)
  76. return csize
  77. def seen_chunk(self, id):
  78. count, size = self.chunkmap.get(id, (0, 0))
  79. return count
  80. def chunk_incref(self, id):
  81. count, size = self.chunkmap[id]
  82. self.chunkmap[id] = (count + 1, size)
  83. return size
  84. def chunk_decref(self, id):
  85. count, size = self.chunkmap[id]
  86. if count == 1:
  87. del self.chunkmap[id]
  88. self.store.delete(NS_CHUNKS, id)
  89. else:
  90. self.chunkmap[id] = (count - 1, size)