-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
64 lines (54 loc) · 1.24 KB
/
db.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
package models
import (
"fmt"
"github.com/tidwall/buntdb"
)
type Store interface {
CreateValue(key string, value string) error
GetValue(key string) (string, error)
}
var store Store
var currDb dbStore
type dbStore struct {
db *buntdb.DB
}
func InitStore(s *buntdb.DB) {
currDb.db = s
currDb.db.CreateIndex("idx_name","*", buntdb.IndexJSON("name"))
}
func getObjLabel(objIdLabel string,id string) string {
return objIdLabel + ":" + id
}
func (currDb *dbStore) CreateValue(key string, value string) (string, error) {
err := currDb.db.Update(func(tx *buntdb.Tx) error {
_, _, err := tx.Set(key, value, nil)
return err
})
return "", err
}
func (currDb *dbStore) GetValue(key string) (string,error) {
outvar := ""
err := currDb.db.View(func(tx *buntdb.Tx) error {
val, err := tx.Get(key)
if err != nil{
return err
}
outvar = val
fmt.Printf("value is %s\n", val)
return nil
})
return outvar, err
}
func (currDB *dbStore) GetAllValues(objType string) ([]string,error) {
outvar := []string{}
err := currDb.db.View(func(tx *buntdb.Tx) error {
tx.AscendKeys(objType + ":*", func(k, v string) bool {
if true == true {
outvar = append(outvar, k)
}
return true // continue
})
return nil
})
return outvar, err
}