-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcamellia.go
625 lines (499 loc) · 12.5 KB
/
camellia.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// camellia is a lightweight, persistent, hierarchical key-value store.
//
// Its minimal footprint (just a single SQLite .db file) makes it suitable for usage in embedded systems, or simply as a minimalist application settings container.
//
// For more info about usage and examples, see the README at https://github.com/debevv/camellia
package camellia
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
_ "github.com/mattn/go-sqlite3"
)
/* BaseType is the type set of built-in types accepted by Get/Set functions */
type BaseType interface {
~int | ~uint | ~int8 | ~uint8 | ~int32 | ~uint32 | ~int64 | ~uint64 | ~float32 | ~float64 | ~bool | ~string
}
/*
TODO: go1.18 doesn't support union of explicit types and interfaces when defining type sets in constraint interfaces
So this is not possible for now:
/*
type CustomStringable interface {
String() string
FromString(s string) error
}
type Stringable interface {
BaseType | CustomStringable
}
For more details: https://github.com/golang/go/issues/45346#issuecomment-862505803
*/
/* Stringable is the type set of types accepted by Get/Set functions */
type Stringable interface {
BaseType
}
/*
Entry represents a single node in the hierarchical store.
When IsValue == true, the Entry carries a value, and it's a leaf node in the hierarchy.
When IsValue == false, the Entry does not carry a value, but its Children map can contain Entires.
*/
type Entry struct {
Path string
LastUpdate time.Time
IsValue bool
Value string
Children map[string]*Entry
}
var (
ErrPathInvalid = errors.New("invalid path")
ErrPathNotFound = errors.New("path not found")
ErrPathIsNotAValue = errors.New("path is not a value")
ErrValueEmpty = errors.New("value is empty")
ErrNoDB = errors.New("no DB currently opened")
ErrDBVersionMismatch = errors.New("DB version mismatch")
)
var initialized = int32(0)
var mutex sync.Mutex
/*
Open initializes a camellia DB for usage.
Most of the API methods will return ErrNoDB if Open is not called first.
*/
func Open(path string) (bool, error) {
mutex.Lock()
defer mutex.Unlock()
if atomic.LoadInt32(&initialized) == 1 {
return false, nil
}
wipeHooks()
created, err := openDB(path)
if err != nil {
return false, fmt.Errorf("error opening DB - %w", err)
}
atomic.StoreInt32(&initialized, 1)
return created, nil
}
/*
Close closes a camellia DB.
*/
func Close() error {
mutex.Lock()
defer mutex.Unlock()
if atomic.LoadInt32(&initialized) == 0 {
return ErrNoDB
}
err := closeDB()
if err != nil {
return fmt.Errorf("error closing DB - %w", err)
}
wipeHooks()
atomic.StoreInt32(&initialized, 0)
return nil
}
/*
IsOpen returns whether a DB is currently open.
*/
func IsOpen() bool {
i := atomic.LoadInt32(&initialized)
if i == 0 {
return false
} else {
return true
}
}
/*
Migrate migrates a DB at dbPath to the current supported DB schema version.
Returns true if the DB was actually migrated, false if it was already at the current supported DB schema version.
*/
func Migrate(dbPath string) (bool, error) {
mutex.Lock()
defer mutex.Unlock()
created, err := Open(dbPath)
if err != nil {
return false, err
}
if created {
return true, nil
}
return migrate()
}
/*
GetDBPath returns the path of the current open DB.
*/
func GetDBPath() string {
mutex.Lock()
defer mutex.Unlock()
return dbPath
}
/*
GetSupportedDBSchemaVersion returns the current supported DB schema version.
*/
func GetSupportedDBSchemaVersion() uint64 {
mutex.Lock()
defer mutex.Unlock()
return dbVersion
}
/*
Set sets a value of type T to the specified path.
*/
func Set[T Stringable](path string, value T) error {
mutex.Lock()
defer mutex.Unlock()
if atomic.LoadInt32(&initialized) == 0 {
return ErrNoDB
}
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("error beginning transaction - %w", err)
}
err = setValue(normalizePath(path), fmt.Sprint(value), tx, false, false)
if err != nil {
tx.Rollback()
return err
}
err = tx.Commit()
if err != nil {
tx.Rollback()
return fmt.Errorf("error committing transaction - %w", err)
}
return nil
}
/*
Force sets a value of type T to the specified path.
If a non-value Entry already exists at the specified path, it is deleted first.
*/
func Force[T Stringable](path string, value T) error {
mutex.Lock()
defer mutex.Unlock()
if atomic.LoadInt32(&initialized) == 0 {
return ErrNoDB
}
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("error beginning transaction - %w", err)
}
err = setValue(normalizePath(path), fmt.Sprint(value), tx, true, false)
if err != nil {
tx.Rollback()
return err
}
err = tx.Commit()
if err != nil {
tx.Rollback()
return fmt.Errorf("error committing transaction - %w", err)
}
return nil
}
/*
SetOrPanic calls Set with the specified parameters, and panics in case of error.
*/
func SetOrPanic[T Stringable](path string, value T) {
mutex.Lock()
defer mutex.Unlock()
if atomic.LoadInt32(&initialized) == 0 {
panic(ErrNoDB)
}
tx, err := db.Begin()
if err != nil {
panic(fmt.Errorf("error beginning transaction - %w", err))
}
err = setValue(normalizePath(path), fmt.Sprint(value), tx, false, false)
if err != nil {
tx.Rollback()
panic(err)
}
err = tx.Commit()
if err != nil {
tx.Rollback()
panic(fmt.Errorf("error committing transaction - %w", err))
}
}
/*
ForceOrPanic calls Force with the specified parameters, and panics in case of error.
*/
func ForceOrPanic[T Stringable](path string, value T) {
mutex.Lock()
defer mutex.Unlock()
if atomic.LoadInt32(&initialized) == 0 {
panic(ErrNoDB)
}
tx, err := db.Begin()
if err != nil {
panic(fmt.Errorf("error beginning transaction - %w", err))
}
err = setValue(normalizePath(path), fmt.Sprint(value), tx, true, false)
if err != nil {
tx.Rollback()
panic(err)
}
err = tx.Commit()
if err != nil {
tx.Rollback()
panic(fmt.Errorf("error committing transaction - %w", err))
}
}
/*
Get reads the value a the specified path and returns it as type T.
*/
func Get[T Stringable](path string) (T, error) {
mutex.Lock()
defer mutex.Unlock()
var value T
if atomic.LoadInt32(&initialized) == 0 {
return value, ErrNoDB
}
tx, err := db.Begin()
if err != nil {
return value, fmt.Errorf("error beginning transaction - %w", err)
}
valueString, err := getValue(normalizePath(path), tx)
if err != nil {
tx.Rollback()
return value, err
}
value, err = convertValue[T](valueString)
if err != nil {
tx.Rollback()
return value, fmt.Errorf("error converting value %v to string - %w", value, err)
}
err = tx.Commit()
if err != nil {
tx.Rollback()
return value, fmt.Errorf("error committing transaction - %w", err)
}
return value, nil
}
/*
GetOrPanic calls Get with the specified parameters, and panics in case of error.
*/
func GetOrPanic[T Stringable](path string) T {
mutex.Lock()
defer mutex.Unlock()
var value T
if atomic.LoadInt32(&initialized) == 0 {
panic(ErrNoDB)
}
tx, err := db.Begin()
if err != nil {
panic(fmt.Errorf("error beginning transaction - %w", err))
}
valueString, err := getValue(normalizePath(path), tx)
if err != nil {
tx.Rollback()
panic(err)
}
value, err = convertValue[T](valueString)
if err != nil {
tx.Rollback()
panic(fmt.Errorf("error converting value %v to string - %w", value, err))
}
err = tx.Commit()
if err != nil {
tx.Rollback()
panic(fmt.Errorf("error committing transaction - %w", err))
}
return value
}
/*
GetOrPanic calls Get with the specified parameters, and panics if the read value is empty or in case of error.
*/
func GetOrPanicEmpty[T Stringable](path string) T {
mutex.Lock()
defer mutex.Unlock()
if atomic.LoadInt32(&initialized) == 0 {
panic(ErrNoDB)
}
var value T
tx, err := db.Begin()
if err != nil {
panic(fmt.Errorf("error beginning transaction - %w", err))
}
valueString, err := getValue(normalizePath(path), tx)
if err != nil {
tx.Rollback()
panic(fmt.Errorf("error getting value %s - %w", path, err))
}
if valueString == "" {
tx.Rollback()
panic(ErrValueEmpty)
}
value, err = convertValue[T](valueString)
if err != nil {
tx.Rollback()
panic(fmt.Errorf("error converting value %s - %w", path, err))
}
err = tx.Commit()
if err != nil {
tx.Rollback()
panic(fmt.Errorf("error committing transaction - %w", err))
}
return value
}
/*
GetEntry returns the Entry at the specified path, including the eventual full hierarchy of children Entries.
*/
func GetEntry(path string) (*Entry, error) {
return GetEntryDepth(path, -1)
}
/*
GetEntry returns the Entry at the specified path, including the eventual hierarchy of children Entries, but stopping
at a specified depth.
With depth > 0, stops at the specified depth.
With depth == 0, returns the Entry with an empty Children map.
With depth < 0, returns the full hierarchy of children Entries.
*/
func GetEntryDepth(path string, depth int) (*Entry, error) {
mutex.Lock()
defer mutex.Unlock()
if atomic.LoadInt32(&initialized) == 0 {
return nil, ErrNoDB
}
tx, err := db.Begin()
if err != nil {
return nil, fmt.Errorf("error beginning transaction - %w", err)
}
entry, err := getEntryDepth(normalizePath(path), depth, tx)
if err != nil {
tx.Rollback()
return nil, err
}
err = tx.Commit()
if err != nil {
tx.Rollback()
return nil, fmt.Errorf("error committing transaction - %w", err)
}
return entry, nil
}
/*
Exists returns whether an Entry exists at the specified path.
*/
func Exists(path string) (bool, error) {
mutex.Lock()
defer mutex.Unlock()
if atomic.LoadInt32(&initialized) == 0 {
return false, ErrNoDB
}
tx, err := db.Begin()
if err != nil {
return false, fmt.Errorf("error beginning transaction - %w", err)
}
exists, err := exists(normalizePath(path), tx)
if err != nil {
tx.Rollback()
return false, err
}
err = tx.Commit()
if err != nil {
tx.Rollback()
return false, fmt.Errorf("error committing transaction - %w", err)
}
return exists, nil
}
/*
Recurse recurses, breadth-first, the hierarchy of Entries at the specified path, starting with the Entry at the path.
For each entry, calls the specified callback with the Entry itself, its parent Entry, and the current relative depth
in the hierarchy, with 0 being the depth of the Entry at the specified path.
*/
func Recurse(path string, depth int, cb func(entry *Entry, parent *Entry, depth uint) error) error {
mutex.Lock()
defer mutex.Unlock()
if atomic.LoadInt32(&initialized) == 0 {
return ErrNoDB
}
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("error beginning transaction - %w", err)
}
err = recurse(normalizePath(path), depth, cb, tx)
if err != nil {
tx.Rollback()
return err
}
err = tx.Commit()
if err != nil {
return fmt.Errorf("error committing transaction - %w", err)
}
return nil
}
/*
Delete recursively deletes the Entry at the specified path and its children, if any.
*/
func Delete(path string) error {
mutex.Lock()
defer mutex.Unlock()
if atomic.LoadInt32(&initialized) == 0 {
return ErrNoDB
}
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("error beginning transaction - %w", err)
}
err = deleteEntry(normalizePath(path), tx)
if err != nil {
tx.Rollback()
return err
}
err = tx.Commit()
if err != nil {
return fmt.Errorf("error committing transaction - %w", err)
}
return nil
}
/*
Wipe deletes every Entry in the database, except for the root one (at path "").
*/
func Wipe() error {
mutex.Lock()
defer mutex.Unlock()
if atomic.LoadInt32(&initialized) == 0 {
return ErrNoDB
}
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("error beginning transaction - %w", err)
}
root, err := getEntryDepth(normalizePath(""), 1, tx)
if err != nil {
return err
}
for _, child := range root.Children {
err = deleteEntry(child.Path, tx)
if err != nil {
tx.Rollback()
return err
}
}
err = tx.Commit()
if err != nil {
return fmt.Errorf("error committing transaction - %w", err)
}
return nil
}
func convertValue[T Stringable](valueString string) (T, error) {
var value T
/*
TODO: See up
xValue := reflect.ValueOf(&value)
method := xValue.MethodByName("FromString")
if method.IsValid() {
ret := method.Call([]reflect.Value{reflect.ValueOf(valueString)})
if !ret[0].IsNil() {
err, ok := ret[1].Interface().(error)
if !ok {
return value, fmt.Errorf("error converting value to requested type")
} else {
return value, err
}
}
} else {
*/
n, err := fmt.Sscan(valueString, &value)
if n != 1 {
return value, fmt.Errorf("error converting value to requested type")
}
if err != nil {
return value, fmt.Errorf("error converting value to requested type - %w", err)
}
//}
return value, nil
}