2019-01-08 19:53:04 -05:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2019 Minio, 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 json
|
|
|
|
|
|
|
|
import (
|
2019-02-06 21:34:42 -05:00
|
|
|
"encoding/json"
|
2019-01-08 19:53:04 -05:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"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.
|
|
|
|
func (r *Reader) Read() (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-02-06 21:34:42 -05:00
|
|
|
var data []byte
|
|
|
|
var err error
|
2019-01-08 19:53:04 -05:00
|
|
|
|
2019-02-06 21:34:42 -05:00
|
|
|
if v.ValueType == jstream.Object {
|
|
|
|
data, err = json.Marshal(v.Value)
|
|
|
|
} 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.
|
|
|
|
data, err = json.Marshal(jstream.KVS{jstream.KV{Key: "_1", Value: v.Value}})
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
2019-02-06 21:34:42 -05:00
|
|
|
if err != nil {
|
2019-01-08 19:53:04 -05:00
|
|
|
return nil, errJSONParsingError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Record{
|
2019-03-07 03:20:10 -05:00
|
|
|
Data: data,
|
2019-01-08 19:53:04 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close - closes underlaying reader.
|
|
|
|
func (r *Reader) Close() error {
|
|
|
|
return r.readCloser.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewReader - creates new JSON reader using readCloser.
|
|
|
|
func NewReader(readCloser io.ReadCloser, args *ReaderArgs) *Reader {
|
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
|
|
|
}
|
|
|
|
}
|