forked from eliquious/kappa
/
system.go
57 lines (46 loc) · 1.34 KB
/
system.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
package datamodel
import "github.com/eliquious/leaf"
const (
// Users is the name of the user keyspace
Users = "users"
// Namespaces is the name of the namespace keyspace
Namespaces = "namespaces"
)
// System provides an interface for accessing information about the database.
type System interface {
Users() (UserStore, error)
Namespaces() (NamespaceStore, error)
Close()
}
// NewSystem creates a database connection to access system metadata
func NewSystem(filename string) (System, error) {
leaf, err := leaf.NewLeaf(filename)
if err != nil {
return nil, err
}
return &BoltSystemStore{leaf}, nil
}
// BoltSystemStore implements the System interface on top of a boltdb connection
type BoltSystemStore struct {
db leaf.KeyValueDatabase
}
// Users returns a UserStore
func (s BoltSystemStore) Users() (UserStore, error) {
ks, err := s.db.GetOrCreateKeyspace(Users)
if err != nil {
return nil, err
}
return NewBoltUserStore(ks), nil
}
// Namespaces returns a NamespaceStore
func (s BoltSystemStore) Namespaces() (NamespaceStore, error) {
ks, err := s.db.GetOrCreateKeyspace(Namespaces)
if err != nil {
return nil, err
}
return NewBoltNamespaceStore(ks), nil
}
// Close closes the database connection
func (s BoltSystemStore) Close() {
s.db.Close()
}