mem.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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().__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 as e:
  60. raise BadObject(sha) from e
  61. # END exception handling
  62. def size(self):
  63. return len(self._cache)
  64. def sha_iter(self):
  65. return self._cache.keys()
  66. #{ Interface
  67. def stream_copy(self, sha_iter, odb):
  68. """Copy the streams as identified by sha's yielded by sha_iter into the given odb
  69. The streams will be copied directly
  70. **Note:** the object will only be written if it did not exist in the target db
  71. :return: amount of streams actually copied into odb. If smaller than the amount
  72. of input shas, one or more objects did already exist in odb"""
  73. count = 0
  74. for sha in sha_iter:
  75. if odb.has_object(sha):
  76. continue
  77. # END check object existence
  78. ostream = self.stream(sha)
  79. # compressed data including header
  80. sio = BytesIO(ostream.stream.data())
  81. istream = IStream(ostream.type, ostream.size, sio, sha)
  82. odb.store(istream)
  83. count += 1
  84. # END for each sha
  85. return count
  86. #} END interface