cache.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import msgpack
  2. import os
  3. from . import NS_ARCHIVE_CHUNKS, NS_CHUNK
  4. class Cache(object):
  5. """Client Side cache
  6. """
  7. def __init__(self, store, keychain):
  8. self.store = store
  9. self.keychain = keychain
  10. self.path = os.path.join(os.path.expanduser('~'), '.darc', 'cache',
  11. '%s.cache' % self.store.id.encode('hex'))
  12. self.tid = -1
  13. self.open()
  14. if self.tid != self.store.tid:
  15. self.init()
  16. def open(self):
  17. if not os.path.exists(self.path):
  18. return
  19. with open(self.path, 'rb') as fd:
  20. data, hash = self.keychain.decrypt(fd.read())
  21. cache = msgpack.unpackb(data)
  22. assert cache['version'] == 1
  23. self.chunk_counts = cache['chunk_counts']
  24. self.file_chunks = cache['file_chunks']
  25. self.tid = cache['tid']
  26. def init(self):
  27. """Initializes cache by fetching and reading all archive indicies
  28. """
  29. print '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 = self.keychain.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 filter_file_chunks(self):
  46. for key, value in self.file_chunks.iteritems():
  47. if value[0] < 8:
  48. yield key, (value[0] + 1,) + value[1:]
  49. def save(self):
  50. assert self.store.state == self.store.OPEN
  51. cache = {'version': 1,
  52. 'tid': self.store.tid,
  53. 'chunk_counts': self.chunk_counts,
  54. 'file_chunks': dict(self.filter_file_chunks()),
  55. }
  56. data, hash = self.keychain.encrypt_create(msgpack.packb(cache))
  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. fd.write(data)
  62. def add_chunk(self, id, data):
  63. if self.seen_chunk(id):
  64. return self.chunk_incref(id)
  65. data, hash = self.keychain.encrypt_read(data)
  66. csize = len(data)
  67. self.store.put(NS_CHUNK, id, data)
  68. self.chunk_counts[id] = (1, csize)
  69. return csize
  70. def seen_chunk(self, id):
  71. return self.chunk_counts.get(id, (0, 0))[0]
  72. def chunk_incref(self, id):
  73. count, size = self.chunk_counts[id]
  74. self.chunk_counts[id] = (count + 1, size)
  75. return size
  76. def chunk_decref(self, id):
  77. count, size = self.chunk_counts[id]
  78. if count == 1:
  79. del self.chunk_counts[id]
  80. self.store.delete(NS_CHUNK, id)
  81. else:
  82. self.chunk_counts[id] = (count - 1, size)
  83. def file_known_and_unchanged(self, path_hash, st):
  84. entry = self.file_chunks.get(path_hash)
  85. if (entry and entry[3] == st.st_mtime
  86. and entry[2] == st.st_size and entry[1] == st.st_ino):
  87. # reset entry age
  88. self.file_chunks[path_hash] = (0,) + entry[1:]
  89. return entry[4], entry[2]
  90. else:
  91. return None, 0
  92. def memorize_file_chunks(self, path_hash, st, ids):
  93. # Entry: Age, inode, size, mtime, chunk ids
  94. self.file_chunks[path_hash] = 0, st.st_ino, st.st_size, st.st_mtime, ids