Merge pull request #46 from go-redis/feature/add-bitpos2

Add bitpos command.
This commit is contained in:
Vladimir Mihailenco 2015-04-06 12:57:23 +03:00
commit 8096f43489
2 changed files with 57 additions and 0 deletions

View File

@ -361,6 +361,22 @@ func (c *commandable) BitOpNot(destKey string, key string) *IntCmd {
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 {
cmd := NewIntCmd("DECR", key)
c.Process(cmd)

View File

@ -668,6 +668,47 @@ var _ = Describe("Commands", func() {
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() {
set := client.Set("key", "10", 0)
Expect(set.Err()).NotTo(HaveOccurred())