cache.py 3.6 KB

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