forked from mirror/redis
Merge pull request #46 from go-redis/feature/add-bitpos2
Add bitpos command.
This commit is contained in:
commit
8096f43489
16
commands.go
16
commands.go
|
@ -361,6 +361,22 @@ func (c *commandable) BitOpNot(destKey string, key string) *IntCmd {
|
||||||
return c.bitOp("NOT", destKey, key)
|
return c.bitOp("NOT", destKey, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *commandable) BitPos(key string, bit int64, pos ...int64) *IntCmd {
|
||||||
|
args := []string{"BITPOS", key, formatInt(bit)}
|
||||||
|
switch len(pos) {
|
||||||
|
case 0:
|
||||||
|
case 1:
|
||||||
|
args = append(args, formatInt(pos[0]))
|
||||||
|
case 2:
|
||||||
|
args = append(args, formatInt(pos[0]), formatInt(pos[1]))
|
||||||
|
default:
|
||||||
|
panic("too many arguments")
|
||||||
|
}
|
||||||
|
cmd := NewIntCmd(args...)
|
||||||
|
c.Process(cmd)
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
func (c *commandable) Decr(key string) *IntCmd {
|
func (c *commandable) Decr(key string) *IntCmd {
|
||||||
cmd := NewIntCmd("DECR", key)
|
cmd := NewIntCmd("DECR", key)
|
||||||
c.Process(cmd)
|
c.Process(cmd)
|
||||||
|
|
|
@ -668,6 +668,47 @@ var _ = Describe("Commands", func() {
|
||||||
Expect(get.Val()).To(Equal("\xff"))
|
Expect(get.Val()).To(Equal("\xff"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("should BitPos", func() {
|
||||||
|
err := client.Set("mykey", "\xff\xf0\x00", 0).Err()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
pos, err := client.BitPos("mykey", 0).Result()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(pos).To(Equal(int64(12)))
|
||||||
|
|
||||||
|
pos, err = client.BitPos("mykey", 1).Result()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(pos).To(Equal(int64(0)))
|
||||||
|
|
||||||
|
pos, err = client.BitPos("mykey", 0, 2).Result()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(pos).To(Equal(int64(16)))
|
||||||
|
|
||||||
|
pos, err = client.BitPos("mykey", 1, 2).Result()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(pos).To(Equal(int64(-1)))
|
||||||
|
|
||||||
|
pos, err = client.BitPos("mykey", 0, -1).Result()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(pos).To(Equal(int64(16)))
|
||||||
|
|
||||||
|
pos, err = client.BitPos("mykey", 1, -1).Result()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(pos).To(Equal(int64(-1)))
|
||||||
|
|
||||||
|
pos, err = client.BitPos("mykey", 0, 2, 1).Result()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(pos).To(Equal(int64(-1)))
|
||||||
|
|
||||||
|
pos, err = client.BitPos("mykey", 0, 0, -3).Result()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(pos).To(Equal(int64(-1)))
|
||||||
|
|
||||||
|
pos, err = client.BitPos("mykey", 0, 0, 0).Result()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(pos).To(Equal(int64(-1)))
|
||||||
|
})
|
||||||
|
|
||||||
It("should Decr", func() {
|
It("should Decr", func() {
|
||||||
set := client.Set("key", "10", 0)
|
set := client.Set("key", "10", 0)
|
||||||
Expect(set.Err()).NotTo(HaveOccurred())
|
Expect(set.Err()).NotTo(HaveOccurred())
|
||||||
|
|
Loading…
Reference in New Issue