Scan: swap return values and change cursor type.

This commit is contained in:
Vladimir Mihailenco 2016-04-09 13:07:42 +03:00
parent 2add1e06fb
commit 38be24b025
5 changed files with 25 additions and 27 deletions

View File

@ -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 {
@ -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 {

View File

@ -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)
@ -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)
@ -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)
@ -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)

View File

@ -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() {
@ -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() {
@ -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() {
@ -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())
})
})

View File

@ -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)
}

View File

@ -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
@ -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
}