Skip to content

Commit d06b874

Browse files
committed
add redis list type
add list commands (not ready) add redis RegisterCommands func add list tests (not ready) update vendor rename command files with prefix 'cmd_'
1 parent 812a090 commit d06b874

25 files changed

+675
-149
lines changed

Gopkg.lock

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ go get -u github.com/redis-go/redis
3939
- [x] active key expirer
4040
- [ ] Implementing data structures
4141
- [x] String
42-
- [ ] List
42+
- [x] List
4343
- [ ] Set
4444
- [ ] Sorted Set
4545
- [ ] Hash

del.go renamed to cmd_del.go

File renamed without changes.

get.go renamed to cmd_get.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func GetCommand(c *Client, cmd redcon.Command) {
1414
return
1515
}
1616

17-
if i.ValueType() != StringType {
18-
c.Conn().WriteError(fmt.Sprintf("%s: key is a %s not a %s", WrongTypeErr, i.ValueTypeFancy(), StringTypeFancy))
17+
if i.Type() != StringType {
18+
c.Conn().WriteError(fmt.Sprintf("%s: key is a %s not a %s", WrongTypeErr, i.TypeFancy(), StringTypeFancy))
1919
return
2020
}
2121

cmd_lpop.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package redis
2+
3+
import (
4+
"fmt"
5+
"github.com/redis-go/redcon"
6+
)
7+
8+
func LPopCommand(c *Client, cmd redcon.Command) {
9+
if len(cmd.Args) < 2 {
10+
c.Conn().WriteError(fmt.Sprintf(WrongNumOfArgsErr, "lpop"))
11+
return
12+
}
13+
key := string(cmd.Args[1])
14+
15+
db := c.Db()
16+
i := db.GetOrExpire(&key, true)
17+
if i == nil {
18+
c.Conn().WriteNull()
19+
return
20+
} else if i.Type() != ListType {
21+
c.Conn().WriteError(fmt.Sprintf("%s: key is a %s not a %s", WrongTypeErr, i.TypeFancy(), ListTypeFancy))
22+
return
23+
}
24+
25+
l := i.(*List)
26+
c.Redis().Mu().Lock()
27+
v, b := l.LPop()
28+
if b {
29+
db.Delete(&key)
30+
}
31+
c.Redis().Mu().Unlock()
32+
33+
c.Conn().WriteBulkString(*v)
34+
}

cmd_lpush.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package redis
2+
3+
import (
4+
"fmt"
5+
"github.com/redis-go/redcon"
6+
"time"
7+
)
8+
9+
func LPushCommand(c *Client, cmd redcon.Command) {
10+
if len(cmd.Args) < 3 {
11+
c.Conn().WriteError(fmt.Sprintf(WrongNumOfArgsErr, "lpush"))
12+
return
13+
}
14+
key := string(cmd.Args[1])
15+
16+
db := c.Db()
17+
i := db.GetOrExpire(&key, true)
18+
if i == nil {
19+
i = NewList()
20+
db.Set(&key, i, false, time.Time{})
21+
} else if i.Type() != ListType {
22+
c.Conn().WriteError(fmt.Sprintf("%s: key is a %s not a %s", WrongTypeErr, i.TypeFancy(), ListTypeFancy))
23+
return
24+
}
25+
26+
l := i.(*List)
27+
var length int
28+
c.Redis().Mu().Lock()
29+
for j := 2; j < len(cmd.Args); j++ {
30+
v := string(cmd.Args[j])
31+
length = l.LPush(&v)
32+
}
33+
c.Redis().Mu().Unlock()
34+
35+
c.Conn().WriteInt(length)
36+
}

cmd_lrange.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package redis
2+
3+
import (
4+
"fmt"
5+
"github.com/redis-go/redcon"
6+
"strconv"
7+
)
8+
9+
func LRangeCommand(c *Client, cmd redcon.Command) {
10+
if len(cmd.Args) < 3 {
11+
c.Conn().WriteError(fmt.Sprintf(WrongNumOfArgsErr, "lrange"))
12+
return
13+
}
14+
key := string(cmd.Args[1])
15+
start, err := strconv.Atoi(string(cmd.Args[2]))
16+
if err != nil {
17+
c.Conn().WriteError(fmt.Sprintf("%s: %s", InvalidIntErr, err.Error()))
18+
return
19+
}
20+
end, err := strconv.Atoi(string(cmd.Args[3]))
21+
if err != nil {
22+
c.Conn().WriteError(fmt.Sprintf("%s: %s", InvalidIntErr, err.Error()))
23+
return
24+
}
25+
26+
db := c.Db()
27+
i := db.GetOrExpire(&key, true)
28+
if i == nil {
29+
c.Conn().WriteNull()
30+
return
31+
} else if i.Type() != ListType {
32+
c.Conn().WriteError(fmt.Sprintf("%s: key is a %s not a %s", WrongTypeErr, i.TypeFancy(), ListTypeFancy))
33+
return
34+
}
35+
36+
l := i.(*List)
37+
c.Redis().Mu().RLock()
38+
values := l.LRange(start, end)
39+
c.Redis().Mu().RUnlock()
40+
41+
c.Conn().WriteArray(len(values))
42+
for _, v := range values {
43+
c.Conn().WriteBulkString(v)
44+
}
45+
}
File renamed without changes.

cmd_rpop.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package redis
2+
3+
import (
4+
"fmt"
5+
"github.com/redis-go/redcon"
6+
)
7+
8+
func RPopCommand(c *Client, cmd redcon.Command) {
9+
if len(cmd.Args) < 2 {
10+
c.Conn().WriteError(fmt.Sprintf(WrongNumOfArgsErr, "rpop"))
11+
return
12+
}
13+
key := string(cmd.Args[1])
14+
15+
db := c.Db()
16+
i := db.GetOrExpire(&key, true)
17+
if i == nil {
18+
c.Conn().WriteNull()
19+
return
20+
} else if i.Type() != ListType {
21+
c.Conn().WriteError(fmt.Sprintf("%s: key is a %s not a %s", WrongTypeErr, i.TypeFancy(), ListTypeFancy))
22+
return
23+
}
24+
25+
l := i.(*List)
26+
c.Redis().Mu().Lock()
27+
v, b := l.RPop()
28+
if b {
29+
db.Delete(&key)
30+
}
31+
c.Redis().Mu().Unlock()
32+
33+
c.Conn().WriteBulkString(*v)
34+
}

cmd_rpush.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package redis
2+
3+
import (
4+
"fmt"
5+
"github.com/redis-go/redcon"
6+
"time"
7+
)
8+
9+
func RPushCommand(c *Client, cmd redcon.Command) {
10+
if len(cmd.Args) < 3 {
11+
c.Conn().WriteError(fmt.Sprintf(WrongNumOfArgsErr, "rpush"))
12+
return
13+
}
14+
key := string(cmd.Args[1])
15+
16+
db := c.Db()
17+
i := db.GetOrExpire(&key, true)
18+
if i == nil {
19+
i := NewList()
20+
db.Set(&key, i, false, time.Time{})
21+
} else if i.Type() != ListType {
22+
c.Conn().WriteError(fmt.Sprintf("%s: key is a %s not a %s", WrongTypeErr, i.TypeFancy(), ListTypeFancy))
23+
return
24+
}
25+
26+
l := i.(*List)
27+
var length int
28+
c.Redis().Mu().Lock()
29+
for j := 2; j < len(cmd.Args); j++ {
30+
v := string(cmd.Args[j])
31+
length = l.RPush(&v)
32+
}
33+
c.Redis().Mu().Unlock()
34+
35+
c.Conn().WriteInt(length)
36+
}

0 commit comments

Comments
 (0)