forked from tekton/ccdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdel.go
68 lines (54 loc) · 1.4 KB
/
del.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
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"bytes"
"errors"
"strings"
badger "github.com/dgraph-io/badger/v4"
"github.com/rs/zerolog/log"
"github.com/tidwall/redcon"
)
func del(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 'DEL' command")
}
log.Debug().Str("key", string(cmd.Args[1])).Msg("del")
mu.Lock()
defer mu.Unlock()
amountDeleted := 0
if err := db.Update(func(txn *badger.Txn) error {
if !strings.Contains(string(cmd.Args[1]), "*") {
i, err := txn.Get(cmd.Args[1])
if err != nil {
return err
}
if err := i.Value(func(val []byte) error {
amountDeleted++
return txn.Delete(cmd.Args[1])
}); err != nil {
return err
}
return nil
}
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
if string(cmd.Args[1]) != "*" && 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 := it.Item().KeyCopy(nil)
if err := txn.Delete(k); err != nil {
return err
}
amountDeleted++
}
return nil
}); err != nil {
return redcon.SimpleInt(0), nil
}
return amountDeleted, nil
}