-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht_hash.go
More file actions
513 lines (457 loc) · 11.4 KB
/
t_hash.go
File metadata and controls
513 lines (457 loc) · 11.4 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package main
import (
"fmt"
"github.com/pengdafu/redis-golang/dict"
"github.com/pengdafu/redis-golang/sds"
"github.com/pengdafu/redis-golang/ziplist"
"unsafe"
)
func hsetCommand(c *Client) {
if c.argc%2 == 1 {
addReplyErrorFormat(c, "wrong number of arguments for '%s' command", c.cmd.name)
return
}
var o *robj
if o = hashTypeLookupWriteOrCreate(c, c.argv[1]); o == nil {
return
}
hashTypeTryConversion(o, c.argv, 2, c.argc-1)
var created int
for i := 2; i < c.argc; i += 2 {
created += (hashTypeSet(o, *(*sds.SDS)(c.argv[i].ptr), *(*sds.SDS)(c.argv[i+1].ptr), hashSetCopy) + 1) % 2
}
cmdname := (*sds.SDS)(c.argv[0].ptr).BufData(0)
if cmdname[1] == 's' || cmdname[1] == 'S' {
addReplyLongLong(c, created)
} else {
addReply(c, shared.ok)
}
signalModifiedKey(c, c.db, c.argv[1])
notifyKeySpaceEvent(notifyHash, "hset", c.argv[1], c.db.id)
server.dirty++
}
const (
hashSetCopy = 0
hashSetTakeFiled = 1 << 0
hashSetTakeValue = 1 << 1
)
func hashTypeSet(o *robj, field, value sds.SDS, flags int) int {
var update = 0
if o.getEncoding() == ObjEncodingZipList {
zl := *(*[]byte)(o.ptr)
fptr := ziplist.Index(zl, ziplist.Head)
var vptr []byte
if fptr != nil {
fptr = ziplist.Find(fptr, field.BufData(0), sds.Len(field), 1)
if fptr != nil {
vptr = ziplist.Next(zl, fptr)
update = 1
zl = ziplist.Delete(zl, &vptr)
zl = ziplist.Insert(zl, vptr, value.BufData(0))
}
}
if update == 0 {
zl = ziplist.Push(zl, field.BufData(0), ziplist.Tail)
zl = ziplist.Push(zl, value.BufData(0), ziplist.Tail)
}
o.ptr = unsafe.Pointer(&zl)
if hashTypeLength(o) > server.hashMaxZipListEntries {
hashTypeConvert(o, ObjEncodingHt)
}
} else if o.getEncoding() == ObjEncodingHt {
d := (*dict.Dict)(o.ptr)
de := d.Find(unsafe.Pointer(&field))
if de != nil {
if flags&hashSetTakeValue > 0 {
dict.SetVal(de, unsafe.Pointer(&value))
} else {
dup := sds.Dup(value)
dict.SetVal(de, unsafe.Pointer(&dup))
}
update = 1
} else {
var f, v sds.SDS
if flags&hashSetTakeFiled > 0 {
f = field
} else {
f = sds.Dup(field)
}
if flags&hashSetTakeValue > 0 {
v = value
} else {
v = sds.Dup(value)
}
d.Add(unsafe.Pointer(&f), unsafe.Pointer(&v))
}
} else {
panic("Unknown hash encoding")
}
if flags&hashSetTakeFiled > 0 {
//sdsFree(field)
}
if flags&hashSetTakeValue > 0 {
//sdsFree(value)
}
return update
}
func hgetCommand(c *Client) {
var o *robj
if o = lookupKeyReadOrReply(c, c.argv[1], shared.null[c.resp]); o == nil || o.checkType(c, ObjHash) {
return
}
addHashFieldToReply(c, o, *(*sds.SDS)(c.argv[2].ptr))
}
func hmgetCommand(c *Client) {
o := c.db.lookupKeyRead(c.argv[1])
if o != nil && o.getType() != ObjHash {
addReply(c, shared.wrongTypeErr)
return
}
addReplyArrayLen(c, c.argc-2)
for i := 2; i < c.argc; i++ {
addHashFieldToReply(c, o, *(*sds.SDS)(c.argv[i].ptr))
}
}
func hdelCommand(c *Client) {
var o *robj
if o = lookupKeyWriteOrReply(c, c.argv[1], shared.czero); o == nil || o.checkType(c, ObjHash) {
return
}
var deleted int
var keyRemoved bool
for j := 2; j < c.argc; j++ {
if hashTypeDelete(o, *(*sds.SDS)(c.argv[j].ptr)) {
deleted++
}
if hashTypeLength(o) == 0 {
dbDelete(c.db, c.argv[1])
keyRemoved = true
break
}
}
if deleted > 0 {
signalModifiedKey(c, c.db, c.argv[1])
notifyKeySpaceEvent(notifyHash, "hdel", c.argv[1], c.db.id)
if keyRemoved {
notifyKeySpaceEvent(notifyGeneric, "del", c.argv[1], c.db.id)
}
server.dirty++
}
addReplyLongLong(c, deleted)
}
func hashTypeDelete(o *robj, field sds.SDS) bool {
var deleted bool
if o.getEncoding() == ObjEncodingZipList {
zl := *(*[]byte)(o.ptr)
fptr := ziplist.Index(zl, ziplist.Head)
if fptr != nil {
fptr = ziplist.Find(fptr, field.BufData(0), sds.Len(field), 1)
if fptr != nil {
zl = ziplist.Delete(zl, &fptr) // delete key
zl = ziplist.Delete(zl, &fptr) // delete value
o.ptr = unsafe.Pointer(&zl)
deleted = true
}
}
} else if o.getEncoding() == ObjEncodingHt {
d := (*dict.Dict)(o.ptr)
if d.Delete(unsafe.Pointer(&field)) {
deleted = true
if htNeedResize(d) {
d.Resize()
}
}
} else {
panic("Unknown hash encoding")
}
return deleted
}
func hgetallCommand(c *Client) {
genericHgetallCommand(c, objHashKey|objHashValue)
}
func hkeysCommand(c *Client) {
genericHgetallCommand(c, objHashKey)
}
func hvalsCommand(c *Client) {
genericHgetallCommand(c, objHashValue)
}
func addHashIteratorCursorToReply(c *Client, hi *hashTypeIterator, what int) {
if hi.encoding == ObjEncodingZipList {
var vstr []byte
var vlen int
var vll int64
hashTypeCurrentFromZiplist(hi, what, &vstr, &vlen, &vll)
if vstr != nil {
addReplyBulkBuffer(c, vstr, vlen)
} else {
addReplyBulkLongLong(c, vll)
}
} else if hi.encoding == ObjEncodingHt {
value := hashTypeCurrentFromHashTable(hi, what)
addReplyBulkBuffer(c, value.BufData(0), sds.Len(value))
} else {
panic("Unknown hash encoding")
}
}
func genericHgetallCommand(c *Client, flags int) {
var o *robj
var hi *hashTypeIterator
emptyResp := shared.emptyMap[c.resp]
if !(flags&objHashKey > 0 && flags&objHashValue > 0) {
emptyResp = shared.emptyArray
}
if o = lookupKeyReadOrReply(c, c.argv[1], emptyResp); o == nil || o.checkType(c, ObjHash) {
return
}
length := hashTypeLength(o)
if flags&objHashKey > 0 && flags&objHashValue > 0 {
addReplyMapLen(c, length)
} else {
addReplyArrayLen(c, length)
}
var count int
hi = hashTypeInitIterator(o)
for hashTypeNext(hi) != C_ERR {
if flags&objHashKey > 0 {
addHashIteratorCursorToReply(c, hi, objHashKey)
count++
}
if flags&objHashValue > 0 {
addHashIteratorCursorToReply(c, hi, objHashValue)
count++
}
}
hashTypeReleaseIterator(hi)
if flags&objHashKey > 0 && flags&objHashValue > 0 {
count /= 2
}
if count != length {
panic(fmt.Sprintf("genericHgetallCommand count != length(%d! = %d)", count, length))
}
}
func addHashFieldToReply(c *Client, o *robj, field sds.SDS) {
if o == nil {
addReplyNull(c)
return
}
if o.getEncoding() == ObjEncodingZipList {
var vstr []byte
var vlen int
var vll int64
if hashTypeGetFromZiplist(o, field, &vstr, &vlen, &vll) < 0 {
addReplyNull(c)
} else {
if vstr != nil {
addReplyBulkBuffer(c, vstr, vlen)
} else {
addReplyBulkLongLong(c, vll)
}
}
} else if o.getEncoding() == ObjEncodingHt {
value := hashTypeGetFromHashTable(o, field)
if value != nil {
addReplyBulkBuffer(c, value.BufData(0), sds.Len(*value))
} else {
addReplyNull(c)
}
} else {
panic("Unknown hash encoding")
}
}
func hashTypeGetFromHashTable(o *robj, field sds.SDS) *sds.SDS {
d := (*dict.Dict)(o.ptr)
de := d.Find(unsafe.Pointer(&field))
if de == nil {
return nil
}
return (*sds.SDS)(dict.GetVal(de))
}
func hashTypeGetFromZiplist(o *robj, field sds.SDS, vstr *[]byte, vlen *int, vll *int64) (ret int) {
zl := *(*[]byte)(o.ptr)
fptr := ziplist.Index(zl, ziplist.Head)
var vptr []byte
if fptr != nil {
fptr = ziplist.Find(fptr, field.BufData(0), sds.Len(field), 1)
if fptr != nil {
vptr = ziplist.Next(zl, fptr)
}
}
if vptr != nil {
if !ziplist.Get(vptr, vstr, vlen, vll) {
return -1
}
return 0
}
return -1
}
func hashTypeLookupWriteOrCreate(c *Client, key *robj) *robj {
o := c.db.lookupKeyWrite(key)
if o == nil {
o = createHashObject()
c.db.dbAdd(key, o)
} else {
if o.getType() != ObjHash {
addReply(c, shared.wrongTypeErr)
return nil
}
}
return o
}
func hashTypeTryConversion(o *robj, argv []*robj, start, end int) {
sum := 0
if o.getEncoding() != ObjEncodingZipList {
return
}
for i := start; i <= end; i++ {
if !argv[i].sdsEncodedObject() {
continue
}
l := sds.Len(*(*sds.SDS)(o.ptr))
if l > server.hashMaxZipListValue {
hashTypeConvert(o, ObjEncodingHt)
return
}
sum += l
}
if !ziplist.SafeToAdd(*(*[]byte)(o.ptr), sum) {
hashTypeConvert(o, ObjEncodingHt)
}
}
func hashTypeConvert(o *robj, enc uint32) {
if o.getEncoding() == ObjEncodingZipList {
hashTypeConvertZiplist(o, enc)
} else if o.getEncoding() == ObjEncodingHt {
panic("Not implemented")
} else {
panic("Unknown hash encoding")
}
}
type hashTypeIterator struct {
subject *robj
encoding int
fptr, vptr []byte // ziplist fptr: key, vptr: value
de *dict.Entry
di *dict.Iterator
}
const (
objHashKey = 1
objHashValue = 2
)
func hashTypeConvertZiplist(o *robj, enc uint32) {
if enc == ObjEncodingZipList {
/* Nothing to do */
} else if enc == ObjEncodingHt {
hi := hashTypeInitIterator(o)
dt := dict.Create(hashDictType, nil)
for hashTypeNext(hi) != C_ERR {
key := hashTypeCurrentObjectNewSds(hi, objHashKey)
value := hashTypeCurrentObjectNewSds(hi, objHashValue)
if !dt.Add(unsafe.Pointer(&key), unsafe.Pointer(&value)) {
panic("Ziplist corruption detected")
}
}
hashTypeReleaseIterator(hi)
o.setEncoding(ObjEncodingHt)
o.ptr = unsafe.Pointer(dt)
} else {
panic("Unknown hash encoding")
}
}
func hashTypeLength(o *robj) (l int) {
if o.getEncoding() == ObjEncodingZipList {
l = ziplist.Len(*(*[]byte)(o.ptr)) / 2 // kv
} else if o.getEncoding() == ObjEncodingHt {
l = int((*dict.Dict)(o.ptr).Size())
} else {
panic("Unknown hash encoding")
}
return l
}
func hashTypeCurrentObjectNewSds(hi *hashTypeIterator, what int) sds.SDS {
var vstr []byte
var vlen int
var vll int64
hashTypeCurrentObject(hi, what, &vstr, &vlen, &vll)
if vstr != nil {
return sds.NewLen(vstr)
}
return sds.NewLen(fmt.Sprintf("%d", vll))
}
func hashTypeCurrentObject(hi *hashTypeIterator, what int, vstr *[]byte, vlen *int, vll *int64) {
if hi.encoding == ObjEncodingZipList {
*vstr = nil
hashTypeCurrentFromZiplist(hi, what, vstr, vlen, vll)
} else if hi.encoding == ObjEncodingHt {
ele := hashTypeCurrentFromHashTable(hi, what)
*vstr = ele.BufData(0)
*vlen = sds.Len(ele)
} else {
panic("Unknown hash encoding")
}
}
func hashTypeCurrentFromHashTable(hi *hashTypeIterator, what int) sds.SDS {
var p unsafe.Pointer
if what&objHashKey > 0 {
p = dict.GetKey(hi.de)
} else {
p = dict.GetVal(hi.de)
}
return *(*sds.SDS)(p)
}
func hashTypeCurrentFromZiplist(hi *hashTypeIterator, what int, vstr *[]byte, vlen *int, vll *int64) {
if what&objHashKey > 0 {
ziplist.Get(hi.fptr, vstr, vlen, vll)
} else {
ziplist.Get(hi.vptr, vstr, vlen, vll)
}
}
func hashTypeReleaseIterator(hi *hashTypeIterator) {
if hi.encoding == ObjEncodingHt {
hi.di.Release()
}
}
func hashTypeNext(hi *hashTypeIterator) error {
if hi.encoding == ObjEncodingZipList {
zl := *(*[]byte)(hi.subject.ptr)
fptr := hi.fptr
vptr := hi.vptr
if fptr == nil {
fptr = ziplist.Index(zl, 0)
} else {
fptr = ziplist.Next(zl, vptr)
}
if fptr == nil {
return C_ERR
}
vptr = ziplist.Next(zl, fptr)
hi.fptr = fptr
hi.vptr = vptr
} else if hi.encoding == ObjEncodingHt {
if hi.de = hi.di.Next(); hi.de == nil {
return C_ERR
}
} else {
panic("Unknown hash encoding")
}
return C_OK
}
func hashTypeInitIterator(subject *robj) *hashTypeIterator {
hi := new(hashTypeIterator)
hi.subject = subject
hi.encoding = int(subject.getEncoding())
if hi.encoding == ObjEncodingZipList {
hi.fptr = nil
hi.vptr = nil
} else if hi.encoding == ObjEncodingHt {
hi.di = (*dict.Dict)(subject.ptr).GetIterator()
} else {
panic("Unknown hash encoding")
}
return hi
}
func createHashObject() *robj {
zl := ziplist.New()
o := createObject(ObjHash, unsafe.Pointer(&zl))
o.setEncoding(ObjEncodingZipList)
return o
}