This repository has been archived by the owner on Dec 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
106 lines (86 loc) · 2.31 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
// Package db creates and verifies BoltDB database files.
package pbdb
import (
"toba.io/lib/config"
"os"
"strings"
"regexp"
"github.com/boltdb/bolt"
)
type DataFile int
const (
// SystemFile is the path to the data file storing values common to all
// tenants, such as statistics and licenses. By default it is the
// root path plus configuration.Database.Name.
SystemFile DataFile = iota
// LogFile is the path to the data file that records common application
// activity. By default it is the root path plus logs.db.
LogFile
)
var (
// rootPath is the root directory for storing database files. It is
// usually specified by configuration.Database.Path.
rootPath string
path map[DataFile]string
// Ready indicates database files have been created and write access
// validated.
Ready = false
slash = string(os.PathSeparator)
validFileName = regexp.MustCompile(`^[a-zA-Z0-9]{3,}$`)
dataFiles = OpenFiles{files: make(map[string]*bolt.DB)}
)
// Open returns connection to specific data file.
func Open(f DataFile) (*bolt.DB, error) {
if !Ready {
return nil, ErrNotInitialized
}
return dataFiles.Connect(path[f])
}
// OpenFile returns a connection to the named file within the root path.
func OpenFile(name string) (*bolt.DB, error) {
if !Ready {
return nil, ErrNotInitialized
}
if !validFileName.MatchString(name) {
return nil, ErrInvalidDataFileName
}
return dataFiles.Connect(rootPath + name)
}
// Initialize database directory and files. Operations will use the
// validated paths until re-initialized.
func Initialize(config config.Database) error {
Reset()
rootPath = config.Path
if rootPath != "" {
// empty path means current directory
if !strings.HasSuffix(rootPath, slash) {
rootPath += slash
}
_, err := os.Stat(rootPath)
if err != nil && os.IsNotExist(err) {
// create path if it doesn't exist
err = os.Mkdir(rootPath, 0700)
if err != nil {
return err
}
}
}
path[SystemFile] = rootPath + config.Name
path[LogFile] = rootPath + "logs.db"
// ensure database file can be opened
_, err := dataFiles.Connect(path[SystemFile])
if err == nil {
Ready = true
}
return err
}
func Close() {
dataFiles.CloseAll()
}
// Reset sets Ready to false and clears data file paths.
func Reset() {
Close()
Ready = false
rootPath = ""
path = make(map[DataFile]string)
}