This repository has been archived on 2022-07-30. You can view files and clone it, but cannot push or open issues/pull-requests.
mdb/kvstore/shipping.go

46 lines
901 B
Go
Raw Normal View History

2022-07-29 19:36:42 +00:00
package kvstore
2022-07-25 05:39:16 +00:00
2022-07-29 19:36:42 +00:00
import "encoding/binary"
2022-07-25 05:39:16 +00:00
const recHeaderSize = 22
2022-07-29 19:36:42 +00:00
func encodeRecordHeader(rec record, buf []byte) {
2022-07-25 05:39:16 +00:00
// 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))
}
2022-07-29 19:36:42 +00:00
func decodeRecHeader(header []byte) (rec record, colLen, dataLen int) {
2022-07-25 05:39:16 +00:00
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
}