/
hash.go
169 lines (148 loc) · 4.53 KB
/
hash.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package node
import (
"strconv"
"github.com/absolute8511/ZanRedisDB/common"
"github.com/absolute8511/redcon"
)
func (nd *KVNode) hgetCommand(conn redcon.Conn, cmd redcon.Command) {
val, err := nd.store.HGet(cmd.Args[1], cmd.Args[2])
if err != nil || val == nil {
conn.WriteNull()
} else {
conn.WriteBulk(val)
}
}
func (nd *KVNode) hgetallCommand(conn redcon.Conn, cmd redcon.Command) {
n, valCh, err := nd.store.HGetAll(cmd.Args[1])
if err != nil {
conn.WriteError("ERR for " + string(cmd.Args[0]) + " command: " + err.Error())
}
conn.WriteArray(int(n) * 2)
for v := range valCh {
conn.WriteBulk(v.Rec.Key)
conn.WriteBulk(v.Rec.Value)
}
}
func (nd *KVNode) hkeysCommand(conn redcon.Conn, cmd redcon.Command) {
n, valCh, _ := nd.store.HKeys(cmd.Args[1])
conn.WriteArray(int(n))
for v := range valCh {
conn.WriteBulk(v.Rec.Key)
}
}
func (nd *KVNode) hexistsCommand(conn redcon.Conn, cmd redcon.Command) {
val, err := nd.store.HGet(cmd.Args[1], cmd.Args[2])
if err != nil || val == nil {
conn.WriteInt(0)
} else {
conn.WriteInt(1)
}
}
func (nd *KVNode) hmgetCommand(conn redcon.Conn, cmd redcon.Command) {
vals, _ := nd.store.HMget(cmd.Args[1], cmd.Args[2:]...)
conn.WriteArray(len(vals))
for _, v := range vals {
conn.WriteBulk(v)
}
}
func (nd *KVNode) hlenCommand(conn redcon.Conn, cmd redcon.Command) {
val, err := nd.store.HLen(cmd.Args[1])
if err != nil {
conn.WriteInt(0)
} else {
conn.WriteInt64(val)
}
}
func (nd *KVNode) hsetCommand(conn redcon.Conn, cmd redcon.Command, v interface{}) {
if rsp, ok := v.(int64); ok {
conn.WriteInt64(rsp)
} else {
conn.WriteError(errInvalidResponse.Error())
}
}
func (nd *KVNode) hsetnxCommand(conn redcon.Conn, cmd redcon.Command, v interface{}) {
if rsp, ok := v.(int64); ok {
conn.WriteInt64(rsp)
} else {
conn.WriteError(errInvalidResponse.Error())
}
}
func (nd *KVNode) hmsetCommand(conn redcon.Conn, cmd redcon.Command, v interface{}) {
conn.WriteString("OK")
}
func (nd *KVNode) hdelCommand(conn redcon.Conn, cmd redcon.Command, v interface{}) {
if rsp, ok := v.(int64); ok {
conn.WriteInt64(rsp)
} else {
conn.WriteError(errInvalidResponse.Error())
}
}
func (nd *KVNode) hincrbyCommand(conn redcon.Conn, cmd redcon.Command, v interface{}) {
if rsp, ok := v.(int64); ok {
conn.WriteInt64(rsp)
} else {
conn.WriteError(errInvalidResponse.Error())
}
}
func (nd *KVNode) hclearCommand(conn redcon.Conn, cmd redcon.Command, v interface{}) {
if rsp, ok := v.(int64); ok {
conn.WriteInt64(rsp)
} else {
conn.WriteError(errInvalidResponse.Error())
}
}
// local write command execute only on follower or on the local commit of leader
// the return value of follower is ignored, return value of local leader will be
// return to the future response.
func (kvsm *kvStoreSM) localHSetCommand(cmd redcon.Command, ts int64) (interface{}, error) {
v, err := kvsm.store.HSet(ts, false, cmd.Args[1], cmd.Args[2], cmd.Args[3])
return v, err
}
func (kvsm *kvStoreSM) localHSetNXCommand(cmd redcon.Command, ts int64) (interface{}, error) {
v, err := kvsm.store.HSet(ts, true, cmd.Args[1], cmd.Args[2], cmd.Args[3])
return v, err
}
func (kvsm *kvStoreSM) localHMsetCommand(cmd redcon.Command, ts int64) (interface{}, error) {
args := cmd.Args[2:]
if len(args)%2 != 0 {
return nil, common.ErrInvalidArgs
}
fvs := make([]common.KVRecord, 0, len(args)/2)
for i := 0; i < len(args); i += 2 {
fvs = append(fvs, common.KVRecord{Key: args[i], Value: args[i+1]})
}
err := kvsm.store.HMset(ts, cmd.Args[1], fvs...)
return nil, err
}
func (kvsm *kvStoreSM) localHIncrbyCommand(cmd redcon.Command, ts int64) (interface{}, error) {
v, err := strconv.ParseInt(string(cmd.Args[3]), 10, 64)
if err != nil {
return 0, err
}
ret, err := kvsm.store.HIncrBy(ts, cmd.Args[1], cmd.Args[2], int64(v))
return ret, err
}
func (kvsm *kvStoreSM) localHDelCommand(cmd redcon.Command, ts int64) (interface{}, error) {
// TODO: delete should only handled on the old value, if the value is newer than the timestamp proposal
// we should ignore delete
n, err := kvsm.store.HDel(cmd.Args[1], cmd.Args[2:]...)
if err != nil {
// leader write need response
return int64(0), err
}
return n, nil
}
func (kvsm *kvStoreSM) localHclearCommand(cmd redcon.Command, ts int64) (interface{}, error) {
return kvsm.store.HClear(cmd.Args[1])
}
func (kvsm *kvStoreSM) localHMClearCommand(cmd redcon.Command, ts int64) (interface{}, error) {
var count int64
for _, hkey := range cmd.Args[1:] {
if _, err := kvsm.store.HClear(hkey); err == nil {
count++
} else {
return count, err
}
}
return count, nil
}