m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.4 KiB

7 months ago
  1. from __future__ import absolute_import
  2. import abc
  3. class ABCRecord(object):
  4. __metaclass__ = abc.ABCMeta
  5. __slots__ = ()
  6. @abc.abstractproperty
  7. def offset(self):
  8. """ Absolute offset of record
  9. """
  10. @abc.abstractproperty
  11. def timestamp(self):
  12. """ Epoch milliseconds
  13. """
  14. @abc.abstractproperty
  15. def timestamp_type(self):
  16. """ CREATE_TIME(0) or APPEND_TIME(1)
  17. """
  18. @abc.abstractproperty
  19. def key(self):
  20. """ Bytes key or None
  21. """
  22. @abc.abstractproperty
  23. def value(self):
  24. """ Bytes value or None
  25. """
  26. @abc.abstractproperty
  27. def checksum(self):
  28. """ Prior to v2 format CRC was contained in every message. This will
  29. be the checksum for v0 and v1 and None for v2 and above.
  30. """
  31. @abc.abstractproperty
  32. def headers(self):
  33. """ If supported by version list of key-value tuples, or empty list if
  34. not supported by format.
  35. """
  36. class ABCRecordBatchBuilder(object):
  37. __metaclass__ = abc.ABCMeta
  38. __slots__ = ()
  39. @abc.abstractmethod
  40. def append(self, offset, timestamp, key, value, headers=None):
  41. """ Writes record to internal buffer.
  42. Arguments:
  43. offset (int): Relative offset of record, starting from 0
  44. timestamp (int or None): Timestamp in milliseconds since beginning
  45. of the epoch (midnight Jan 1, 1970 (UTC)). If omitted, will be
  46. set to current time.
  47. key (bytes or None): Key of the record
  48. value (bytes or None): Value of the record
  49. headers (List[Tuple[str, bytes]]): Headers of the record. Header
  50. keys can not be ``None``.
  51. Returns:
  52. (bytes, int): Checksum of the written record (or None for v2 and
  53. above) and size of the written record.
  54. """
  55. @abc.abstractmethod
  56. def size_in_bytes(self, offset, timestamp, key, value, headers):
  57. """ Return the expected size change on buffer (uncompressed) if we add
  58. this message. This will account for varint size changes and give a
  59. reliable size.
  60. """
  61. @abc.abstractmethod
  62. def build(self):
  63. """ Close for append, compress if needed, write size and header and
  64. return a ready to send buffer object.
  65. Return:
  66. bytearray: finished batch, ready to send.
  67. """
  68. class ABCRecordBatch(object):
  69. """ For v2 incapsulates a RecordBatch, for v0/v1 a single (maybe
  70. compressed) message.
  71. """
  72. __metaclass__ = abc.ABCMeta
  73. __slots__ = ()
  74. @abc.abstractmethod
  75. def __iter__(self):
  76. """ Return iterator over records (ABCRecord instances). Will decompress
  77. if needed.
  78. """
  79. class ABCRecords(object):
  80. __metaclass__ = abc.ABCMeta
  81. __slots__ = ()
  82. @abc.abstractmethod
  83. def __init__(self, buffer):
  84. """ Initialize with bytes-like object conforming to the buffer
  85. interface (ie. bytes, bytearray, memoryview etc.).
  86. """
  87. @abc.abstractmethod
  88. def size_in_bytes(self):
  89. """ Returns the size of inner buffer.
  90. """
  91. @abc.abstractmethod
  92. def next_batch(self):
  93. """ Return next batch of records (ABCRecordBatch instances).
  94. """
  95. @abc.abstractmethod
  96. def has_next(self):
  97. """ True if there are more batches to read, False otherwise.
  98. """