forked from tekton/ccdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.go
55 lines (46 loc) · 1.16 KB
/
keys.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
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"bytes"
"errors"
"slices"
"strings"
badger "github.com/dgraph-io/badger/v4"
"github.com/rs/zerolog/log"
"github.com/tidwall/redcon"
)
func keys(db *badger.DB, conn redcon.Conn, cmd redcon.Command) (any, error) {
if len(cmd.Args) != 2 {
return nil, errors.New("ERR wrong number of arguments for 'KEYS' command")
}
log.Debug().Str("pattern", string(cmd.Args[1])).Msg("keys")
keys := []string{}
if err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
if string(cmd.Args[1]) != "*" {
opts.Prefix = []byte(cmd.Args[1])
}
if strings.Contains(string(cmd.Args[1]), "*") {
opts.Prefix = []byte(strings.ReplaceAll(string(cmd.Args[1]), "*", ""))
}
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid() || it.ValidForPrefix(opts.Prefix); it.Next() {
if bytes.Contains(it.Item().Key(), []byte("set::")) {
continue
}
k := string(it.Item().Key())
if slices.Contains(keys, k) {
continue
}
keys = append(keys, k)
}
return nil
}); err != nil {
return nil, err
}
if len(keys) == 0 {
return nil, nil
}
return keys, nil
}