-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
75 lines (61 loc) · 1.51 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
65
66
67
68
69
70
71
72
73
74
75
package puredb
import (
"github.com/dgraph-io/badger"
"os"
"log"
)
type PureDB struct {
DB *badger.DB
badgerOpts badger.Options
Pathname string
buckets buckets
}
// use "functional options"
// see:
// - https://stackoverflow.com/a/26326418/1363486
// - http://commandcenter.blogspot.com.au/2014/01/self-referential-functions-and-design.html
// - http://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
type PureDBOptionFn func (*PureDB) error
func Open(pathname string, options ...PureDBOptionFn) (*PureDB, error) {
opts := badger.DefaultOptions
opts.SyncWrites = true
opts.Dir = pathname
opts.ValueDir = pathname
pureDb := PureDB{
badgerOpts: opts,
Pathname: pathname,
}
for _, option := range options {
err := option(&pureDb)
if err != nil {
return nil, err
}
}
badgerDb, err := badger.Open(pureDb.badgerOpts)
if err != nil {
log.Printf("can't open/create DB pathname: %q err: %v", pureDb.badgerOpts.Dir, err)
return nil, err
}
pureDb.DB = badgerDb
pureDb.buckets.Init(&pureDb)
return &pureDb, nil
}
func (db *PureDB) Close() {
db.buckets.Cleanup()
db.DB.Close()
}
func (db *PureDB) Destroy() {
db.buckets.Cleanup()
db.DB.Close()
os.RemoveAll(db.Pathname)
}
func (db *PureDB) Badger() *badger.DB {
return db.DB
}
func (db *PureDB) AddBucket(name string, opts BucketOpts) error {
log.Printf("PureDB::AddBucket - name:%v opts:%v", name, opts)
return db.buckets.Add(name, opts)
}
func (db *PureDB) GetBucket(name string) *Bucket {
return db.buckets.Get(name)
}