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 json
|
|
|
|
|
|
|
|
import (
|
2019-11-05 17:20:37 -05:00
|
|
|
"errors"
|
2019-01-08 19:53:04 -05:00
|
|
|
"io"
|
2019-11-05 17:20:37 -05:00
|
|
|
"sync"
|
2019-01-08 19:53:04 -05:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/s3select/sql"
|
2019-02-06 21:34:42 -05:00
|
|
|
|
|
|
|
"github.com/bcicen/jstream"
|
2019-01-08 19:53:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Reader - JSON record reader for S3Select.
|
|
|
|
type Reader struct {
|
2019-02-06 21:34:42 -05:00
|
|
|
args *ReaderArgs
|
|
|
|
decoder *jstream.Decoder
|
|
|
|
valueCh chan *jstream.MetaValue
|
|
|
|
readCloser io.ReadCloser
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read - reads single record.
|
2019-09-13 17:18:35 -04:00
|
|
|
func (r *Reader) Read(dst sql.Record) (sql.Record, error) {
|
2019-02-06 21:34:42 -05:00
|
|
|
v, ok := <-r.valueCh
|
|
|
|
if !ok {
|
|
|
|
if err := r.decoder.Err(); err != nil {
|
|
|
|
return nil, errJSONParsingError(err)
|
|
|
|
}
|
|
|
|
return nil, io.EOF
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2019-03-09 11:13:37 -05:00
|
|
|
var kvs jstream.KVS
|
2019-02-06 21:34:42 -05:00
|
|
|
if v.ValueType == jstream.Object {
|
2019-03-09 11:13:37 -05:00
|
|
|
// This is a JSON object type (that preserves key
|
|
|
|
// order)
|
|
|
|
kvs = v.Value.(jstream.KVS)
|
2019-02-06 21:34:42 -05:00
|
|
|
} else {
|
2019-03-07 03:20:10 -05:00
|
|
|
// To be AWS S3 compatible Select for JSON needs to
|
|
|
|
// output non-object JSON as single column value
|
|
|
|
// i.e. a map with `_1` as key and value as the
|
|
|
|
// non-object.
|
2019-03-09 11:13:37 -05:00
|
|
|
kvs = jstream.KVS{jstream.KV{Key: "_1", Value: v.Value}}
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2019-09-13 17:18:35 -04:00
|
|
|
dstRec, ok := dst.(*Record)
|
|
|
|
if !ok {
|
|
|
|
dstRec = &Record{}
|
|
|
|
}
|
|
|
|
dstRec.KVS = kvs
|
|
|
|
dstRec.SelectFormat = sql.SelectFmtJSON
|
|
|
|
return dstRec, nil
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2019-09-13 17:18:35 -04:00
|
|
|
// Close - closes underlying reader.
|
2019-01-08 19:53:04 -05:00
|
|
|
func (r *Reader) Close() error {
|
2019-09-13 17:18:35 -04:00
|
|
|
// Close the input.
|
|
|
|
err := r.readCloser.Close()
|
|
|
|
for range r.valueCh {
|
|
|
|
// Drain values so we don't leak a goroutine.
|
|
|
|
// Since we have closed the input, it should fail rather quickly.
|
|
|
|
}
|
|
|
|
return err
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewReader - creates new JSON reader using readCloser.
|
|
|
|
func NewReader(readCloser io.ReadCloser, args *ReaderArgs) *Reader {
|
2019-11-05 17:20:37 -05:00
|
|
|
readCloser = &syncReadCloser{rc: readCloser}
|
2019-02-21 02:59:23 -05:00
|
|
|
d := jstream.NewDecoder(readCloser, 0).ObjectAsKVS()
|
2019-01-08 19:53:04 -05:00
|
|
|
return &Reader{
|
2019-02-06 21:34:42 -05:00
|
|
|
args: args,
|
|
|
|
decoder: d,
|
|
|
|
valueCh: d.Stream(),
|
|
|
|
readCloser: readCloser,
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-05 17:20:37 -05:00
|
|
|
|
|
|
|
// syncReadCloser will wrap a readcloser and make it safe to call Close
|
|
|
|
// while reads are running.
|
|
|
|
// All read errors are also postponed until Close is called and
|
|
|
|
// io.EOF is returned instead.
|
|
|
|
type syncReadCloser struct {
|
|
|
|
rc io.ReadCloser
|
|
|
|
errMu sync.Mutex
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *syncReadCloser) Read(p []byte) (n int, err error) {
|
|
|
|
// This ensures that Close will block until Read has completed.
|
|
|
|
// This allows another goroutine to close the reader.
|
|
|
|
pr.errMu.Lock()
|
|
|
|
defer pr.errMu.Unlock()
|
|
|
|
if pr.err != nil {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
n, pr.err = pr.rc.Read(p)
|
|
|
|
if pr.err != nil {
|
|
|
|
// Translate any error into io.EOF, so we don't crash:
|
|
|
|
// https://github.com/bcicen/jstream/blob/master/scanner.go#L48
|
|
|
|
return n, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errClosed = errors.New("read after close")
|
|
|
|
|
|
|
|
func (pr *syncReadCloser) Close() error {
|
|
|
|
pr.errMu.Lock()
|
|
|
|
defer pr.errMu.Unlock()
|
|
|
|
if pr.err == errClosed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if pr.err != nil {
|
|
|
|
return pr.err
|
|
|
|
}
|
|
|
|
pr.err = errClosed
|
|
|
|
return pr.rc.Close()
|
|
|
|
}
|