message.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package sarama
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "fmt"
  6. "io/ioutil"
  7. )
  8. // CompressionCodec represents the various compression codecs recognized by Kafka in messages.
  9. type CompressionCodec int8
  10. // only the last two bits are really used
  11. const compressionCodecMask int8 = 0x03
  12. const (
  13. CompressionNone CompressionCodec = 0
  14. CompressionGZIP CompressionCodec = 1
  15. CompressionSnappy CompressionCodec = 2
  16. )
  17. // The spec just says: "This is a version id used to allow backwards compatible evolution of the message
  18. // binary format." but it doesn't say what the current value is, so presumably 0...
  19. const messageFormat int8 = 0
  20. type Message struct {
  21. Codec CompressionCodec // codec used to compress the message contents
  22. Key []byte // the message key, may be nil
  23. Value []byte // the message contents
  24. Set *MessageSet // the message set a message might wrap
  25. compressedCache []byte
  26. }
  27. func (m *Message) encode(pe packetEncoder) error {
  28. pe.push(&crc32Field{})
  29. pe.putInt8(messageFormat)
  30. attributes := int8(m.Codec) & compressionCodecMask
  31. pe.putInt8(attributes)
  32. err := pe.putBytes(m.Key)
  33. if err != nil {
  34. return err
  35. }
  36. var payload []byte
  37. if m.compressedCache != nil {
  38. payload = m.compressedCache
  39. m.compressedCache = nil
  40. } else {
  41. switch m.Codec {
  42. case CompressionNone:
  43. payload = m.Value
  44. case CompressionGZIP:
  45. var buf bytes.Buffer
  46. writer := gzip.NewWriter(&buf)
  47. if _, err = writer.Write(m.Value); err != nil {
  48. return err
  49. }
  50. if err = writer.Close(); err != nil {
  51. return err
  52. }
  53. m.compressedCache = buf.Bytes()
  54. payload = m.compressedCache
  55. case CompressionSnappy:
  56. tmp := snappyEncode(m.Value)
  57. m.compressedCache = tmp
  58. payload = m.compressedCache
  59. default:
  60. return PacketEncodingError{fmt.Sprintf("unsupported compression codec (%d)", m.Codec)}
  61. }
  62. }
  63. if err = pe.putBytes(payload); err != nil {
  64. return err
  65. }
  66. return pe.pop()
  67. }
  68. func (m *Message) decode(pd packetDecoder) (err error) {
  69. err = pd.push(&crc32Field{})
  70. if err != nil {
  71. return err
  72. }
  73. format, err := pd.getInt8()
  74. if err != nil {
  75. return err
  76. }
  77. if format != messageFormat {
  78. return PacketDecodingError{"unexpected messageFormat"}
  79. }
  80. attribute, err := pd.getInt8()
  81. if err != nil {
  82. return err
  83. }
  84. m.Codec = CompressionCodec(attribute & compressionCodecMask)
  85. m.Key, err = pd.getBytes()
  86. if err != nil {
  87. return err
  88. }
  89. m.Value, err = pd.getBytes()
  90. if err != nil {
  91. return err
  92. }
  93. switch m.Codec {
  94. case CompressionNone:
  95. // nothing to do
  96. case CompressionGZIP:
  97. if m.Value == nil {
  98. return PacketDecodingError{"GZIP compression specified, but no data to uncompress"}
  99. }
  100. reader, err := gzip.NewReader(bytes.NewReader(m.Value))
  101. if err != nil {
  102. return err
  103. }
  104. if m.Value, err = ioutil.ReadAll(reader); err != nil {
  105. return err
  106. }
  107. if err := m.decodeSet(); err != nil {
  108. return err
  109. }
  110. case CompressionSnappy:
  111. if m.Value == nil {
  112. return PacketDecodingError{"Snappy compression specified, but no data to uncompress"}
  113. }
  114. if m.Value, err = snappyDecode(m.Value); err != nil {
  115. return err
  116. }
  117. if err := m.decodeSet(); err != nil {
  118. return err
  119. }
  120. default:
  121. return PacketDecodingError{fmt.Sprintf("invalid compression specified (%d)", m.Codec)}
  122. }
  123. return pd.pop()
  124. }
  125. // decodes a message set from a previousy encoded bulk-message
  126. func (m *Message) decodeSet() (err error) {
  127. pd := realDecoder{raw: m.Value}
  128. m.Set = &MessageSet{}
  129. return m.Set.decode(&pd)
  130. }