Skip to content

Commit 097cddb

Browse files
gerzseofekshenawa
andauthoredJun 19, 2024··
Support NOVALUES parameter for HSCAN (#2925)
* Support NOVALUES parameter for HSCAN Issue #2919 The NOVALUES parameter instructs HSCAN to only return the hash keys, without values. * Update hash_commands.go --------- Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com>
1 parent 0777247 commit 097cddb

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed
 

‎commands_test.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -1134,8 +1134,26 @@ var _ = Describe("Commands", func() {
11341134

11351135
keys, cursor, err := client.HScan(ctx, "myhash", 0, "", 0).Result()
11361136
Expect(err).NotTo(HaveOccurred())
1137-
Expect(keys).NotTo(BeEmpty())
1138-
Expect(cursor).NotTo(BeZero())
1137+
// If we don't get at least two items back, it's really strange.
1138+
Expect(cursor).To(BeNumerically(">=", 2))
1139+
Expect(len(keys)).To(BeNumerically(">=", 2))
1140+
Expect(keys[0]).To(HavePrefix("key"))
1141+
Expect(keys[1]).To(Equal("hello"))
1142+
})
1143+
1144+
It("should HScan without values", func() {
1145+
for i := 0; i < 1000; i++ {
1146+
sadd := client.HSet(ctx, "myhash", fmt.Sprintf("key%d", i), "hello")
1147+
Expect(sadd.Err()).NotTo(HaveOccurred())
1148+
}
1149+
1150+
keys, cursor, err := client.HScanNoValues(ctx, "myhash", 0, "", 0).Result()
1151+
Expect(err).NotTo(HaveOccurred())
1152+
// If we don't get at least two items back, it's really strange.
1153+
Expect(cursor).To(BeNumerically(">=", 2))
1154+
Expect(len(keys)).To(BeNumerically(">=", 2))
1155+
Expect(keys[0]).To(HavePrefix("key"))
1156+
Expect(keys[1]).To(HavePrefix("key"))
11391157
})
11401158

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

‎hash_commands.go

+15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type HashCmdable interface {
1919
HMSet(ctx context.Context, key string, values ...interface{}) *BoolCmd
2020
HSetNX(ctx context.Context, key, field string, value interface{}) *BoolCmd
2121
HScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
22+
HScanNoValues(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
2223
HVals(ctx context.Context, key string) *StringSliceCmd
2324
HRandField(ctx context.Context, key string, count int) *StringSliceCmd
2425
HRandFieldWithValues(ctx context.Context, key string, count int) *KeyValueSliceCmd
@@ -176,6 +177,20 @@ func (c cmdable) HScan(ctx context.Context, key string, cursor uint64, match str
176177
return cmd
177178
}
178179

180+
func (c cmdable) HScanNoValues(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd {
181+
args := []interface{}{"hscan", key, cursor}
182+
if match != "" {
183+
args = append(args, "match", match)
184+
}
185+
if count > 0 {
186+
args = append(args, "count", count)
187+
}
188+
args = append(args, "novalues")
189+
cmd := NewScanCmd(ctx, c, args...)
190+
_ = c(ctx, cmd)
191+
return cmd
192+
}
193+
179194
type HExpireArgs struct {
180195
NX bool
181196
XX bool

‎iterator_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ var _ = Describe("ScanIterator", func() {
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.HScanNoValues(ctx, hashKey, 0, "", 10).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)
Please sign in to comment.