2011-05-26 23:46:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2011-05-31 17:20:28 +00:00
|
|
|
"json"
|
|
|
|
"log"
|
2011-05-26 23:46:27 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"camli/blobref"
|
|
|
|
)
|
|
|
|
|
2011-05-31 17:35:45 +00:00
|
|
|
var _ = log.Printf
|
|
|
|
|
2011-05-26 23:46:27 +00:00
|
|
|
type FileReader struct {
|
2011-06-04 15:56:03 +00:00
|
|
|
fetcher blobref.SeekFetcher
|
2011-05-26 23:46:27 +00:00
|
|
|
ss *Superset
|
|
|
|
ci int // index into contentparts
|
|
|
|
ccon uint64 // bytes into current chunk already consumed
|
2011-05-31 17:35:45 +00:00
|
|
|
|
|
|
|
cr blobref.ReadSeekCloser // cached reader
|
|
|
|
crbr *blobref.BlobRef // the blobref that cr is for
|
2011-05-26 23:46:27 +00:00
|
|
|
}
|
|
|
|
|
2011-05-31 17:20:28 +00:00
|
|
|
// TODO: make this take a blobref.FetcherAt instead?
|
2011-06-04 15:56:03 +00:00
|
|
|
func NewFileReader(fetcher blobref.SeekFetcher, fileBlobRef *blobref.BlobRef) (*FileReader, os.Error) {
|
2011-05-31 17:20:28 +00:00
|
|
|
ss := new(Superset)
|
|
|
|
rsc, _, err := fetcher.Fetch(fileBlobRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("schema/filereader: fetching file schema blob: %v", err)
|
|
|
|
}
|
|
|
|
if err = json.NewDecoder(rsc).Decode(ss); err != nil {
|
|
|
|
return nil, fmt.Errorf("schema/filereader: decoding file schema blob: %v", err)
|
|
|
|
}
|
|
|
|
if ss.Type != "file" {
|
|
|
|
return nil, fmt.Errorf("schema/filereader: expected \"file\" schema blob, got %q", ss.Type)
|
|
|
|
}
|
|
|
|
return ss.NewFileReader(fetcher), nil
|
|
|
|
}
|
|
|
|
|
2011-06-04 15:56:03 +00:00
|
|
|
func (ss *Superset) NewFileReader(fetcher blobref.SeekFetcher) *FileReader {
|
2011-05-26 23:46:27 +00:00
|
|
|
// TODO: return an error if ss isn't a Type "file" ?
|
|
|
|
// TODO: return some error if the redundant ss.Size field doesn't match ContentParts?
|
2011-05-31 17:35:45 +00:00
|
|
|
return &FileReader{fetcher: fetcher, ss: ss}
|
2011-05-26 23:46:27 +00:00
|
|
|
}
|
|
|
|
|
2011-05-31 17:20:28 +00:00
|
|
|
// FileSchema returns the reader's schema superset. Don't mutate it.
|
|
|
|
func (fr *FileReader) FileSchema() *Superset {
|
|
|
|
return fr.ss
|
|
|
|
}
|
|
|
|
|
2011-05-26 23:46:27 +00:00
|
|
|
func (fr *FileReader) Skip(skipBytes uint64) {
|
|
|
|
for skipBytes != 0 && fr.ci < len(fr.ss.ContentParts) {
|
|
|
|
cp := fr.ss.ContentParts[fr.ci]
|
|
|
|
thisChunkSkippable := cp.Size - fr.ccon
|
|
|
|
toSkip := minu64(skipBytes, thisChunkSkippable)
|
|
|
|
fr.ccon += toSkip
|
|
|
|
if fr.ccon == cp.Size {
|
|
|
|
fr.ci++
|
|
|
|
fr.ccon = 0
|
|
|
|
}
|
|
|
|
skipBytes -= toSkip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 17:35:45 +00:00
|
|
|
func (fr *FileReader) closeOpenBlobs() {
|
|
|
|
if fr.cr != nil {
|
|
|
|
fr.cr.Close()
|
|
|
|
fr.cr = nil
|
|
|
|
fr.crbr = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fr *FileReader) readerFor(br *blobref.BlobRef) (blobref.ReadSeekCloser, os.Error) {
|
|
|
|
if fr.crbr == br {
|
|
|
|
return fr.cr, nil
|
|
|
|
}
|
|
|
|
fr.closeOpenBlobs()
|
|
|
|
rsc, _, ferr := fr.fetcher.Fetch(br)
|
|
|
|
if ferr != nil {
|
|
|
|
return nil, ferr
|
|
|
|
}
|
|
|
|
fr.crbr = br
|
|
|
|
fr.cr = rsc
|
|
|
|
return rsc, nil
|
|
|
|
}
|
|
|
|
|
2011-05-26 23:46:27 +00:00
|
|
|
func (fr *FileReader) Read(p []byte) (n int, err os.Error) {
|
|
|
|
var cp *ContentPart
|
|
|
|
for {
|
|
|
|
if fr.ci >= len(fr.ss.ContentParts) {
|
2011-05-31 17:35:45 +00:00
|
|
|
fr.closeOpenBlobs()
|
2011-05-26 23:46:27 +00:00
|
|
|
return 0, os.EOF
|
|
|
|
}
|
|
|
|
cp = fr.ss.ContentParts[fr.ci]
|
|
|
|
thisChunkReadable := cp.Size - fr.ccon
|
|
|
|
if thisChunkReadable == 0 {
|
|
|
|
fr.ci++
|
|
|
|
fr.ccon = 0
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
br := cp.blobref()
|
|
|
|
if br == nil {
|
|
|
|
return 0, fmt.Errorf("no blobref in content part %d", fr.ci)
|
|
|
|
}
|
2011-05-31 17:35:45 +00:00
|
|
|
|
|
|
|
rsc, ferr := fr.readerFor(br)
|
2011-05-26 23:46:27 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return 0, fmt.Errorf("schema: FileReader.Read error fetching blob %s: %v", br, ferr)
|
|
|
|
}
|
|
|
|
|
|
|
|
seekTo := cp.Offset + fr.ccon
|
|
|
|
if seekTo != 0 {
|
|
|
|
_, serr := rsc.Seek(int64(seekTo), 0)
|
|
|
|
if serr != nil {
|
|
|
|
return 0, fmt.Errorf("schema: FileReader.Read seek error on blob %s: %v", br, serr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
readSize := cp.Size - fr.ccon
|
|
|
|
if uint64(len(p)) < readSize {
|
|
|
|
readSize = uint64(len(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err = rsc.Read(p[:int(readSize)])
|
|
|
|
if err == nil || err == os.EOF {
|
|
|
|
fr.ccon += uint64(n)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func minu64(a, b uint64) uint64 {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|