-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.go
More file actions
385 lines (342 loc) · 8.33 KB
/
object.go
File metadata and controls
385 lines (342 loc) · 8.33 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
package main
import (
"fmt"
"github.com/pengdafu/redis-golang/dict"
"github.com/pengdafu/redis-golang/intset"
"github.com/pengdafu/redis-golang/sds"
"github.com/pengdafu/redis-golang/util"
"github.com/pengdafu/redis-golang/ziplist"
"math"
"unsafe"
)
const (
typeBitMask = 0xF
encodingMask = 0xF0
lruBitMask = math.MaxUint32 >> 8 << 8
lruBitOffset = 8
encodingBitOffset = 4
)
const (
ObjString = iota
ObjList
ObjSet
ObjZSet
ObjHash
ObjModule
ObjStream
)
const (
ObjEncodingRaw = iota
ObjEncodingInt
ObjEncodingHt
ObjEncodingZipMap
ObjEncodingLinkedList
ObjEncodingZipList
ObjEncodingIntSet
ObjEncodingSkipList
ObjEncodingEmbStr
ObjEncodingQuickList
ObjEncodingStream // listpack
)
const (
ObjSharedRefCount = math.MaxInt
ObjStaticRefCount = ObjSharedRefCount - 1
ObjFirstSpecialRefCount = ObjStaticRefCount
)
type robj struct {
refCount int
ptr unsafe.Pointer //*int|*sds.SDS
// 4bit type, 4bit encoding,
// 24bit lru(lru time or lfu data(8bit frq and 16bit time))
__ uint32
}
func (robj *robj) getType() int {
return int(robj.__ & typeBitMask)
}
func (robj *robj) getEncoding() uint32 {
return robj.__ & encodingMask >> encodingBitOffset
}
func (robj *robj) getLru() uint32 {
return robj.__ >> lruBitOffset
}
func (robj *robj) setType(typ int) {
mask := uint32(0xFFFFFFF0)
robj.__ &= mask
robj.__ |= uint32(typ & typeBitMask)
}
func (robj *robj) setEncoding(encoding uint32) {
mask := uint32(0xFFFFFF0F)
robj.__ &= mask
robj.__ |= (encoding << encodingBitOffset) & encodingMask
}
func (robj *robj) setLru(lru uint32) {
mask := uint32(0x000000FF)
robj.__ &= mask
robj.__ |= lru << lruBitOffset & lruBitMask
}
func (robj *robj) makeObjectShared() *robj {
robj.refCount = ObjSharedRefCount
return robj
}
func isSdsRepresentableAsLongLong(s sds.SDS, llval *int64) bool {
return util.String2Int64(s.BufData(0), llval)
}
func createObject(typ int, ptr unsafe.Pointer) *robj {
o := new(robj)
o.setType(typ)
o.setEncoding(ObjEncodingRaw)
o.ptr = ptr
o.refCount = 1
if server.maxMemoryPolicy&MaxMemoryFlagLfu > 0 {
o.setLru(uint32(LFUGetTimeInMinutes()<<lruBitOffset | LfuInitVal))
} else {
o.setLru(LRU_CLOCK())
}
return o
}
func createObject2(typ int, elesds sds.SDS) *robj {
o := new(robj)
o.setType(typ)
o.setEncoding(ObjEncodingRaw)
o.ptr = unsafe.Pointer(&elesds)
o.refCount = 1
if server.maxMemoryPolicy&MaxMemoryFlagLfu > 0 {
o.setLru(uint32(LFUGetTimeInMinutes()<<lruBitOffset | LfuInitVal))
} else {
o.setLru(LRU_CLOCK())
}
return o
}
const (
ObjEncodingEmbStrLimitSize = 44
)
func createStringObject(ptr string) *robj {
if len(ptr) <= ObjEncodingEmbStrLimitSize {
return createEmbeddedStringObject(util.String2Bytes(ptr))
} else {
return createRawStringObject(util.String2Bytes(ptr))
}
}
func createIntsetObject() *robj {
is := intset.New()
o := createObject(ObjSet, unsafe.Pointer(is))
o.setEncoding(ObjEncodingIntSet)
return o
}
func createSetObject() *robj {
dt := dict.Create(setDictType, nil)
o := createObject(ObjSet, unsafe.Pointer(dt))
o.setEncoding(ObjEncodingHt)
return o
}
func createZsetZiplistObject() *robj {
zl := ziplist.New()
o := createObject(ObjZSet, unsafe.Pointer(&zl))
o.setEncoding(ObjEncodingZipList)
return o
}
func createZsetObject() *robj {
zs := new(zset)
zs.dict = dict.Create(zsetDictType, nil)
zs.zsl = zslCreate()
o := createObject(ObjZSet, unsafe.Pointer(zs))
o.setEncoding(ObjEncodingSkipList)
return o
}
// 说明长度肯定小于等于44
func createEmbeddedStringObject(ptr []byte) *robj {
/**
embStr 是指,robj和ptr对应的sds一起申请一个连续的内存。
robj *o = zmalloc(sizeof(robj)+sizeof(struct sdshdr8)+len+1);
struct sdshdr8 *sh = (void*)(o+1);
*/
s := sds.NewLen(util.Bytes2String(ptr))
o := createObject(ObjString, unsafe.Pointer(&s))
o.setEncoding(ObjEncodingEmbStr)
return o
}
func createRawStringObject(ptr []byte) *robj {
s := sds.NewLen(util.Bytes2String(ptr))
return createObject(ObjString, unsafe.Pointer(&s))
}
func createStringObjFromLongLongForValue(v int64) *robj {
return createStringObjectFromLongLongWithOptions(v, 1)
}
func createStringObjectFromLongLongWithOptions(v int64, vobj int) *robj {
if server.maxMemory == 0 || server.maxMemoryPolicy&MaxMemoryFlagNoSharedIntegers == 0 {
vobj = 0
}
if v >= 0 && v < ObjSharedIntegers && vobj == 0 {
shared.integers[v].incrRefCount()
return shared.integers[v]
} else {
o := createObject(ObjString, unsafe.Pointer(&v))
o.setEncoding(ObjEncodingInt)
o.ptr = unsafe.Pointer(&v)
return o
}
}
func getDoubleFromObject(o *robj, target *float64) bool {
var value float64
if o == nil {
value = 0
} else {
if o.sdsEncodedObject() {
if !util.String2D(string((*sds.SDS)(o.ptr).BufData(0)), &value) {
return false
}
} else if o.getEncoding() == ObjEncodingInt {
value = float64(*(*int64)(o.ptr))
} else {
panic("Unknown string encoding")
}
}
*target = value
return true
}
func getDoubleFromObjectOrReply(c *Client, o *robj, target *float64, msg string) bool {
var value float64
if !getDoubleFromObject(o, &value) {
if msg != "" {
addReplyError(c, msg)
} else {
addReplyError(c, "value is not a valid float")
}
return false
}
*target = value
return true
}
func (o *robj) sdsEncodedObject() bool {
ed := o.getEncoding()
return ed == ObjEncodingRaw || ed == ObjEncodingEmbStr
}
func (o *robj) decrRefCount() {
if o.refCount == 1 {
switch o.getType() {
}
//zfree(o)
} else {
if o.refCount <= 0 {
panic("decrRefCount against refCount <= 0")
}
if o.refCount != ObjSharedRefCount {
o.refCount--
}
}
}
func (o *robj) incrRefCount() {
if o.refCount < ObjFirstSpecialRefCount {
o.refCount++
} else {
if o.refCount == ObjSharedRefCount {
// nothing to do
} else if o.refCount == ObjStaticRefCount {
panic("You tried to retain an object allocated in the stack")
}
}
}
func (o *robj) getDecodedObject() *robj {
if o.sdsEncodedObject() {
o.incrRefCount()
return o
}
if o.getType() == ObjString && o.getEncoding() == ObjEncodingInt {
llStr := fmt.Sprintf("%v", *(*int)(o.ptr))
return createStringObject(llStr)
}
panic("Unknown encoding type")
}
func (o *robj) tryObjectEncoding() *robj {
if !o.sdsEncodedObject() {
return o
}
s := *(*sds.SDS)(o.ptr)
if o.refCount > 1 {
return o
}
slen := sds.Len(s)
var value int64
if slen <= 20 && util.String2Int64(s.BufData(0), &value) {
if (server.maxMemory == 0 || server.maxMemoryPolicy&MaxMemoryFlagNoSharedIntegers == 0) &&
value >= 0 && value < ObjSharedIntegers {
o.decrRefCount()
shared.integers[value].incrRefCount()
return shared.integers[value]
} else {
if o.getEncoding() == ObjEncodingRaw {
o.setEncoding(ObjEncodingInt)
o.ptr = unsafe.Pointer(&value)
return o
} else if o.getEncoding() == ObjEncodingEmbStr {
o.decrRefCount()
return createStringObjFromLongLongForValue(value)
}
}
}
if slen <= ObjEncodingEmbStrLimitSize {
if o.getEncoding() == ObjEncodingEmbStr {
return o
}
o.decrRefCount()
return createEmbeddedStringObject(s.BufData(0))
}
o.trimStringObjectIfNeeded()
return o
}
func (o *robj) trimStringObjectIfNeeded() {
s := *(*sds.SDS)(o.ptr)
if o.getEncoding() == ObjEncodingRaw && sds.Avail(s) > sds.Len(s)/10 {
n := sds.RemoveFreeSpace(s)
o.ptr = unsafe.Pointer(&n)
}
}
func (o *robj) getLongLongFromObjectOrReply(c *Client, target *int64, msg string) error {
var value int64
if o.getLongLongFromObject(&value) != C_OK {
if msg != "" {
addReplyError(c, msg)
} else {
addReplyError(c, "value is not an integer or out of range")
}
return C_ERR
}
*target = value
return C_OK
}
func (o *robj) getLongLongFromObject(target *int64) error {
var value int64
if o == nil {
value = 0
} else {
if o.sdsEncodedObject() {
if !util.String2Int64((*sds.SDS)(o.ptr).BufData(0), &value) {
return C_ERR
}
} else if o.getEncoding() == ObjEncodingInt {
value = int64(*(*int)(o.ptr))
} else {
panic("Unknown string encoding")
}
}
if target != nil {
*target = value
}
return C_OK
}
func (o *robj) stringObjectLen() int {
if o.getType() != ObjString {
panic("not string obj")
}
if o.sdsEncodedObject() {
return sds.Len(*(*sds.SDS)(o.ptr))
}
return len(fmt.Sprintf("%v", *(*int)(o.ptr)))
}
func (o *robj) checkType(c *Client, typ int) bool {
if o.getType() != typ {
addReply(c, shared.wrongTypeErr)
return true
}
return false
}