Skip to content

Commit 69276d9

Browse files
committed
Support NOVALUES parameter for HSCAN
Issue #2919 The NOVALUES parameter instructs HSCAN to only return the hash keys, without values.
1 parent d43a9fa commit 69276d9

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

commands_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,10 +1098,28 @@ var _ = Describe("Commands", func() {
10981098
Expect(sadd.Err()).NotTo(HaveOccurred())
10991099
}
11001100

1101-
keys, cursor, err := client.HScan(ctx, "myhash", 0, "", 0).Result()
1101+
keys, cursor, err := client.HScan(ctx, "myhash", 0, "", 0, false).Result()
11021102
Expect(err).NotTo(HaveOccurred())
1103-
Expect(keys).NotTo(BeEmpty())
1104-
Expect(cursor).NotTo(BeZero())
1103+
// If we don't get at least two items back, it's really strange.
1104+
Expect(len(keys)).To(BeNumerically(">=", 2))
1105+
Expect(keys[0]).To(HavePrefix("key"))
1106+
Expect(keys[1]).To(Equal("hello"))
1107+
Expect(cursor).To(BeNumerically(">=", 2))
1108+
})
1109+
1110+
It("should HScan without values", func() {
1111+
for i := 0; i < 1000; i++ {
1112+
sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello")
1113+
Expect(sadd.Err()).NotTo(HaveOccurred())
1114+
}
1115+
1116+
keys, cursor, err := client.HScan(ctx, "myhash", 0, "", 0, true).Result()
1117+
Expect(err).NotTo(HaveOccurred())
1118+
// If we don't get at least two items back, it's really strange.
1119+
Expect(len(keys)).To(BeNumerically(">=", 2))
1120+
Expect(keys[0]).To(HavePrefix("key"))
1121+
Expect(keys[1]).To(HavePrefix("key"))
1122+
Expect(cursor).To(BeNumerically(">=", 2))
11051123
})
11061124

11071125
It("should ZScan", func() {

hash_commands.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type HashCmdable interface {
1515
HSet(ctx context.Context, key string, values ...interface{}) *IntCmd
1616
HMSet(ctx context.Context, key string, values ...interface{}) *BoolCmd
1717
HSetNX(ctx context.Context, key, field string, value interface{}) *BoolCmd
18-
HScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
18+
HScan(ctx context.Context, key string, cursor uint64, match string, count int64, noValues bool) *ScanCmd
1919
HVals(ctx context.Context, key string) *StringSliceCmd
2020
HRandField(ctx context.Context, key string, count int) *StringSliceCmd
2121
HRandFieldWithValues(ctx context.Context, key string, count int) *KeyValueSliceCmd
@@ -160,14 +160,17 @@ func (c cmdable) HRandFieldWithValues(ctx context.Context, key string, count int
160160
return cmd
161161
}
162162

163-
func (c cmdable) HScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd {
163+
func (c cmdable) HScan(ctx context.Context, key string, cursor uint64, match string, count int64, noValues bool) *ScanCmd {
164164
args := []interface{}{"hscan", key, cursor}
165165
if match != "" {
166166
args = append(args, "match", match)
167167
}
168168
if count > 0 {
169169
args = append(args, "count", count)
170170
}
171+
if noValues {
172+
args = append(args, "novalues")
173+
}
171174
cmd := NewScanCmd(ctx, c, args...)
172175
_ = c(ctx, cmd)
173176
return cmd

iterator_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,30 @@ var _ = Describe("ScanIterator", func() {
8888
Expect(hashSeed(71)).NotTo(HaveOccurred())
8989

9090
var vals []string
91-
iter := client.HScan(ctx, hashKey, 0, "", 10).Iterator()
91+
iter := client.HScan(ctx, hashKey, 0, "", 10, false).Iterator()
9292
for iter.Next(ctx) {
9393
vals = append(vals, iter.Val())
9494
}
9595
Expect(iter.Err()).NotTo(HaveOccurred())
9696
Expect(vals).To(HaveLen(71 * 2))
9797
Expect(vals).To(ContainElement("K01"))
9898
Expect(vals).To(ContainElement("K71"))
99+
Expect(vals).To(ContainElement("x"))
100+
})
101+
102+
It("should hscan without values across multiple pages", func() {
103+
Expect(hashSeed(71)).NotTo(HaveOccurred())
104+
105+
var vals []string
106+
iter := client.HScan(ctx, hashKey, 0, "", 10, true).Iterator()
107+
for iter.Next(ctx) {
108+
vals = append(vals, iter.Val())
109+
}
110+
Expect(iter.Err()).NotTo(HaveOccurred())
111+
Expect(vals).To(HaveLen(71))
112+
Expect(vals).To(ContainElement("K01"))
113+
Expect(vals).To(ContainElement("K71"))
114+
Expect(vals).NotTo(ContainElement("x"))
99115
})
100116

101117
It("should scan to page borders", func() {

0 commit comments

Comments
 (0)