From 754c11ab7f0ba24a6c977daac0d35a5b2c272ef7 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Tue, 7 Oct 2014 13:54:43 +0300 Subject: [PATCH] Add bitpos command. --- commands.go | 16 ++++++++++++++++ commands_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/commands.go b/commands.go index 2d2817f..c43fcd2 100644 --- a/commands.go +++ b/commands.go @@ -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) diff --git a/commands_test.go b/commands_test.go index 7ef9408..e87845a 100644 --- a/commands_test.go +++ b/commands_test.go @@ -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())