-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
413 lines (375 loc) · 7.82 KB
/
string.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package main
import (
"github.com/gushitong/aryadb/stor"
"github.com/gushitong/aryadb/ut"
"math/bits"
"strconv"
"time"
)
func _append(db stor.DB, conn aryConnection, cmd aryCommand) {
var n int
err := db.Update(func(txn stor.Transaction) error {
val, err := txn.Get(cmd.StringKey())
if err != nil {
return err
}
val = append(val, cmd.Args[1]...)
n = len(val)
if err := txn.Set(cmd.StringKey(), val); err != nil {
return err
}
return nil
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteInt(n)
}
func bitcount(db stor.DB, conn aryConnection, cmd aryCommand) {
var n int
err := db.View(func(txn stor.Transaction) error {
val, err := txn.Get(cmd.StringKey())
if err != nil {
return err
}
if val == nil {
n = 0
return nil
}
for i := 0; i < len(val); i++ {
n += bits.OnesCount8(val[i])
}
return nil
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteInt(n)
}
func decr(db stor.DB, conn aryConnection, cmd aryCommand) {
var v int64
err := db.Update(func(txn stor.Transaction) error {
n, err := txn.IncrBy(cmd.StringKey(), -1)
if err != nil {
return err
}
v = n
return nil
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteInt64(v)
}
func decrby(db stor.DB, conn aryConnection, cmd aryCommand) {
var v int64
err := db.Update(func(txn stor.Transaction) error {
num, err := strconv.ParseInt(string(cmd.Args[1]), 10, 64)
if err != nil {
return err
}
n, err := txn.IncrBy(cmd.StringKey(), -1*num)
if err != nil {
return err
}
v = n
return nil
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteInt64(v)
}
func get(db stor.DB, conn aryConnection, cmd aryCommand) {
var v []byte
err := db.View(func(txn stor.Transaction) error {
val, err := txn.Get(cmd.StringKey())
if err != nil {
return err
}
v = val
return nil
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteBulk(v)
}
func getbit(db stor.DB, conn aryConnection, cmd aryCommand) {
var v int
err := db.View(func(txn stor.Transaction) error {
val, err := txn.Get(cmd.StringKey())
if err != nil {
return err
}
i, err := strconv.Atoi(string(cmd.Args[1]))
if err != nil {
return err
}
if val == nil || len(val)*8 < i {
v = 0
return nil
}
v = int(val[i/8]>>uint(i%8)) & 1
return nil
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteInt(v)
}
func getrange(db stor.DB, conn aryConnection, cmd aryCommand) {
var s string
err := db.View(func(txn stor.Transaction) error {
val, err := txn.Get(cmd.StringKey())
if err != nil {
return err
}
start, err := ut.ParseInt64(cmd.Args[1])
if err != nil {
return err
}
end, err := ut.ParseInt64(cmd.Args[2])
if err != nil {
return err
}
l := int64(len(val))
o1 := ut.FixBoundary(l, start)
o2 := ut.FixBoundary(l, end)
if o2 == l-1 {
s = string(val[o1:])
} else {
s = string(val[o1:o2]) + string(val[o2])
}
return nil
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteString(s)
}
func getset(db stor.DB, conn aryConnection, cmd aryCommand) {
var v string
err := db.Update(func(txn stor.Transaction) error {
val, err := txn.Get(cmd.StringKey())
if err != nil {
return err
}
v = string(val)
return txn.Set(cmd.StringKey(), cmd.Args[1])
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteString(v)
}
func incr(db stor.DB, conn aryConnection, cmd aryCommand) {
var v int64
err := db.Update(func(txn stor.Transaction) error {
val, err := txn.IncrBy(cmd.StringKey(), 1)
if err != nil {
return err
}
v = val
return nil
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteInt64(v)
}
func incrby(db stor.DB, conn aryConnection, cmd aryCommand) {
var v int64
err := db.Update(func(txn stor.Transaction) error {
n, err := strconv.ParseInt(string(cmd.Args[1]), 10, 64)
if err != nil {
return err
}
val, err := txn.IncrBy(cmd.StringKey(), n)
if err != nil {
return err
}
v = val
return nil
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteInt64(v)
}
func incrfloat(db stor.DB, conn aryConnection, cmd aryCommand) {
var v float64
err := db.Update(func(txn stor.Transaction) error {
n1, err := strconv.ParseFloat(string(cmd.Args[1]), 64)
if err != nil {
return err
}
val, err := txn.Get(cmd.StringKey())
if err != nil {
return err
}
n2, err := strconv.ParseFloat(string(val), 64)
if err != nil {
return err
}
v = n1 + n2
return txn.Set(cmd.Args[1], []byte(strconv.FormatFloat(v, 'f', -1, 64)))
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
}
func mget(db stor.DB, conn aryConnection, cmd aryCommand) {
v := make([][]byte, 0)
db.View(func(txn stor.Transaction) error {
for _, key := range cmd.Args {
if val, err := txn.Get(NewStringEncoder(key).Encode()); err != nil {
v = append(v, nil)
} else {
v = append(v, val)
}
}
return nil
})
conn.WriteArray(len(v))
for _, val := range v {
conn.WriteBulk(val)
}
}
func mset(db stor.DB, conn aryConnection, cmd aryCommand) {
err := db.Update(func(txn stor.Transaction) error {
if len(cmd.Args)%2 != 1 {
return ErrWrongNumOfArguments
}
for i := 1; i < len(cmd.Args); i++ {
if err := txn.Set(cmd.StringKey(), cmd.Args[i+1]); err != nil {
return err
}
}
return nil
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteString("OK")
}
func msetnx(db stor.DB, conn aryConnection, cmd aryCommand) {
err := db.Update(func(txn stor.Transaction) error {
if len(cmd.Args)%2 != 1 {
return ErrWrongNumOfArguments
}
for i := 1; i < len(cmd.Args); i += 2 {
if val, err := txn.Get(cmd.StringKey()); err != nil {
return err
} else if val != nil {
return ErrKeyExists
}
}
for i := 1; i < len(cmd.Args); i++ {
if err := txn.Set(cmd.StringKey(), cmd.Args[i+1]); err != nil {
return err
}
}
return nil
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteInt(1)
}
func set(db stor.DB, conn aryConnection, cmd aryCommand) {
err := db.Update(func(txn stor.Transaction) error {
return txn.Set(cmd.StringKey(), cmd.Args[1])
})
if err != nil {
conn.WriteError(err.Error())
return
}
conn.WriteString("OK")
}
func setbit(db stor.DB, conn aryConnection, cmd aryCommand) {
v := 0
err := db.Update(func(txn stor.Transaction) error {
b := 0
if string(cmd.Args[2]) == "1" {
b = 1
} else if string(cmd.Args[2]) != "0" {
return ErrBitValue
}
pos, err := strconv.Atoi(string(cmd.Args[1]))
if err != nil {
return ErrBitOffset
}
val, err := txn.Get(cmd.StringKey())
if pos+1 > len(val)*8 {
return nil
}
if b == 0 {
val[pos/8] = ut.ClearBit(val[pos/8], uint(pos%8))
} else {
val[pos/8] = ut.SetBit(val[pos/8], uint(pos%8))
}
v = 1
return nil
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteInt(v)
}
func setex(db stor.DB, conn aryConnection, cmd aryCommand) {
err := db.Update(func(txn stor.Transaction) error {
val, err := strconv.Atoi(string(cmd.Args[1]))
if err != nil {
return ErrIntegerValue
}
return txn.SetWithTTL(cmd.StringKey(), cmd.Args[2], time.Duration(val)*time.Second)
})
if err != nil {
conn.WriteErr(err)
return
}
conn.WriteString("OK")
}
func setnx(db stor.DB, conn aryConnection, cmd aryCommand) {
var v bool
db.Update(func(txn stor.Transaction) error {
if val, _ := txn.Get(cmd.StringKey()); val != nil {
return nil
} else {
v = true
return txn.Set(cmd.StringKey(), cmd.Args[1])
}
})
conn.WriteBool(v)
}
func setrange(db stor.DB, conn aryConnection, cmd aryCommand) {
conn.WriteErr(ErrCommandNotSupported)
}
func strlen(db stor.DB, conn aryConnection, cmd aryCommand) {
var n int
db.View(func(txn stor.Transaction) error {
if val, _ := txn.Get(cmd.StringKey()); val == nil {
n = 0
} else {
n = len(val)
}
return nil
})
conn.WriteInt(n)
}