-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.go
359 lines (332 loc) · 9.04 KB
/
str.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package nodis
import (
"strconv"
"time"
"unsafe"
"github.com/diiyw/nodis/ds"
"github.com/diiyw/nodis/ds/str"
"github.com/diiyw/nodis/patch"
)
func (n *Nodis) newStr() ds.Value {
return str.NewString()
}
// Set a key with a value and a TTL
func (n *Nodis) Set(key string, value []byte, keepTTL bool) {
_ = n.exec(func(tx *Tx) error {
meta := tx.writeKey(key, n.newStr)
meta.value.(*str.String).Set(value)
if !keepTTL {
meta.key.Expiration = 0
}
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: value, KeepTTL: keepTTL}}}
})
return nil
})
}
// GetSet set a key with a value and return the old value
func (n *Nodis) GetSet(key string, value []byte) []byte {
var v []byte
_ = n.exec(func(tx *Tx) error {
meta := tx.writeKey(key, n.newStr)
v = meta.value.(*str.String).GetSet(value)
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: value}}}
})
return nil
})
return v
}
// SetEX set a key with specified expire time, in seconds (a positive integer).
func (n *Nodis) SetEX(key string, value []byte, seconds int64) {
_ = n.exec(func(tx *Tx) error {
meta := tx.writeKey(key, n.newStr)
meta.key.Expiration = time.Now().UnixMilli()
meta.key.Expiration += seconds * 1000
meta.value.(*str.String).Set(value)
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: value, Expiration: meta.key.Expiration}}}
})
return nil
})
}
// SetPX set a key with specified expire time, in milliseconds (a positive integer).
func (n *Nodis) SetPX(key string, value []byte, milliseconds int64) {
_ = n.exec(func(tx *Tx) error {
meta := tx.writeKey(key, n.newStr)
meta.key.Expiration = time.Now().UnixMilli()
meta.key.Expiration += milliseconds
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: value, Expiration: meta.key.Expiration}}}
})
meta.value.(*str.String).Set(value)
return nil
})
}
// SetNX set a key with a value if it does not exist
func (n *Nodis) SetNX(key string, value []byte, keepTTL bool) bool {
var ok bool
_ = n.exec(func(tx *Tx) error {
meta := tx.writeKey(key, nil)
if meta.isOk() {
return nil
}
meta = tx.newKey(meta, key, n.newStr)
if !keepTTL {
meta.key.Expiration = 0
}
meta.value.(*str.String).Set(value)
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: value, KeepTTL: keepTTL}}}
})
ok = true
return nil
})
return ok
}
// SetXX set a key with a value if it exists
func (n *Nodis) SetXX(key string, value []byte, keepTTL bool) bool {
var ok bool
_ = n.exec(func(tx *Tx) error {
meta := tx.writeKey(key, nil)
if !meta.isOk() {
return nil
}
meta.value.(*str.String).Set(value)
if !keepTTL {
meta.key.Expiration = 0
}
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: value, KeepTTL: keepTTL}}}
})
ok = true
return nil
})
return ok
}
// Get a key
func (n *Nodis) Get(key string) []byte {
var v []byte
_ = n.exec(func(tx *Tx) error {
meta := tx.readKey(key)
if !meta.isOk() {
return nil
}
v = meta.value.(*str.String).Get()
return nil
})
return v
}
// Incr increment the integer value of a key by one
func (n *Nodis) Incr(key string) (int64, error) {
var v int64
err := n.exec(func(tx *Tx) error {
var err error
meta := tx.writeKey(key, n.newStr)
v, err = meta.value.(*str.String).Incr(1)
if err != nil {
return err
}
vv := strconv.FormatInt(v, 10)
m := unsafe.Slice(unsafe.StringData(vv), len(vv))
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: m}}}
})
return nil
})
return v, err
}
func (n *Nodis) IncrBy(key string, increment int64) (int64, error) {
var v int64
err := n.exec(func(tx *Tx) error {
var err error
meta := tx.writeKey(key, n.newStr)
v, err = meta.value.(*str.String).Incr(increment)
if err != nil {
return nil
}
vv := strconv.FormatInt(v, 10)
m := unsafe.Slice(unsafe.StringData(vv), len(vv))
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: m}}}
})
return nil
})
return v, err
}
// Decr decrement the integer value of a key by one
func (n *Nodis) Decr(key string) (int64, error) {
var v int64
err := n.exec(func(tx *Tx) error {
var err error
meta := tx.writeKey(key, n.newStr)
v, err = meta.value.(*str.String).Decr(1)
if err != nil {
return err
}
vv := strconv.FormatInt(v, 10)
m := unsafe.Slice(unsafe.StringData(vv), len(vv))
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: m}}}
})
return nil
})
return v, err
}
func (n *Nodis) DecrBy(key string, decrement int64) (int64, error) {
var v int64
err := n.exec(func(tx *Tx) error {
var err error
meta := tx.writeKey(key, n.newStr)
v, err = meta.value.(*str.String).Decr(decrement)
if err != nil {
return err
}
vv := strconv.FormatInt(v, 10)
m := unsafe.Slice(unsafe.StringData(vv), len(vv))
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: m}}}
})
return nil
})
return v, err
}
func (n *Nodis) IncrByFloat(key string, increment float64) (float64, error) {
var v float64
err := n.exec(func(tx *Tx) error {
var err error
meta := tx.writeKey(key, n.newStr)
v, err = meta.value.(*str.String).IncrByFloat(increment)
if err != nil {
return err
}
vv := strconv.FormatFloat(v, 'f', -1, 64)
m := unsafe.Slice(unsafe.StringData(vv), len(vv))
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: m}}}
})
return nil
})
return v, err
}
// SetBit set a bit in a key
func (n *Nodis) SetBit(key string, offset int64, value bool) int64 {
var v int64
_ = n.exec(func(tx *Tx) error {
meta := tx.writeKey(key, n.newStr)
k := meta.value.(*str.String)
v = k.SetBit(offset, value)
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: k.Get()}}}
})
return nil
})
return v
}
// GetBit get a bit in a key
func (n *Nodis) GetBit(key string, offset int64) int64 {
var v int64
_ = n.exec(func(tx *Tx) error {
meta := tx.readKey(key)
if !meta.isOk() {
return nil
}
v = meta.value.(*str.String).GetBit(offset)
return nil
})
return v
}
// BitCount returns the number of bits set to 1
func (n *Nodis) BitCount(key string, start, end int64, bit bool) int64 {
var v int64
_ = n.exec(func(tx *Tx) error {
meta := tx.readKey(key)
if !meta.isOk() {
return nil
}
if bit {
v = meta.value.(*str.String).BitCountByBit(start, end)
} else {
v = meta.value.(*str.String).BitCount(start, end)
}
return nil
})
return v
}
// Append a value to a key
func (n *Nodis) Append(key string, value []byte) int64 {
var v int64
_ = n.exec(func(tx *Tx) error {
meta := tx.writeKey(key, n.newStr)
k := meta.value.(*str.String)
v = k.Append(value)
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: k.Get()}}}
})
return nil
})
return v
}
// GetRange returns the substring of the string value stored at key, determined by the offsets start and end (both are inclusive).
func (n *Nodis) GetRange(key string, start, end int64) []byte {
var v []byte
_ = n.exec(func(tx *Tx) error {
meta := tx.readKey(key)
if !meta.isOk() {
return nil
}
v = meta.value.(*str.String).GetRange(start, end)
return nil
})
return v
}
// StrLen returns the length of the string value stored at key
func (n *Nodis) StrLen(key string) int64 {
var v int64
_ = n.exec(func(tx *Tx) error {
meta := tx.readKey(key)
if !meta.isOk() {
return nil
}
v = meta.value.(*str.String).Strlen()
return nil
})
return v
}
// SetRange overwrite part of a string at key starting at the specified offset
func (n *Nodis) SetRange(key string, offset int64, value []byte) int64 {
var v int64
_ = n.exec(func(tx *Tx) error {
meta := tx.writeKey(key, n.newStr)
k := meta.value.(*str.String)
v = k.SetRange(offset, value)
n.signalModifiedKey(key, meta)
n.notify(func() []patch.Op {
return []patch.Op{{Type: patch.OpTypeSet, Data: &patch.OpSet{Key: key, Value: k.Get()}}}
})
return nil
})
return v
}
// MSet sets the given keys to their respective values
func (n *Nodis) MSet(pairs ...string) {
if len(pairs)%2 != 0 {
return
}
for i := 0; i < len(pairs); i += 2 {
n.Set(pairs[i], unsafe.Slice(unsafe.StringData(pairs[i+1]), len(pairs[i+1])), false)
}
}