repository.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/usr/bin/env python
  2. import fcntl
  3. import tempfile
  4. import logging
  5. import os
  6. import posixpath
  7. import shutil
  8. import unittest
  9. CHUNKSIZE = 256 * 1024
  10. log = logging.getLogger('')
  11. class Repository(object):
  12. """
  13. """
  14. IDLE = 'Idle'
  15. OPEN = 'Open'
  16. ACTIVE = 'Active'
  17. VERSION = 'DEDUPSTORE REPOSITORY VERSION 1'
  18. def __init__(self, path):
  19. self.tid = -1
  20. self.state = Repository.IDLE
  21. if not os.path.exists(path):
  22. self.create(path)
  23. self.open(path)
  24. def create(self, path):
  25. log.info('Initializing Repository at "%s"' % path)
  26. os.mkdir(path)
  27. open(os.path.join(path, 'VERSION'), 'wb').write(self.VERSION)
  28. open(os.path.join(path, 'tid'), 'wb').write('0')
  29. os.mkdir(os.path.join(path, 'data'))
  30. def open(self, path):
  31. self.path = path
  32. if not os.path.isdir(path):
  33. raise Exception('%s Does not look like a repository')
  34. version_path = os.path.join(path, 'version')
  35. if not os.path.exists(version_path) or open(version_path, 'rb').read() != self.VERSION:
  36. raise Exception('%s Does not look like a repository2')
  37. self.lock_fd = open(os.path.join(path, 'lock'), 'w')
  38. fcntl.flock(self.lock_fd, fcntl.LOCK_EX)
  39. self.tid = int(open(os.path.join(path, 'tid'), 'r').read())
  40. self.recover()
  41. def recover(self):
  42. if os.path.exists(os.path.join(self.path, 'txn-active')):
  43. self.rollback()
  44. if os.path.exists(os.path.join(self.path, 'txn-commit')):
  45. self.apply_txn()
  46. if os.path.exists(os.path.join(self.path, 'txn-applied')):
  47. shutil.rmtree(os.path.join(self.path, 'txn-applied'))
  48. self.state = Repository.OPEN
  49. def close(self):
  50. self.recover()
  51. self.lock_fd.close()
  52. self.state = Repository.IDLE
  53. def commit(self):
  54. """
  55. """
  56. if self.state == Repository.OPEN:
  57. return
  58. assert self.state == Repository.ACTIVE
  59. remove_fd = open(os.path.join(self.path, 'txn-active', 'remove'), 'wb')
  60. remove_fd.write('\n'.join(self.txn_removed))
  61. remove_fd.close()
  62. tid_fd = open(os.path.join(self.path, 'txn-active', 'tid'), 'wb')
  63. tid_fd.write(str(self.tid + 1))
  64. tid_fd.close()
  65. os.rename(os.path.join(self.path, 'txn-active'),
  66. os.path.join(self.path, 'txn-commit'))
  67. self.apply_txn()
  68. def apply_txn(self):
  69. assert os.path.isdir(os.path.join(self.path, 'txn-commit'))
  70. tid = int(open(os.path.join(self.path, 'txn-commit', 'tid'), 'rb').read())
  71. assert tid >= self.tid
  72. remove_list = [line.strip() for line in
  73. open(os.path.join(self.path, 'txn-commit', 'remove'), 'rb').readlines()]
  74. for name in remove_list:
  75. path = os.path.join(self.path, 'data', name)
  76. os.unlink(path)
  77. add_path = os.path.join(self.path, 'txn-commit', 'add')
  78. for name in os.listdir(add_path):
  79. shutil.move(os.path.join(add_path, name),
  80. os.path.join(self.path, 'data'))
  81. tid_fd = open(os.path.join(self.path, 'tid'), 'wb')
  82. tid_fd.write(str(tid))
  83. tid_fd.close()
  84. os.rename(os.path.join(self.path, 'txn-commit'),
  85. os.path.join(self.path, 'txn-applied'))
  86. shutil.rmtree(os.path.join(self.path, 'txn-applied'))
  87. self.state = Repository.IDLE
  88. def rollback(self):
  89. """
  90. """
  91. txn_path = os.path.join(self.path, 'txn-active')
  92. if os.path.exists(txn_path):
  93. shutil.rmtree(txn_path)
  94. self.state = Repository.OPEN
  95. def prepare_txn(self):
  96. if self.state == Repository.ACTIVE:
  97. return os.path.join(self.path, 'txn-active')
  98. elif self.state == Repository.OPEN:
  99. os.mkdir(os.path.join(self.path, 'txn-active'))
  100. os.mkdir(os.path.join(self.path, 'txn-active', 'add'))
  101. self.txn_removed = []
  102. self.state = Repository.ACTIVE
  103. def get_file(self, path):
  104. """
  105. """
  106. if os.path.exists(os.path.join(self.path, 'txn-active', 'add', path)):
  107. return open(os.path.join(self.path, 'txn-active', 'add', path), 'rb').read()
  108. elif os.path.exists(os.path.join(self.path, 'data', path)):
  109. return open(os.path.join(self.path, 'data', path), 'rb').read()
  110. else:
  111. raise Exception('FileNotFound: %s' % path)
  112. def put_file(self, path, data):
  113. """
  114. """
  115. self.prepare_txn()
  116. if path in self.txn_removed:
  117. self.txn_removed.remove(path)
  118. if os.path.exists(os.path.join(self.path, 'txn-active', 'add', path)):
  119. raise Exception('FileAlreadyExists: %s' % path)
  120. fd = open(os.path.join(self.path, 'txn-active', 'add', path), 'wb')
  121. try:
  122. fd.write(data)
  123. finally:
  124. fd.close()
  125. def delete(self, path):
  126. """
  127. """
  128. self.prepare_txn()
  129. if os.path.exists(os.path.join(self.path, 'txn-active', 'add', path)):
  130. os.unlink(os.path.join(self.path, 'txn-active', 'add', path))
  131. elif os.path.exists(os.path.join(self.path, 'data', path)):
  132. self.txn_removed.append(path)
  133. else:
  134. raise Exception('FileNotFound: %s' % path)
  135. def listdir(self, path):
  136. """
  137. """
  138. entries = set(os.listdir(os.path.join(self.path, 'data', path)))
  139. if self.state == Repository.ACTIVE:
  140. txn_entries = set(os.listdir(os.path.join(self.path, 'txn-active', 'add', path)))
  141. entries = entries.union(txn_entries)
  142. for e in entries:
  143. if posixpath.join(path, e) in self.txn_removed:
  144. entries.remove(e)
  145. return list(entries)
  146. def mkdir(self, path):
  147. """
  148. """
  149. def rmdir(self, path):
  150. """
  151. """
  152. class RepositoryTestCase(unittest.TestCase):
  153. def setUp(self):
  154. self.tmppath = tempfile.mkdtemp()
  155. self.repo = Repository(os.path.join(self.tmppath, 'repo'))
  156. def tearDown(self):
  157. shutil.rmtree(self.tmppath)
  158. def test1(self):
  159. self.assertEqual(self.repo.tid, 0)
  160. self.assertEqual(self.repo.state, Repository.OPEN)
  161. self.assertEqual(self.repo.listdir(''), [])
  162. self.repo.put_file('foo', 'SOMEDATA')
  163. self.assertRaises(Exception, lambda: self.repo.put_file('foo', 'SOMETHINGELSE'))
  164. self.assertEqual(self.repo.get_file('foo'), 'SOMEDATA')
  165. self.assertEqual(self.repo.listdir(''), ['foo'])
  166. self.repo.rollback()
  167. self.assertEqual(self.repo.listdir(''), [])
  168. def test2(self):
  169. self.repo.put_file('foo', 'SOMEDATA')
  170. self.repo.put_file('bar', 'SOMEDATAbar')
  171. self.assertEqual(self.repo.listdir(''), ['foo', 'bar'])
  172. self.assertEqual(self.repo.get_file('foo'), 'SOMEDATA')
  173. self.repo.delete('foo')
  174. self.assertRaises(Exception, lambda: self.repo.get_file('foo'))
  175. self.assertEqual(self.repo.listdir(''), ['bar'])
  176. self.assertEqual(self.repo.state, Repository.ACTIVE)
  177. self.assertEqual(os.path.exists(os.path.join(self.tmppath, 'repo', 'data', 'bar')), False)
  178. self.repo.commit()
  179. self.assertEqual(os.path.exists(os.path.join(self.tmppath, 'repo', 'data', 'bar')), True)
  180. self.assertEqual(self.repo.listdir(''), ['bar'])
  181. self.assertEqual(self.repo.state, Repository.IDLE)
  182. if __name__ == '__main__':
  183. unittest.main()