-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjstore.go
310 lines (257 loc) · 6.11 KB
/
jstore.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
304
305
306
307
308
309
310
package jstore
import (
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"sync"
)
type Db struct {
file string
mutex sync.Mutex
content map[string]interface{}
// flags
inMemory bool
ManualFlush bool
humanReadable bool
}
type DbFlag int
const (
MinimizedJson DbFlag = iota
ManualFlush // force manual flush instead of automatically write/read
)
const InMemoryDb = "memory"
func isFlagSet(in []DbFlag, search DbFlag) bool {
for i := 0; i < len(in); i++ {
if in[i] == search {
return true
}
}
return false
}
func New(file string, flags ...DbFlag) (*Db, error) {
db := Db{
file: file,
content: map[string]interface{}{},
inMemory: true,
ManualFlush: isFlagSet(flags, ManualFlush),
humanReadable: !isFlagSet(flags, MinimizedJson),
}
// create a file
if file != "" && file != "memory" {
// If the file doesn't exist, create it, or append to the file
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
f.Close()
db.inMemory = false
}
return &db, nil
}
func (db *Db) DelCollection(in string) error {
delete(db.content, in)
if !db.inMemory && !db.ManualFlush {
return db.flushToFile()
}
return nil
}
// colExists returns true if the specific key for the collection exits, else it returns false
func (db *Db) colExists(name string) bool {
if _, ok := db.content[name]; !ok {
return false
}
return true
}
func (db *Db) flushToFile() error {
bytes := db.Json()
err := os.WriteFile(db.file, bytes, 0644)
if err != nil {
return err
}
return nil
}
func (db *Db) Json() []byte {
var bytes []byte
var err error
// json.Marshal function can return two types of errors: UnsupportedTypeError or UnsupportedValueError
// both cases are handled when adding data with Set, hence omitting error handling here
if db.humanReadable {
bytes, err = json.MarshalIndent(db.content, "", " ")
if err != nil {
panic(err)
}
} else {
bytes, err = json.Marshal(db.content)
if err != nil {
panic(err)
}
}
return bytes
}
func (db *Db) readFile() error {
f, err := os.Open(db.file)
if err != nil {
return fmt.Errorf("unable to open file: %v", err)
}
defer f.Close()
bytes, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("unable to read file: %v", err)
}
if len(bytes) == 0 {
return fmt.Errorf("file is empty")
}
var data map[string]interface{}
err = json.Unmarshal(bytes, &data)
if err != nil {
return fmt.Errorf("unable to unmarshal file: %v", err)
}
for k, v := range data {
db.content[k] = v
}
return nil
}
type payloadT int
const (
payloadMultiple payloadT = iota
payloadSingleStruct
payloadSingleItem
payloadNotSupported
)
func payloadType(in any) payloadT {
rt := reflect.TypeOf(in)
switch rt.Kind() {
case reflect.Slice, reflect.Array:
return payloadMultiple
case reflect.String, reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
return payloadSingleItem
case reflect.Struct:
return payloadSingleStruct
default:
return payloadNotSupported
}
}
type NonExistentCollectionErr struct{}
func (e NonExistentCollectionErr) Error() string {
return "collection does not exists"
}
type ValueNotAPointer struct{}
func (e ValueNotAPointer) Error() string {
return "the passed value is not a pointer"
}
type UnsupportedData struct{}
func (e UnsupportedData) Error() string {
return "the provided type of data is not supported"
}
// baseKv is the base struct on top what other collection types extend
type baseKv struct {
name string
db *Db
content map[string]interface{}
}
func (kv *baseKv) set(key string, value interface{}) error {
dataType := payloadType(value)
// early data type check
if dataType == payloadNotSupported {
return UnsupportedData{}
}
kv.db.mutex.Lock() // optimisation opportunity, make one mutex per collection instead of a global one
defer kv.db.mutex.Unlock()
b, err := json.Marshal(value)
if err != nil {
return err
}
switch dataType {
case payloadSingleStruct:
var data map[string]interface{}
err = json.Unmarshal(b, &data)
if err != nil {
return err
}
kv.content[key] = data
case payloadMultiple:
var data []interface{}
err = json.Unmarshal(b, &data)
if err != nil {
return err
}
kv.content[key] = data
case payloadSingleItem:
var data interface{}
err = json.Unmarshal(b, &data)
if err != nil {
return err
}
kv.content[key] = data
}
if !kv.db.inMemory && !kv.db.ManualFlush {
return kv.db.flushToFile()
}
return nil
}
func (kv *baseKv) get(key string, value interface{}) error {
if reflect.ValueOf(value).Kind() != reflect.Ptr {
return ValueNotAPointer{}
}
if !kv.db.colExists(kv.name) {
return NonExistentCollectionErr{}
}
kv.db.mutex.Lock()
defer kv.db.mutex.Unlock()
if !kv.db.inMemory {
err := kv.db.readFile()
if err != nil {
return err
}
}
jsonData, err := json.Marshal(kv.content[key])
if err != nil {
return err
}
err = json.Unmarshal(jsonData, &value)
if err != nil {
return err
}
return nil
}
// append adds one item of the type slice stored in value
func (kv *baseKv) append(key string, value interface{}) error {
dataType := payloadType(value)
// early data type check
if dataType == payloadNotSupported {
return UnsupportedData{}
}
kv.db.mutex.Lock() // optimisation opportunity, make one mutex per collection instead of a global one
defer kv.db.mutex.Unlock()
b, err := json.Marshal(value)
if err != nil {
return err
}
if kv.content[key] == nil {
kv.content[key] = []interface{}{}
}
switch dataType {
case payloadSingleStruct, payloadSingleItem:
var data interface{}
err = json.Unmarshal(b, &data)
if err != nil {
return err
}
kv.content[key] = append(kv.content[key].([]interface{}), data)
case payloadMultiple:
var data []interface{}
err = json.Unmarshal(b, &data)
if err != nil {
return err
}
kv.content[key] = append(kv.content[key].([]interface{}), data...)
default:
return UnsupportedData{}
}
return nil
}