/
entry.go
126 lines (96 loc) · 2.63 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
// Copyright 2018-2021 Burak Sezer
//
// 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 kvstore
import (
"encoding/binary"
"github.com/buraksezer/olric/pkg/storage"
)
// In-memory layout for an entry:
//
// KEY-LENGTH(uint8) | KEY(bytes) | TTL(uint64) | | Timestamp(uint64) | VALUE-LENGTH(uint32) | VALUE(bytes)
// Entry represents a value with its metadata.
type Entry struct {
key string
ttl int64
timestamp int64
value []byte
}
var _ storage.Entry = (*Entry)(nil)
func NewEntry() *Entry {
return &Entry{}
}
func (e *Entry) SetKey(key string) {
e.key = key
}
func (e *Entry) Key() string {
return e.key
}
func (e *Entry) SetValue(value []byte) {
e.value = value
}
func (e *Entry) Value() []byte {
return e.value
}
func (e *Entry) SetTTL(ttl int64) {
e.ttl = ttl
}
func (e *Entry) TTL() int64 {
return e.ttl
}
func (e *Entry) SetTimestamp(timestamp int64) {
e.timestamp = timestamp
}
func (e *Entry) Timestamp() int64 {
return e.timestamp
}
func (e *Entry) Encode() []byte {
var offset int
klen := uint8(len(e.Key()))
vlen := len(e.Value())
length := 21 + len(e.Key()) + vlen
buf := make([]byte, length)
// Set key length. It's 1 byte.
copy(buf[offset:], []byte{klen})
offset++
// Set the key.
copy(buf[offset:], e.Key())
offset += len(e.Key())
// Set the TTL. It's 8 bytes.
binary.BigEndian.PutUint64(buf[offset:], uint64(e.TTL()))
offset += 8
// Set the Timestamp. It's 8 bytes.
binary.BigEndian.PutUint64(buf[offset:], uint64(e.Timestamp()))
offset += 8
// Set the value length. It's 4 bytes.
binary.BigEndian.PutUint32(buf[offset:], uint32(len(e.Value())))
offset += 4
// Set the value.
copy(buf[offset:], e.Value())
offset += len(e.Value())
return buf
}
func (e *Entry) Decode(buf []byte) {
var offset int
keyLength := int(buf[offset])
offset++
e.key = string(buf[offset : offset+keyLength])
offset += keyLength
e.ttl = int64(binary.BigEndian.Uint64(buf[offset : offset+8]))
offset += 8
e.timestamp = int64(binary.BigEndian.Uint64(buf[offset : offset+8]))
offset += 8
vlen := binary.BigEndian.Uint32(buf[offset : offset+4])
offset += 4
e.value = buf[offset : offset+int(vlen)]
}