9 "git.curoverse.com/arvados.git/sdk/go/arvados"
10 "git.curoverse.com/arvados.git/sdk/go/manifest"
14 // After reading a data block from Keep, cfReader slices it up
15 // and sends the slices to a buffered channel to be consumed
16 // by the caller via Read().
18 // dataSliceSize is the maximum size of the slices, and
19 // therefore the maximum number of bytes that will be returned
20 // by a single call to Read().
21 dataSliceSize = 1 << 20
24 // ErrNoManifest indicates the given collection has no manifest
25 // information (e.g., manifest_text was excluded by a "select"
26 // parameter when retrieving the collection record).
27 var ErrNoManifest = errors.New("Collection has no manifest")
29 // CollectionFileReader returns a Reader that reads content from a single file
30 // in the collection. The filename must be relative to the root of the
31 // collection. A leading prefix of "/" or "./" in the filename is ignored.
32 func (kc *KeepClient) CollectionFileReader(collection map[string]interface{}, filename string) (arvados.File, error) {
33 mText, ok := collection["manifest_text"].(string)
35 return nil, ErrNoManifest
37 m := manifest.Manifest{Text: mText}
38 return kc.ManifestFileReader(m, filename)
41 func (kc *KeepClient) ManifestFileReader(m manifest.Manifest, filename string) (arvados.File, error) {
45 err := f.load(m, filename)
54 segments []*manifest.FileSegment
55 size int64 // total file size
56 offset int64 // current read offset
58 // current/latest segment accessed -- might or might not match pos
59 seg *manifest.FileSegment
60 segStart int64 // position of segment relative to file
62 segNext []*manifest.FileSegment
66 // Close implements io.Closer.
67 func (f *file) Close() error {
74 // Read implements io.Reader.
75 func (f *file) Read(buf []byte) (int, error) {
76 if f.seg == nil || f.offset < f.segStart || f.offset >= f.segStart+int64(f.seg.Len) {
77 // f.seg does not cover the current read offset
78 // (f.pos). Iterate over f.segments to find the one
83 f.segNext = f.segments
84 for len(f.segNext) > 0 {
86 f.segNext = f.segNext[1:]
87 segEnd := f.segStart + int64(seg.Len)
88 if segEnd > f.offset {
94 f.readaheadDone = false
100 data, err := f.kc.cache().Get(f.kc, f.seg.Locator)
104 if len(data) < f.seg.Offset+f.seg.Len {
105 return 0, fmt.Errorf("invalid segment (offset %d len %d) in %d-byte block %s", f.seg.Offset, f.seg.Len, len(data), f.seg.Locator)
107 f.segData = data[f.seg.Offset : f.seg.Offset+f.seg.Len]
109 // dataOff and dataLen denote a portion of f.segData
110 // corresponding to a portion of the file at f.offset.
111 dataOff := int(f.offset - f.segStart)
112 dataLen := f.seg.Len - dataOff
114 if !f.readaheadDone && len(f.segNext) > 0 && f.offset >= 1048576 && dataOff+dataLen > len(f.segData)/16 {
115 // If we have already read more than just the first
116 // few bytes of this file, and we have already
117 // consumed a noticeable portion of this segment, and
118 // there's more data for this file in the next segment
119 // ... then there's a good chance we are going to need
120 // the data for that next segment soon. Start getting
121 // it into the cache now.
122 go f.kc.cache().Get(f.kc, f.segNext[0].Locator)
123 f.readaheadDone = true
130 copy(buf[:n], f.segData[dataOff:dataOff+n])
135 // Seek implements io.Seeker.
136 func (f *file) Seek(offset int64, whence int) (int64, error) {
142 want = f.offset + offset
144 want = f.size + offset
146 return f.offset, fmt.Errorf("invalid whence %d", whence)
149 return f.offset, fmt.Errorf("attempted seek to %d", want)
158 // Size returns the file size in bytes.
159 func (f *file) Size() int64 {
163 func (f *file) load(m manifest.Manifest, path string) error {
166 for seg := range m.FileSegmentIterByName(path) {
167 f.segments = append(f.segments, seg)
168 f.size += int64(seg.Len)
170 if f.segments == nil {
171 return os.ErrNotExist