-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.go
More file actions
65 lines (52 loc) · 1.34 KB
/
entry.go
File metadata and controls
65 lines (52 loc) · 1.34 KB
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
63
64
65
package bitcask
import (
"encoding/binary"
"hash/crc32"
)
const (
HeaderSize = 4 + 8 + 4 + 4; // Header size in bytes. See the Header struct for more detail
)
type Header struct {
crc uint32 // 4 bytes
timestamp uint64 // 8 bytes
keySize uint32 // 4 bytes
valueSize uint32 // 4 bytes
}
type Entry struct {
Header
key []byte
value []byte
}
func (e *Entry) GetSize() int64 {
return int64(HeaderSize + e.keySize + e.valueSize)
}
func Encode(e Entry) []byte {
b := make([]byte, e.GetSize())
binary.BigEndian.PutUint64(b[4:12], e.timestamp)
binary.BigEndian.PutUint32(b[12:16], e.keySize)
binary.BigEndian.PutUint32(b[16:20], e.valueSize)
copy(b[HeaderSize: HeaderSize + e.keySize], e.key)
copy(b[HeaderSize + e.keySize: HeaderSize + e.keySize + e.valueSize], e.value)
crc := crc32.ChecksumIEEE(b[4:])
binary.BigEndian.PutUint32(b[0:4], crc)
return b
}
func DecodeHeader(buf []byte) Header {
crc := binary.BigEndian.Uint32(buf[0:4])
timestamp := binary.BigEndian.Uint64(buf[4:12])
keySize := binary.BigEndian.Uint32(buf[12:16])
valueSize := binary.BigEndian.Uint32(buf[16:20])
return Header{
crc: crc,
timestamp: timestamp,
keySize: keySize,
valueSize: valueSize,
}
}
func Decode(b []byte, h Header) (*Entry, error) {
return &Entry{
Header: h,
key: b[0: h.keySize],
value: b[h.keySize: h.keySize + h.valueSize],
}, nil
}