File tree Expand file tree Collapse file tree 2 files changed +100
-0
lines changed Expand file tree Collapse file tree 2 files changed +100
-0
lines changed Original file line number Diff line number Diff line change 1
1
package kv
2
+
3
+ import (
4
+ "github.com/dgraph-io/badger/v4"
5
+ "golang.org/x/sync/errgroup"
6
+ )
7
+
8
+ // Get retrieves the value at a specific key from the database
9
+ func (d * Database ) Get (key string ) (value string , err error ) {
10
+ buf := make ([]byte , 0 )
11
+ keyB := []byte (key )
12
+ g := new (errgroup.Group )
13
+
14
+ g .Go (func () error {
15
+ value , err := d .get (keyB )
16
+ if err != nil {
17
+ return err
18
+ }
19
+
20
+ buf = append (buf , value ... )
21
+ return nil
22
+ })
23
+
24
+ err = g .Wait ()
25
+ if err != nil {
26
+ return "" , err
27
+ }
28
+
29
+ return string (buf ), nil
30
+ }
31
+
32
+ func (d * Database ) get (key []byte ) (value []byte , err error ) {
33
+ d .lock .Lock ()
34
+ defer d .lock .Unlock ()
35
+
36
+ buf := make ([]byte , 0 )
37
+ err = d .kv .View (func (txn * badger.Txn ) error {
38
+ item , err := txn .Get (key )
39
+ if err != nil {
40
+ return err
41
+ }
42
+
43
+ value , err := item .ValueCopy (nil )
44
+ if err != nil {
45
+ return err
46
+ }
47
+
48
+ buf = append (buf , value ... )
49
+ return nil
50
+ })
51
+
52
+ if err != nil {
53
+ return nil , err
54
+ }
55
+
56
+ return buf , nil
57
+ }
58
+
59
+ // Set sets the value at a specific key within the database
60
+ func (d * Database ) Set (key string , value []byte ) error {
61
+ keyB := []byte (key )
62
+ g := new (errgroup.Group )
63
+
64
+ g .Go (func () error {
65
+ err := d .set (keyB , value )
66
+ if err != nil {
67
+ return err
68
+ }
69
+
70
+ return nil
71
+ })
72
+
73
+ err := g .Wait ()
74
+ if err != nil {
75
+ return err
76
+ }
77
+
78
+ return nil
79
+ }
80
+
81
+ func (d * Database ) set (key , value []byte ) error {
82
+ d .lock .Lock ()
83
+ defer d .lock .Unlock ()
84
+
85
+ var err error
86
+ err = d .kv .Update (func (txn * badger.Txn ) error {
87
+ err := txn .Set (key , value )
88
+ if err != nil {
89
+ return err
90
+ }
91
+
92
+ return nil
93
+ })
94
+
95
+ if err != nil {
96
+ return err
97
+ }
98
+
99
+ return nil
100
+ }
Original file line number Diff line number Diff line change @@ -68,6 +68,7 @@ type Store interface {
68
68
Set (key string , value []byte , opts ... OperationOpt ) error
69
69
Has (key string , opts ... OperationOpt ) (contains bool , err error )
70
70
Namespaces (opts ... OperationOpt ) (namespaces []string , err error )
71
+ Delete (key string , opts ... OperationOpt ) error
71
72
Close () error
72
73
Path () string
73
74
Config () badger.Options
You can’t perform that action at this time.
0 commit comments