mirror of
https://github.com/minio/minio.git
synced 2024-12-27 15:45:55 -05:00
c9b8bd8de2
Queue output items and reuse them. Remove the unneeded type system in sql and just use the Go type system. In best case this is more than an order of magnitude speedup: ``` BenchmarkSelectAll_1M-12 1 1841049400 ns/op 274299728 B/op 4198522 allocs/op BenchmarkSelectAll_1M-12 14 84833400 ns/op 169228346 B/op 3146541 allocs/op ```
152 lines
3.7 KiB
Go
152 lines
3.7 KiB
Go
/*
|
|
* 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 (
|
|
"encoding/csv"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/bcicen/jstream"
|
|
"github.com/minio/minio/pkg/s3select/sql"
|
|
)
|
|
|
|
// RawJSON is a byte-slice that contains valid JSON
|
|
type RawJSON []byte
|
|
|
|
// MarshalJSON instance for []byte that assumes that byte-slice is
|
|
// already serialized JSON
|
|
func (b RawJSON) MarshalJSON() ([]byte, error) {
|
|
return b, nil
|
|
}
|
|
|
|
// Record - is JSON record.
|
|
type Record struct {
|
|
// Used in Set(), Marshal*()
|
|
KVS jstream.KVS
|
|
|
|
SelectFormat sql.SelectObjectFormat
|
|
}
|
|
|
|
// Get - gets the value for a column name.
|
|
func (r *Record) Get(name string) (*sql.Value, error) {
|
|
// Get is implemented directly in the sql package.
|
|
return nil, errors.New("not implemented here")
|
|
}
|
|
|
|
// Reset the record.
|
|
func (r *Record) Reset() {
|
|
if len(r.KVS) > 0 {
|
|
r.KVS = r.KVS[:0]
|
|
}
|
|
}
|
|
|
|
// CopyFrom will copy all records from the incoming and append them to the existing records.
|
|
// The source record must be of a similar type.
|
|
func (r *Record) CopyFrom(record sql.Record) error {
|
|
other, ok := record.(*Record)
|
|
if !ok {
|
|
return fmt.Errorf("unexpected record type, expected %T, got %T", r, record)
|
|
}
|
|
r.KVS = append(r.KVS, other.KVS...)
|
|
return nil
|
|
}
|
|
|
|
// Set - sets the value for a column name.
|
|
func (r *Record) Set(name string, value *sql.Value) error {
|
|
var v interface{}
|
|
if b, ok := value.ToBool(); ok {
|
|
v = b
|
|
} else if f, ok := value.ToFloat(); ok {
|
|
v = f
|
|
} else if i, ok := value.ToInt(); ok {
|
|
v = i
|
|
} else if t, ok := value.ToTimestamp(); ok {
|
|
v = sql.FormatSQLTimestamp(t)
|
|
} else if s, ok := value.ToString(); ok {
|
|
v = s
|
|
} else if value.IsNull() {
|
|
v = nil
|
|
} else if b, ok := value.ToBytes(); ok {
|
|
v = RawJSON(b)
|
|
} else {
|
|
return fmt.Errorf("unsupported sql value %v and type %v", value, value.GetTypeString())
|
|
}
|
|
|
|
name = strings.Replace(name, "*", "__ALL__", -1)
|
|
r.KVS = append(r.KVS, jstream.KV{Key: name, Value: v})
|
|
return nil
|
|
}
|
|
|
|
// WriteCSV - encodes to CSV data.
|
|
func (r *Record) WriteCSV(writer io.Writer, fieldDelimiter rune) error {
|
|
var csvRecord []string
|
|
for _, kv := range r.KVS {
|
|
var columnValue string
|
|
switch val := kv.Value.(type) {
|
|
case bool, float64, int64, string:
|
|
columnValue = fmt.Sprintf("%v", val)
|
|
case nil:
|
|
columnValue = ""
|
|
case RawJSON:
|
|
columnValue = string([]byte(val))
|
|
default:
|
|
return errors.New("Cannot marshal unhandled type")
|
|
}
|
|
csvRecord = append(csvRecord, columnValue)
|
|
}
|
|
|
|
w := csv.NewWriter(writer)
|
|
w.Comma = fieldDelimiter
|
|
if err := w.Write(csvRecord); err != nil {
|
|
return err
|
|
}
|
|
w.Flush()
|
|
if err := w.Error(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Raw - returns the underlying representation.
|
|
func (r *Record) Raw() (sql.SelectObjectFormat, interface{}) {
|
|
return r.SelectFormat, r.KVS
|
|
}
|
|
|
|
// WriteJSON - encodes to JSON data.
|
|
func (r *Record) WriteJSON(writer io.Writer) error {
|
|
return json.NewEncoder(writer).Encode(r.KVS)
|
|
}
|
|
|
|
// Replace the underlying buffer of json data.
|
|
func (r *Record) Replace(k jstream.KVS) error {
|
|
r.KVS = k
|
|
return nil
|
|
}
|
|
|
|
// NewRecord - creates new empty JSON record.
|
|
func NewRecord(f sql.SelectObjectFormat) *Record {
|
|
return &Record{
|
|
KVS: jstream.KVS{},
|
|
SelectFormat: f,
|
|
}
|
|
}
|