Skip to content

Commit

Permalink
Proxy single Put/Delete to the Many variants (#12313)
Browse files Browse the repository at this point in the history
There are no perf gains to be had here anyway
  • Loading branch information
ribasushi authored Jul 27, 2024
1 parent f234267 commit 5fdc75c
Showing 1 changed file with 3 additions and 66 deletions.
69 changes: 3 additions & 66 deletions blockstore/badger/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,55 +732,7 @@ func (b *Blockstore) GetSize(ctx context.Context, cid cid.Cid) (int, error) {

// Put implements Blockstore.Put.
func (b *Blockstore) Put(ctx context.Context, block blocks.Block) error {
if err := b.access(); err != nil {
return err
}
defer b.viewers.Done()

b.lockDB()
defer b.unlockDB()

k, pooled := b.PooledStorageKey(block.Cid())
if pooled {
defer KeyPool.Put(k)
}

put := func(db *badger.DB) error {
// Check if we have it before writing it.
switch err := db.View(func(txn *badger.Txn) error {
_, err := txn.Get(k)
return err
}); err {
case badger.ErrKeyNotFound:
case nil:
// Already exists, skip the put.
return nil
default:
return err
}

// Then write it.
err := db.Update(func(txn *badger.Txn) error {
return txn.Set(k, block.RawData())
})
if err != nil {
return fmt.Errorf("failed to put block in badger blockstore: %w", err)
}

return nil
}

if err := put(b.db); err != nil {
return err
}

if b.dbNext != nil {
if err := put(b.dbNext); err != nil {
return err
}
}

return nil
return b.PutMany(ctx, []blocks.Block{block})
}

// PutMany implements Blockstore.PutMany.
Expand Down Expand Up @@ -869,23 +821,8 @@ func (b *Blockstore) PutMany(ctx context.Context, blocks []blocks.Block) error {
}

// DeleteBlock implements Blockstore.DeleteBlock.
func (b *Blockstore) DeleteBlock(ctx context.Context, cid cid.Cid) error {
if err := b.access(); err != nil {
return err
}
defer b.viewers.Done()

b.lockDB()
defer b.unlockDB()

k, pooled := b.PooledStorageKey(cid)
if pooled {
defer KeyPool.Put(k)
}

return b.db.Update(func(txn *badger.Txn) error {
return txn.Delete(k)
})
func (b *Blockstore) DeleteBlock(ctx context.Context, c cid.Cid) error {
return b.DeleteMany(ctx, []cid.Cid{c})
}

func (b *Blockstore) DeleteMany(ctx context.Context, cids []cid.Cid) error {
Expand Down

0 comments on commit 5fdc75c

Please sign in to comment.