54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package kvstore
|
|
|
|
/*
|
|
Copyright (c) 2022, John David Lee
|
|
All rights reserved.
|
|
|
|
This source code is licensed under the BSD-style license found in the
|
|
LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
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
|
|
}
|