github.com/QuangHoangHao/kafka-go@v0.4.36/message.go (about)

     1  package kafka
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // Message is a data structure representing kafka messages.
     8  type Message struct {
     9  	// Topic indicates which topic this message was consumed from via Reader.
    10  	//
    11  	// When being used with Writer, this can be used to configure the topic if
    12  	// not already specified on the writer itself.
    13  	Topic string
    14  
    15  	// Partition is read-only and MUST NOT be set when writing messages
    16  	Partition     int
    17  	Offset        int64
    18  	HighWaterMark int64
    19  	Key           []byte
    20  	Value         []byte
    21  	Headers       []Header
    22  
    23  	// If not set at the creation, Time will be automatically set when
    24  	// writing the message.
    25  	Time time.Time
    26  }
    27  
    28  func (msg Message) message(cw *crc32Writer) message {
    29  	m := message{
    30  		MagicByte: 1,
    31  		Key:       msg.Key,
    32  		Value:     msg.Value,
    33  		Timestamp: timestamp(msg.Time),
    34  	}
    35  	if cw != nil {
    36  		m.CRC = m.crc32(cw)
    37  	}
    38  	return m
    39  }
    40  
    41  const timestampSize = 8
    42  
    43  func (msg *Message) size() int32 {
    44  	return 4 + 1 + 1 + sizeofBytes(msg.Key) + sizeofBytes(msg.Value) + timestampSize
    45  }
    46  
    47  type message struct {
    48  	CRC        int32
    49  	MagicByte  int8
    50  	Attributes int8
    51  	Timestamp  int64
    52  	Key        []byte
    53  	Value      []byte
    54  }
    55  
    56  func (m message) crc32(cw *crc32Writer) int32 {
    57  	cw.crc32 = 0
    58  	cw.writeInt8(m.MagicByte)
    59  	cw.writeInt8(m.Attributes)
    60  	if m.MagicByte != 0 {
    61  		cw.writeInt64(m.Timestamp)
    62  	}
    63  	cw.writeBytes(m.Key)
    64  	cw.writeBytes(m.Value)
    65  	return int32(cw.crc32)
    66  }
    67  
    68  func (m message) size() int32 {
    69  	size := 4 + 1 + 1 + sizeofBytes(m.Key) + sizeofBytes(m.Value)
    70  	if m.MagicByte != 0 {
    71  		size += timestampSize
    72  	}
    73  	return size
    74  }
    75  
    76  func (m message) writeTo(wb *writeBuffer) {
    77  	wb.writeInt32(m.CRC)
    78  	wb.writeInt8(m.MagicByte)
    79  	wb.writeInt8(m.Attributes)
    80  	if m.MagicByte != 0 {
    81  		wb.writeInt64(m.Timestamp)
    82  	}
    83  	wb.writeBytes(m.Key)
    84  	wb.writeBytes(m.Value)
    85  }
    86  
    87  type messageSetItem struct {
    88  	Offset      int64
    89  	MessageSize int32
    90  	Message     message
    91  }
    92  
    93  func (m messageSetItem) size() int32 {
    94  	return 8 + 4 + m.Message.size()
    95  }
    96  
    97  func (m messageSetItem) writeTo(wb *writeBuffer) {
    98  	wb.writeInt64(m.Offset)
    99  	wb.writeInt32(m.MessageSize)
   100  	m.Message.writeTo(wb)
   101  }
   102  
   103  type messageSet []messageSetItem
   104  
   105  func (s messageSet) size() (size int32) {
   106  	for _, m := range s {
   107  		size += m.size()
   108  	}
   109  	return
   110  }
   111  
   112  func (s messageSet) writeTo(wb *writeBuffer) {
   113  	for _, m := range s {
   114  		m.writeTo(wb)
   115  	}
   116  }