-
Notifications
You must be signed in to change notification settings - Fork 0
/
LevelDBOperations.go
60 lines (52 loc) · 1.14 KB
/
LevelDBOperations.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
package main
import (
"fmt"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/filter"
"github.com/syndtr/goleveldb/leveldb/opt"
"os"
)
var storage *leveldb.DB = nil
type LevelDBConfig struct {
database string
}
func initLevelDBClient(config *Configuration) {
options := &opt.Options{
Filter: filter.NewBloomFilter(10),
}
database, err := leveldb.OpenFile(config.leveldb.database, options)
if nil != err {
database, err = leveldb.RecoverFile(config.leveldb.database, options)
}
if nil == err {
storage = database
} else {
fmt.Println("Failed to open level db connection")
os.Exit(-1)
}
}
func callGet(key string) (string, error) {
if nil != storage {
val, err := storage.Get([]byte(key), nil)
if nil != err {
return "", err
}
return string(val), nil
}
return "", fmt.Errorf("db not found")
}
func callSet(key string, value string) error {
if nil != storage {
err := storage.Put([]byte(key), []byte(value), &opt.WriteOptions{Sync: true})
if nil != err {
return err
}
return nil
}
return fmt.Errorf("db not found")
}
func closeLevelDBClient() {
if storage != nil {
storage.Close()
}
}