Skip to content

Commit

Permalink
Scan: swap return values and change cursor type.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Apr 26, 2016
1 parent 2add1e0 commit 38be24b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
12 changes: 5 additions & 7 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,8 @@ func (cmd *ZSliceCmd) readReply(cn *pool.Conn) error {
type ScanCmd struct {
baseCmd

cursor int64
page []string
cursor uint64
}

func NewScanCmd(args ...interface{}) *ScanCmd {
Expand All @@ -709,14 +709,12 @@ func (cmd *ScanCmd) reset() {
cmd.err = nil
}

// TODO: swap return values

func (cmd *ScanCmd) Val() (int64, []string) {
return cmd.cursor, cmd.page
func (cmd *ScanCmd) Val() (keys []string, cursor uint64) {
return cmd.page, cmd.cursor
}

func (cmd *ScanCmd) Result() (int64, []string, error) {
return cmd.cursor, cmd.page, cmd.err
func (cmd *ScanCmd) Result() (keys []string, cursor uint64, err error) {
return cmd.page, cmd.cursor, cmd.err
}

func (cmd *ScanCmd) String() string {
Expand Down
8 changes: 4 additions & 4 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (c *commandable) Type(key string) *StatusCmd {
return cmd
}

func (c *commandable) Scan(cursor int64, match string, count int64) Scanner {
func (c *commandable) Scan(cursor uint64, match string, count int64) Scanner {
args := []interface{}{"SCAN", cursor}
if match != "" {
args = append(args, "MATCH", match)
Expand All @@ -334,7 +334,7 @@ func (c *commandable) Scan(cursor int64, match string, count int64) Scanner {
}
}

func (c *commandable) SScan(key string, cursor int64, match string, count int64) Scanner {
func (c *commandable) SScan(key string, cursor uint64, match string, count int64) Scanner {
args := []interface{}{"SSCAN", key, cursor}
if match != "" {
args = append(args, "MATCH", match)
Expand All @@ -350,7 +350,7 @@ func (c *commandable) SScan(key string, cursor int64, match string, count int64)
}
}

func (c *commandable) HScan(key string, cursor int64, match string, count int64) Scanner {
func (c *commandable) HScan(key string, cursor uint64, match string, count int64) Scanner {
args := []interface{}{"HSCAN", key, cursor}
if match != "" {
args = append(args, "MATCH", match)
Expand All @@ -366,7 +366,7 @@ func (c *commandable) HScan(key string, cursor int64, match string, count int64)
}
}

func (c *commandable) ZScan(key string, cursor int64, match string, count int64) Scanner {
func (c *commandable) ZScan(key string, cursor uint64, match string, count int64) Scanner {
args := []interface{}{"ZSCAN", key, cursor}
if match != "" {
args = append(args, "MATCH", match)
Expand Down
24 changes: 12 additions & 12 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,10 @@ var _ = Describe("Commands", func() {
Expect(set.Err()).NotTo(HaveOccurred())
}

cursor, keys, err := client.Scan(0, "", 0).Result()
keys, cursor, err := client.Scan(0, "", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(cursor > 0).To(Equal(true))
Expect(len(keys) > 0).To(Equal(true))
Expect(keys).NotTo(BeEmpty())
Expect(cursor).NotTo(BeZero())
})

It("should SScan", func() {
Expand All @@ -602,10 +602,10 @@ var _ = Describe("Commands", func() {
Expect(sadd.Err()).NotTo(HaveOccurred())
}

cursor, keys, err := client.SScan("myset", 0, "", 0).Result()
keys, cursor, err := client.SScan("myset", 0, "", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(cursor > 0).To(Equal(true))
Expect(len(keys) > 0).To(Equal(true))
Expect(keys).NotTo(BeEmpty())
Expect(cursor).NotTo(BeZero())
})

It("should HScan", func() {
Expand All @@ -614,10 +614,10 @@ var _ = Describe("Commands", func() {
Expect(sadd.Err()).NotTo(HaveOccurred())
}

cursor, keys, err := client.HScan("myhash", 0, "", 0).Result()
keys, cursor, err := client.HScan("myhash", 0, "", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(cursor > 0).To(Equal(true))
Expect(len(keys) > 0).To(Equal(true))
Expect(keys).NotTo(BeEmpty())
Expect(cursor).NotTo(BeZero())
})

It("should ZScan", func() {
Expand All @@ -626,10 +626,10 @@ var _ = Describe("Commands", func() {
Expect(sadd.Err()).NotTo(HaveOccurred())
}

cursor, keys, err := client.ZScan("myset", 0, "", 0).Result()
keys, cursor, err := client.ZScan("myset", 0, "", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(cursor > 0).To(Equal(true))
Expect(len(keys) > 0).To(Equal(true))
Expect(keys).NotTo(BeEmpty())
Expect(cursor).NotTo(BeZero())
})

})
Expand Down
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ func ExampleClient_Scan() {
}
}

var cursor int64
var cursor uint64
var n int
for {
var keys []string
var err error
cursor, keys, err = client.Scan(cursor, "", 10).Result()
keys, cursor, err = client.Scan(cursor, "", 10).Result()
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func readReply(cn *pool.Conn, p multiBulkParser) (interface{}, error) {
return nil, fmt.Errorf("redis: can't parse %.100q", line)
}

func readScanReply(cn *pool.Conn) ([]string, int64, error) {
func readScanReply(cn *pool.Conn) ([]string, uint64, error) {
n, err := readArrayHeader(cn)
if err != nil {
return nil, 0, err
Expand All @@ -413,7 +413,7 @@ func readScanReply(cn *pool.Conn) ([]string, int64, error) {
return nil, 0, err
}

cursor, err := strconv.ParseInt(bytesToString(b), 10, 64)
cursor, err := strconv.ParseUint(bytesToString(b), 10, 64)
if err != nil {
return nil, 0, err
}
Expand Down

0 comments on commit 38be24b

Please sign in to comment.