Skip to content

Commit e7bb29b

Browse files
authored
Merge pull request #1 from redis-go/list
List
2 parents 26aa282 + 928ef5c commit e7bb29b

35 files changed

+1336
-203
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

client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ func (r *Redis) NewClient(conn redcon.Conn) *Client {
3434
return c
3535
}
3636

37+
// NextClientId atomically gets and increments a counter to return the next client id.
38+
func (r *Redis) NextClientId() ClientId {
39+
r.Mu().Lock()
40+
defer r.Mu().Unlock()
41+
id := r.nextClientId
42+
r.nextClientId++
43+
return id
44+
}
45+
3746
// Clients gets the current connected clients.
3847
func (r *Redis) Clients() Clients {
3948
r.Mu().RLock()

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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
fmt.Println("KEY:", key)
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+
fmt.Println("CREATED NEW LIST")
22+
} else if i.Type() != ListType {
23+
c.Conn().WriteError(fmt.Sprintf("%s: key is a %s not a %s", WrongTypeErr, i.TypeFancy(), ListTypeFancy))
24+
return
25+
}
26+
27+
l := i.(*List)
28+
var length int
29+
c.Redis().Mu().Lock()
30+
for j := 2; j < len(cmd.Args); j++ {
31+
v := string(cmd.Args[j])
32+
length = l.LPush(&v)
33+
}
34+
c.Redis().Mu().Unlock()
35+
36+
c.Conn().WriteInt(length)
37+
}

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+
}

0 commit comments

Comments
 (0)