mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-05 00:42:43 +08:00
119 lines
2.9 KiB
Go
119 lines
2.9 KiB
Go
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)
|
|
}
|