forked from bluesky-social/indigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbs.go
714 lines (587 loc) · 16 KB
/
bs.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
package carstore
import (
"bufio"
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"time"
util "github.com/bluesky-social/indigo/util"
blockformat "github.com/ipfs/go-block-format"
carutil "github.com/ipfs/go-car/util"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
cbor "github.com/ipfs/go-ipld-cbor"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-libipfs/blocks"
car "github.com/ipld/go-car"
"go.opentelemetry.io/otel"
"gorm.io/gorm"
)
const MaxSliceLength = 2 << 20
type CarStore struct {
meta *gorm.DB
rootDir string
lscLk sync.Mutex
lastShardCache map[util.Uid]*CarShard
}
func NewCarStore(meta *gorm.DB, root string) (*CarStore, error) {
if _, err := os.Stat(root); err != nil {
if !os.IsNotExist(err) {
return nil, err
}
if err := os.Mkdir(root, 0775); err != nil {
return nil, err
}
}
if err := meta.AutoMigrate(&CarShard{}, &blockRef{}); err != nil {
return nil, err
}
return &CarStore{
meta: meta,
rootDir: root,
lastShardCache: make(map[util.Uid]*CarShard),
}, nil
}
type UserInfo struct {
gorm.Model
Head string
}
type CarShard struct {
gorm.Model
Root util.DbCID
DataStart int64
Seq int `gorm:"index"`
Path string
Usr util.Uid `gorm:"index"`
}
type blockRef struct {
ID uint `gorm:"primarykey"`
Cid util.DbCID `gorm:"index"`
Shard uint
Offset int64
//User uint `gorm:"index"`
}
type userView struct {
cs *CarStore
user util.Uid
cache map[cid.Cid]blockformat.Block
prefetch bool
}
var _ blockstore.Blockstore = (*userView)(nil)
func (uv *userView) HashOnRead(hor bool) {
//noop
}
func (uv *userView) Has(ctx context.Context, k cid.Cid) (bool, error) {
var count int64
if err := uv.cs.meta.
Model(blockRef{}).
Select("path, block_refs.offset").
Joins("left join car_shards on block_refs.shard = car_shards.id").
Where("usr = ? AND cid = ?", uv.user, util.DbCID{k}).
Count(&count).Error; err != nil {
return false, err
}
return count > 0, nil
}
func (uv *userView) Get(ctx context.Context, k cid.Cid) (blockformat.Block, error) {
if uv.cache != nil {
blk, ok := uv.cache[k]
if ok {
return blk, nil
}
}
// TODO: for now, im using a join to ensure we only query blocks from the
// correct user. maybe it makes sense to put the user in the blockRef
// directly? tradeoff of time vs space
var info struct {
Path string
Offset int64
}
if err := uv.cs.meta.
Model(blockRef{}).
Select("path, block_refs.offset").
Joins("left join car_shards on block_refs.shard = car_shards.id").
Where("usr = ? AND cid = ?", uv.user, util.DbCID{k}).
Find(&info).Error; err != nil {
return nil, err
}
if info.Path == "" {
return nil, ipld.ErrNotFound{k}
}
if uv.prefetch {
return uv.prefetchRead(ctx, k, info.Path, info.Offset)
} else {
return uv.singleRead(ctx, k, info.Path, info.Offset)
}
}
func (uv *userView) prefetchRead(ctx context.Context, k cid.Cid, path string, offset int64) (blockformat.Block, error) {
fi, err := os.Open(path)
if err != nil {
return nil, err
}
defer fi.Close()
cr, err := car.NewCarReader(fi)
if err != nil {
return nil, err
}
for {
blk, err := cr.Next()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
uv.cache[blk.Cid()] = blk
}
outblk, ok := uv.cache[k]
if !ok {
return nil, fmt.Errorf("requested block was not found in car slice")
}
return outblk, nil
}
func (uv *userView) singleRead(ctx context.Context, k cid.Cid, path string, offset int64) (blockformat.Block, error) {
fi, err := os.Open(path)
if err != nil {
return nil, err
}
defer fi.Close()
seeked, err := fi.Seek(offset, io.SeekStart)
if err != nil {
return nil, err
}
if seeked != offset {
return nil, fmt.Errorf("failed to seek to offset (%d != %d)", seeked, offset)
}
bufr := bufio.NewReader(fi)
rcid, data, err := carutil.ReadNode(bufr)
if err != nil {
return nil, err
}
if rcid != k {
return nil, fmt.Errorf("mismatch in cid on disk: %s != %s", rcid, k)
}
return blocks.NewBlockWithCid(data, rcid)
}
func (uv *userView) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return nil, fmt.Errorf("not implemented")
}
func (uv *userView) Put(ctx context.Context, blk blockformat.Block) error {
return fmt.Errorf("puts not supported to car view blockstores")
}
func (uv *userView) PutMany(ctx context.Context, blks []blockformat.Block) error {
return fmt.Errorf("puts not supported to car view blockstores")
}
func (uv *userView) DeleteBlock(ctx context.Context, k cid.Cid) error {
return fmt.Errorf("deletes not supported to car view blockstore")
}
func (uv *userView) GetSize(ctx context.Context, k cid.Cid) (int, error) {
// TODO: maybe block size should be in the database record...
blk, err := uv.Get(ctx, k)
if err != nil {
return 0, err
}
return len(blk.RawData()), nil
}
type DeltaSession struct {
fresh blockstore.Blockstore
blks map[cid.Cid]blockformat.Block
base blockstore.Blockstore
user util.Uid
seq int
readonly bool
cs *CarStore
}
func (cs *CarStore) checkLastShardCache(user util.Uid) *CarShard {
cs.lscLk.Lock()
defer cs.lscLk.Unlock()
ls, ok := cs.lastShardCache[user]
if ok {
return ls
}
return nil
}
func (cs *CarStore) putLastShardCache(user util.Uid, ls *CarShard) {
cs.lscLk.Lock()
defer cs.lscLk.Unlock()
cs.lastShardCache[user] = ls
}
func (cs *CarStore) getLastShard(ctx context.Context, user util.Uid) (*CarShard, error) {
maybeLs := cs.checkLastShardCache(user)
if maybeLs != nil {
return maybeLs, nil
}
var lastShard CarShard
if err := cs.meta.WithContext(ctx).Model(CarShard{}).Limit(1).Order("id desc").Find(&lastShard, "usr = ?", user).Error; err != nil {
//if err := cs.meta.Model(CarShard{}).Where("user = ?", user).Last(&lastShard).Error; err != nil {
//if err != gorm.ErrRecordNotFound {
return nil, err
//}
}
cs.putLastShardCache(user, &lastShard)
return &lastShard, nil
}
var ErrRepoBaseMismatch = fmt.Errorf("attempted a delta session on top of the wrong previous head")
var ErrRepoFork = fmt.Errorf("repo fork detected")
func (cs *CarStore) NewDeltaSession(ctx context.Context, user util.Uid, prev *cid.Cid) (*DeltaSession, error) {
ctx, span := otel.Tracer("carstore").Start(ctx, "NewSession")
defer span.End()
// TODO: ensure that we don't write updates on top of the wrong head
// this needs to be a compare and swap type operation
lastShard, err := cs.getLastShard(ctx, user)
if err != nil {
return nil, err
}
if prev != nil {
if lastShard.Root.CID != *prev {
fork, err := cs.checkFork(ctx, user, *prev)
if err != nil {
return nil, fmt.Errorf("failed to check carstore base mismatch for fork condition: %w", err)
}
if fork {
return nil, fmt.Errorf("fork at %s: %w", prev.String(), ErrRepoFork)
}
return nil, fmt.Errorf("mismatch: %s != %s: %w", lastShard.Root.CID, prev.String(), ErrRepoBaseMismatch)
}
}
return &DeltaSession{
fresh: blockstore.NewBlockstore(datastore.NewMapDatastore()),
blks: make(map[cid.Cid]blockformat.Block),
base: &userView{
user: user,
cs: cs,
prefetch: true,
cache: make(map[cid.Cid]blockformat.Block),
},
user: user,
cs: cs,
seq: lastShard.Seq + 1,
}, nil
}
func (cs *CarStore) ReadOnlySession(user util.Uid) (*DeltaSession, error) {
return &DeltaSession{
base: &userView{
user: user,
cs: cs,
prefetch: false,
cache: make(map[cid.Cid]blockformat.Block),
},
readonly: true,
user: user,
cs: cs,
}, nil
}
func (cs *CarStore) ReadUserCar(ctx context.Context, user util.Uid, earlyCid, lateCid cid.Cid, incremental bool, w io.Writer) error {
ctx, span := otel.Tracer("carstore").Start(ctx, "ReadUserCar")
defer span.End()
var lateSeq, earlySeq int
if earlyCid.Defined() {
var untilShard CarShard
if err := cs.meta.First(&untilShard, "root = ? AND usr = ?", util.DbCID{earlyCid}, user).Error; err != nil {
return err
}
earlySeq = untilShard.Seq
}
if lateCid.Defined() {
var fromShard CarShard
if err := cs.meta.First(&fromShard, "root = ? AND usr = ?", util.DbCID{lateCid}, user).Error; err != nil {
return err
}
lateSeq = fromShard.Seq
}
q := cs.meta.Order("seq desc").Where("usr = ? AND seq > ?", user, earlySeq)
if lateCid.Defined() {
q = q.Where("seq <= ?", lateSeq)
}
var shards []CarShard
if err := q.Find(&shards).Error; err != nil {
return err
}
if !incremental && earlyCid.Defined() {
// have to do it the ugly way
return fmt.Errorf("nyi")
}
if len(shards) == 0 {
return fmt.Errorf("no data found for user %d", user)
}
// fast path!
if err := car.WriteHeader(&car.CarHeader{
Roots: []cid.Cid{shards[0].Root.CID},
Version: 1,
}, w); err != nil {
return err
}
for _, sh := range shards {
if err := cs.writeShardBlocks(ctx, &sh, w); err != nil {
return err
}
}
return nil
}
func (cs *CarStore) writeShardBlocks(ctx context.Context, sh *CarShard, w io.Writer) error {
ctx, span := otel.Tracer("carstore").Start(ctx, "writeShardBlocks")
defer span.End()
fi, err := os.Open(sh.Path)
if err != nil {
return err
}
defer fi.Close()
_, err = fi.Seek(sh.DataStart, io.SeekStart)
if err != nil {
return err
}
_, err = io.Copy(w, fi)
if err != nil {
return err
}
return nil
}
var _ blockstore.Blockstore = (*DeltaSession)(nil)
func (ds *DeltaSession) Put(ctx context.Context, b blockformat.Block) error {
if ds.readonly {
return fmt.Errorf("cannot write to readonly deltaSession")
}
ds.blks[b.Cid()] = b
return nil
}
func (ds *DeltaSession) PutMany(ctx context.Context, bs []blockformat.Block) error {
if ds.readonly {
return fmt.Errorf("cannot write to readonly deltaSession")
}
for _, b := range bs {
ds.blks[b.Cid()] = b
}
return nil
}
func (ds *DeltaSession) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return nil, fmt.Errorf("AllKeysChan not implemented")
}
func (ds *DeltaSession) DeleteBlock(ctx context.Context, c cid.Cid) error {
if ds.readonly {
return fmt.Errorf("cannot write to readonly deltaSession")
}
if _, ok := ds.blks[c]; !ok {
return ipld.ErrNotFound{c}
}
delete(ds.blks, c)
return nil
}
func (ds *DeltaSession) Get(ctx context.Context, c cid.Cid) (blockformat.Block, error) {
b, ok := ds.blks[c]
if ok {
return b, nil
}
return ds.base.Get(ctx, c)
}
func (ds *DeltaSession) Has(ctx context.Context, c cid.Cid) (bool, error) {
_, ok := ds.blks[c]
if ok {
return true, nil
}
return ds.base.Has(ctx, c)
}
func (ds *DeltaSession) HashOnRead(hor bool) {
// noop?
}
func (ds *DeltaSession) GetSize(ctx context.Context, c cid.Cid) (int, error) {
b, ok := ds.blks[c]
if ok {
return len(b.RawData()), nil
}
return ds.base.GetSize(ctx, c)
}
func (cs *CarStore) openNewShardFile(ctx context.Context, user util.Uid, seq int) (*os.File, string, error) {
// TODO: some overwrite protections
fname := filepath.Join(cs.rootDir, fmt.Sprintf("sh-%d-%d", user, seq))
fi, err := os.Create(fname)
if err != nil {
return nil, "", err
}
return fi, fname, nil
}
func (cs *CarStore) writeNewShardFile(ctx context.Context, user util.Uid, seq int, data []byte) (string, error) {
// TODO: some overwrite protections
fname := filepath.Join(cs.rootDir, fmt.Sprintf("sh-%d-%d", user, seq))
if err := os.WriteFile(fname, data, 0664); err != nil {
return "", err
}
return fname, nil
}
// CloseWithRoot writes all new blocks in a car file to the writer with the
// given cid as the 'root'
func (ds *DeltaSession) CloseWithRoot(ctx context.Context, root cid.Cid) ([]byte, error) {
ctx, span := otel.Tracer("carstore").Start(ctx, "CloseWithRoot")
defer span.End()
if ds.readonly {
return nil, fmt.Errorf("cannot write to readonly deltaSession")
}
buf := new(bytes.Buffer)
h := &car.CarHeader{
Roots: []cid.Cid{root},
Version: 1,
}
hb, err := cbor.DumpObject(h)
if err != nil {
return nil, err
}
hnw, err := LdWrite(buf, hb)
if err != nil {
return nil, err
}
// TODO: writing these blocks in map traversal order is bad, I believe the
// optimal ordering will be something like reverse-write-order, but random
// is definitely not it
var offset int64 = hnw
//brefs := make([]*blockRef, 0, len(ds.blks))
brefs := make([]map[string]interface{}, 0, len(ds.blks))
for k, blk := range ds.blks {
nw, err := LdWrite(buf, k.Bytes(), blk.RawData())
if err != nil {
return nil, err
}
/*
brefs = append(brefs, &blockRef{
Cid: k.String(),
Offset: offset,
Shard: shard.ID,
})
*/
// adding things to the db by map is the only way to get gorm to not
// add the 'returning' clause, which costs a lot of time
brefs = append(brefs, map[string]interface{}{
"cid": util.DbCID{k},
"offset": offset,
})
offset += nw
}
path, err := ds.cs.writeNewShardFile(ctx, ds.user, ds.seq, buf.Bytes())
if err != nil {
return nil, err
}
// TODO: all this database work needs to be in a single transaction
shard := CarShard{
Root: util.DbCID{root},
DataStart: hnw,
Seq: ds.seq,
Path: path,
Usr: ds.user,
}
// TODO: there should be a way to create the shard and block_refs that
// reference it in the same query, would save a lot of time
if err := ds.cs.meta.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Create(&shard).Error; err != nil {
return err
}
ds.cs.putLastShardCache(ds.user, &shard)
for _, ref := range brefs {
ref["shard"] = shard.ID
}
if err := tx.Table("block_refs").CreateInBatches(brefs, 100).Error; err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func LdWrite(w io.Writer, d ...[]byte) (int64, error) {
var sum uint64
for _, s := range d {
sum += uint64(len(s))
}
buf := make([]byte, 8)
n := binary.PutUvarint(buf, sum)
nw, err := w.Write(buf[:n])
if err != nil {
return 0, err
}
for _, s := range d {
onw, err := w.Write(s)
if err != nil {
return int64(nw), err
}
nw += onw
}
return int64(nw), nil
}
func (cs *CarStore) ImportSlice(ctx context.Context, uid util.Uid, prev *cid.Cid, carslice []byte) (cid.Cid, *DeltaSession, error) {
carr, err := car.NewCarReader(bytes.NewReader(carslice))
if err != nil {
return cid.Undef, nil, err
}
if len(carr.Header.Roots) != 1 {
return cid.Undef, nil, fmt.Errorf("invalid car file, header must have a single root (has %d)", len(carr.Header.Roots))
}
ds, err := cs.NewDeltaSession(ctx, uid, prev)
if err != nil {
return cid.Undef, nil, err
}
for {
blk, err := carr.Next()
if err != nil {
if err == io.EOF {
break
}
return cid.Undef, nil, err
}
if err := ds.Put(ctx, blk); err != nil {
return cid.Undef, nil, err
}
}
return carr.Header.Roots[0], ds, nil
}
func (cs *CarStore) GetUserRepoHead(ctx context.Context, user util.Uid) (cid.Cid, error) {
lastShard, err := cs.getLastShard(ctx, user)
if err != nil {
return cid.Undef, err
}
if lastShard.ID == 0 {
return cid.Undef, nil
}
return lastShard.Root.CID, nil
}
type UserStat struct {
Seq int
Root string
Created time.Time
}
func (cs *CarStore) Stat(ctx context.Context, usr util.Uid) ([]UserStat, error) {
var shards []CarShard
if err := cs.meta.Order("seq asc").Find(&shards, "usr = ?", usr).Error; err != nil {
return nil, err
}
var out []UserStat
for _, s := range shards {
out = append(out, UserStat{
Seq: s.Seq,
Root: s.Root.CID.String(),
Created: s.CreatedAt,
})
}
return out, nil
}
func (cs *CarStore) checkFork(ctx context.Context, user util.Uid, prev cid.Cid) (bool, error) {
lastShard, err := cs.getLastShard(ctx, user)
if err != nil {
return false, err
}
var maybeShard CarShard
if err := cs.meta.WithContext(ctx).Model(CarShard{}).Find(&maybeShard, "usr = ? AND root = ?", user, &util.DbCID{prev}).Error; err != nil {
return false, err
}
if maybeShard.ID == lastShard.ID {
// somehow we are checking if a valid 'append' is a fork, seems buggy, throw an error
return false, fmt.Errorf("invariant broken: checked for forkiness of a valid append")
}
if maybeShard.ID == 0 {
return false, nil
}
return true, nil
}