github.com/pachyderm/pachyderm@v1.13.4/src/server/pkg/storage/fileset/reader.go (about)

     1  package fileset
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  
     7  	"github.com/gogo/protobuf/proto"
     8  	"github.com/pachyderm/pachyderm/src/server/pkg/storage/chunk"
     9  	"github.com/pachyderm/pachyderm/src/server/pkg/storage/fileset/index"
    10  )
    11  
    12  // Reader is an abstraction for reading a fileset.
    13  type Reader struct {
    14  	store     Store
    15  	chunks    *chunk.Storage
    16  	path      string
    17  	indexOpts []index.Option
    18  }
    19  
    20  func newReader(store Store, chunks *chunk.Storage, p string, opts ...index.Option) *Reader {
    21  	r := &Reader{
    22  		store:     store,
    23  		chunks:    chunks,
    24  		path:      p,
    25  		indexOpts: opts,
    26  	}
    27  	return r
    28  }
    29  
    30  // Iterate iterates over the files in the file set.
    31  func (r *Reader) Iterate(ctx context.Context, cb func(File) error, deletive ...bool) error {
    32  	md, err := r.store.Get(ctx, r.path)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	if len(deletive) > 0 && deletive[0] {
    37  		ir := index.NewReader(r.chunks, md.Deletive, r.indexOpts...)
    38  		return ir.Iterate(ctx, func(idx *index.Index) error {
    39  			return cb(newFileReader(ctx, r.chunks, idx))
    40  		})
    41  	}
    42  	ir := index.NewReader(r.chunks, md.Additive, r.indexOpts...)
    43  	return ir.Iterate(ctx, func(idx *index.Index) error {
    44  		return cb(newFileReader(ctx, r.chunks, idx))
    45  	})
    46  }
    47  
    48  // FileReader is an abstraction for reading a file.
    49  type FileReader struct {
    50  	ctx    context.Context
    51  	chunks *chunk.Storage
    52  	idx    *index.Index
    53  }
    54  
    55  func newFileReader(ctx context.Context, chunks *chunk.Storage, idx *index.Index) *FileReader {
    56  	return &FileReader{
    57  		ctx:    ctx,
    58  		chunks: chunks,
    59  		idx:    proto.Clone(idx).(*index.Index),
    60  	}
    61  }
    62  
    63  // Index returns the index for the file.
    64  func (fr *FileReader) Index() *index.Index {
    65  	return proto.Clone(fr.idx).(*index.Index)
    66  }
    67  
    68  // Content writes the content of the file.
    69  func (fr *FileReader) Content(w io.Writer) error {
    70  	dataRefs := getDataRefs(fr.idx.File.Parts)
    71  	r := fr.chunks.NewReader(fr.ctx, dataRefs)
    72  	return r.Get(w)
    73  }
    74  
    75  type emptyReader struct{}
    76  
    77  func (*emptyReader) Iterate(_ context.Context, _ func(File) error, _ ...bool) error {
    78  	return nil
    79  }