Skip to content

Commit

Permalink
finish GEORADIUS, GEORADIUS_RO, GEOPOS, and GEOADD
Browse files Browse the repository at this point in the history
  • Loading branch information
alicebob committed Sep 18, 2019
1 parent 5e2cf96 commit 8148e2a
Show file tree
Hide file tree
Showing 31 changed files with 1,942 additions and 271 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
/integration/redis_src/
/integration/dump.rdb
*.swp
14 changes: 14 additions & 0 deletions CHANGELOG.md
@@ -1,15 +1,29 @@
## Changelog

### unreleased

- added UNLINK
- fix DEL zero-argument case


### v2.9.1

- fix issue with ZRANGEBYLEX
- fix issue with BRPOPLPUSH and direct access


### v2.9.0

- proper versioned import of github.com/gomodule/redigo (thanks @yfei1)
- fix messages generated by PSUBSCRIBE
- optional internal seed (thanks @zikaeroh)


### v2.8.0

Proper `v2` in go.mod.


### older

See https://github.com/alicebob/miniredis/releases for the full changelog
19 changes: 11 additions & 8 deletions README.md
Expand Up @@ -41,9 +41,10 @@ Implemented commands:
- RENAME
- RENAMENX
- RANDOMKEY -- see m.Seed(...)
- SCAN
- TTL
- TYPE
- SCAN
- UNLINK
- Transactions (complete)
- DISCARD
- EXEC
Expand Down Expand Up @@ -165,6 +166,15 @@ Implemented commands:
- SCRIPT LOAD
- SCRIPT EXISTS
- SCRIPT FLUSH
- GEO
- GEOADD
- ~~GEODIST~~
- ~~GEOHASH~~
- GEOPOS
- GEORADIUS
- GEORADIUS_RO
- ~~GEORADIUSBYMEMBER~~
- ~~GEORADIUSBYMEMBER_RO~~

## TTLs, key expiration, and time

Expand Down Expand Up @@ -235,13 +245,6 @@ Commands which will probably not be implemented:
- ~~CLUSTER *~~
- ~~READONLY~~
- ~~READWRITE~~
- GEO (all) -- unless someone needs these
- ~~GEOADD~~
- ~~GEODIST~~
- ~~GEOHASH~~
- ~~GEOPOS~~
- ~~GEORADIUS~~
- ~~GEORADIUSBYMEMBER~~
- HyperLogLog (all) -- unless someone needs these
- ~~PFADD~~
- ~~PFCOUNT~~
Expand Down
13 changes: 10 additions & 3 deletions cmd_generic.go
Expand Up @@ -13,6 +13,7 @@ import (
// commandsGeneric handles EXPIRE, TTL, PERSIST, &c.
func commandsGeneric(m *Miniredis) {
m.srv.Register("DEL", m.cmdDel)
m.srv.Register("UNLINK", m.cmdDel)
// DUMP
m.srv.Register("EXISTS", m.cmdExists)
m.srv.Register("EXPIRE", makeCmdExpire(m, false, time.Second))
Expand Down Expand Up @@ -200,7 +201,7 @@ func (m *Miniredis) cmdPersist(c *server.Peer, cmd string, args []string) {
})
}

// DEL
// DEL and UNLINK
func (m *Miniredis) cmdDel(c *server.Peer, cmd string, args []string) {
if !m.handleAuth(c) {
return
Expand All @@ -209,6 +210,12 @@ func (m *Miniredis) cmdDel(c *server.Peer, cmd string, args []string) {
return
}

if len(args) == 0 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}

withTx(m, c, func(c *server.Peer, ctx *connCtx) {
db := m.db(ctx.selectedDB)

Expand Down Expand Up @@ -334,7 +341,7 @@ func (m *Miniredis) cmdKeys(c *server.Peer, cmd string, args []string) {
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
db := m.db(ctx.selectedDB)

keys := matchKeys(db.allKeys(), key)
keys, _ := matchKeys(db.allKeys(), key)
c.WriteLen(len(keys))
for _, s := range keys {
c.WriteBulk(s)
Expand Down Expand Up @@ -507,7 +514,7 @@ func (m *Miniredis) cmdScan(c *server.Peer, cmd string, args []string) {

keys := db.allKeys()
if withMatch {
keys = matchKeys(keys, match)
keys, _ = matchKeys(keys, match)
}

c.WriteLen(2)
Expand Down
66 changes: 51 additions & 15 deletions cmd_generic_test.go
Expand Up @@ -227,22 +227,58 @@ func TestDel(t *testing.T) {
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)

s.Set("foo", "bar")
s.HSet("aap", "noot", "mies")
s.Set("one", "two")
s.SetTTL("one", time.Second*1234)
s.Set("three", "four")
r, err := redis.Int(c.Do("DEL", "one", "aap", "nosuch"))
t.Run("simple", func(t *testing.T) {
s.Set("foo", "bar")
s.HSet("aap", "noot", "mies")
s.Set("one", "two")
s.SetTTL("one", time.Second*1234)
s.Set("three", "four")
r, err := redis.Int(c.Do("DEL", "one", "aap", "nosuch"))
ok(t, err)
equals(t, 2, r)
equals(t, time.Duration(0), s.TTL("one"))
})

t.Run("failure cases", func(t *testing.T) {
_, err := c.Do("DEL")
mustFail(t, err, "ERR wrong number of arguments for 'del' command")
})

t.Run("direct", func(t *testing.T) {
s.Set("foo", "bar")
s.Del("foo")
got, err := s.Get("foo")
equals(t, ErrKeyNotFound, err)
equals(t, "", got)
})
}

func TestUnlink(t *testing.T) {
s, err := Run()
ok(t, err)
equals(t, 2, r)
equals(t, time.Duration(0), s.TTL("one"))

// Direct also works:
s.Set("foo", "bar")
s.Del("foo")
got, err := s.Get("foo")
equals(t, ErrKeyNotFound, err)
equals(t, "", got)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)

t.Run("simple", func(t *testing.T) {
s.Set("foo", "bar")
s.HSet("aap", "noot", "mies")
s.Set("one", "two")
s.SetTTL("one", time.Second*1234)
s.Set("three", "four")
r, err := redis.Int(c.Do("UNLINK", "one", "aap", "nosuch"))
ok(t, err)
equals(t, 2, r)
equals(t, time.Duration(0), s.TTL("one"))
})

t.Run("direct", func(t *testing.T) {
s.Set("foo", "bar")
s.Unlink("foo")
got, err := s.Get("foo")
equals(t, ErrKeyNotFound, err)
equals(t, "", got)
})
}

func TestType(t *testing.T) {
Expand Down

0 comments on commit 8148e2a

Please sign in to comment.