-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.go
78 lines (69 loc) · 1.63 KB
/
object.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
package main
import (
"log"
"strconv"
)
const OBJ_ENCODING_RAW = 0 // Raw representation
const OBJ_ENCODING_INT = 1 // Encoded as integer
const OBJ_ENCODING_HT = 2
const OBJ_ENCODING_EMBSTR = 8 // Embedded sds string encoding
const OBJ_STRING = 0 // String object.
const OBJ_LIST = 1 // List object.
const OBJ_SET = 2 // Set object.
const OBJ_ZSET = 3 // Sorted set object.
const OBJ_HASH = 4 // Hash object.
type robj struct {
tp uint8
encoding uint8
lru uint32
refcount int
ptr interface{}
}
func createStringObject(bs []byte) *robj {
newbs := make([]byte, len(bs))
copy(newbs, bs)
return createObject(OBJ_STRING, newbs)
}
func createObject(tp int, ptr interface{}) *robj {
return &robj{
tp: uint8(tp),
encoding: OBJ_ENCODING_RAW,
lru: lruClock(),
refcount: 1,
ptr: ptr,
}
}
func sdsEncodedObject(obj *robj) bool {
return obj.encoding == OBJ_ENCODING_RAW || obj.encoding == OBJ_ENCODING_EMBSTR
}
func getLongLongFromObject(o *robj) (int64, bool) {
if o == nil {
return 0, true
}
if sdsEncodedObject(o) {
v := string(o.ptr.([]byte))
if i, err := strconv.Atoi(v); err != nil {
return 0, false
} else {
return int64(i), true
}
} else if o.encoding == OBJ_ENCODING_INT {
v := o.ptr.(int)
return int64(v), true
} else {
log.Fatalln("Unknown string encoding")
}
return 0, false
}
func getLongLongFromObjectOrReply(c *client, o *robj, msg string) (int64, bool) {
val, ok := getLongLongFromObject(o)
if !ok {
if len(msg) > 0 {
addReplyError(c, msg)
} else {
addReplyError(c, "value is not an integer or out of range")
}
return 0, false
}
return val, true
}