2019-01-08 19:53:04 -05:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
2019-01-08 19:53:04 -05:00
|
|
|
*
|
|
|
|
* 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 s3select
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/bzip2"
|
2019-11-05 17:20:37 -05:00
|
|
|
"errors"
|
2019-01-08 19:53:04 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-11-05 17:20:37 -05:00
|
|
|
"sync"
|
2019-01-08 19:53:04 -05:00
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
gzip "github.com/klauspost/pgzip"
|
|
|
|
)
|
|
|
|
|
|
|
|
type countUpReader struct {
|
|
|
|
reader io.Reader
|
|
|
|
bytesRead int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *countUpReader) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = r.reader.Read(p)
|
|
|
|
atomic.AddInt64(&r.bytesRead, int64(n))
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *countUpReader) BytesRead() int64 {
|
2020-02-13 17:03:52 -05:00
|
|
|
if r == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
return atomic.LoadInt64(&r.bytesRead)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCountUpReader(reader io.Reader) *countUpReader {
|
|
|
|
return &countUpReader{
|
|
|
|
reader: reader,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type progressReader struct {
|
|
|
|
rc io.ReadCloser
|
|
|
|
scannedReader *countUpReader
|
|
|
|
processedReader *countUpReader
|
2019-11-05 17:20:37 -05:00
|
|
|
|
|
|
|
closedMu sync.Mutex
|
|
|
|
closed bool
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *progressReader) Read(p []byte) (n int, err error) {
|
2019-11-05 17:20:37 -05:00
|
|
|
// This ensures that Close will block until Read has completed.
|
|
|
|
// This allows another goroutine to close the reader.
|
|
|
|
pr.closedMu.Lock()
|
|
|
|
defer pr.closedMu.Unlock()
|
|
|
|
if pr.closed {
|
|
|
|
return 0, errors.New("progressReader: read after Close")
|
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
return pr.processedReader.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *progressReader) Close() error {
|
2020-02-13 17:03:52 -05:00
|
|
|
if pr.rc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-05 17:20:37 -05:00
|
|
|
pr.closedMu.Lock()
|
|
|
|
defer pr.closedMu.Unlock()
|
|
|
|
if pr.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pr.closed = true
|
2019-01-08 19:53:04 -05:00
|
|
|
return pr.rc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *progressReader) Stats() (bytesScanned, bytesProcessed int64) {
|
2020-02-13 17:03:52 -05:00
|
|
|
if pr == nil {
|
|
|
|
return 0, 0
|
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
return pr.scannedReader.BytesRead(), pr.processedReader.BytesRead()
|
|
|
|
}
|
|
|
|
|
|
|
|
func newProgressReader(rc io.ReadCloser, compType CompressionType) (*progressReader, error) {
|
|
|
|
scannedReader := newCountUpReader(rc)
|
|
|
|
var r io.Reader
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch compType {
|
|
|
|
case noneType:
|
|
|
|
r = scannedReader
|
|
|
|
case gzipType:
|
2020-03-12 18:34:11 -04:00
|
|
|
r, err = gzip.NewReader(scannedReader)
|
|
|
|
if err != nil {
|
2020-03-05 22:34:04 -05:00
|
|
|
if errors.Is(err, gzip.ErrHeader) || errors.Is(err, gzip.ErrChecksum) {
|
|
|
|
return nil, errInvalidGZIPCompressionFormat(err)
|
|
|
|
}
|
2020-03-12 18:34:11 -04:00
|
|
|
return nil, errTruncatedInput(err)
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
case bzip2Type:
|
|
|
|
r = bzip2.NewReader(scannedReader)
|
|
|
|
default:
|
|
|
|
return nil, errInvalidCompressionFormat(fmt.Errorf("unknown compression type '%v'", compType))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &progressReader{
|
|
|
|
rc: rc,
|
|
|
|
scannedReader: scannedReader,
|
|
|
|
processedReader: newCountUpReader(r),
|
|
|
|
}, nil
|
|
|
|
}
|