github.com/filecoin-project/lassie@v0.23.0/pkg/internal/itest/linksystemutil/linksystemblockstore.go (about)

     1  package linksystemutil
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"errors"
     7  	"io"
     8  
     9  	"github.com/ipfs/boxo/blockstore"
    10  	blocks "github.com/ipfs/go-block-format"
    11  	"github.com/ipfs/go-cid"
    12  	"github.com/ipld/go-ipld-prime/linking"
    13  	cidlink "github.com/ipld/go-ipld-prime/linking/cid"
    14  )
    15  
    16  var _ blockstore.Blockstore = (*LinkSystemBlockstore)(nil)
    17  
    18  type LinkSystemBlockstore struct {
    19  	lsys linking.LinkSystem
    20  }
    21  
    22  func NewLinkSystemBlockstore(lsys linking.LinkSystem) *LinkSystemBlockstore {
    23  	return &LinkSystemBlockstore{lsys}
    24  }
    25  
    26  func (lsbs *LinkSystemBlockstore) DeleteBlock(ctx context.Context, c cid.Cid) error {
    27  	return errors.New("not supported")
    28  }
    29  
    30  func (lsbs *LinkSystemBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
    31  	_, err := lsbs.lsys.StorageReadOpener(linking.LinkContext{Ctx: ctx}, cidlink.Link{Cid: c})
    32  	if err != nil {
    33  		return false, err
    34  	}
    35  	return true, nil
    36  }
    37  
    38  func (lsbs *LinkSystemBlockstore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
    39  	rdr, err := lsbs.lsys.StorageReadOpener(linking.LinkContext{Ctx: ctx}, cidlink.Link{Cid: c})
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	var buf bytes.Buffer
    44  	_, err = io.Copy(&buf, rdr)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	return blocks.NewBlockWithCid(buf.Bytes(), c)
    49  }
    50  
    51  func (lsbs *LinkSystemBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
    52  	rdr, err := lsbs.lsys.StorageReadOpener(linking.LinkContext{Ctx: ctx}, cidlink.Link{Cid: c})
    53  	if err != nil {
    54  		return 0, err
    55  	}
    56  	i, err := io.Copy(io.Discard, rdr)
    57  	if err != nil {
    58  		return 0, err
    59  	}
    60  	return int(i), nil
    61  }
    62  
    63  func (lsbs *LinkSystemBlockstore) Put(ctx context.Context, blk blocks.Block) error {
    64  	w, wc, err := lsbs.lsys.StorageWriteOpener(linking.LinkContext{Ctx: ctx})
    65  	if err != nil {
    66  		return err
    67  	}
    68  	if _, err = io.Copy(w, bytes.NewReader(blk.RawData())); err != nil {
    69  		return err
    70  	}
    71  	return wc(cidlink.Link{Cid: blk.Cid()})
    72  }
    73  
    74  func (lsbs *LinkSystemBlockstore) PutMany(ctx context.Context, blks []blocks.Block) error {
    75  	for _, blk := range blks {
    76  		if err := lsbs.Put(ctx, blk); err != nil {
    77  			return err
    78  		}
    79  	}
    80  	return nil
    81  }
    82  
    83  func (lsbs *LinkSystemBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
    84  	return nil, errors.New("not supported")
    85  }
    86  
    87  func (lsbs *LinkSystemBlockstore) HashOnRead(enabled bool) {
    88  	lsbs.lsys.TrustedStorage = !enabled
    89  }