MCPcopy
hub / github.com/perkeep/perkeep / NewFileReader

Function NewFileReader

pkg/schema/filereader.go:70–94  ·  view source on GitHub ↗

NewFileReader returns a new FileReader reading the contents of fileBlobRef, fetching blobs from fetcher. The fileBlobRef must be of a "bytes" or "file" schema blob. The caller should call Close on the FileReader when done reading.

(ctx context.Context, fetcher blob.Fetcher, fileBlobRef blob.Ref)

Source from the content-addressed store, hash-verified

68//
69// The caller should call Close on the FileReader when done reading.
70func NewFileReader(ctx context.Context, fetcher blob.Fetcher, fileBlobRef blob.Ref) (*FileReader, error) {
71 // TODO(bradfitz): rename this into bytes reader? but for now it's still
72 // named FileReader, but can also read a "bytes" schema.
73 if !fileBlobRef.Valid() {
74 return nil, errors.New("schema/filereader: NewFileReader blobref invalid")
75 }
76 rc, _, err := fetcher.Fetch(ctx, fileBlobRef)
77 if err != nil {
78 return nil, fmt.Errorf("schema/filereader: fetching file schema blob: %w", err)
79 }
80 defer rc.Close()
81 ss, err := parseSuperset(rc)
82 if err != nil {
83 return nil, fmt.Errorf("schema/filereader: decoding file schema blob: %w", err)
84 }
85 ss.BlobRef = fileBlobRef
86 if ss.Type != "file" && ss.Type != "bytes" {
87 return nil, fmt.Errorf("schema/filereader: expected \"file\" or \"bytes\" schema blob, got %q", ss.Type)
88 }
89 fr, err := ss.NewFileReader(fetcher)
90 if err != nil {
91 return nil, fmt.Errorf("schema/filereader: creating FileReader for %s: %w", fileBlobRef, err)
92 }
93 return fr, nil
94}
95
96func (b *Blob) NewFileReader(fetcher blob.Fetcher) (*FileReader, error) {
97 return b.ss.NewFileReader(fetcher)

Callers 15

RunMethod · 0.92
vivifyFunction · 0.92
packFileMethod · 0.92
newFileReaderMethod · 0.92
fileInfoMethod · 0.92
fileInfoPackedFunction · 0.92
imprtMethod · 0.92
OpenMethod · 0.92
OpenMethod · 0.92
OpenMethod · 0.92
TestStorageFunction · 0.92
serveRefFunction · 0.92

Calls 5

parseSupersetFunction · 0.85
FetchMethod · 0.65
CloseMethod · 0.65
ValidMethod · 0.45
NewFileReaderMethod · 0.45

Tested by 6

TestStorageFunction · 0.74
TestWriteThenReadFunction · 0.68
TestReaderSeekStressFunction · 0.68
TestReaderEfficiencyFunction · 0.68
TestReaderForeachChunkFunction · 0.68