-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrys.go
50 lines (43 loc) · 1.09 KB
/
entrys.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
package kvDB
import "encoding/binary"
const (
PUT uint16 = iota
DEL
GET
)
const entryHeaderSize = 10
type Entry struct {
Key []byte
Value []byte
KeyLen uint32
ValueLen uint32
Mark uint16
}
func NewEntry(k, v []byte, mark uint16) *Entry {
return &Entry{
Key: k,
Value: v,
KeyLen: uint32(len(k)),
ValueLen: uint32(len(v)),
Mark: mark,
}
}
func (e *Entry) Len() int64 {
return int64(entryHeaderSize + e.KeyLen + e.ValueLen)
}
func (e *Entry) Encode() ([]byte, error) {
buf := make([]byte, e.Len())
binary.BigEndian.PutUint32(buf[0:4], e.KeyLen)
binary.BigEndian.PutUint32(buf[4:8], e.ValueLen)
binary.BigEndian.PutUint16(buf[8:10], uint16(e.Mark))
copy(buf[entryHeaderSize:entryHeaderSize+e.KeyLen], e.Key)
copy(buf[entryHeaderSize+e.KeyLen:], e.Value)
return buf, nil
}
func (e *Entry) Decode(buf []byte) {
e.KeyLen = binary.BigEndian.Uint32(buf[0:4])
e.ValueLen = binary.BigEndian.Uint32(buf[4:8])
e.Mark = binary.BigEndian.Uint16(buf[8:10])
// e.Key = buf[entryHeaderSize : entryHeaderSize+e.KeyLen]
// e.Value = buf[entryHeaderSize+e.KeyLen:]
}