Skip to content

Commit d50b12d

Browse files
committed
Complete the hash data service
1 parent ce0f026 commit d50b12d

2 files changed

Lines changed: 186 additions & 0 deletions

File tree

lib/redis/client.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ var FlyDBSupportCommands = map[string]CmdHandler{
4444
"use-hash": UseHash,
4545
"hset": HSet,
4646
"hget": HGet,
47+
"hdel": HDel,
48+
"hdelall": HDelAll,
49+
"hexists": HExists,
50+
"hexpire": HExpire,
51+
"hlen": HLen,
52+
"hupdate": HUpdate,
53+
"hkeys": HKeys,
54+
"hstrlen": HStrlen,
55+
"hmove": HMove,
56+
"hsize": HSize,
57+
"httl": HTTL,
58+
"hincrby": HIncrBy,
59+
"hdecrby": HDecrBy,
4760
}
4861

4962
// ClientCommands is the handler for all redis commands

lib/redis/hash.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redis
22

33
import (
4+
"encoding/binary"
45
"github.com/ByteStorage/FlyDB/structure"
56
"github.com/tidwall/redcon"
67
)
@@ -36,6 +37,178 @@ func HGet(cli *FlyDBClient, args [][]byte) (interface{}, error) {
3637
return value, nil
3738
}
3839

40+
// HDel key field
41+
func HDel(cli *FlyDBClient, args [][]byte) (interface{}, error) {
42+
if len(args) != 2 {
43+
return nil, NewWrongNumberOfArgsError("hdel")
44+
}
45+
46+
if _, err := cli.DB[1].(*structure.HashStructure).HDel(string(args[0]), args[1]); err != nil {
47+
return nil, err
48+
}
49+
return redcon.SimpleString("OK"), nil
50+
}
51+
52+
// HDelAll key
53+
func HDelAll(cli *FlyDBClient, args [][]byte) (interface{}, error) {
54+
if len(args) != 1 {
55+
return nil, NewWrongNumberOfArgsError("hdelall")
56+
}
57+
58+
if _, err := cli.DB[1].(*structure.HashStructure).HDelAll(string(args[0])); err != nil {
59+
return nil, err
60+
}
61+
return redcon.SimpleString("OK"), nil
62+
}
63+
64+
// HExists key field
65+
func HExists(cli *FlyDBClient, args [][]byte) (interface{}, error) {
66+
if len(args) != 2 {
67+
return nil, NewWrongNumberOfArgsError("hexists")
68+
}
69+
70+
var ok = 0
71+
res, err := cli.DB[1].(*structure.HashStructure).HExists(string(args[0]), args[1])
72+
if err != nil {
73+
return nil, err
74+
}
75+
if res {
76+
ok = 1
77+
}
78+
return redcon.SimpleInt(ok), nil
79+
}
80+
81+
// HExpire key seconds
82+
func HExpire(cli *FlyDBClient, args [][]byte) (interface{}, error) {
83+
if len(args) != 2 {
84+
return nil, NewWrongNumberOfArgsError("hexpire")
85+
}
86+
87+
if _, err := cli.DB[1].(*structure.HashStructure).HExpire(string(args[0]),
88+
int64(binary.BigEndian.Uint64(args[1]))); err != nil {
89+
return nil, err
90+
}
91+
return redcon.SimpleString("OK"), nil
92+
}
93+
94+
// HLen key
95+
func HLen(cli *FlyDBClient, args [][]byte) (interface{}, error) {
96+
if len(args) != 1 {
97+
return nil, NewWrongNumberOfArgsError("hlen")
98+
}
99+
100+
length, err := cli.DB[1].(*structure.HashStructure).HLen(string(args[0]))
101+
if err != nil {
102+
return nil, err
103+
}
104+
return redcon.SimpleInt(length), nil
105+
}
106+
107+
// HUpdate key field value
108+
func HUpdate(cli *FlyDBClient, args [][]byte) (interface{}, error) {
109+
if len(args) != 1 {
110+
return nil, NewWrongNumberOfArgsError("hupdate")
111+
}
112+
113+
if _, err := cli.DB[1].(*structure.HashStructure).HUpdate(string(args[0]), args[1], args[2]); err != nil {
114+
return nil, err
115+
}
116+
return redcon.SimpleString("OK"), nil
117+
}
118+
119+
// HKeys key
120+
func HKeys(cli *FlyDBClient, args [][]byte) (interface{}, error) {
121+
if len(args) != 1 {
122+
return nil, NewWrongNumberOfArgsError("hkeys")
123+
}
124+
125+
keys, err := cli.DB[1].(*structure.HashStructure).Keys(string(args[0]))
126+
if err != nil {
127+
return nil, err
128+
}
129+
return keys, nil
130+
}
131+
132+
// HStrlen key field
133+
func HStrlen(cli *FlyDBClient, args [][]byte) (interface{}, error) {
134+
if len(args) != 2 {
135+
return nil, NewWrongNumberOfArgsError("hstrlen")
136+
}
137+
138+
length, err := cli.DB[1].(*structure.HashStructure).HStrLen(string(args[0]), args[1])
139+
if err != nil {
140+
return nil, err
141+
}
142+
return redcon.SimpleInt(length), nil
143+
}
144+
145+
// HMove source destination field
146+
func HMove(cli *FlyDBClient, args [][]byte) (interface{}, error) {
147+
if len(args) != 3 {
148+
return nil, NewWrongNumberOfArgsError("hmove")
149+
}
150+
151+
if _, err := cli.DB[1].(*structure.HashStructure).HMove(string(args[0]), string(args[1]),
152+
int64(binary.BigEndian.Uint64(args[2]))); err != nil {
153+
return nil, err
154+
}
155+
return redcon.SimpleString("OK"), nil
156+
}
157+
158+
// HSize key
159+
func HSize(cli *FlyDBClient, args [][]byte) (interface{}, error) {
160+
if len(args) != 1 {
161+
return nil, NewWrongNumberOfArgsError("hsize")
162+
}
163+
164+
size, err := cli.DB[1].(*structure.HashStructure).Size(string(args[0]))
165+
if err != nil {
166+
return nil, err
167+
}
168+
return redcon.SimpleString(size), nil
169+
}
170+
171+
// HTTL key
172+
func HTTL(cli *FlyDBClient, args [][]byte) (interface{}, error) {
173+
if len(args) != 1 {
174+
return nil, NewWrongNumberOfArgsError("httl")
175+
}
176+
177+
ttl, err := cli.DB[1].(*structure.HashStructure).TTL(string(args[0]))
178+
if err != nil {
179+
return nil, err
180+
}
181+
return redcon.SimpleInt(ttl), nil
182+
}
183+
184+
// HIncrBy key field increment
185+
func HIncrBy(cli *FlyDBClient, args [][]byte) (interface{}, error) {
186+
if len(args) != 3 {
187+
return nil, NewWrongNumberOfArgsError("hincrby")
188+
}
189+
190+
value, err := cli.DB[1].(*structure.HashStructure).HIncrBy(string(args[0]), args[1],
191+
int64(binary.BigEndian.Uint64(args[2])))
192+
if err != nil {
193+
return nil, err
194+
}
195+
return redcon.SimpleInt(value), nil
196+
}
197+
198+
// HDecrBy key field decrement
199+
func HDecrBy(cli *FlyDBClient, args [][]byte) (interface{}, error) {
200+
if len(args) != 3 {
201+
return nil, NewWrongNumberOfArgsError("hdecrby")
202+
}
203+
204+
value, err := cli.DB[1].(*structure.HashStructure).HDecrBy(string(args[0]), args[1],
205+
int64(binary.BigEndian.Uint64(args[2])))
206+
if err != nil {
207+
return nil, err
208+
}
209+
return redcon.SimpleInt(value), nil
210+
}
211+
39212
// UseHash change to hash db
40213
func UseHash(cli *FlyDBClient, args [][]byte) (interface{}, error) {
41214
if len(args) != 0 {

0 commit comments

Comments
 (0)