forked from tekton/ccdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexists.go
42 lines (35 loc) · 802 Bytes
/
exists.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
package main
import (
"errors"
badger "github.com/dgraph-io/badger/v4"
"github.com/rs/zerolog/log"
"github.com/tidwall/redcon"
)
func exists(db *badger.DB, _ redcon.Conn, cmd redcon.Command) (any, error) {
if len(cmd.Args) != 2 {
return nil, errors.New("ERR wrong number of arguments for 'EXISTS' command")
}
log.Debug().Str("key", string(cmd.Args[1])).Msg("exists")
exists := 0
if err := db.View(func(txn *badger.Txn) error {
item, gErr := txn.Get(cmd.Args[1])
if gErr != nil {
if errors.Is(gErr, badger.ErrKeyNotFound) {
return nil
}
return gErr
}
if err := item.Value(func(val []byte) error {
if len(val) != 0 {
exists = 1
}
return nil
}); err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
return exists, nil
}