github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/storage/combo/storage.go (about)

     1  package combo
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/filecoin-project/bacalhau/pkg/model"
     8  	"github.com/filecoin-project/bacalhau/pkg/storage"
     9  	"github.com/filecoin-project/bacalhau/pkg/system"
    10  )
    11  
    12  type AllProviderFetcher func(ctx context.Context) ([]storage.Storage, error)
    13  type ReadProviderFetcher func(ctx context.Context, spec model.StorageSpec) (storage.Storage, error)
    14  type WriteProviderFetcher func(ctx context.Context) (storage.Storage, error)
    15  
    16  type ComboStorageProvider struct {
    17  	AllFetcher   AllProviderFetcher
    18  	ReadFetcher  ReadProviderFetcher
    19  	WriteFetcher WriteProviderFetcher
    20  }
    21  
    22  func NewStorage(
    23  	_ *system.CleanupManager,
    24  	allFetcher AllProviderFetcher,
    25  	readFetcher ReadProviderFetcher,
    26  	writeFetcher WriteProviderFetcher,
    27  ) (*ComboStorageProvider, error) {
    28  	storageHandler := &ComboStorageProvider{
    29  		AllFetcher:   allFetcher,
    30  		ReadFetcher:  readFetcher,
    31  		WriteFetcher: writeFetcher,
    32  	}
    33  	return storageHandler, nil
    34  }
    35  
    36  func (driver *ComboStorageProvider) IsInstalled(ctx context.Context) (bool, error) {
    37  	allProviders, err := driver.AllFetcher(ctx)
    38  	if err != nil {
    39  		return false, err
    40  	}
    41  	for _, provider := range allProviders {
    42  		installed, err := provider.IsInstalled(ctx)
    43  		if err != nil {
    44  			return false, err
    45  		}
    46  		if !installed {
    47  			return false, nil
    48  		}
    49  	}
    50  	return true, nil
    51  }
    52  
    53  func (driver *ComboStorageProvider) HasStorageLocally(ctx context.Context, storageSpec model.StorageSpec) (bool, error) {
    54  	provider, err := driver.getReadProvider(ctx, storageSpec)
    55  	if err != nil {
    56  		return false, err
    57  	}
    58  	return provider.HasStorageLocally(ctx, storageSpec)
    59  }
    60  
    61  func (driver *ComboStorageProvider) GetVolumeSize(ctx context.Context, storageSpec model.StorageSpec) (uint64, error) {
    62  	provider, err := driver.getReadProvider(ctx, storageSpec)
    63  	if err != nil {
    64  		return 0, err
    65  	}
    66  	return provider.GetVolumeSize(ctx, storageSpec)
    67  }
    68  
    69  func (driver *ComboStorageProvider) PrepareStorage(
    70  	ctx context.Context,
    71  	storageSpec model.StorageSpec,
    72  ) (storage.StorageVolume, error) {
    73  	provider, err := driver.getReadProvider(ctx, storageSpec)
    74  	if err != nil {
    75  		return storage.StorageVolume{}, err
    76  	}
    77  	return provider.PrepareStorage(ctx, storageSpec)
    78  }
    79  
    80  func (driver *ComboStorageProvider) CleanupStorage(
    81  	ctx context.Context,
    82  	storageSpec model.StorageSpec,
    83  	volume storage.StorageVolume,
    84  ) error {
    85  	provider, err := driver.getReadProvider(ctx, storageSpec)
    86  	if err != nil {
    87  		return err
    88  	}
    89  	return provider.CleanupStorage(ctx, storageSpec, volume)
    90  }
    91  
    92  func (driver *ComboStorageProvider) Upload(
    93  	ctx context.Context,
    94  	localPath string,
    95  ) (model.StorageSpec, error) {
    96  	provider, err := driver.getWriteProvider(ctx)
    97  	if err != nil {
    98  		return model.StorageSpec{}, err
    99  	}
   100  	return provider.Upload(ctx, localPath)
   101  }
   102  
   103  func (driver *ComboStorageProvider) Explode(ctx context.Context, storageSpec model.StorageSpec) ([]model.StorageSpec, error) {
   104  	provider, err := driver.getReadProvider(ctx, storageSpec)
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  	if provider == nil {
   109  		return nil, fmt.Errorf("no storage provider found for %s", storageSpec.CID)
   110  	}
   111  	return provider.Explode(ctx, storageSpec)
   112  }
   113  
   114  func (driver *ComboStorageProvider) getReadProvider(ctx context.Context, spec model.StorageSpec) (storage.Storage, error) {
   115  	return driver.ReadFetcher(ctx, spec)
   116  }
   117  
   118  func (driver *ComboStorageProvider) getWriteProvider(ctx context.Context) (storage.Storage, error) {
   119  	return driver.WriteFetcher(ctx)
   120  }
   121  
   122  // Compile time interface check:
   123  var _ storage.Storage = (*ComboStorageProvider)(nil)