This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
file.go
1379 lines (1180 loc) · 25.7 KB
/
file.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 ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Well known handles
// 1: root
// 2: id
package ql
import (
"bytes"
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"sync"
"time"
"github.com/cznic/lldb"
"github.com/cznic/mathutil"
"github.com/cznic/ql/vendored/github.com/camlistore/go4/lock"
)
const (
magic = "\x60\xdbql"
)
var (
_ btreeIndex = (*fileIndex)(nil)
_ btreeIterator = (*fileBTreeIterator)(nil)
_ indexIterator = (*fileIndexIterator)(nil)
_ storage = (*file)(nil)
_ temp = (*fileTemp)(nil)
)
type chunk struct { // expanded to blob types lazily
f *file
b []byte
}
func (c chunk) expand() (v interface{}, err error) {
return c.f.loadChunks(c.b)
}
func expand1(data interface{}, e error) (v interface{}, err error) {
if e != nil {
return nil, e
}
c, ok := data.(chunk)
if !ok {
return data, nil
}
return c.expand()
}
func expand(data []interface{}) (err error) {
for i, v := range data {
if data[i], err = expand1(v, nil); err != nil {
return
}
}
return
}
// OpenFile returns a DB backed by a named file. The back end limits the size
// of a record to about 64 kB.
func OpenFile(name string, opt *Options) (db *DB, err error) {
var f lldb.OSFile
if f = opt.OSFile; f == nil {
f, err = os.OpenFile(name, os.O_RDWR, 0666)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
if !opt.CanCreate {
return nil, err
}
switch opt.FileFormat {
case 0, 1, 2:
// ok
default:
return nil, fmt.Errorf("OpenFile: invalid option.FileFormat value: %v", opt.FileFormat)
}
f, err = os.OpenFile(name, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666)
if err != nil {
return nil, err
}
}
}
nfo, err := f.Stat()
if err != nil {
return nil, err
}
var new bool
switch nfo.Size() {
case 0:
new = true
if opt.FileFormat == 2 {
return openFile2(name, f, opt, new)
}
default:
b := make([]byte, 16)
if _, err := f.ReadAt(b, 0); err != nil {
return nil, err
}
switch {
case bytes.Equal(b[:len(magic)], []byte(magic)):
// ok
case bytes.Equal(b[:len(magic2)], []byte(magic2)):
return openFile2(name, f, opt, new)
default:
return nil, fmt.Errorf("OpenFile: unrecognized file format")
}
}
fi, err := newFileFromOSFile(f, opt.Headroom, new)
if err != nil {
return
}
if fi.tempFile = opt.TempFile; fi.tempFile == nil {
fi.tempFile = func(dir, prefix string) (f lldb.OSFile, err error) {
f0, err := ioutil.TempFile(dir, prefix)
return f0, err
}
}
fi.removeEmptyWAL = opt.RemoveEmptyWAL
return newDB(fi)
}
// Options amend the behavior of OpenFile.
//
// CanCreate
//
// The CanCreate option enables OpenFile to create the DB file if it does not
// exists.
//
// OSFile
//
// OSFile allows to pass an os.File like back end providing, for example,
// encrypted storage. If this field is nil then OpenFile uses the file named by
// the 'name' parameter instead.
//
// TempFile
//
// TempFile provides a temporary file used for evaluating the GROUP BY, ORDER
// BY, ... clauses. The hook is intended to be used by encrypted DB back ends
// to avoid leaks of unecrypted data to such temp files by providing temp files
// which are encrypted as well. Note that *os.File satisfies the lldb.OSFile
// interface.
//
// If TempFile is nil it defaults to ioutil.TempFile.
//
// Headroom
//
// Headroom selects the minimum size a WAL file will have. The "extra"
// allocated file space serves as a headroom. Commits that fit into the
// headroom should not fail due to 'not enough space on the volume' errors. The
// headroom parameter is first rounded-up to a non negative multiple of the
// size of the lldb.Allocator atom.
//
// RemoveEmptyWAL
//
// RemoveEmptyWAL controls whether empty WAL files should be deleted on
// clean exit.
//
// FileVersion
//
// Select DB backend format when creating a new DB file.
//
// Supported values
//
// 0, 1 The original file format (version 1)
// 2 File format version 2
type Options struct {
CanCreate bool
OSFile lldb.OSFile
TempFile func(dir, prefix string) (f lldb.OSFile, err error)
Headroom int64
RemoveEmptyWAL bool
FileFormat int
}
type fileBTreeIterator struct {
en *lldb.BTreeEnumerator
t *fileTemp
}
func (it *fileBTreeIterator) Next() (k, v []interface{}, err error) {
bk, bv, err := it.en.Next()
if err != nil {
return
}
if k, err = lldb.DecodeScalars(bk); err != nil {
return
}
for i, val := range k {
b, ok := val.([]byte)
if !ok {
continue
}
c := chunk{it.t.file, b}
if k[i], err = c.expand(); err != nil {
return nil, nil, err
}
}
if err = enforce(k, it.t.colsK); err != nil {
return
}
if v, err = lldb.DecodeScalars(bv); err != nil {
return
}
for i, val := range v {
b, ok := val.([]byte)
if !ok {
continue
}
c := chunk{it.t.file, b}
if v[i], err = c.expand(); err != nil {
return nil, nil, err
}
}
err = enforce(v, it.t.colsV)
return
}
func enforce(val []interface{}, cols []*col) (err error) {
for i, v := range val {
if val[i], err = convert(v, cols[i].typ); err != nil {
return
}
}
return
}
//NTYPE
func infer(from []interface{}, to *[]*col) {
if len(*to) == 0 {
*to = make([]*col, len(from))
for i := range *to {
(*to)[i] = &col{}
}
}
for i, c := range *to {
if f := from[i]; f != nil {
switch x := f.(type) {
//case nil:
case idealComplex:
c.typ = qComplex128
from[i] = complex128(x)
case idealFloat:
c.typ = qFloat64
from[i] = float64(x)
case idealInt:
c.typ = qInt64
from[i] = int64(x)
case idealRune:
c.typ = qInt32
from[i] = int32(x)
case idealUint:
c.typ = qUint64
from[i] = uint64(x)
case bool:
c.typ = qBool
case complex128:
c.typ = qComplex128
case complex64:
c.typ = qComplex64
case float64:
c.typ = qFloat64
case float32:
c.typ = qFloat32
case int8:
c.typ = qInt8
case int16:
c.typ = qInt16
case int32:
c.typ = qInt32
case int64:
c.typ = qInt64
case string:
c.typ = qString
case uint8:
c.typ = qUint8
case uint16:
c.typ = qUint16
case uint32:
c.typ = qUint32
case uint64:
c.typ = qUint64
case []byte:
c.typ = qBlob
case *big.Int:
c.typ = qBigInt
case *big.Rat:
c.typ = qBigRat
case time.Time:
c.typ = qTime
case time.Duration:
c.typ = qDuration
case chunk:
vals, err := lldb.DecodeScalars(x.b)
if err != nil {
panic(err)
}
if len(vals) == 0 {
panic("internal error 040")
}
i, ok := vals[0].(int64)
if !ok {
panic("internal error 041")
}
c.typ = int(i)
case map[string]interface{}: // map of ids of a cross join
default:
panic("internal error 042")
}
}
}
}
type fileTemp struct {
*file
colsK []*col
colsV []*col
t *lldb.BTree
}
func (t *fileTemp) BeginTransaction() error { return nil }
func (t *fileTemp) Get(k []interface{}) (v []interface{}, err error) {
if err = expand(k); err != nil {
return
}
if err = t.flatten(k); err != nil {
return nil, err
}
bk, err := lldb.EncodeScalars(k...)
if err != nil {
return
}
bv, err := t.t.Get(nil, bk)
if err != nil {
return
}
return lldb.DecodeScalars(bv)
}
func (t *fileTemp) Drop() (err error) {
if t.f0 == nil {
return
}
fn := t.f0.Name()
if err = t.f0.Close(); err != nil {
return
}
if fn == "" {
return
}
return os.Remove(fn)
}
func (t *fileTemp) SeekFirst() (it btreeIterator, err error) {
en, err := t.t.SeekFirst()
if err != nil {
return
}
return &fileBTreeIterator{t: t, en: en}, nil
}
func (t *fileTemp) Set(k, v []interface{}) (err error) {
if err = expand(k); err != nil {
return
}
if err = expand(v); err != nil {
return
}
infer(k, &t.colsK)
infer(v, &t.colsV)
if err = t.flatten(k); err != nil {
return
}
bk, err := lldb.EncodeScalars(k...)
if err != nil {
return
}
if err = t.flatten(v); err != nil {
return
}
bv, err := lldb.EncodeScalars(v...)
if err != nil {
return
}
return t.t.Set(bk, bv)
}
type file struct {
a *lldb.Allocator
codec *gobCoder
f lldb.Filer
f0 lldb.OSFile
id int64
lck io.Closer
mu sync.Mutex
name string
tempFile func(dir, prefix string) (f lldb.OSFile, err error)
wal *os.File
removeEmptyWAL bool // Whether empty WAL files should be removed on close
}
func newFileFromOSFile(f lldb.OSFile, headroom int64, new bool) (fi *file, err error) {
nm := lockName(f.Name())
lck, err := lock.Lock(nm)
if err != nil {
if lck != nil {
lck.Close()
}
return nil, err
}
close := true
defer func() {
if close && lck != nil {
lck.Close()
}
}()
var w *os.File
closew := false
wn := walName(f.Name())
w, err = os.OpenFile(wn, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666)
closew = true
defer func() {
if w != nil && closew {
nm := w.Name()
w.Close()
os.Remove(nm)
w = nil
}
}()
if err != nil {
if !os.IsExist(err) {
return nil, err
}
closew = false
w, err = os.OpenFile(wn, os.O_RDWR, 0666)
if err != nil {
return nil, err
}
closew = true
st, err := w.Stat()
if err != nil {
return nil, err
}
closew = st.Size() == 0
}
switch {
case new:
b := make([]byte, 16)
copy(b, []byte(magic))
if _, err := f.Write(b); err != nil {
return nil, err
}
filer := lldb.Filer(lldb.NewOSFiler(f))
filer = lldb.NewInnerFiler(filer, 16)
if filer, err = lldb.NewACIDFiler(filer, w, lldb.MinWAL(headroom)); err != nil {
return nil, err
}
a, err := lldb.NewAllocator(filer, &lldb.Options{})
if err != nil {
return nil, err
}
a.Compress = true
s := &file{
a: a,
codec: newGobCoder(),
f0: f,
f: filer,
lck: lck,
name: f.Name(),
wal: w,
}
if err = s.BeginTransaction(); err != nil {
return nil, err
}
h, err := s.Create()
if err != nil {
return nil, err
}
if h != 1 { // root
panic("internal error 043")
}
if h, err = s.a.Alloc(make([]byte, 8)); err != nil {
return nil, err
}
if h != 2 { // id
panic("internal error 044")
}
close, closew = false, false
return s, s.Commit()
default:
filer := lldb.Filer(lldb.NewOSFiler(f))
filer = lldb.NewInnerFiler(filer, 16)
if filer, err = lldb.NewACIDFiler(filer, w, lldb.MinWAL(headroom)); err != nil {
return nil, err
}
a, err := lldb.NewAllocator(filer, &lldb.Options{})
if err != nil {
return nil, err
}
bid, err := a.Get(nil, 2) // id
if err != nil {
return nil, err
}
if len(bid) != 8 {
return nil, fmt.Errorf("(file-003) corrupted DB: id |% x|", bid)
}
id := int64(0)
for _, v := range bid {
id = (id << 8) | int64(v)
}
a.Compress = true
s := &file{
a: a,
codec: newGobCoder(),
f0: f,
f: filer,
id: id,
lck: lck,
name: f.Name(),
wal: w,
}
close, closew = false, false
return s, nil
}
}
func (s *file) OpenIndex(unique bool, handle int64) (btreeIndex, error) {
t, err := lldb.OpenBTree(s.a, s.collate, handle)
if err != nil {
return nil, err
}
return &fileIndex{s, handle, t, unique, newGobCoder()}, nil
}
func (s *file) CreateIndex(unique bool) ( /* handle */ int64, btreeIndex, error) {
t, h, err := lldb.CreateBTree(s.a, s.collate)
if err != nil {
return -1, nil, err
}
return h, &fileIndex{s, h, t, unique, newGobCoder()}, nil
}
func (s *file) Acid() bool { return s.wal != nil }
func errSet(p *error, errs ...error) (err error) {
err = *p
for _, e := range errs {
if err != nil {
return
}
*p, err = e, e
}
return
}
func (s *file) lock() func() {
s.mu.Lock()
return s.mu.Unlock
}
func (s *file) Close() (err error) {
defer s.lock()()
es := s.f0.Sync()
ef := s.f0.Close()
var ew, estat, eremove error
if s.wal != nil {
remove := false
wn := s.wal.Name()
if s.removeEmptyWAL {
var stat os.FileInfo
stat, estat = s.wal.Stat()
remove = stat.Size() == 0
}
ew = s.wal.Close()
if remove {
eremove = os.Remove(wn)
}
}
el := s.lck.Close()
return errSet(&err, es, ef, ew, el, estat, eremove)
}
func (s *file) Name() string { return s.name }
func (s *file) Verify() (allocs int64, err error) {
defer s.lock()()
var stat lldb.AllocStats
if err = s.a.Verify(lldb.NewMemFiler(), nil, &stat); err != nil {
return
}
allocs = stat.AllocAtoms
return
}
func (s *file) expandBytes(d []interface{}) (err error) {
for i, v := range d {
b, ok := v.([]byte)
if !ok {
continue
}
d[i], err = s.loadChunks(b)
if err != nil {
return
}
}
return
}
func (s *file) collate(a, b []byte) int { //TODO w/ error return
da, err := lldb.DecodeScalars(a)
if err != nil {
panic(err)
}
if err = s.expandBytes(da); err != nil {
panic(err)
}
db, err := lldb.DecodeScalars(b)
if err != nil {
panic(err)
}
if err = s.expandBytes(db); err != nil {
panic(err)
}
//dbg("da: %v, db: %v", da, db)
return collate(da, db)
}
func (s *file) CreateTemp(asc bool) (bt temp, err error) {
f, err := s.tempFile("", "ql-tmp-")
if err != nil {
return nil, err
}
fn := f.Name()
filer := lldb.NewOSFiler(f)
a, err := lldb.NewAllocator(filer, &lldb.Options{})
if err != nil {
f.Close()
os.Remove(fn)
return nil, err
}
k := 1
if !asc {
k = -1
}
t, _, err := lldb.CreateBTree(a, func(a, b []byte) int { //TODO w/ error return
return k * s.collate(a, b)
})
if err != nil {
f.Close()
if fn != "" {
os.Remove(fn)
}
return nil, err
}
x := &fileTemp{file: &file{
a: a,
codec: newGobCoder(),
f0: f,
},
t: t}
return x, nil
}
func (s *file) BeginTransaction() (err error) {
defer s.lock()()
return s.f.BeginUpdate()
}
func (s *file) Rollback() (err error) {
defer s.lock()()
return s.f.Rollback()
}
func (s *file) Commit() (err error) {
defer s.lock()()
return s.f.EndUpdate()
}
func (s *file) Create(data ...interface{}) (h int64, err error) {
if err = expand(data); err != nil {
return
}
if err = s.flatten(data); err != nil {
return
}
b, err := lldb.EncodeScalars(data...)
if err != nil {
return
}
defer s.lock()()
return s.a.Alloc(b)
}
func (s *file) Delete(h int64, blobCols ...*col) (err error) {
switch len(blobCols) {
case 0:
defer s.lock()()
return s.a.Free(h)
default:
return s.free(h, blobCols)
}
}
func (s *file) ResetID() (err error) {
s.id = 0
return
}
func (s *file) ID() (int64, error) {
defer s.lock()()
s.id++
b := make([]byte, 8)
id := s.id
for i := 7; i >= 0; i-- {
b[i] = byte(id)
id >>= 8
}
return s.id, s.a.Realloc(2, b)
}
func (s *file) free(h int64, blobCols []*col) (err error) {
b, err := s.a.Get(nil, h) //LATER +bufs
if err != nil {
return
}
rec, err := lldb.DecodeScalars(b)
if err != nil {
return
}
for _, col := range blobCols {
if col.index >= len(rec) {
return fmt.Errorf("(file-004) file.free: corrupted DB (record len)")
}
if col.index+2 >= len(rec) {
continue
}
switch x := rec[col.index+2].(type) {
case nil:
// nop
case []byte:
if err = s.freeChunks(x); err != nil {
return
}
}
}
defer s.lock()()
return s.a.Free(h)
}
func (s *file) Read(dst []interface{}, h int64, cols ...*col) (data []interface{}, err error) { //NTYPE
b, err := s.a.Get(nil, h) //LATER +bufs
if err != nil {
return
}
rec, err := lldb.DecodeScalars(b)
if err != nil {
return
}
for _, col := range cols {
i := col.index + 2
if i >= len(rec) || rec[i] == nil {
continue
}
switch col.typ {
case 0:
case qBool:
case qComplex64:
rec[i] = complex64(rec[i].(complex128))
case qComplex128:
case qFloat32:
rec[i] = float32(rec[i].(float64))
case qFloat64:
case qInt8:
rec[i] = int8(rec[i].(int64))
case qInt16:
rec[i] = int16(rec[i].(int64))
case qInt32:
rec[i] = int32(rec[i].(int64))
case qInt64:
case qString:
case qUint8:
rec[i] = uint8(rec[i].(uint64))
case qUint16:
rec[i] = uint16(rec[i].(uint64))
case qUint32:
rec[i] = uint32(rec[i].(uint64))
case qUint64:
case qBlob, qBigInt, qBigRat, qTime, qDuration:
switch x := rec[i].(type) {
case []byte:
rec[i] = chunk{f: s, b: x}
default:
return nil, fmt.Errorf("(file-006) corrupted DB: non nil chunk type is not []byte")
}
default:
panic("internal error 045")
}
}
if cols != nil {
for n, dn := len(cols)+2, len(rec); dn < n; dn++ {
rec = append(rec, nil)
}
}
return rec, nil
}
func (s *file) freeChunks(enc []byte) (err error) {
items, err := lldb.DecodeScalars(enc)
if err != nil {
return
}
var ok bool
var next int64
switch len(items) {
case 2:
return
case 3:
if next, ok = items[1].(int64); !ok || next == 0 {
return fmt.Errorf("(file-007) corrupted DB: first chunk link")
}
default:
return fmt.Errorf("(file-008) corrupted DB: first chunk")
}
for next != 0 {
b, err := s.a.Get(nil, next)
if err != nil {
return err
}
if items, err = lldb.DecodeScalars(b); err != nil {
return err
}
var h int64
switch len(items) {
case 1:
// nop
case 2:
if h, ok = items[0].(int64); !ok {
return fmt.Errorf("(file-009) corrupted DB: chunk link")
}
default:
return fmt.Errorf("(file-010) corrupted DB: chunk items %d (%v)", len(items), items)
}
s.mu.Lock()
if err = s.a.Free(next); err != nil {
s.mu.Unlock()
return err
}
s.mu.Unlock()
next = h
}
return
}
func (s *file) loadChunks(enc []byte) (v interface{}, err error) {
items, err := lldb.DecodeScalars(enc)
if err != nil {
return
}
var ok bool
var next int64
switch len(items) {
case 2:
// nop
case 3:
if next, ok = items[1].(int64); !ok || next == 0 {
return nil, fmt.Errorf("(file-011) corrupted DB: first chunk link")
}
default:
//fmt.Printf("%d: %#v\n", len(items), items)
return nil, fmt.Errorf("(file-012) corrupted DB: first chunk")
}
typ, ok := items[0].(int64)
if !ok {
return nil, fmt.Errorf("(file-013) corrupted DB: first chunk tag")
}
buf, ok := items[len(items)-1].([]byte)
if !ok {
return nil, fmt.Errorf("(file-014) corrupted DB: first chunk data")
}
for next != 0 {
b, err := s.a.Get(nil, next)
if err != nil {
return nil, err
}
if items, err = lldb.DecodeScalars(b); err != nil {
return nil, err
}
switch len(items) {
case 1:
next = 0
case 2: