-
Notifications
You must be signed in to change notification settings - Fork 88
/
entry.go
52 lines (44 loc) · 1.19 KB
/
entry.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
package minibitcask
import "encoding/binary"
const entryHeaderSize = 10
const (
PUT uint16 = iota
DEL
)
// Entry 写入文件的记录
type Entry struct {
Key []byte
Value []byte
KeySize uint32
ValueSize uint32
Mark uint16
}
func NewEntry(key, value []byte, mark uint16) *Entry {
return &Entry{
Key: key,
Value: value,
KeySize: uint32(len(key)),
ValueSize: uint32(len(value)),
Mark: mark,
}
}
func (e *Entry) GetSize() int64 {
return int64(entryHeaderSize + e.KeySize + e.ValueSize)
}
// Encode 编码 Entry,返回字节数组
func (e *Entry) Encode() ([]byte, error) {
buf := make([]byte, e.GetSize())
binary.BigEndian.PutUint32(buf[0:4], e.KeySize)
binary.BigEndian.PutUint32(buf[4:8], e.ValueSize)
binary.BigEndian.PutUint16(buf[8:10], e.Mark)
copy(buf[entryHeaderSize:entryHeaderSize+e.KeySize], e.Key)
copy(buf[entryHeaderSize+e.KeySize:], e.Value)
return buf, nil
}
// Decode 解码 buf 字节数组,返回 Entry
func Decode(buf []byte) (*Entry, error) {
ks := binary.BigEndian.Uint32(buf[0:4])
vs := binary.BigEndian.Uint32(buf[4:8])
mark := binary.BigEndian.Uint16(buf[8:10])
return &Entry{KeySize: ks, ValueSize: vs, Mark: mark}, nil
}