forked from perkeep/perkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbm.go
1151 lines (974 loc) · 24.4 KB
/
dbm.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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 The dbm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dbm
//DONE +Top level Sync? Optional? (Measure it)
// Too slow. Added db.Sync() instead.
//DONE user defined collating
// - on DB create (sets the default)
// - per Array? (probably a MUST HAVE feature)
//----
// After Go will support Unicode locale collating. But that would have
// to bee a too different API then. (package udbm?)
import (
"fmt"
"os"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
"camlistore.org/third_party/github.com/cznic/exp/lldb"
"camlistore.org/third_party/github.com/cznic/fileutil"
)
const (
aCacheSize = 500
fCacheSize = 500
sCacheSize = 50
rname = "2remove" // Array shredder queue
arraysPrefix = 'A'
filesPrefix = 'F'
systemPrefix = 'S'
magic = "\x60\xdb\xf1\x1e"
)
// Test hooks
var (
compress = true // Dev hook
activeVictors int32
)
const (
stDisabled = iota // stDisabled must be zero
stIdle
stCollecting
stIdleArmed
stCollectingArmed
stCollectingTriggered
stEndUpdateFailed
)
func init() {
if stDisabled != 0 {
panic("stDisabled != 0")
}
}
type DB struct {
_root *Array // Root directory, do not access directly
acache treeCache // Arrays cache
acidNest int // Grace period nesting level
acidState int // Grace period FSM state.
acidTimer *time.Timer // Grace period timer
alloc *lldb.Allocator // The machinery. Wraps filer
bkl sync.Mutex // Big Kernel Lock
closeMu sync.Mutex // Close() coordination
closed chan bool
emptySize int64 // Any header size including FLT.
f *os.File // Underlying file. Potentially nil (if filer is lldb.MemFiler)
fcache treeCache // Files cache
filer lldb.Filer // Wraps f
gracePeriod time.Duration // WAL grace period
isMem bool // No signal capture
lastCommitErr error
lock *os.File // The DB file lock
removing map[int64]bool // BTrees being removed
removingMu sync.Mutex // Remove() coordination
scache treeCache // System arrays cache
stop chan int // Remove() coordination
wg sync.WaitGroup // Remove() coordination
xact bool // Updates are made within automatic structural transactions
}
// Create creates the named DB file mode 0666 (before umask). The file must not
// already exist. If successful, methods on the returned DB can be used for
// I/O; the associated file descriptor has mode os.O_RDWR. If there is an
// error, it will be of type *os.PathError.
//
// For the meaning of opts please see documentation of Options.
func Create(name string, opts *Options) (db *DB, err error) {
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
return
}
return create(f, lldb.NewSimpleFileFiler(f), opts, false)
}
func create(f *os.File, filer lldb.Filer, opts *Options, isMem bool) (db *DB, err error) {
defer func() {
lock := opts.lock
if err != nil && lock != nil {
n := lock.Name()
lock.Close()
os.Remove(n)
db = nil
}
}()
if err = opts.check(filer.Name(), true, !isMem); err != nil {
return
}
b := [16]byte{byte(magic[0]), byte(magic[1]), byte(magic[2]), byte(magic[3]), 0x00} // ver 0x00
if n, err := filer.WriteAt(b[:], 0); n != 16 {
return nil, &os.PathError{Op: "dbm.Create.WriteAt", Path: filer.Name(), Err: err}
}
db = &DB{emptySize: 128, f: f, lock: opts.lock, closed: make(chan bool)}
if filer, err = opts.acidFiler(db, filer); err != nil {
return nil, err
}
db.filer = filer
if err = filer.BeginUpdate(); err != nil {
return
}
defer func() {
if e := filer.EndUpdate(); e != nil {
if err == nil {
err = e
}
}
}()
if db.alloc, err = lldb.NewAllocator(lldb.NewInnerFiler(filer, 16), &lldb.Options{}); err != nil {
return nil, &os.PathError{Op: "dbm.Create", Path: filer.Name(), Err: err}
}
db.alloc.Compress = compress
db.isMem = isMem
return db, db.boot()
}
// CreateMem creates an in-memory DB not backed by a disk file. Memory DBs are
// resource limited as they are completely held in memory and are not
// automatically persisted.
//
// For the meaning of opts please see documentation of Options.
func CreateMem(opts *Options) (db *DB, err error) {
f := lldb.NewMemFiler()
if opts.ACID == ACIDFull {
opts.ACID = ACIDTransactions
}
return create(nil, f, opts, true)
}
// CreateTemp creates a new temporary DB in the directory dir with a basename
// beginning with prefix and name ending in suffix. If dir is the empty string,
// CreateTemp uses the default directory for temporary files (see os.TempDir).
// Multiple programs calling CreateTemp simultaneously will not choose the same
// file name for the DB. The caller can use Name() to find the pathname of the
// DB file. It is the caller's responsibility to remove the file when no longer
// needed.
//
// For the meaning of opts please see documentation of Options.
func CreateTemp(dir, prefix, suffix string, opts *Options) (db *DB, err error) {
f, err := fileutil.TempFile(dir, prefix, suffix)
if err != nil {
return
}
return create(f, lldb.NewSimpleFileFiler(f), opts, false)
}
// Open opens the named DB file for reading/writing. If successful, methods on
// the returned DB can be used for I/O; the associated file descriptor has mode
// os.O_RDWR. If there is an error, it will be of type *os.PathError.
//
// For the meaning of opts please see documentation of Options.
func Open(name string, opts *Options) (db *DB, err error) {
defer func() {
lock := opts.lock
if err != nil && lock != nil {
n := lock.Name()
lock.Close()
os.Remove(n)
db = nil
}
if err != nil {
if db != nil {
db.Close()
db = nil
}
}
}()
if err = opts.check(name, false, true); err != nil {
return
}
f, err := os.OpenFile(name, os.O_RDWR, 0666)
if err != nil {
return
}
filer := lldb.Filer(lldb.NewSimpleFileFiler(f))
sz, err := filer.Size()
if err != nil {
return
}
if sz%16 != 0 {
return nil, &os.PathError{Op: "dbm.Open:", Path: name, Err: fmt.Errorf("file size %d(%#x) is not 0 (mod 16)", sz, sz)}
}
var b [16]byte
if n, err := filer.ReadAt(b[:], 0); n != 16 || err != nil {
return nil, &os.PathError{Op: "dbm.Open.ReadAt", Path: name, Err: err}
}
var h header
if err = h.rd(b[:]); err != nil {
return nil, &os.PathError{Op: "dbm.Open:validate header", Path: name, Err: err}
}
db = &DB{f: f, lock: opts.lock, closed: make(chan bool)}
if filer, err = opts.acidFiler(db, filer); err != nil {
return nil, err
}
db.filer = filer
switch h.ver {
default:
return nil, &os.PathError{Op: "dbm.Open", Path: name, Err: fmt.Errorf("unknown dbm file format version %#x", h.ver)}
case 0x00:
return open00(name, db)
}
}
// Close closes the DB, rendering it unusable for I/O. It returns an error, if
// any. Failing to call Close before exiting a program can render the DB
// unusable or, in case of using WAL/2PC, the last committed transaction may
// get lost.
//
// Close is idempotent.
func (db *DB) Close() (err error) {
if err = db.enter(); err != nil {
return
}
doLeave := true
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
if doLeave {
db.leave(&err)
}
}()
db.closeMu.Lock()
defer db.closeMu.Unlock()
select {
case _ = <-db.closed:
return
default:
}
defer close(db.closed)
if db.acidTimer != nil {
db.acidTimer.Stop()
}
var e error
for db.acidNest > 0 {
db.acidNest--
if err := db.filer.EndUpdate(); err != nil {
e = err
}
}
err = e
doLeave = false
e = db.leave(&err)
if err = db.close(); err == nil {
err = e
}
if lock := db.lock; lock != nil {
n := lock.Name()
e1 := lock.Close()
db.lock = nil
e2 := os.Remove(n)
if err == nil {
err = e1
}
if err == nil {
err = e2
}
}
return
}
func (db *DB) close() (err error) {
if db.stop != nil {
close(db.stop)
db.wg.Wait()
db.stop = nil
}
if db.f == nil { // lldb.MemFiler
return
}
err = db.filer.Sync()
if err2 := db.filer.Close(); err2 != nil && err == nil {
err = err2
}
return
}
func (db *DB) root() (r *Array, err error) {
if r = db._root; r != nil {
return
}
sz, err := db.filer.Size()
if err != nil {
return
}
switch {
case sz < db.emptySize:
panic(fmt.Errorf("internal error: %d", sz))
case sz == db.emptySize:
tree, h, err := lldb.CreateBTree(db.alloc, collate)
if err != nil {
return nil, err
}
if h != 1 {
panic("internal error")
}
r = &Array{db, tree, nil, "", 0}
db._root = r
return r, nil
default:
tree, err := lldb.OpenBTree(db.alloc, collate, 1)
if err != nil {
return nil, err
}
r = &Array{db, tree, nil, "", 0}
db._root = r
return r, nil
}
}
// Array returns an Array associated with a subtree of array, determined by
// subscripts.
func (db *DB) Array(array string, subscripts ...interface{}) (a Array, err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
return db.array_(false, array, subscripts...)
}
func (db *DB) array_(canCreate bool, array string, subscripts ...interface{}) (a Array, err error) {
a.db = db
if a, err = a.array(subscripts...); err != nil {
return
}
a.tree, err = db.acache.getTree(db, arraysPrefix, array, canCreate, aCacheSize)
a.name = array
a.namespace = arraysPrefix
return
}
func (db *DB) sysArray(canCreate bool, array string) (a Array, err error) {
a.db = db
a.tree, err = db.scache.getTree(db, systemPrefix, array, canCreate, sCacheSize)
a.name = array
a.namespace = systemPrefix
return a, err
}
func (db *DB) fileArray(canCreate bool, name string) (f File, err error) {
var a Array
a.db = db
a.tree, err = db.fcache.getTree(db, filesPrefix, name, canCreate, fCacheSize)
a.name = name
a.namespace = filesPrefix
return File(a), err
}
// Set sets the value at subscripts in array. Any previous value, if existed,
// is overwritten by the new one.
func (db *DB) Set(value interface{}, array string, subscripts ...interface{}) (err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
a, err := db.array_(true, array, subscripts...)
if err != nil {
return
}
return a.set(value)
}
// Get returns the value at subscripts in array, or nil if no such value
// exists.
func (db *DB) Get(array string, subscripts ...interface{}) (value interface{}, err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
a, err := db.array_(false, array, subscripts...)
if a.tree == nil || err != nil {
return
}
return a.get()
}
// Slice returns a new Slice of array, with a subscripts range of [from, to].
// If from is nil it works as 'from lowest existing key'. If to is nil it
// works as 'to highest existing key'.
func (db *DB) Slice(array string, subscripts, from, to []interface{}) (s *Slice, err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
a, err := db.array_(false, array, subscripts...)
if a.tree == nil || err != nil {
return
}
return a.Slice(from, to)
}
// Delete deletes the value at subscripts in array.
func (db *DB) Delete(array string, subscripts ...interface{}) (err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
a, err := db.array_(false, array, subscripts...)
if a.tree == nil || err != nil {
return
}
return a.delete(subscripts...)
}
// Clear empties the subtree at subscripts in array.
func (db *DB) Clear(array string, subscripts ...interface{}) (err error) {
if err = db.enter(); err != nil {
return
}
doLeave := true
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
if doLeave {
db.leave(&err)
}
}()
a, err := db.array_(false, array, subscripts...)
if a.tree == nil || err != nil {
return
}
doLeave = false
e := db.leave(&err)
if err = a.Clear(); err == nil {
err = e
}
return
}
// Name returns the name of the DB file.
func (db *DB) Name() string {
return db.filer.Name()
}
// Size returns the size of the DB file.
func (db *DB) Size() (sz int64, err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
return db.filer.Size()
}
func (db *DB) setRemoving(h int64, flag bool) (r bool) {
db.removingMu.Lock()
defer db.removingMu.Unlock()
if db.removing == nil {
db.removing = map[int64]bool{h: flag}
return
}
r = db.removing[h]
switch flag {
case true:
db.removing[h] = flag
case false:
delete(db.removing, h)
}
return
}
// RemoveArray removes array from the DB.
func (db *DB) RemoveArray(array string) (err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
return db.removeArray(arraysPrefix, array)
}
// RemoveFile removes file from the DB.
func (db *DB) RemoveFile(file string) (err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
return db.removeArray(filesPrefix, file)
}
func (db *DB) removeArray(prefix int, array string) (err error) {
if db.stop == nil {
db.stop = make(chan int)
}
t, err := db.acache.getTree(db, prefix, array, false, aCacheSize)
if t == nil || err != nil {
return
}
h := t.Handle()
if db.setRemoving(h, true) {
return
}
delete(db.acache, array)
root, err := db.root()
if err != nil {
return
}
removes, err := db.sysArray(true, rname)
if err != nil {
return
}
if err = removes.set(nil, h); err != nil {
return
}
if err = root.delete(prefix, array); err != nil {
return
}
db.wg.Add(1)
go db.victor(removes, h)
return
}
func (db *DB) boot() (err error) {
const tmp = "/tmp/"
aa, err := db.Arrays()
if err != nil {
return
}
s, err := aa.Slice([]interface{}{tmp}, nil)
if err = noEof(err); err != nil {
return
}
s.Do(func(subscripts, value []interface{}) (r bool, err error) {
k := subscripts[0].(string)
if !strings.HasPrefix(k, tmp) {
return false, nil
}
return true, db.RemoveArray(k)
})
ff, err := db.Files()
if err != nil {
return
}
s, err = ff.Slice([]interface{}{tmp}, nil)
if err = noEof(err); err != nil {
return
}
s.Do(func(subscripts, value []interface{}) (r bool, err error) {
k := subscripts[0].(string)
if !strings.HasPrefix(k, tmp) {
return false, nil
}
return true, db.RemoveFile(k)
})
removes, err := db.sysArray(false, rname)
if removes.tree == nil || err != nil {
return
}
s, err = removes.Slice(nil, nil)
if err = noEof(err); err != nil {
return
}
var a []int64
s.Do(func(subscripts, value []interface{}) (r bool, err error) {
r = true
switch {
case len(subscripts) == 1:
h, ok := subscripts[0].(int64)
if ok {
a = append(a, h)
return
}
fallthrough
default:
err = removes.Delete(subscripts)
return
}
})
if db.stop == nil {
db.stop = make(chan int)
}
for _, h := range a {
if db.setRemoving(h, true) {
continue
}
db.wg.Add(1)
go db.victor(removes, h)
}
return
}
func (db *DB) victor(removes Array, h int64) {
atomic.AddInt32(&activeVictors, 1)
var err error
var finished bool
defer func() {
if finished {
func() {
db.enter()
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
lldb.RemoveBTree(db.alloc, h)
removes.delete(h)
db.setRemoving(h, false)
}()
}
db.wg.Done()
atomic.AddInt32(&activeVictors, -1)
}()
db.enter()
doLeave := true
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
if doLeave {
db.leave(&err)
}
}()
t, err := lldb.OpenBTree(db.alloc, collate, h)
if err != nil {
finished = true
return
}
doLeave = false
if db.leave(&err) != nil {
return
}
for {
runtime.Gosched()
select {
case _, ok := <-db.stop:
if !ok {
return
}
default:
}
db.enter()
doLeave = true
if finished, err = t.DeleteAny(); finished || err != nil {
return
}
doLeave = false
if db.leave(&err) != nil {
return
}
}
}
// Arrays returns a read-only meta array which registers other arrays by name
// as its keys. The associated values are meaningless but non-nil if the value
// exists.
func (db *DB) Arrays() (a Array, err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
p, err := db.root()
if err != nil {
return a, err
}
return p.array(arraysPrefix)
}
// Files returns a read-only meta array which registers all Files in the DB by
// name as its keys. The associated values are meaningless but non-nil if the
// value exists.
func (db *DB) Files() (a Array, err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
p, err := db.root()
if err != nil {
return a, err
}
return p.array(filesPrefix)
}
func (db *DB) enter() (err error) {
db.bkl.Lock()
switch db.acidState {
default:
panic("internal error")
case stDisabled:
// nop
case stIdle:
if err = db.filer.BeginUpdate(); err != nil {
return
}
db.acidNest = 1
db.acidTimer = time.AfterFunc(db.gracePeriod, db.timeout)
db.acidState = stCollecting
case stCollecting:
db.acidNest++
case stIdleArmed:
db.acidNest = 1
db.acidState = stCollectingArmed
case stCollectingArmed:
db.acidNest++
case stCollectingTriggered:
db.acidNest++
case stEndUpdateFailed:
return db.leave(&err)
}
if db.xact {
err = db.filer.BeginUpdate()
}
return
}
func (db *DB) leave(err *error) error {
switch db.acidState {
default:
panic("internal error")
case stDisabled:
// nop
case stIdle:
panic("internal error")
case stCollecting:
db.acidNest--
if db.acidNest == 0 {
db.acidState = stIdleArmed
}
case stIdleArmed:
panic("internal error")
case stCollectingArmed:
db.acidNest--
if db.acidNest == 0 {
db.acidState = stIdleArmed
}
case stCollectingTriggered:
db.acidNest--
if db.acidNest == 0 {
if e := db.filer.EndUpdate(); e != nil && err == nil {
*err = e
}
db.acidState = stIdle
}
case stEndUpdateFailed:
db.bkl.Unlock()
return fmt.Errorf("Last transaction commit failed: %v", db.lastCommitErr)
}
if db.xact {
switch {
case *err != nil:
db.filer.Rollback() // return the original, input error
default:
*err = db.filer.EndUpdate()
if *err != nil {
db.acidState = stEndUpdateFailed
db.lastCommitErr = *err
}
}
}
db.bkl.Unlock()
return *err
}
func (db *DB) timeout() {
db.bkl.Lock()
defer db.bkl.Unlock()
select {
case _ = <-db.closed:
return
default:
}
switch db.acidState {
default:
panic("internal error")
case stIdle:
panic("internal error")
case stCollecting:
db.acidState = stCollectingTriggered
case stIdleArmed:
if err := db.filer.EndUpdate(); err != nil { // If EndUpdate fails, no WAL was written (automatic Rollback)
db.acidState = stEndUpdateFailed
db.lastCommitErr = err
return
}
db.acidState = stIdle
case stCollectingArmed:
db.acidState = stCollectingTriggered
case stCollectingTriggered:
panic("internal error")
}
}
// Sync commits the current contents of the DB file to stable storage.
// Typically, this means flushing the file system's in-memory copy of recently
// written data to disk.
//
// NOTE: There's no good reason to invoke Sync if db uses 2PC/WAL (see
// Options.ACID).
func (db *DB) Sync() (err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
return db.filer.Sync()
}
// File returns a File associated with name.
func (db *DB) File(name string) (f File, err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
f, err = db.fileArray(false, name)