-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.go
303 lines (225 loc) · 5.36 KB
/
database.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package AlgoeDB
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"sync"
)
type Database struct {
config DatabaseConfig
mutex sync.Mutex
documents []map[string]interface{}
}
type DatabaseConfig struct {
Path string
SchemaValidator SchemaValidator
}
type SchemaValidator func(document interface{}) bool
type QueryFunc func(value interface{}) bool
func NewDatabase(config *DatabaseConfig) (*Database, error) {
documents := []map[string]interface{}{}
database := Database{
documents: documents,
config: *config,
}
err := database.load()
if err != nil {
log.Fatal(err)
}
return &database, nil
}
func (d *Database) InsertOne(document map[string]interface{}) error {
d.mutex.Lock()
defer d.mutex.Unlock()
if d.config.SchemaValidator != nil && !d.config.SchemaValidator(document) {
return errors.New("document failed schema validtion: " + fmt.Sprint(document))
}
d.documents = append(d.documents, document)
err := d.save()
if err != nil {
return err
}
return nil
}
func (d *Database) InsertMany(documents []map[string]interface{}) error {
d.mutex.Lock()
defer d.mutex.Unlock()
for _, document := range documents {
if d.config.SchemaValidator != nil && !d.config.SchemaValidator(document) {
return errors.New("document failed schema validtion: " + fmt.Sprint(document))
}
}
d.documents = append(d.documents, documents...)
err := d.save()
if err != nil {
return err
}
return nil
}
func (d *Database) FindOne(query map[string]interface{}) map[string]interface{} {
d.mutex.Lock()
defer d.mutex.Unlock()
found := d.searchDocuments(query)
if len(found) == 0 {
return nil
}
return d.documents[found[0]]
}
func (d *Database) FindMany(query map[string]interface{}) []map[string]interface{} {
d.mutex.Lock()
defer d.mutex.Unlock()
found := d.searchDocuments(query)
if len(found) == 0 {
return nil
}
results := []map[string]interface{}{}
for _, index := range found {
results = append(results, d.documents[index])
}
return results
}
func (d *Database) UpdateOne(query map[string]interface{}, document map[string]interface{}) error {
d.mutex.Lock()
defer d.mutex.Unlock()
found := d.searchDocuments(query)
if len(found) == 0 {
return errors.New("could not find document to update")
}
temp := d.documents[found[0]]
for key, value := range document {
temp[key] = value
}
d.documents[found[0]] = temp
err := d.save()
if err != nil {
return err
}
return nil
}
func (d *Database) UpdateMany(query map[string]interface{}, document map[string]interface{}) error {
d.mutex.Lock()
defer d.mutex.Unlock()
found := d.searchDocuments(query)
if len(found) == 0 {
return errors.New("could not find document(s) to update")
}
for _, index := range found {
temp := d.documents[index]
for key, value := range document {
temp[key] = value
}
d.documents[index] = temp
}
err := d.save()
if err != nil {
return err
}
return nil
}
func (d *Database) DeleteOne(query map[string]interface{}) error {
d.mutex.Lock()
defer d.mutex.Unlock()
found := d.searchDocuments(query)
if len(found) == 0 {
return errors.New("could not find document to update")
}
d.documents = append(d.documents[:found[0]], d.documents[found[0]+1:]...)
err := d.save()
if err != nil {
return err
}
return nil
}
func (d *Database) DeleteMany(query map[string]interface{}) error {
d.mutex.Lock()
defer d.mutex.Unlock()
found := d.searchDocuments(query)
if len(found) == 0 {
return errors.New("could not find document(s) to update")
}
foundMap := map[int]bool{}
for _, value := range found {
foundMap[value] = true
}
temp := []map[string]interface{}{}
for index, value := range d.documents {
if foundMap[index] {
temp = append(temp, value)
}
}
d.documents = temp
err := d.save()
if err != nil {
return err
}
return nil
}
func (d *Database) load() error {
content := "[]"
if d.config.Path != "" {
if _, err := os.Stat(d.config.Path); os.IsNotExist(err) {
_, err := os.Create(d.config.Path)
if err != nil {
return errors.New("failed to create file: " + d.config.Path)
}
}
bytes, err := ioutil.ReadFile(d.config.Path)
if err != nil {
return errors.New("failed to read file: " + d.config.Path)
}
if len(bytes) != 0 {
content = string(bytes)
}
}
documents := []map[string]interface{}{}
err := json.Unmarshal([]byte(content), &documents)
if err != nil {
return err
}
d.documents = documents
return nil
}
func (d *Database) save() error {
if d.config.Path == "" {
return nil
}
bytes, err := json.MarshalIndent(d.documents, "", "\t")
if err != nil {
return errors.New("failed to marshal JSON")
}
err = ioutil.WriteFile(d.config.Path, bytes, 0644)
if err != nil {
return errors.New("failed to write data to file")
}
return nil
}
func (d *Database) searchDocuments(query map[string]interface{}) []int {
found := []int{}
for index, document := range d.documents {
include := true
for key, queryValue := range query {
if !include {
break
}
documentValue := document[key]
include = matchValues(queryValue, documentValue)
}
if include {
found = append(found, index)
}
}
return found
}
func matchValues(queryValue interface{}, documentValue interface{}) bool {
if queryValue == documentValue {
return true
}
switch x := queryValue.(type) {
case QueryFunc:
return x(documentValue)
}
return false
}