Skip to content

Commit af18024

Browse files
committed
Complete the list data service
1 parent 4af8131 commit af18024

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed

lib/redis/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ var FlyDBSupportCommands = map[string]CmdHandler{
5757
"httl": HTTL,
5858
"hincrby": HIncrBy,
5959
"hdecrby": HDecrBy,
60+
61+
// list
62+
"use-list": UseList,
63+
"lpush": LPush,
64+
"rpush": RPush,
65+
"lpop": LPop,
66+
"rpop": RPop,
67+
"lrange": LRange,
68+
"llen": LLen,
69+
"lindex": LIndex,
70+
"lset": LSet,
71+
"lrem": LRem,
72+
"ltrim": LTrim,
73+
"lkeys": LKeys,
74+
"lsize": LSize,
6075
}
6176

6277
// ClientCommands is the handler for all redis commands

lib/redis/list.go

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package redis
2+
3+
import (
4+
"encoding/binary"
5+
"github.com/ByteStorage/FlyDB/structure"
6+
"github.com/tidwall/redcon"
7+
)
8+
9+
// LPush key value
10+
func LPush(cli *FlyDBClient, args [][]byte) (interface{}, error) {
11+
if len(args) < 2 {
12+
return nil, NewWrongNumberOfArgsError("lpush")
13+
}
14+
15+
key := string(args[0])
16+
values := make([]string, len(args)-1)
17+
for i, v := range args[1:] {
18+
values[i] = string(v)
19+
}
20+
21+
if err := cli.DB[2].(*structure.ListStructure).LPush(key, values, 0); err != nil {
22+
return nil, err
23+
}
24+
return redcon.SimpleInt(int64(len(values))), nil
25+
}
26+
27+
// RPush key value
28+
func RPush(cli *FlyDBClient, args [][]byte) (interface{}, error) {
29+
if len(args) < 2 {
30+
return nil, NewWrongNumberOfArgsError("rpush")
31+
}
32+
33+
key := string(args[0])
34+
values := make([]string, len(args)-1)
35+
for i, v := range args[1:] {
36+
values[i] = string(v)
37+
}
38+
39+
if err := cli.DB[2].(*structure.ListStructure).RPush(key, values, 0); err != nil {
40+
return nil, err
41+
}
42+
return redcon.SimpleInt(int64(len(values))), nil
43+
}
44+
45+
// LPop key
46+
func LPop(cli *FlyDBClient, args [][]byte) (interface{}, error) {
47+
if len(args) != 1 {
48+
return nil, NewWrongNumberOfArgsError("lpop")
49+
}
50+
51+
value, err := cli.DB[2].(*structure.ListStructure).LPop(string(args[0]))
52+
if err != nil {
53+
return nil, err
54+
}
55+
return value, nil
56+
}
57+
58+
// RPop key
59+
func RPop(cli *FlyDBClient, args [][]byte) (interface{}, error) {
60+
if len(args) != 1 {
61+
return nil, NewWrongNumberOfArgsError("rpop")
62+
}
63+
64+
value, err := cli.DB[2].(*structure.ListStructure).RPop(string(args[0]))
65+
if err != nil {
66+
return nil, err
67+
}
68+
return value, nil
69+
}
70+
71+
// LIndex key index
72+
func LIndex(cli *FlyDBClient, args [][]byte) (interface{}, error) {
73+
if len(args) != 2 {
74+
return nil, NewWrongNumberOfArgsError("lindex")
75+
}
76+
77+
value, err := cli.DB[2].(*structure.ListStructure).LIndex(string(args[0]), int(binary.BigEndian.Uint64(args[1])))
78+
if err != nil {
79+
return nil, err
80+
}
81+
return value, nil
82+
}
83+
84+
// LRange key start stop
85+
func LRange(cli *FlyDBClient, args [][]byte) (interface{}, error) {
86+
if len(args) != 3 {
87+
return nil, NewWrongNumberOfArgsError("lrange")
88+
}
89+
90+
values, err := cli.DB[2].(*structure.ListStructure).LRange(string(args[0]), int(binary.BigEndian.Uint64(args[1])), int(binary.BigEndian.Uint64(args[2])))
91+
if err != nil {
92+
return nil, err
93+
}
94+
return values, nil
95+
}
96+
97+
// LRem key count value
98+
func LRem(cli *FlyDBClient, args [][]byte) (interface{}, error) {
99+
if len(args) != 3 {
100+
return nil, NewWrongNumberOfArgsError("lrem")
101+
}
102+
103+
count := int(binary.BigEndian.Uint64(args[1]))
104+
if err := cli.DB[2].(*structure.ListStructure).LRem(string(args[0]),
105+
count, string(args[2])); err != nil {
106+
return nil, err
107+
}
108+
109+
return redcon.SimpleString("OK"), nil
110+
}
111+
112+
// LLen key
113+
func LLen(cli *FlyDBClient, args [][]byte) (interface{}, error) {
114+
if len(args) != 1 {
115+
return nil, NewWrongNumberOfArgsError("llen")
116+
}
117+
118+
length, err := cli.DB[2].(*structure.ListStructure).LLen(string(args[0]))
119+
if err != nil {
120+
return nil, err
121+
}
122+
return redcon.SimpleInt(int64(length)), nil
123+
}
124+
125+
// LSet key index value
126+
func LSet(cli *FlyDBClient, args [][]byte) (interface{}, error) {
127+
if len(args) != 3 {
128+
return nil, NewWrongNumberOfArgsError("lset")
129+
}
130+
131+
index := int(binary.BigEndian.Uint64(args[1]))
132+
if err := cli.DB[2].(*structure.ListStructure).LSet(string(args[0]), index, string(args[2]), 0); err != nil {
133+
return nil, err
134+
}
135+
return redcon.SimpleString("OK"), nil
136+
}
137+
138+
// LTrim key start stop
139+
func LTrim(cli *FlyDBClient, args [][]byte) (interface{}, error) {
140+
if len(args) != 3 {
141+
return nil, NewWrongNumberOfArgsError("ltrim")
142+
}
143+
144+
if err := cli.DB[2].(*structure.ListStructure).LTrim(string(args[0]), int(binary.BigEndian.Uint64(args[1])), int(binary.BigEndian.Uint64(args[2]))); err != nil {
145+
return nil, err
146+
}
147+
return redcon.SimpleString("OK"), nil
148+
}
149+
150+
// LKeys key
151+
func LKeys(cli *FlyDBClient, args [][]byte) (interface{}, error) {
152+
if len(args) != 1 {
153+
return nil, NewWrongNumberOfArgsError("lkeys")
154+
}
155+
156+
keys, err := cli.DB[2].(*structure.ListStructure).Keys(string(args[0]))
157+
if err != nil {
158+
return nil, err
159+
}
160+
return keys, nil
161+
}
162+
163+
// LSize key
164+
func LSize(cli *FlyDBClient, args [][]byte) (interface{}, error) {
165+
if len(args) != 1 {
166+
return nil, NewWrongNumberOfArgsError("lsize")
167+
}
168+
169+
size, err := cli.DB[2].(*structure.ListStructure).Size(string(args[0]))
170+
if err != nil {
171+
return nil, err
172+
}
173+
return redcon.SimpleString(size), nil
174+
}
175+
176+
// UseList change to list db
177+
func UseList(cli *FlyDBClient, args [][]byte) (interface{}, error) {
178+
if len(args) != 0 {
179+
return nil, NewWrongNumberOfArgsError("use-list")
180+
}
181+
return redcon.SimpleString("OK"), nil
182+
}

lib/redis/server.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ func (svr *FlyDBServer) Close(conn redcon.Conn, err error) {
3939
db.Clean()
4040
} else if dbh, ok := svr.Dbs[1].(*structure.HashStructure); ok {
4141
dbh.Clean()
42+
} else if dbl, ok := svr.Dbs[2].(*structure.ListStructure); ok {
43+
dbl.Clean()
4244
}
4345
_ = svr.Server.Close()
4446
log.Println("FlyDB-Redis Server Stop Success On: ", config.DefaultRedisAddr)
@@ -48,24 +50,35 @@ func (svr *FlyDBServer) Close(conn redcon.Conn, err error) {
4850
func StartRedisServer() {
4951
// open Redis data structure service
5052
options := config.DefaultOptions
53+
54+
// Redis String Service
5155
options.DirPath = config.RedisStringDirPath
5256
stringStructure, err := structure.NewStringStructure(options)
5357
if err != nil {
5458
panic(err)
5559
}
5660

61+
// Redis Hash Service
5762
options.DirPath = config.RedisHashDirPath
5863
hashStructure, err := structure.NewHashStructure(options)
5964
if err != nil {
6065
panic(err)
6166
}
6267

68+
// Redis List Service
69+
options.DirPath = config.RedisListDirPath
70+
listStructure, err := structure.NewListStructure(options)
71+
if err != nil {
72+
panic(err)
73+
}
74+
6375
// initialize FlyDBServer
6476
flydbServer := FlyDBServer{
6577
Dbs: make(map[int]interface{}),
6678
}
6779
flydbServer.Dbs[0] = stringStructure
6880
flydbServer.Dbs[1] = hashStructure
81+
flydbServer.Dbs[2] = listStructure
6982

7083
// initialize a Redis server
7184
flydbServer.Server = redcon.NewServer(config.DefaultRedisAddr,

structure/list.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,7 @@ func (l *ListStructure) Size(key string) (string, error) {
767767

768768
return size, nil
769769
}
770+
771+
func (s *ListStructure) Clean() {
772+
s.db.Clean()
773+
}

0 commit comments

Comments
 (0)