cache.py 3.3 KB

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