-
Notifications
You must be signed in to change notification settings - Fork 2
/
record.go
91 lines (79 loc) · 2.27 KB
/
record.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package mossdb
import (
"encoding/binary"
"errors"
"time"
)
// |------------------------------------------------------------|
// | Op | KeySize | ValSize | Timestamp | TTL | Key | Val |
// |------------------------------------------------------------|
// | 2 | 4 | 4 | 8 | 8 | []byte | []byte |
// |------------------------------------------------------------|
const RecordHeaderSize = 2 + 4 + 4 + 8 + 8
type Record struct {
Op uint16
KeySize uint32
ValSize uint32
Timestamp uint64
TTL uint64
Key []byte
Val []byte
}
func (r *Record) Size() uint32 {
return RecordHeaderSize + r.KeySize + r.ValSize
}
func (r *Record) Encode() ([]byte, error) {
if r == nil || len(r.Key) == 0 {
return nil, ErrInvalidRecord
}
buf := make([]byte, r.Size())
binary.BigEndian.PutUint16(buf[:2], r.Op)
binary.BigEndian.PutUint32(buf[2:6], r.KeySize)
binary.BigEndian.PutUint32(buf[6:10], r.ValSize)
binary.BigEndian.PutUint64(buf[10:18], r.Timestamp)
binary.BigEndian.PutUint64(buf[18:26], r.TTL)
copy(buf[RecordHeaderSize:RecordHeaderSize+r.KeySize], r.Key)
copy(buf[RecordHeaderSize+r.KeySize:RecordHeaderSize+r.KeySize+r.ValSize], r.Val)
return buf, nil
}
var (
ErrInvalidRecord = errors.New("invalid record")
)
func Decode(data []byte) (*Record, error) {
if len(data) < RecordHeaderSize {
return nil, ErrInvalidRecord
}
ks := binary.BigEndian.Uint32(data[2:6])
vs := binary.BigEndian.Uint32(data[6:10])
if len(data) != int(RecordHeaderSize+ks+vs) {
return nil, ErrInvalidRecord
}
return &Record{
Op: binary.BigEndian.Uint16(data[:2]),
KeySize: ks,
ValSize: vs,
Timestamp: binary.BigEndian.Uint64(data[10:18]),
TTL: binary.BigEndian.Uint64(data[18:26]),
Key: data[RecordHeaderSize : RecordHeaderSize+ks],
Val: data[RecordHeaderSize+ks : RecordHeaderSize+ks+vs],
}, nil
}
func NewRecord(opt *Op) *Record {
key_b := []byte(opt.key)
return &Record{
Op: uint16(opt.op),
KeySize: uint32(len(key_b)),
ValSize: uint32(len(opt.val)),
Key: key_b,
Val: opt.val,
Timestamp: uint64(time.Now().Nanosecond()),
TTL: uint64(opt.ttl),
}
}
func OpFromRecord(record *Record) *Op {
return &Op{
key: string(record.Key),
val: record.Val,
ttl: int64(record.TTL),
}
}