-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
60 lines (45 loc) · 930 Bytes
/
header.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
package logdb
import (
"encoding/binary"
"io"
)
const MaxHeaderSize = 11
const MinHeaderSize = 3
type header struct {
// The right most one bit is for tombstone.
// tombstone: 1 = deleted, 0 = not deleted.
meta byte
klen uint32
vlen uint32
// Offset to the entry.
// Internal use only. Not stored on disk.
offset int64
}
func (h header) encode(buf []byte) int {
buf[0] = h.meta
index := 1
index += binary.PutUvarint(buf[index:], uint64(h.klen))
index += binary.PutUvarint(buf[index:], uint64(h.vlen))
return index
}
func (h *header) decodeFrom(r io.ByteReader) error {
var err error
h.meta, err = r.ReadByte()
if err != nil {
return err
}
klen, err := binary.ReadUvarint(r)
if err != nil {
return err
}
h.klen = uint32(klen)
vlen, err := binary.ReadUvarint(r)
if err != nil {
return err
}
h.vlen = uint32(vlen)
return nil
}
func (h header) deleted() bool {
return h.meta&1 == 1
}