forked from HDT3213/godis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshal.go
118 lines (105 loc) · 2.9 KB
/
marshal.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
package godis
import (
"github.com/hdt3213/godis/datastruct/dict"
List "github.com/hdt3213/godis/datastruct/list"
"github.com/hdt3213/godis/datastruct/set"
SortedSet "github.com/hdt3213/godis/datastruct/sortedset"
"github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/reply"
"strconv"
"time"
)
// EntityToCmd serialize data entity to redis command
func EntityToCmd(key string, entity *DataEntity) *reply.MultiBulkReply {
if entity == nil {
return nil
}
var cmd *reply.MultiBulkReply
switch val := entity.Data.(type) {
case []byte:
cmd = stringToCmd(key, val)
case *List.LinkedList:
cmd = listToCmd(key, val)
case *set.Set:
cmd = setToCmd(key, val)
case dict.Dict:
cmd = hashToCmd(key, val)
case *SortedSet.SortedSet:
cmd = zSetToCmd(key, val)
}
return cmd
}
// toTTLCmd serialize ttl config
func toTTLCmd(db *DB, key string) *reply.MultiBulkReply {
raw, exists := db.ttlMap.Get(key)
if !exists {
// 无 TTL
return reply.MakeMultiBulkReply(utils.ToCmdLine("PERSIST", key))
}
expireTime, _ := raw.(time.Time)
timestamp := strconv.FormatInt(expireTime.UnixNano()/1000/1000, 10)
return reply.MakeMultiBulkReply(utils.ToCmdLine("PEXPIREAT", key, timestamp))
}
var setCmd = []byte("SET")
func stringToCmd(key string, bytes []byte) *reply.MultiBulkReply {
args := make([][]byte, 3)
args[0] = setCmd
args[1] = []byte(key)
args[2] = bytes
return reply.MakeMultiBulkReply(args)
}
var rPushAllCmd = []byte("RPUSH")
func listToCmd(key string, list *List.LinkedList) *reply.MultiBulkReply {
args := make([][]byte, 2+list.Len())
args[0] = rPushAllCmd
args[1] = []byte(key)
list.ForEach(func(i int, val interface{}) bool {
bytes, _ := val.([]byte)
args[2+i] = bytes
return true
})
return reply.MakeMultiBulkReply(args)
}
var sAddCmd = []byte("SADD")
func setToCmd(key string, set *set.Set) *reply.MultiBulkReply {
args := make([][]byte, 2+set.Len())
args[0] = sAddCmd
args[1] = []byte(key)
i := 0
set.ForEach(func(val string) bool {
args[2+i] = []byte(val)
i++
return true
})
return reply.MakeMultiBulkReply(args)
}
var hMSetCmd = []byte("HMSET")
func hashToCmd(key string, hash dict.Dict) *reply.MultiBulkReply {
args := make([][]byte, 2+hash.Len()*2)
args[0] = hMSetCmd
args[1] = []byte(key)
i := 0
hash.ForEach(func(field string, val interface{}) bool {
bytes, _ := val.([]byte)
args[2+i*2] = []byte(field)
args[3+i*2] = bytes
i++
return true
})
return reply.MakeMultiBulkReply(args)
}
var zAddCmd = []byte("ZADD")
func zSetToCmd(key string, zset *SortedSet.SortedSet) *reply.MultiBulkReply {
args := make([][]byte, 2+zset.Len()*2)
args[0] = zAddCmd
args[1] = []byte(key)
i := 0
zset.ForEach(int64(0), int64(zset.Len()), true, func(element *SortedSet.Element) bool {
value := strconv.FormatFloat(element.Score, 'f', -1, 64)
args[2+i*2] = []byte(value)
args[3+i*2] = []byte(element.Member)
i++
return true
})
return reply.MakeMultiBulkReply(args)
}