cache.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import cPickle
  2. import hashlib
  3. import os
  4. import sys
  5. import zlib
  6. NS_ARCHIVES = 'ARCHIVES'
  7. NS_CHUNKS = 'CHUNKS'
  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 = cPickle.loads(zlib.decompress(data))
  28. if data['uuid'] != self.store.uuid:
  29. raise Exception('Cache UUID mismatch')
  30. self.chunkmap = data['chunkmap']
  31. self.archives = data['archives']
  32. self.tid = data['tid']
  33. def init(self):
  34. """Initializes cache by fetching and reading all archive indicies
  35. """
  36. self.chunkmap = {}
  37. self.archives = {}
  38. self.tid = self.store.tid
  39. if self.store.tid == 0:
  40. return
  41. for id in list(self.store.list(NS_ARCHIVES)):
  42. data = self.store.get(NS_ARCHIVES, id)
  43. if hashlib.sha256(data).digest() != id:
  44. raise Exception('Archive hash did not match')
  45. archive = cPickle.loads(zlib.decompress(data))
  46. self.archives[archive['name']] = id
  47. for id, csize, osize in archive['chunks']:
  48. if self.seen_chunk(id):
  49. self.chunk_incref(id)
  50. else:
  51. self.init_chunk(id, csize, osize)
  52. def save(self):
  53. assert self.store.state == self.store.OPEN
  54. data = {'uuid': self.store.uuid,
  55. 'chunkmap': self.chunkmap,
  56. 'tid': self.store.tid, 'archives': self.archives}
  57. cachedir = os.path.dirname(self.path)
  58. if not os.path.exists(cachedir):
  59. os.makedirs(cachedir)
  60. with open(self.path, 'wb') as fd:
  61. data = zlib.compress(cPickle.dumps(data))
  62. id = hashlib.sha256(data).digest()
  63. fd.write(id + data)
  64. def add_chunk(self, data):
  65. id = hashlib.sha256(data).digest()
  66. if self.seen_chunk(id):
  67. return self.chunk_incref(id)
  68. osize = len(data)
  69. data = zlib.compress(data)
  70. data = hashlib.sha256(data).digest() + data
  71. csize = len(data)
  72. self.store.put(NS_CHUNKS, id, data)
  73. return self.init_chunk(id, csize, osize)
  74. def init_chunk(self, id, csize, osize):
  75. self.chunkmap[id] = (1, csize, osize)
  76. return id, csize, osize
  77. def seen_chunk(self, id):
  78. count, csize, osize = self.chunkmap.get(id, (0, 0, 0))
  79. return count
  80. def chunk_incref(self, id):
  81. count, csize, osize = self.chunkmap[id]
  82. self.chunkmap[id] = (count + 1, csize, osize)
  83. return id, csize, osize
  84. def chunk_decref(self, id):
  85. count, csize, osize = 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, csize, osize)