您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

113 行
3.4KB

  1. # Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors
  2. #
  3. # This module is part of GitDB and is released under
  4. # the New BSD License: http://www.opensource.org/licenses/bsd-license.php
  5. """Contains the MemoryDatabase implementation"""
  6. from gitdb.db.loose import LooseObjectDB
  7. from gitdb.db.base import (
  8. ObjectDBR,
  9. ObjectDBW
  10. )
  11. from gitdb.base import (
  12. OStream,
  13. IStream,
  14. )
  15. from gitdb.exc import (
  16. BadObject,
  17. UnsupportedOperation
  18. )
  19. from gitdb.stream import (
  20. ZippedStoreShaWriter,
  21. DecompressMemMapReader,
  22. )
  23. from io import BytesIO
  24. __all__ = ("MemoryDB", )
  25. class MemoryDB(ObjectDBR, ObjectDBW):
  26. """A memory database stores everything to memory, providing fast IO and object
  27. retrieval. It should be used to buffer results and obtain SHAs before writing
  28. it to the actual physical storage, as it allows to query whether object already
  29. exists in the target storage before introducing actual IO"""
  30. def __init__(self):
  31. super(MemoryDB, self).__init__()
  32. self._db = LooseObjectDB("path/doesnt/matter")
  33. # maps 20 byte shas to their OStream objects
  34. self._cache = dict()
  35. def set_ostream(self, stream):
  36. raise UnsupportedOperation("MemoryDB's always stream into memory")
  37. def store(self, istream):
  38. zstream = ZippedStoreShaWriter()
  39. self._db.set_ostream(zstream)
  40. istream = self._db.store(istream)
  41. zstream.close() # close to flush
  42. zstream.seek(0)
  43. # don't provide a size, the stream is written in object format, hence the
  44. # header needs decompression
  45. decomp_stream = DecompressMemMapReader(zstream.getvalue(), close_on_deletion=False)
  46. self._cache[istream.binsha] = OStream(istream.binsha, istream.type, istream.size, decomp_stream)
  47. return istream
  48. def has_object(self, sha):
  49. return sha in self._cache
  50. def info(self, sha):
  51. # we always return streams, which are infos as well
  52. return self.stream(sha)
  53. def stream(self, sha):
  54. try:
  55. ostream = self._cache[sha]
  56. # rewind stream for the next one to read
  57. ostream.stream.seek(0)
  58. return ostream
  59. except KeyError:
  60. raise BadObject(sha)
  61. # END exception handling
  62. def size(self):
  63. return len(self._cache)
  64. def sha_iter(self):
  65. try:
  66. return self._cache.iterkeys()
  67. except AttributeError:
  68. return self._cache.keys()
  69. #{ Interface
  70. def stream_copy(self, sha_iter, odb):
  71. """Copy the streams as identified by sha's yielded by sha_iter into the given odb
  72. The streams will be copied directly
  73. **Note:** the object will only be written if it did not exist in the target db
  74. :return: amount of streams actually copied into odb. If smaller than the amount
  75. of input shas, one or more objects did already exist in odb"""
  76. count = 0
  77. for sha in sha_iter:
  78. if odb.has_object(sha):
  79. continue
  80. # END check object existence
  81. ostream = self.stream(sha)
  82. # compressed data including header
  83. sio = BytesIO(ostream.stream.data())
  84. istream = IStream(ostream.type, ostream.size, sio, sha)
  85. odb.store(istream)
  86. count += 1
  87. # END for each sha
  88. return count
  89. #} END interface