-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminidb.go
96 lines (78 loc) · 2.34 KB
/
minidb.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
package minidb
import (
"sync"
)
const Version = "0.1.6"
type (
// BaseMiniDB is the base db structure.
BaseMiniDB struct {
db string // combined path and filename
mutex *sync.Mutex
}
// MiniDB is the base store file.
MiniDB struct {
path string
filename string
content MiniDBContent
mutexes map[string]*sync.Mutex
BaseMiniDB
}
// MiniDBContent is the types of MiniDB.store
MiniDBContent struct {
Keys map[string]string `json:"keys"`
Collections map[string]string `json:"collections"`
Store map[string]string `json:"store"`
}
// MiniCollections is a collections store.
MiniCollections struct {
content []interface{}
mutexes map[int]*sync.Mutex
BaseMiniDB
}
// MiniStore is a key-value store.
MiniStore struct {
content map[string]interface{}
mutexes map[string]*sync.Mutex
BaseMiniDB
}
)
// New creates a new MiniDB struct.
// The dir will be created if it doesn't exist and a file named `__default.json` will also be generated.
// It is better to use this in managing multiple json files.
func New(dir string) *MiniDB {
return minidb(dir, "__default.json")
}
// NewStore creates and returns a new key-store collection json db.
func NewStore(f string) *MiniStore {
return ministore(f, nil)
}
// NewCollections creates and returns a new collections json db.
func NewCollections(f string) *MiniCollections {
return minicollection(f, nil)
}
// NewMiniStore creates and returns a new key-store collection json db.
// If store does not exist, it will write the default value as its base content.
func NewStoreWithDefault(f string, defaultValue interface{}) *MiniStore {
return ministore(f, defaultValue)
}
// NewMiniCollections creates and returns a new collections json db.
// If collections does not exist, it will write the default value as its base content.
func NewCollectionsWithDefault(f string, defaultValue interface{}) *MiniCollections {
return minicollection(f, defaultValue)
}
// ListCollections returns the list of collections created.
func (db *MiniDB) ListCollections() []string {
cols := []string{}
for i := range db.content.Collections {
cols = append(cols, i)
}
return cols
}
// ListCollections returns the list of collections created.
func (db *MiniDB) ListStores() []string {
stores := []string{}
for i := range db.content.Store {
stores = append(stores, i)
}
return stores
}