cache.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from itertools import ifilter
  2. import logging
  3. import msgpack
  4. import os
  5. from . import NS_ARCHIVE_CHUNKS, NS_CHUNK
  6. class Cache(object):
  7. """Client Side cache
  8. """
  9. def __init__(self, store, crypto):
  10. self.store = store
  11. self.path = os.path.join(os.path.expanduser('~'), '.dedupestore', 'cache',
  12. '%s.cache' % self.store.id.encode('hex'))
  13. self.tid = -1
  14. self.open()
  15. if self.tid != self.store.tid:
  16. self.init(crypto)
  17. def open(self):
  18. if not os.path.exists(self.path):
  19. return
  20. cache = msgpack.unpackb(open(self.path, 'rb').read())
  21. assert cache['version'] == 1
  22. self.chunk_counts = cache['chunk_counts']
  23. # Discard old file_chunks entries
  24. self.file_chunks = cache['file_chunks']
  25. self.tid = cache['tid']
  26. def init(self, crypto):
  27. """Initializes cache by fetching and reading all archive indicies
  28. """
  29. logging.info('Initializing cache...')
  30. self.chunk_counts = {}
  31. self.file_chunks = {}
  32. self.tid = self.store.tid
  33. if self.store.tid == 0:
  34. return
  35. for id in list(self.store.list(NS_ARCHIVE_CHUNKS)):
  36. data, hash = crypto.decrypt(self.store.get(NS_ARCHIVE_CHUNKS, id))
  37. cindex = msgpack.unpackb(data)
  38. for id, size in cindex['chunks']:
  39. try:
  40. count, size = self.chunk_counts[id]
  41. self.chunk_counts[id] = count + 1, size
  42. except KeyError:
  43. self.chunk_counts[id] = 1, size
  44. self.save()
  45. def save(self):
  46. assert self.store.state == self.store.OPEN
  47. cache = {'version': 1,
  48. 'tid': self.store.tid,
  49. 'chunk_counts': self.chunk_counts,
  50. 'file_chunks': dict(ifilter(lambda i: i[1][0] < 8,
  51. self.file_chunks.iteritems())),
  52. }
  53. data = msgpack.packb(cache)
  54. cachedir = os.path.dirname(self.path)
  55. if not os.path.exists(cachedir):
  56. os.makedirs(cachedir)
  57. with open(self.path, 'wb') as fd:
  58. fd.write(data)
  59. def add_chunk(self, id, data, crypto):
  60. if self.seen_chunk(id):
  61. return self.chunk_incref(id)
  62. data, hash = crypto.encrypt_read(data)
  63. csize = len(data)
  64. self.store.put(NS_CHUNK, id, data)
  65. self.chunk_counts[id] = (1, csize)
  66. return csize
  67. def seen_chunk(self, id):
  68. return self.chunk_counts.get(id, (0, 0))[0]
  69. def chunk_incref(self, id):
  70. count, size = self.chunk_counts[id]
  71. self.chunk_counts[id] = (count + 1, size)
  72. return size
  73. def chunk_decref(self, id):
  74. count, size = self.chunk_counts[id]
  75. if count == 1:
  76. del self.chunk_counts[id]
  77. self.store.delete(NS_CHUNK, id)
  78. else:
  79. self.chunk_counts[id] = (count - 1, size)
  80. def file_known_and_unchanged(self, path_hash, st):
  81. entry = self.file_chunks.get(path_hash)
  82. if (entry and entry[3] == st.st_mtime
  83. and entry[2] == st.st_size and entry[1] == st.st_ino):
  84. # reset entry age
  85. self.file_chunks[path_hash] = (0,) + entry[1:]
  86. return entry[4], entry[2]
  87. else:
  88. return None, 0
  89. def memorize_file_chunks(self, path_hash, st, ids):
  90. # Entry: Age, inode, size, mtime, chunk ids
  91. self.file_chunks[path_hash] = 0, st.st_ino, st.st_size, st.st_mtime, ids