-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdb.go
243 lines (229 loc) · 4.18 KB
/
mdb.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package mdb
import (
"bytes"
"encoding/gob"
"fmt"
"os"
"sort"
"sync"
)
var (
// DefaultConfig .
// Path == "" // run in ram
//
DefaultConfig = defaultConfig()
)
type (
// Config holds the db configuration:
// Path is the filepath of the db file
Config struct {
// Path if this is an empty string,
// then its stored in ram,
// otherwise its the filepath
Path string
}
// DB .
// just a simple map of maps of strings that can persist to disk
DB struct {
db map[string]map[string]string // the db data
config Config // db config
file *os.File // db file
sync.RWMutex // sync lock
}
// KV is just a key-value pair
KV struct {
Key, Value string
}
)
// Open opens an existing database file, or a new path.
// open nil to receive the default config
func Open(config *Config) (*DB, func() error, error) {
db := &DB{}
db.db = map[string]map[string]string{}
if config == nil {
db.config = DefaultConfig
} else {
db.config = *config
}
if db.config.Path != "" {
err := *new(error)
db.file, err = os.OpenFile(db.config.Path, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return nil, nil, err
}
err = db.load()
if err != nil {
db.file.Close()
return nil, nil, err
}
}
return db,
func() error {
err := db.Save()
if err != nil {
return err
}
db.Lock()
defer db.Unlock()
if db.config.Path != "" {
err = db.file.Sync()
if err != nil {
return err
}
err = db.file.Close()
if err != nil {
return err
}
}
db = &DB{db: map[string]map[string]string{}}
return nil
},
nil
}
// Save .
func (db *DB) Save() error {
if db.config.Path != "" {
db.RLock()
defer db.RUnlock()
b := bytes.Buffer{}
enc := gob.NewEncoder(&b)
err := enc.Encode(db.db)
if err != nil {
return err
}
// wtf am i doing here? TODO
_, err = db.file.WriteAt(b.Bytes(), 0)
if err != nil {
return err
}
}
return nil
}
// Close .
func (db *DB) Close() error {
err := db.Save()
if err != nil {
return err
}
db.Lock()
defer db.Unlock()
if db.config.Path != "" {
err = db.file.Sync()
if err != nil {
return err
}
err = db.file.Close()
if err != nil {
return err
}
}
db = &DB{db: map[string]map[string]string{}}
return nil
}
// GetMap .
func (db *DB) GetMap(s string) (map[string]string, error) {
db.RLock()
defer db.RUnlock()
if (len(db.db[s]) == 0 || db.db[s] == nil) {
return map[string]string{}, fmt.Errorf("%s", "missing")
}
return db.db[s], nil
}
// GetKV .
func (db *DB) GetKV(s string) ([]KV, error) {
db.RLock()
defer db.RUnlock()
if (len(db.db[s]) == 0 || db.db[s] == nil) {
return []KV{}, fmt.Errorf("%s", "missing")
}
kv := []KV{}
for k, v := range db.db[s] {
kv = append(kv, KV{k, v})
}
sort.SliceStable(
kv,
func(i, j int) bool {
return kv[i].Key < kv[j].Key
},
)
return kv, nil
}
// SetMap .
func (db *DB) SetMap(s string, m map[string]string) error {
db.Lock()
defer db.Unlock()
for k, v := range m {
if db.db[s] == nil {
db.db[s] = map[string]string{}
}
db.db[s][k] = v
}
return nil
}
// SetKV .
func (db *DB) SetKV(s string, kv []KV) error {
db.Lock()
defer db.Unlock()
m := map[string]string{}
for _, p := range kv {
if _, present := m[p.Key]; !present {
m[p.Key] = p.Value
} else {
return fmt.Errorf("%s", "duplicate key detected")
}
}
for k, v := range m {
if db.db[s] == nil {
db.db[s] = map[string]string{}
}
db.db[s][k] = v
}
return nil
}
// Delete .
func (db *DB) Delete(s string) error {
delete(db.db, s)
return nil
}
func (db *DB) load() error {
fi, err := db.file.Stat()
if err != nil {
return err
}
if fi.Size() == 0 {
err = db.init()
if err != nil {
return err
}
}
err = gob.NewDecoder(db.file).Decode(&db.db)
if err != nil {
return err
}
return nil
}
func (db *DB) init() error {
db.Lock()
defer db.Unlock()
db.db = map[string]map[string]string{}
b := bytes.Buffer{}
err := gob.NewEncoder(&b).Encode(db.db)
if err != nil {
return err
}
// wtf am i doing here? TODO
_, err = db.file.WriteAt(b.Bytes(), 0)
if err != nil {
return err
}
err = db.file.Sync()
if err != nil {
return err
}
return nil
}
func defaultConfig() Config {
return Config{
Path: "",
}
}