-
Notifications
You must be signed in to change notification settings - Fork 5
/
db.go
112 lines (89 loc) · 2.68 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
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
111
112
package tormenta
import (
"encoding/json"
"errors"
"os"
"github.com/dgraph-io/badger"
)
// DB is the wrapper of the BadgerDB connection
type DB struct {
KV *badger.DB
Options Options
}
type Options struct {
SerialiseFunc func(interface{}) ([]byte, error)
UnserialiseFunc func([]byte, interface{}) error
BadgerOptions badger.Options
DebugMode bool
}
var DefaultOptions = Options{
SerialiseFunc: json.Marshal,
UnserialiseFunc: json.Unmarshal,
BadgerOptions: badger.DefaultOptions,
DebugMode: false,
}
// testDirectory alters a specified data directory to mark it as for tests
func testDirectory(dir string) string {
return dir + "-test"
}
func Open(dir string) (*DB, error) {
return OpenWithOptions(dir, DefaultOptions)
}
// Open returns a connection to TormentDB connection
func OpenWithOptions(dir string, options Options) (*DB, error) {
if dir == "" {
return nil, errors.New("No valid data directory provided")
}
// Create directory if does not exist
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return nil, errors.New("Could not create data directory")
}
}
opts := options.BadgerOptions
opts.Dir = dir
opts.ValueDir = dir
badgerDB, err := badger.Open(opts)
if err != nil {
return nil, err
}
return openDB(badgerDB, options)
}
func OpenTest(dir string) (*DB, error) {
testDir := testDirectory(dir)
// Attempt to remove the existing directory
os.RemoveAll("./" + testDir)
// Now check if it exists
if _, err := os.Stat(testDir); !os.IsNotExist(err) {
return nil, errors.New("Could not remove existing data directory")
}
return OpenWithOptions(testDir, DefaultOptions)
}
// OpenTest is a convenience function to wipe the existing data at the specified location and create a new connection. As a safety measure against production use, it will append "-test" to the directory name
func OpenTestWithOptions(dir string, options Options) (*DB, error) {
testDir := testDirectory(dir)
// Attempt to remove the existing directory
os.RemoveAll("./" + testDir)
// Now check if it exists
if _, err := os.Stat(testDir); !os.IsNotExist(err) {
return nil, errors.New("Could not remove existing data directory")
}
return OpenWithOptions(testDir, options)
}
// Close closes the connection to the DB
func (db DB) Close() error {
return db.KV.Close()
}
func openDB(badgerDB *badger.DB, options Options) (*DB, error) {
return &DB{
KV: badgerDB,
Options: options,
}, nil
}
func (db DB) unserialise(val []byte, entity interface{}) error {
return db.Options.UnserialiseFunc(val, entity)
}
func (db DB) serialise(entity interface{}) ([]byte, error) {
return db.Options.SerialiseFunc(entity)
}