48 lines
902 B
Go
48 lines
902 B
Go
|
package wal
|
||
|
|
||
|
import (
|
||
|
"encoding/binary"
|
||
|
)
|
||
|
|
||
|
const recHeaderSize = 22
|
||
|
|
||
|
func encodeRecordHeader(rec Record, buf []byte) {
|
||
|
// SeqNum (8)
|
||
|
// ID (8)
|
||
|
// DataLen (4)
|
||
|
// Store (1)
|
||
|
// CollectionLen (1)
|
||
|
|
||
|
binary.LittleEndian.PutUint64(buf[:8], rec.SeqNum)
|
||
|
buf = buf[8:]
|
||
|
binary.LittleEndian.PutUint64(buf[:8], rec.ID)
|
||
|
buf = buf[8:]
|
||
|
|
||
|
if rec.Store {
|
||
|
binary.LittleEndian.PutUint32(buf[:4], uint32(len(rec.Data)))
|
||
|
buf[4] = 1
|
||
|
} else {
|
||
|
binary.LittleEndian.PutUint32(buf[:4], 0)
|
||
|
buf[4] = 0
|
||
|
}
|
||
|
buf = buf[5:]
|
||
|
|
||
|
buf[0] = byte(len(rec.Collection))
|
||
|
}
|
||
|
|
||
|
func decodeRecHeader(header []byte) (rec Record, colLen, dataLen int) {
|
||
|
buf := header
|
||
|
|
||
|
rec.SeqNum = binary.LittleEndian.Uint64(buf[:8])
|
||
|
buf = buf[8:]
|
||
|
rec.ID = binary.LittleEndian.Uint64(buf[:8])
|
||
|
buf = buf[8:]
|
||
|
dataLen = int(binary.LittleEndian.Uint32(buf[:4]))
|
||
|
buf = buf[4:]
|
||
|
rec.Store = buf[0] != 0
|
||
|
buf = buf[1:]
|
||
|
colLen = int(buf[0])
|
||
|
|
||
|
return
|
||
|
}
|