9 "git.curoverse.com/arvados.git/sdk/go/manifest"
12 // A Reader implements, io.Reader, io.Seeker, and io.Closer, and has a
13 // Len() method that returns the total number of bytes available to
15 type Reader interface {
23 // After reading a data block from Keep, cfReader slices it up
24 // and sends the slices to a buffered channel to be consumed
25 // by the caller via Read().
27 // dataSliceSize is the maximum size of the slices, and
28 // therefore the maximum number of bytes that will be returned
29 // by a single call to Read().
30 dataSliceSize = 1 << 20
33 // ErrNoManifest indicates the given collection has no manifest
34 // information (e.g., manifest_text was excluded by a "select"
35 // parameter when retrieving the collection record).
36 var ErrNoManifest = errors.New("Collection has no manifest")
38 // CollectionFileReader returns a Reader that reads content from a single file
39 // in the collection. The filename must be relative to the root of the
40 // collection. A leading prefix of "/" or "./" in the filename is ignored.
41 func (kc *KeepClient) CollectionFileReader(collection map[string]interface{}, filename string) (Reader, error) {
42 mText, ok := collection["manifest_text"].(string)
44 return nil, ErrNoManifest
46 m := manifest.Manifest{Text: mText}
47 return kc.ManifestFileReader(m, filename)
50 func (kc *KeepClient) ManifestFileReader(m manifest.Manifest, filename string) (Reader, error) {
54 err := f.load(m, filename)
63 segments []*manifest.FileSegment
64 size int64 // total file size
65 offset int64 // current read offset
67 // current/latest segment accessed -- might or might not match pos
68 seg *manifest.FileSegment
69 segStart int64 // position of segment relative to file
71 segNext []*manifest.FileSegment
75 // Close implements io.Closer.
76 func (f *file) Close() error {
83 // Read implements io.Reader.
84 func (f *file) Read(buf []byte) (int, error) {
85 if f.seg == nil || f.offset < f.segStart || f.offset >= f.segStart+int64(f.seg.Len) {
86 // f.seg does not cover the current read offset
87 // (f.pos). Iterate over f.segments to find the one
92 f.segNext = f.segments
93 for len(f.segNext) > 0 {
95 f.segNext = f.segNext[1:]
96 segEnd := f.segStart + int64(seg.Len)
97 if segEnd > f.offset {
103 f.readaheadDone = false
108 if f.segData == nil {
109 data, err := f.kc.cache().Get(f.kc, f.seg.Locator)
113 if len(data) < f.seg.Offset+f.seg.Len {
114 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)
116 f.segData = data[f.seg.Offset : f.seg.Offset+f.seg.Len]
118 // dataOff and dataLen denote a portion of f.segData
119 // corresponding to a portion of the file at f.offset.
120 dataOff := int(f.offset - f.segStart)
121 dataLen := f.seg.Len - dataOff
123 if !f.readaheadDone && len(f.segNext) > 0 && f.offset >= 1048576 && dataOff+dataLen > len(f.segData)/16 {
124 // If we have already read more than just the first
125 // few bytes of this file, and we have already
126 // consumed a noticeable portion of this segment, and
127 // there's more data for this file in the next segment
128 // ... then there's a good chance we are going to need
129 // the data for that next segment soon. Start getting
130 // it into the cache now.
131 go f.kc.cache().Get(f.kc, f.segNext[0].Locator)
132 f.readaheadDone = true
139 copy(buf[:n], f.segData[dataOff:dataOff+n])
144 // Seek implements io.Seeker.
145 func (f *file) Seek(offset int64, whence int) (int64, error) {
151 want = f.offset + offset
153 want = f.size + offset
155 return f.offset, fmt.Errorf("invalid whence %d", whence)
158 return f.offset, fmt.Errorf("attempted seek to %d", want)
167 // Len returns the file size in bytes.
168 func (f *file) Len() uint64 {
169 return uint64(f.size)
172 func (f *file) load(m manifest.Manifest, path string) error {
175 for seg := range m.FileSegmentIterByName(path) {
176 f.segments = append(f.segments, seg)
177 f.size += int64(seg.Len)
179 if f.segments == nil {
180 return os.ErrNotExist