milvus/internal/util/importutil/import_options.go
wayblink dbd60757c9
Support pass start_ts and end_ts through Import API (#19810)
Signed-off-by: wayblink <anyang.wang@zilliz.com>

Signed-off-by: wayblink <anyang.wang@zilliz.com>
2022-10-20 12:15:27 +08:00

95 lines
3.0 KiB
Go

// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 importutil
import (
"errors"
"math"
"strconv"
"github.com/milvus-io/milvus-proto/go-api/commonpb"
"github.com/milvus-io/milvus/internal/util/funcutil"
"github.com/milvus-io/milvus/internal/util/tsoutil"
)
// Extra option keys to pass through import API
const (
Bucket = "bucket" // the source files' minio bucket
StartTs = "start_ts" // start timestamp to filter data, only data between StartTs and EndTs will be imported
EndTs = "end_ts" // end timestamp to filter data, only data between StartTs and EndTs will be imported
OptionFormat = "start_ts: 10-digit physical timestamp, e.g. 1665995420, default 0 \n" +
"end_ts: 10-digit physical timestamp, e.g. 1665995420, default math.MaxInt \n"
)
// ValidateOptions the options is illegal, return nil if illegal, return error if not.
// Illegal options:
// start_ts: 10-digit physical timestamp, e.g. 1665995420
// end_ts: 10-digit physical timestamp, e.g. 1665995420
func ValidateOptions(options []*commonpb.KeyValuePair) error {
optionMap := funcutil.KeyValuePair2Map(options)
// StartTs should be int
_, ok := optionMap[StartTs]
var startTs uint64
var endTs uint64 = math.MaxInt64
var err error
if ok {
startTs, err = strconv.ParseUint(optionMap[StartTs], 10, 64)
if err != nil {
return err
}
}
// EndTs should be int
_, ok = optionMap[EndTs]
if ok {
endTs, err = strconv.ParseUint(optionMap[EndTs], 10, 64)
if err != nil {
return err
}
}
if startTs > endTs {
return errors.New("start_ts shouldn't be larger than end_ts")
}
return nil
}
// ParseTSFromOptions get (start_ts, end_ts, error) from input options.
// return value will be composed to milvus system timestamp from physical timestamp
func ParseTSFromOptions(options []*commonpb.KeyValuePair) (uint64, uint64, error) {
err := ValidateOptions(options)
if err != nil {
return 0, 0, err
}
var tsStart uint64
var tsEnd uint64
importOptions := funcutil.KeyValuePair2Map(options)
value, ok := importOptions[StartTs]
if ok {
pTs, _ := strconv.ParseInt(value, 10, 64)
tsStart = tsoutil.ComposeTS(pTs, 0)
} else {
tsStart = 0
}
value, ok = importOptions[EndTs]
if ok {
pTs, _ := strconv.ParseInt(value, 10, 64)
tsEnd = tsoutil.ComposeTS(pTs, 0)
} else {
tsEnd = math.MaxUint64
}
return tsStart, tsEnd, nil
}