EncodeLogRecord encodes a LogRecord and returns the byte array and length. +-------------+------------+------------+--------------+-------+---------+---------+---------+---------+ | crc checksum | record type | key size | value size | key | value | +-------------+----
(logrecord *LogRecord)
| 52 | // | 4 bytes | 1 byte | variable (max 5) | variable (max 5) | variable | variable | |
| 53 | // +-------------+------------+------------+--------------+-------+---------+---------+---------+---------+ |
| 54 | func EncodeLogRecord(logrecord *LogRecord) ([]byte, int64) { |
| 55 | header := make([]byte, maxLogRecordHeaderSize) |
| 56 | |
| 57 | // Store the record type at the fifth byte |
| 58 | header[4] = logrecord.Type |
| 59 | var headerIndex = 5 |
| 60 | |
| 61 | // Store the lengths of key and value after the fifth byte |
| 62 | // Use variable-length encoding to save space |
| 63 | headerIndex += binary.PutVarint(header[headerIndex:], int64(len(logrecord.Key))) |
| 64 | headerIndex += binary.PutVarint(header[headerIndex:], int64(len(logrecord.Value))) |
| 65 | |
| 66 | var size = headerIndex + len(logrecord.Key) + len(logrecord.Value) |
| 67 | encBytes := make([]byte, size) |
| 68 | |
| 69 | // Copy the header content and key/value data |
| 70 | copy(encBytes[:headerIndex], header[:headerIndex]) |
| 71 | copy(encBytes[headerIndex:], logrecord.Key) |
| 72 | copy(encBytes[headerIndex+len(logrecord.Key):], logrecord.Value) |
| 73 | |
| 74 | // Calculate the CRC checksum for the entire LogRecord data |
| 75 | crc := crc32.ChecksumIEEE(encBytes[4:]) |
| 76 | binary.LittleEndian.PutUint32(encBytes[:4], crc) |
| 77 | |
| 78 | return encBytes, int64(size) |
| 79 | } |
| 80 | |
| 81 | // EncodeLogRecordPst encodes the position information of a log record. |
| 82 | func EncodeLogRecordPst(pst *LogRecordPst) []byte { |
no outgoing calls