-
Notifications
You must be signed in to change notification settings - Fork 5
/
storage.go
110 lines (98 loc) · 2.26 KB
/
storage.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package searchrefiner
import (
"fmt"
"github.com/boltdb/bolt"
"os"
"path"
"strings"
)
type PluginStorage struct {
db *bolt.DB
plugin string
}
const PluginStoragePath = "plugin_storage"
func (p *PluginStorage) Close() error {
return p.db.Close()
}
func OpenPluginStorage(plugin string) (*PluginStorage, error) {
err := os.MkdirAll(PluginStoragePath, 0664)
if err != nil {
return nil, nil
}
db, err := bolt.Open(path.Join(PluginStoragePath, plugin), 0664, nil)
return &PluginStorage{
db: db,
plugin: plugin,
}, nil
}
func (p *PluginStorage) PutValue(bucket, key, value string) error {
return p.db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(bucket))
if err != nil {
return err
}
return b.Put([]byte(key), []byte(value))
})
}
func (p *PluginStorage) CreateBucket(bucket string) error {
return p.db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(bucket))
return err
})
}
func (p *PluginStorage) DeleteKey(bucket, key string) error {
return p.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
if b == nil {
return nil
}
return b.Delete([]byte(key))
})
}
func (p *PluginStorage) GetValue(bucket, key string) (string, error) {
var v []byte
err := p.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
if b == nil {
return nil
}
v = b.Get([]byte(key))
return nil
})
return string(v), err
}
func (p *PluginStorage) GetBuckets() ([]string, error) {
var bu []string
err := p.db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(name []byte, b *bolt.Bucket) error {
bu = append(bu, string(name))
return nil
})
})
return bu, err
}
func (p *PluginStorage) GetValues(bucket string) (map[string]string, error) {
vals := make(map[string]string)
err := p.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
if b == nil {
return nil
}
return b.ForEach(func(k, v []byte) error {
vals[string(k)] = string(v)
return nil
})
})
return vals, err
}
func (p *PluginStorage) ToCSV(bucket string) (string, error) {
vals, err := p.GetValues(bucket)
if err != nil {
return "", err
}
var b strings.Builder
for k, v := range vals {
b.WriteString(fmt.Sprintf("%s,%s\n", k, v))
}
return b.String(), nil
}