Skip to content

Commit 9f3843a

Browse files
author
peter.yang
committed
add hash command HLEN
1 parent 58d6238 commit 9f3843a

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

commands_hash.go

+28
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,34 @@ func hexistsCommand(c Context) {
225225
c.WriteInt(found)
226226
}
227227

228+
func hlenCommand(c Context) {
229+
if len(c.args) < 1 {
230+
c.WriteError("HLEN command requires at least one argument: HLEN <hashmap>")
231+
return
232+
}
233+
234+
found := 0
235+
prefix := c.args[0] + "/{HASH}/"
236+
237+
err := c.db.Scan(kvstore.ScannerOptions{
238+
FetchValues: false,
239+
IncludeOffset: true,
240+
Prefix: prefix,
241+
Offset: prefix,
242+
Handler: func(_, _ string) bool {
243+
found++
244+
return true
245+
},
246+
})
247+
248+
if err != nil {
249+
c.WriteError(err.Error())
250+
return
251+
}
252+
253+
c.WriteInt(found)
254+
}
255+
228256
// hincrCommand - HINCR <hash> <key> [<number>]
229257
func hincrCommand(c Context) {
230258
if len(c.args) < 2 {

vars.go

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ var (
7777
"hexists": hexistsCommand,
7878
"hincr": hincrCommand,
7979
"httl": httlCommand,
80+
"hlen": hlenCommand,
8081

8182
// pubsub
8283
"publish": publishCommand,

0 commit comments

Comments
 (0)