···5151}
52525353type blockRef struct {
5454- gorm.Model
5454+ ID uint `gorm:"primarykey"`
5555 Cid string `gorm:"index"`
5656 Shard uint
5757 Offset int64
···7272}
73737474func (uv *userView) Has(ctx context.Context, k cid.Cid) (bool, error) {
7575- // TODO: for now, im using a join to ensure we only query blocks from the
7676- // correct user. maybe it makes sense to put the user in the blockRef
7777- // directly? tradeoff of time vs space
7875 var count int64
7976 if err := uv.cs.meta.
8077 Model(blockRef{}).
···226223 //if err != gorm.ErrRecordNotFound {
227224 return nil, err
228225 //}
226226+ }
227227+228228+ if lastShard.Root != "" && lastShard.Root != prev.String() {
229229+ return nil, fmt.Errorf("attempted a delta session on top of the wrong previous head")
229230 }
230231231232 return &DeltaSession{
···411412 User: ds.user,
412413 }
413414415415+ // TODO: there should be a way to create the shard and block_refs that
416416+ // reference it in the same query, would save a lot of time
414417 if err := ds.cs.meta.Create(&shard).Error; err != nil {
415418 return err
416419 }
417420418421 var offset int64 = hnw
419419- brefs := make([]*blockRef, 0, len(ds.blks))
422422+ //brefs := make([]*blockRef, 0, len(ds.blks))
423423+ brefs := make([]map[string]interface{}, 0, len(ds.blks))
420424 for k, blk := range ds.blks {
421425 nw, err := LdWrite(fi, k.Bytes(), blk.RawData())
422426 if err != nil {
423427 return err
424428 }
425429426426- brefs = append(brefs, &blockRef{
427427- Cid: k.String(),
428428- Offset: offset,
429429- Shard: shard.ID,
430430+ /*
431431+ brefs = append(brefs, &blockRef{
432432+ Cid: k.String(),
433433+ Offset: offset,
434434+ Shard: shard.ID,
435435+ })
436436+ */
437437+ // adding things to the db by map is the only way to get gorm to not
438438+ // add the 'returning' clause, which costs a lot of time
439439+ brefs = append(brefs, map[string]interface{}{
440440+ "cid": k.String(),
441441+ "offset": offset,
442442+ "shard": shard.ID,
430443 })
431444432445 offset += nw
433446 }
434447435435- if err := ds.cs.meta.Create(brefs).Error; err != nil {
448448+ if err := ds.cs.meta.Table("block_refs").Create(brefs).Error; err != nil {
436449 return err
437450 }
438451