2
0

cache.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import msgpack
  2. import os
  3. from . import NS_ARCHIVE_CHUNKS, NS_CHUNK, PACKET_ARCHIVE_CHUNKS, PACKET_CHUNK
  4. class Cache(object):
  5. """Client Side cache
  6. """
  7. def __init__(self, store, keychain):
  8. self.tid = -1
  9. self.store = store
  10. self.keychain = keychain
  11. self.path = os.path.join(Cache.cache_dir_path(),
  12. '%s.cache' % self.store.id.encode('hex'))
  13. self.open()
  14. if self.tid != store.tid:
  15. self.init()
  16. @staticmethod
  17. def cache_dir_path():
  18. """Return path to directory used for storing users cache files"""
  19. return os.path.join(os.path.expanduser('~'), '.darc', 'cache')
  20. def open(self):
  21. if not os.path.exists(self.path):
  22. return
  23. with open(self.path, 'rb') as fd:
  24. #data, hash = self.keychain.decrypt(fd.read())
  25. cache = msgpack.unpackb(fd.read())
  26. assert cache['version'] == 1
  27. self.chunk_counts = cache['chunk_counts']
  28. self.file_chunks = cache['file_chunks']
  29. self.tid = cache['tid']
  30. def init(self):
  31. """Initializes cache by fetching and reading all archive indicies
  32. """
  33. print 'Initializing cache...'
  34. self.chunk_counts = {}
  35. self.file_chunks = {}
  36. for id in self.store.list(NS_ARCHIVE_CHUNKS):
  37. if len(id) != 32:
  38. import ipdb
  39. ipdb.set_trace()
  40. magic, data, hash = self.keychain.decrypt(self.store.get(NS_ARCHIVE_CHUNKS, id))
  41. assert magic == PACKET_ARCHIVE_CHUNKS
  42. chunks = msgpack.unpackb(data)
  43. for id, size in chunks:
  44. try:
  45. count, size = self.chunk_counts[id]
  46. self.chunk_counts[id] = count + 1, size
  47. except KeyError:
  48. self.chunk_counts[id] = 1, size
  49. self.save()
  50. def filter_file_chunks(self):
  51. for key, value in self.file_chunks.iteritems():
  52. if value[0] < 8:
  53. yield key, (value[0] + 1,) + value[1:]
  54. def save(self):
  55. for id, (count, size) in self.chunk_counts.iteritems():
  56. if count > 1000000:
  57. self.chunk_counts[id] = count - 1000000, size
  58. cache = {'version': 1,
  59. 'tid': self.store.tid,
  60. 'chunk_counts': self.chunk_counts,
  61. 'file_chunks': dict(self.filter_file_chunks()),
  62. }
  63. # data, hash = self.keychain.encrypt_create(msgpack.packb(cache))
  64. cachedir = os.path.dirname(self.path)
  65. if not os.path.exists(cachedir):
  66. os.makedirs(cachedir)
  67. with open(self.path, 'wb') as fd:
  68. fd.write(msgpack.packb(cache))
  69. def add_chunk(self, id, data):
  70. if self.seen_chunk(id):
  71. return self.chunk_incref(id)
  72. data, hash = self.keychain.encrypt(PACKET_CHUNK, data)
  73. csize = len(data)
  74. self.store.put(NS_CHUNK, id, data)
  75. self.chunk_counts[id] = (1000001, csize)
  76. return id
  77. def seen_chunk(self, id):
  78. return self.chunk_counts.get(id, (0, 0))[0]
  79. def chunk_incref(self, id):
  80. count, size = self.chunk_counts[id]
  81. if count < 1000000:
  82. self.chunk_counts[id] = (count + 1000001, size)
  83. return id
  84. def chunk_decref(self, id):
  85. count, size = self.chunk_counts[id]
  86. if count == 1:
  87. del self.chunk_counts[id]
  88. self.store.delete(NS_CHUNK, id)
  89. else:
  90. self.chunk_counts[id] = (count - 1, size)
  91. def file_known_and_unchanged(self, path_hash, st):
  92. entry = self.file_chunks.get(path_hash)
  93. if (entry and entry[3] == st.st_mtime
  94. and entry[2] == st.st_size and entry[1] == st.st_ino):
  95. # reset entry age
  96. self.file_chunks[path_hash] = (0,) + entry[1:]
  97. return entry[4], entry[2]
  98. else:
  99. return None, 0
  100. def memorize_file_chunks(self, path_hash, st, ids):
  101. # Entry: Age, inode, size, mtime, chunk ids
  102. self.file_chunks[path_hash] = 0, st.st_ino, st.st_size, st.st_mtime, ids