-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencode.go
62 lines (50 loc) · 1.62 KB
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package bitcask
import (
"encoding/binary"
"hash/crc32"
"os"
)
// crc32 | timestamp | keySize | valueSize | key | value
// 4 | 4 | 4 | 4 | 4 | 4
func encodeEntry(key, value []byte, keySize, valueSize, ts, entrySize uint32) ([]byte, error) {
buf := make([]byte, entrySize)
binary.BigEndian.PutUint32(buf[4:8], ts)
binary.BigEndian.PutUint32(buf[8:12], keySize)
binary.BigEndian.PutUint32(buf[12:HeaderSize], valueSize)
copy(buf[HeaderSize:HeaderSize+keySize], key)
copy(buf[HeaderSize+keySize:HeaderSize+keySize+valueSize], value)
c32 := crc32.ChecksumIEEE(buf[4:])
binary.BigEndian.PutUint32(buf[0:4], c32)
return buf, nil
}
/*
keySize | valueSize | valueOffset | key
4 | 4 | 8 | xxxxx
*/
func encodeHintData(key []byte, keySize, valueSize, valueOffset uint32) []byte {
buf := make([]byte, HintHeaderSize+len(key))
binary.LittleEndian.PutUint32(buf[0:4], keySize)
binary.LittleEndian.PutUint32(buf[4:8], valueSize)
binary.LittleEndian.PutUint32(buf[8:HintHeaderSize], valueOffset)
copy(buf[HintHeaderSize:], []byte(key))
return buf
}
func decodeHintData(fp *os.File, offset int64) (HintHeader, error) {
buf, err := read(fp, offset, HintHeaderSize)
if err != nil {
return HintHeader{}, err
}
keySize := binary.LittleEndian.Uint32(buf[0:4])
kbuf, err := read(fp, offset+HintHeaderSize, keySize)
if err != nil {
return HintHeader{}, err
}
valueSize := binary.LittleEndian.Uint32(buf[4:8])
valueOffset := binary.LittleEndian.Uint32(buf[8:HintHeaderSize])
return HintHeader{
ksize: keySize,
vsize: valueSize,
valueOffset: valueOffset,
key: kbuf,
}, nil
}