-
Notifications
You must be signed in to change notification settings - Fork 562
/
marshal.go
117 lines (103 loc) · 2.92 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
package aof
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/interface/database"
"github.com/hdt3213/godis/redis/protocol"
"strconv"
"time"
)
// EntityToCmd serialize data entity to redis command
func EntityToCmd(key string, entity *database.DataEntity) *protocol.MultiBulkReply {
if entity == nil {
return nil
}
var cmd *protocol.MultiBulkReply
switch val := entity.Data.(type) {
case []byte:
cmd = stringToCmd(key, val)
case List.List:
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
}
var setCmd = []byte("SET")
func stringToCmd(key string, bytes []byte) *protocol.MultiBulkReply {
args := make([][]byte, 3)
args[0] = setCmd
args[1] = []byte(key)
args[2] = bytes
return protocol.MakeMultiBulkReply(args)
}
var rPushAllCmd = []byte("RPUSH")
func listToCmd(key string, list List.List) *protocol.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 protocol.MakeMultiBulkReply(args)
}
var sAddCmd = []byte("SADD")
func setToCmd(key string, set *set.Set) *protocol.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 protocol.MakeMultiBulkReply(args)
}
var hMSetCmd = []byte("HMSET")
func hashToCmd(key string, hash dict.Dict) *protocol.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 protocol.MakeMultiBulkReply(args)
}
var zAddCmd = []byte("ZADD")
func zSetToCmd(key string, zset *SortedSet.SortedSet) *protocol.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 protocol.MakeMultiBulkReply(args)
}
var pExpireAtBytes = []byte("PEXPIREAT")
// MakeExpireCmd generates command line to set expiration for the given key
func MakeExpireCmd(key string, expireAt time.Time) *protocol.MultiBulkReply {
args := make([][]byte, 3)
args[0] = pExpireAtBytes
args[1] = []byte(key)
args[2] = []byte(strconv.FormatInt(expireAt.UnixNano()/1e6, 10))
return protocol.MakeMultiBulkReply(args)
}