github.com/ethersphere/bee/v2@v2.2.0/pkg/file/pipeline/store/store.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package store
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/file/pipeline"
    12  	storage "github.com/ethersphere/bee/v2/pkg/storage"
    13  	"github.com/ethersphere/bee/v2/pkg/swarm"
    14  )
    15  
    16  var errInvalidData = errors.New("store: invalid data")
    17  
    18  type storeWriter struct {
    19  	l    storage.Putter
    20  	ctx  context.Context
    21  	next pipeline.ChainWriter
    22  }
    23  
    24  // NewStoreWriter returns a storeWriter. It just writes the given data
    25  // to a given storage.Putter.
    26  func NewStoreWriter(ctx context.Context, l storage.Putter, next pipeline.ChainWriter) pipeline.ChainWriter {
    27  	return &storeWriter{ctx: ctx, l: l, next: next}
    28  }
    29  
    30  func (w *storeWriter) ChainWrite(p *pipeline.PipeWriteArgs) error {
    31  	if p.Ref == nil || p.Data == nil {
    32  		return errInvalidData
    33  	}
    34  	err := w.l.Put(w.ctx, swarm.NewChunk(swarm.NewAddress(p.Ref), p.Data))
    35  	if err != nil {
    36  		return err
    37  	}
    38  	if w.next == nil {
    39  		return nil
    40  	}
    41  
    42  	return w.next.ChainWrite(p)
    43  }
    44  
    45  func (w *storeWriter) Sum() ([]byte, error) {
    46  	return w.next.Sum()
    47  }