-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbBadger.go
52 lines (44 loc) · 825 Bytes
/
dbBadger.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
package dbBadger // github.com/wehrv/dbBadger
import (
"log"
"github.com/dgraph-io/badger/v3"
)
var db *badger.DB
func init() {
var err error
db, err = badger.Open(badger.DefaultOptions("data"))
if err != nil {
log.Fatal(err)
}
}
func Pull(key []byte) ([]byte, error) {
txn := db.NewTransaction(false)
defer txn.Discard()
itm, err := txn.Get(key)
var val []byte
if err == nil {
val, err = itm.ValueCopy(val)
}
return val, err
}
func Push(key, val []byte) error {
txn := db.NewTransaction(true)
defer txn.Discard()
err := txn.Set(key, val)
if err == nil {
err = txn.Commit()
}
return err
}
func Drop(key []byte) error {
txn := db.NewTransaction(true)
defer txn.Discard()
err := txn.Delete(key)
if err == nil {
err = txn.Commit()
}
return err
}
func Done() error {
return db.Close()
}