-
Notifications
You must be signed in to change notification settings - Fork 29
/
record.go
145 lines (124 loc) · 3.88 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package flashdb
import (
"encoding/binary"
"errors"
"time"
)
/*
Record is based on bitcask entry model design in nutsdb
*/
var (
ErrInvalidEntry = errors.New("invalid entry")
)
const (
// keySize, memberSize, valueSize is uint32 type,4 bytes each.
// timestamp 8 bytes, state 2 bytes.
// 4 + 4 + 4 + 8 + 2 = 22
entryHeaderSize = 22
)
type (
record struct {
meta *meta
state uint16 // state represents two fields, high 8 bits is the data type, low 8 bits is operation mark.
timestamp uint64 // Timestamp is the time when entry was written.
}
// Meta meta info.
meta struct {
key []byte
member []byte
value []byte
keySize uint32
memberSize uint32
valueSize uint32
}
)
func newInternal(key, member, value []byte, state uint16, timestamp uint64) *record {
return &record{
state: state, timestamp: timestamp,
meta: &meta{
key: key,
member: member,
value: value,
keySize: uint32(len(key)),
memberSize: uint32(len(member)),
valueSize: uint32(len(value)),
},
}
}
func newRecord(key, member []byte, t, mark uint16) *record {
var state uint16 = 0
// set type and mark.
state = state | (t << 8)
state = state | mark
return newInternal(key, member, nil, state, uint64(time.Now().UnixNano()))
}
func newRecordWithValue(key, member, value []byte, t, mark uint16) *record {
var state uint16 = 0
// set type and mark.
state = state | (t << 8)
state = state | mark
return newInternal(key, member, value, state, uint64(time.Now().UnixNano()))
}
func newRecordWithExpire(key, member []byte, deadline int64, t, mark uint16) *record {
var state uint16 = 0
// set type and mark.
state = state | (t << 8)
state = state | mark
return newInternal(key, member, nil, state, uint64(deadline))
}
func (e *record) size() uint32 {
return entryHeaderSize + e.meta.keySize + e.meta.memberSize + e.meta.valueSize
}
// Encode returns the slice after the entry be encoded.
//
// the entry stored format:
// |----------------------------------------------------------------------------------------------------------------|
// | ks | ms | vs | state | timestamp | key | member | value |
// |----------------------------------------------------------------------------------------------------------------|
// | uint32| uint32 | uint32 | uint16 | uint64 | []byte | []byte | []byte |
// |----------------------------------------------------------------------------------------------------------------|
//
func (e *record) encode() ([]byte, error) {
if e == nil || e.meta.keySize == 0 {
return nil, ErrInvalidEntry
}
ks, ms := e.meta.keySize, e.meta.memberSize
vs := e.meta.valueSize
buf := make([]byte, e.size())
binary.BigEndian.PutUint32(buf[0:4], ks)
binary.BigEndian.PutUint32(buf[4:8], ms)
binary.BigEndian.PutUint32(buf[8:12], vs)
binary.BigEndian.PutUint16(buf[12:14], e.state)
binary.BigEndian.PutUint64(buf[14:22], e.timestamp)
copy(buf[entryHeaderSize:entryHeaderSize+ks], e.meta.key)
copy(buf[entryHeaderSize+ks:(entryHeaderSize+ks+ms)], e.meta.member)
if vs > 0 {
copy(buf[(entryHeaderSize+ks+ms):(entryHeaderSize+ks+ms+vs)], e.meta.value)
}
return buf, nil
}
func decode(buf []byte) (*record, error) {
ks := binary.BigEndian.Uint32(buf[0:4])
ms := binary.BigEndian.Uint32(buf[4:8])
vs := binary.BigEndian.Uint32(buf[8:12])
state := binary.BigEndian.Uint16(buf[12:14])
timestamp := binary.BigEndian.Uint64(buf[14:22])
return &record{
meta: &meta{
keySize: ks,
memberSize: ms,
valueSize: vs,
key: buf[entryHeaderSize : entryHeaderSize+ks],
member: buf[entryHeaderSize+ks : (entryHeaderSize + ks + ms)],
value: buf[(entryHeaderSize + ks + ms):(entryHeaderSize + ks + ms + vs)],
},
state: state,
timestamp: timestamp,
}, nil
}
func (e *record) getType() uint16 {
return e.state >> 8
}
func (e *record) getMark() uint16 {
return e.state & (2<<7 - 1)
}