forked from tekton/ccdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmembers.go
44 lines (36 loc) · 905 Bytes
/
smembers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"fmt"
badger "github.com/dgraph-io/badger/v4"
"github.com/rs/zerolog/log"
"github.com/tidwall/redcon"
)
func smembers(db *badger.DB, conn redcon.Conn, cmd redcon.Command) (any, error) {
log.Debug().Str("key", string(cmd.Args[1])).Msg("smembers")
keyz := map[string]string{}
err := db.View(func(txn *badger.Txn) error {
itr := txn.NewIterator(badger.DefaultIteratorOptions)
defer itr.Close()
prefix := []byte(fmt.Sprintf("set::%s::", cmd.Args[1]))
for itr.Seek(prefix); itr.ValidForPrefix(prefix); itr.Next() {
_item := itr.Item()
_err := _item.Value(func(_val []byte) error {
keyz[string(_val)] = string(_val)
return nil
})
if _err != nil {
return _err
}
}
return nil
})
if err != nil {
return nil, err
} else {
conn.WriteArray(len(keyz))
for _, v := range keyz {
conn.WriteBulk([]byte(v))
}
}
return keyz, nil
}