-
Notifications
You must be signed in to change notification settings - Fork 3
/
pebble_all.go
203 lines (166 loc) · 4 KB
/
pebble_all.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//go:build !(386 || arm)
package disk
import (
"bytes"
"strconv"
"sync"
"time"
"github.com/cockroachdb/pebble"
)
func init() {
OpenPebbleDB = openPebbleDB
}
// PebbleDB - represents a pebble db implementation
type PebbleDB struct {
db *pebble.DB
sync.RWMutex
}
// openPebbleDB - Opens the specified path
func openPebbleDB(path string) (DB, error) {
db, err := pebble.Open(path, &pebble.Options{})
if err != nil {
return nil, err
}
pdb := new(PebbleDB)
pdb.db = db
return pdb, nil
}
// Size - returns the size of the database in bytes
func (pdb *PebbleDB) Size() int64 {
metrics := pdb.db.Metrics()
if metrics == nil {
return 0
}
return metrics.Total().Size
}
// Close ...
func (pdb *PebbleDB) Close() {
pdb.db.Close()
}
// GC - runs the garbage collector
func (pdb *PebbleDB) GC() error {
// find first and last key
iter := pdb.db.NewIter(&pebble.IterOptions{})
first := iter.Key()
var last []byte
for iter.Next() {
if iter.Last() {
last = iter.Key()
}
}
return pdb.db.Compact(first, last, false)
}
// Incr - increment the key by the specified value
func (pdb *PebbleDB) Incr(k string, by int64) (int64, error) {
pdb.Lock()
defer pdb.Unlock()
val, err := pdb.get(k)
if err != nil {
val = []byte{}
}
valFloat, _ := strconv.ParseInt(string(val), 10, 64)
valFloat += by
err = pdb.set([]byte(k), intToByteSlice(valFloat), -1)
if err != nil {
return 0, err
}
return valFloat, nil
}
func (pdb *PebbleDB) set(k, v []byte, ttl time.Duration) error {
var expires int64
if ttl > 0 {
expires = time.Now().Add(ttl).Unix()
}
expiresBytes := append(intToByteSlice(expires), expSeparator[:]...)
v = append(expiresBytes, v...)
return pdb.db.Set(k, v, pebble.Sync)
}
// Set - sets a key with the specified value and optional ttl
func (pdb *PebbleDB) Set(k string, v []byte, ttl time.Duration) error {
return pdb.set([]byte(k), v, ttl)
}
// MSet - sets multiple key-value pairs
func (pdb *PebbleDB) MSet(data map[string][]byte) error {
return nil
}
func (pdb *PebbleDB) get(k string) ([]byte, error) {
var data []byte
var err error
delete := false
s, closer, err := pdb.db.Get([]byte(k))
if err != nil {
return []byte{}, err
}
defer closer.Close()
// make a copy of the byte slice as we need to return it safely
item := append(s[:0:0], s...)
parts := bytes.SplitN(item, []byte(expSeparator), 2)
expires, actual := parts[0], parts[1]
if exp, _ := strconv.Atoi(string(expires)); exp > 0 && int(time.Now().Unix()) >= exp {
delete = true
} else {
data = actual
}
if delete {
err := pdb.db.Delete([]byte(k), pebble.Sync)
if err != nil {
return data, err
}
return data, ErrNotFound
}
return data, nil
}
// Get - fetches the value of the specified k
func (pdb *PebbleDB) Get(k string) ([]byte, error) {
return pdb.get(k)
}
// MGet - fetch multiple values of the specified keys
func (pdb *PebbleDB) MGet(keys []string) [][]byte {
var data [][]byte
for _, key := range keys {
val, err := pdb.get(key)
if err != nil {
data = append(data, []byte{})
continue
}
data = append(data, val)
}
return data
}
// TTL - returns the time to live of the specified key's value
func (pdb *PebbleDB) TTL(key string) int64 {
item, closer, err := pdb.db.Get([]byte(key))
if err != nil {
return -2
}
defer closer.Close()
parts := bytes.SplitN(item, []byte(expSeparator), 2)
exp, _ := strconv.Atoi(string(parts[0]))
if exp == 0 {
return -1
}
now := time.Now().Unix()
if now >= int64(exp) {
return -2
}
return int64(exp) - now
}
// MDel - removes key(s) from the store
func (pdb *PebbleDB) MDel(keys []string) error {
return nil
}
// Del - removes key from the store
func (pdb *PebbleDB) Del(key string) error {
return pdb.db.Delete([]byte(key), pebble.Sync)
}
// Scan - iterate over the whole store using the handler function
func (pdb *PebbleDB) Scan(scannerOpt ScannerOptions) error {
iter := pdb.db.NewIter(nil)
for iter.First(); iter.Valid(); iter.Next() {
key, val := iter.Key(), iter.Value()
if scannerOpt.Handler(key, val) != nil {
break
}
}
return nil
}