github.com/ipld/go-ipld-prime@v0.21.0/linking/setup.go (about)

     1  package linking
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/ipld/go-ipld-prime/datamodel"
     7  	"github.com/ipld/go-ipld-prime/storage"
     8  )
     9  
    10  // SetReadStorage configures how the LinkSystem will look for information to load,
    11  // setting it to look at the given storage.ReadableStorage.
    12  //
    13  // This will overwrite the LinkSystem.StorageReadOpener field.
    14  //
    15  // This mechanism only supports setting exactly one ReadableStorage.
    16  // If you would like to make a more complex configuration
    17  // (for example, perhaps using information from a LinkContext to decide which storage area to use?)
    18  // then you should set LinkSystem.StorageReadOpener to a custom callback of your own creation instead.
    19  func (lsys *LinkSystem) SetReadStorage(store storage.ReadableStorage) {
    20  	lsys.StorageReadOpener = func(lctx LinkContext, lnk datamodel.Link) (io.Reader, error) {
    21  		return storage.GetStream(lctx.Ctx, store, lnk.Binary())
    22  	}
    23  }
    24  
    25  // SetWriteStorage configures how the LinkSystem will store information,
    26  // setting it to write into the given storage.WritableStorage.
    27  //
    28  // This will overwrite the LinkSystem.StorageWriteOpener field.
    29  //
    30  // This mechanism only supports setting exactly one WritableStorage.
    31  // If you would like to make a more complex configuration
    32  // (for example, perhaps using information from a LinkContext to decide which storage area to use?)
    33  // then you should set LinkSystem.StorageWriteOpener to a custom callback of your own creation instead.
    34  func (lsys *LinkSystem) SetWriteStorage(store storage.WritableStorage) {
    35  	lsys.StorageWriteOpener = func(lctx LinkContext) (io.Writer, BlockWriteCommitter, error) {
    36  		wr, wrcommit, err := storage.PutStream(lctx.Ctx, store)
    37  		return wr, func(lnk datamodel.Link) error {
    38  			return wrcommit(lnk.Binary())
    39  		}, err
    40  	}
    41  }