/
entry.go
185 lines (163 loc) · 4.2 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2021 hardcore-os Project Authors
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package utils
import (
"encoding/binary"
"time"
)
type ValueStruct struct {
Meta byte
Value []byte
ExpiresAt uint64
Version uint64 // This field is not serialized. Only for internal usage.
}
// value只持久化具体的value值和过期时间
func (vs *ValueStruct) EncodedSize() uint32 {
sz := len(vs.Value) + 1 // meta
enc := sizeVarint(vs.ExpiresAt)
return uint32(sz + enc)
}
// DecodeValue
func (vs *ValueStruct) DecodeValue(buf []byte) {
vs.Meta = buf[0]
var sz int
vs.ExpiresAt, sz = binary.Uvarint(buf[1:])
vs.Value = buf[1+sz:]
}
//对value进行编码,并将编码后的字节写入byte
//这里将过期时间和value的值一起编码
func (vs *ValueStruct) EncodeValue(b []byte) uint32 {
b[0] = vs.Meta
sz := binary.PutUvarint(b[1:], vs.ExpiresAt)
n := copy(b[1+sz:], vs.Value)
return uint32(1 + sz + n)
}
func sizeVarint(x uint64) (n int) {
for {
n++
x >>= 7
if x == 0 {
break
}
}
return n
}
//Entry _ 最外层写入的结构体
type Entry struct {
Key []byte
Value []byte
ExpiresAt uint64
Meta byte
Version uint64
Offset uint32
Hlen int // Length of the header.
ValThreshold int64
}
// NewEntry_
func NewEntry(key, value []byte) *Entry {
return &Entry{
Key: key,
Value: value,
}
}
// Entry_
func (e *Entry) Entry() *Entry {
return e
}
func (e *Entry) IsDeletedOrExpired() bool {
if e.Value == nil {
return true
}
if e.ExpiresAt == 0 {
return false
}
return e.ExpiresAt <= uint64(time.Now().Unix())
}
// WithTTL _
func (e *Entry) WithTTL(dur time.Duration) *Entry {
e.ExpiresAt = uint64(time.Now().Add(dur).Unix())
return e
}
// EncodedSize is the size of the ValueStruct when encoded
func (e *Entry) EncodedSize() uint32 {
sz := len(e.Value)
enc := sizeVarint(uint64(e.Meta))
enc += sizeVarint(e.ExpiresAt)
return uint32(sz + enc)
}
// EstimateSize
func (e *Entry) EstimateSize(threshold int) int {
// TODO: 是否考虑 user meta?
if len(e.Value) < threshold {
return len(e.Key) + len(e.Value) + 1 // Meta
}
return len(e.Key) + 12 + 1 // 12 for ValuePointer, 2 for meta.
}
// header 对象
// header is used in value log as a header before Entry.
type Header struct {
KLen uint32
VLen uint32
ExpiresAt uint64
Meta byte
}
// +------+----------+------------+--------------+-----------+
// | Meta | UserMeta | Key Length | Value Length | ExpiresAt |
// +------+----------+------------+--------------+-----------+
func (h Header) Encode(out []byte) int {
out[0] = h.Meta
index := 1
index += binary.PutUvarint(out[index:], uint64(h.KLen))
index += binary.PutUvarint(out[index:], uint64(h.VLen))
index += binary.PutUvarint(out[index:], h.ExpiresAt)
return index
}
// Decode decodes the given header from the provided byte slice.
// Returns the number of bytes read.
func (h *Header) Decode(buf []byte) int {
h.Meta = buf[0]
index := 1
klen, count := binary.Uvarint(buf[index:])
h.KLen = uint32(klen)
index += count
vlen, count := binary.Uvarint(buf[index:])
h.VLen = uint32(vlen)
index += count
h.ExpiresAt, count = binary.Uvarint(buf[index:])
return index + count
}
// DecodeFrom reads the header from the hashReader.
// Returns the number of bytes read.
func (h *Header) DecodeFrom(reader *HashReader) (int, error) {
var err error
h.Meta, err = reader.ReadByte()
if err != nil {
return 0, err
}
klen, err := binary.ReadUvarint(reader)
if err != nil {
return 0, err
}
h.KLen = uint32(klen)
vlen, err := binary.ReadUvarint(reader)
if err != nil {
return 0, err
}
h.VLen = uint32(vlen)
h.ExpiresAt, err = binary.ReadUvarint(reader)
if err != nil {
return 0, err
}
return reader.BytesRead, nil
}