-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht_zset.go
More file actions
411 lines (354 loc) · 7.68 KB
/
t_zset.go
File metadata and controls
411 lines (354 loc) · 7.68 KB
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
package main
import (
"bytes"
"fmt"
"github.com/pengdafu/redis-golang/dict"
"github.com/pengdafu/redis-golang/sds"
"github.com/pengdafu/redis-golang/util"
"github.com/pengdafu/redis-golang/ziplist"
"math"
"strconv"
"unsafe"
)
type zskiplist struct {
header, tail *zskiplistNode
length int64
level int
}
type zskiplistNode struct {
ele *sds.SDS
score float64
backward *zskiplistNode
level []*zskiplistLevel
}
type zskiplistLevel struct {
forward *zskiplistNode
span int64
}
type zset struct {
dict *dict.Dict
zsl *zskiplist
}
func zaddCommand(c *Client) {
zaddGenericCommand(c, zaddNone)
}
func zincrbyCommand(c *Client) {
zaddGenericCommand(c, zaddIncr)
}
const (
zaddNone = 0
zaddIncr = 1 << 0
zaddNx = 1 << 1
zaddXX = 1 << 2
zaddNop = 1 << 3
zaddNan = 1 << 4
zaddAdded = 1 << 5
zaddUpdated = 1 << 6
zaddCh = 1 << 16
)
func zaddGenericCommand(c *Client, flags int) {
nanErr := "resulting score is not number (NaN)"
key := c.argv[1]
var zobj *robj
var ele sds.SDS
var score float64
var scores []float64
var added int
var updated int
var processed int
scoreIdx := 2
for scoreIdx < c.argc {
opt := (*sds.SDS)(c.argv[scoreIdx].ptr).BufData(0)
if util.StrCaseCmp(opt, "nx") {
flags |= zaddNx
} else if util.StrCaseCmp(opt, "xx") {
flags |= zaddXX
} else if util.StrCaseCmp(opt, "ch") {
flags |= zaddCh
} else if util.StrCaseCmp(opt, "incr") {
flags |= zaddIncr
} else {
break
}
scoreIdx++
}
incr := (flags & zaddIncr) > 0
nx := (flags & zaddNx) > 0
xx := (flags & zaddXX) > 0
ch := (flags & zaddCh) > 0
elements := c.argc - scoreIdx
if elements%2 != 0 || elements == 0 {
addReply(c, shared.syntaxErr)
return
}
elements /= 2
if nx && xx {
addReplyError(c, "XX and NX options at the same time are not compatible")
return
}
if incr && elements > 1 {
addReplyError(c,
"INCR option supports a single increment-element pair")
return
}
scores = make([]float64, elements)
for j := 0; j < elements; j++ {
if !getDoubleFromObjectOrReply(c, c.argv[scoreIdx+j*2], &scores[j], "") {
goto cleanUp
}
}
zobj = c.db.lookupKeyWrite(key)
if zobj == nil {
if xx {
goto replyToClient
}
if server.zsetMaxZipListEntries == 0 ||
server.zsetMaxZipListValue < sds.Len(*(*sds.SDS)(c.argv[scoreIdx+1].ptr)) {
zobj = createZsetObject()
} else {
zobj = createZsetZiplistObject()
}
c.db.dbAdd(key, zobj)
} else {
if zobj.checkType(c, ObjSet) {
goto cleanUp
}
}
for j := 0; j < elements; j++ {
var newscore float64
score = scores[j]
retFlags := flags
ele = *(*sds.SDS)(c.argv[scoreIdx+1+j*2].ptr)
retval := zsetAdd(zobj, score, ele, &retFlags, &newscore)
if !retval {
addReplyError(c, nanErr)
goto cleanUp
}
if retFlags&zaddAdded > 0 {
added++
}
if retFlags&zaddUpdated > 0 {
updated++
}
if retFlags&zaddNop == 0 {
processed++
}
score = newscore
}
server.dirty++
replyToClient:
if incr {
if processed > 0 {
addReplyDouble(c, score)
} else {
addReplyNull(c)
}
} else {
num := added
if ch {
num += updated
}
addReplyLongLong(c, num)
}
cleanUp:
if added > 0 || updated > 0 {
signalModifiedKey(c, c.db, key)
cmd := "zincr"
if !incr {
cmd = "zadd"
}
notifyKeySpaceEvent(notifyZset, cmd, key, c.db.id)
}
return
}
func zsetAdd(zobj *robj, score float64, ele sds.SDS, flags *int, newscore *float64) bool {
incr := (*flags & zaddIncr) > 0
nx := (*flags & zaddNx) > 0
xx := (*flags & zaddXX) > 0
*flags = 0
var curScore float64
if math.IsNaN(score) {
*flags = zaddNan
return false
}
if zobj.getEncoding() == ObjEncodingZipList {
var eptr []byte
zl := *(*[]byte)(zobj.ptr)
if eptr = zzlFind(*(*[]byte)(zobj.ptr), ele, &curScore); eptr != nil {
if nx {
*flags |= zaddNop
return true
}
if incr {
score += curScore
if math.IsNaN(score) {
*flags |= zaddNan
return false
}
if newscore != nil {
*newscore = score
}
}
if score != curScore {
zl = zzlDelete(zl, eptr)
zl = zzlInsert(zl, ele, score)
*flags |= zaddUpdated
zobj.ptr = unsafe.Pointer(&zl)
}
return true
} else if !xx {
if zzlLength(zl)+1 > server.zsetMaxZipListEntries ||
sds.Len(ele) > server.zsetMaxZipListValue ||
!ziplist.SafeToAdd(zl, sds.Len(ele)) {
zsetConvert(zobj, ObjEncodingSkipList)
} else {
zl = zzlInsert(zl, ele, score)
zobj.ptr = unsafe.Pointer(&zl)
if newscore != nil {
*newscore = score
}
*flags |= zaddAdded
return true
}
} else {
*flags |= zaddNop
return true
}
}
if zobj.getEncoding() == ObjEncodingSkipList {
}
}
func zremCommand(c *Client) {
}
func zcountCommand(c *Client) {
}
func zcardCommand(c *Client) {
}
func zscoreCommand(c *Client) {
}
func zsetConvert(zobj *robj, encoding int) {
}
// zsl : zset skiplist
const (
zskiplistMaxLevel = 32
zskiplistP = 0.25
)
func zslCreate() *zskiplist {
zsl := new(zskiplist)
zsl.level = 1
zsl.length = 0
zsl.header = zslCreateNode(zskiplistMaxLevel, 0, nil)
for j := 0; j < zskiplistMaxLevel; j++ {
zsl.header.level[j].forward = nil
zsl.header.level[j].span = 0
}
zsl.header.backward = nil
zsl.tail = nil
return zsl
}
func zslCreateNode(level int, score float64, ele *sds.SDS) *zskiplistNode {
zn := new(zskiplistNode)
zn.level = make([]*zskiplistLevel, level)
for i := 0; i < level; i++ {
zn.level[i] = new(zskiplistLevel)
}
zn.score = score
zn.ele = ele
return zn
}
// zzl : zset ziplist
func zzlFind(zl []byte, ele sds.SDS, score *float64) []byte {
eptr := ziplist.Index(zl, 0)
var sptr []byte
for eptr != nil {
sptr = ziplist.Next(zl, eptr)
if ziplist.Compare(eptr, ele.BufData(0), sds.Len(ele)) {
if score != nil {
*score = zzlGetScore(sptr)
}
return eptr
}
eptr = ziplist.Next(zl, sptr)
}
return nil
}
func zzlGetScore(sptr []byte) float64 {
var vstr []byte
var vlen int
var vlong int64
ziplist.Get(sptr, &vstr, &vlen, &vlong)
if vstr != nil {
score, err := strconv.ParseFloat(string(vstr[:vlen]), 64)
if err != nil {
return 0
}
return score
} else {
return float64(vlong)
}
}
func zzlDelete(zl []byte, eptr []byte) []byte {
p := eptr
zl = ziplist.Delete(zl, &p)
zl = ziplist.Delete(zl, &p)
return zl
}
func zzlInsertAt(zl []byte, eptr []byte, ele sds.SDS, score float64) []byte {
scoreBuf := fmt.Sprintf("%v", score)
if eptr == nil {
zl = ziplist.Push(zl, ele.BufData(0), ziplist.Tail)
zl = ziplist.Push(zl, []byte(scoreBuf), ziplist.Tail)
} else {
offset := int(uintptr(unsafe.Pointer(&eptr[0])) - uintptr(unsafe.Pointer(&zl[0])))
zl = ziplist.Insert(zl, eptr, ele.BufData(0))
eptr = zl[offset:]
sptr := ziplist.Next(zl, eptr)
zl = ziplist.Insert(zl, sptr, []byte(scoreBuf))
}
return zl
}
func zzlInsert(zl []byte, ele sds.SDS, score float64) []byte {
eptr := ziplist.Index(zl, 0)
var sptr []byte
for eptr != nil {
sptr = ziplist.Next(zl, eptr)
s := zzlGetScore(sptr)
if s > score {
zl = zzlInsertAt(zl, eptr, ele, score)
break
} else if score == s {
if zzlCompareElements(eptr, ele.BufData(0), sds.Len(ele)) > 0 {
zl = zzlInsertAt(zl, eptr, ele, score)
break
}
}
eptr = ziplist.Next(zl, sptr)
}
if eptr == nil {
zl = zzlInsertAt(zl, nil, ele, score)
}
return zl
}
func zzlCompareElements(eptr, cstr []byte, clen int) int {
var vstr []byte
var vlen int
var vlong int64
ziplist.Get(eptr, &vstr, &vlen, &vlong)
if vstr == nil {
vbuf := fmt.Sprintf("%v", vlong)
vstr = []byte(vbuf)
vlen = len(vstr)
}
minLen := clen
if vlen < clen {
minLen = vlen
}
cmp := bytes.Compare(vstr[:minLen], cstr[:minLen])
if cmp == 0 {
return vlen - clen
}
return cmp
}
func zzlLength(zl []byte) int {
return ziplist.Len(zl) / 2
}