-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.go
120 lines (107 loc) · 2.65 KB
/
commands.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
119
120
package main
import (
"strconv"
"github.com/sybrexsys/RapidKV/datamodel"
)
type commandError string
func (conerr commandError) Error() string { return string(conerr) }
type databasefunc func(*Database, string, datamodel.DataArray) datamodel.CustomDataType
var commandList = map[string]databasefunc{
//keys
"del": delCommand,
"exists": existsCommand,
"expire": expireCommand,
"expireat": expireatCommand,
"keys": keysCommand,
"move": moveCommand,
"persist": persistCommand,
"pexpire": pexpireCommand,
"pexpireat": pexpireatCommand,
"pttl": pttlCommand,
"rename": renameCommand,
"renamenx": renamenxCommand,
"ttl": ttlCommand,
"type": typeCommand,
//strings
"append": appendCommand,
"decr": decrCommand,
"decrby": decrbyCommand,
"get": getCommand,
"getset": getsetCommand,
"incr": incrCommand,
"incrby": incrbyCommand,
"mget": mgetCommand,
"mset": msetCommand,
"msetnx": msetnxCommand,
"psetex": psetexCommand,
"set": setCommand,
"setex": setexCommand,
"setnx": setnxCommand,
"strlen": strlenCommand,
//hashes
"hdel": hdelCommand,
"hexists": hexistsCommand,
"hget": hgetCommand,
"hgetall": hgetallCommand,
"hincrby": hincrbyCommand,
"hkeys": hkeysCommand,
"hlen": hlenCommand,
"hmget": hmgetCommand,
"hmset": hmsetCommand,
"hset": hsetCommand,
"hsetnx": hsetnxCommand,
"hstrlen": hstrlenCommand,
"hvals": hvalsCommand,
//lists
"lindex": lindexCommand,
"linsert": linsertCommand,
"llen": llenCommand,
"lpop": lpopCommand,
"lpush": lpushCommand,
"lpushx": lpushxCommand,
"lrange": lrangeCommand,
"lrem": nil,
"lset": lsetCommand,
"ltrim": ltrimCommand,
"rpop": rpopCommand,
"rpush": rpushCommand,
"rpushx": rpushxCommand,
//sets
"sadd": nil,
"scard": nil,
"sdiff": nil,
"sdifstore": nil,
"sinter": nil,
"sinterstore": nil,
"sismember": nil,
"smembers": nil,
"smove": nil,
"spop": nil,
"srandmember": nil,
"srem": nil,
"sunion": nil,
"sunionstore": nil,
"sscan": nil,
}
func getKey(command datamodel.DataArray, idx int) (string, error) {
a := command.Get(idx)
switch value := a.(type) {
case datamodel.DataString:
return value.Get(), nil
case datamodel.DataInt:
return strconv.Itoa(value.Get()), nil
default:
return "", commandError("Unknown parameter")
}
}
func getInt(command datamodel.DataArray, idx int) (int, error) {
a := command.Get(idx)
switch value := a.(type) {
case datamodel.DataString:
return strconv.Atoi(value.Get())
case datamodel.DataInt:
return value.Get(), nil
default:
return -1, commandError("Unknown parameter")
}
}