Simplify the implementations of the ToPhysicalChannel and the ConvertChannelName func (#17605)

Signed-off-by: SimFG <bang.fu@zilliz.com>
pull/17613/head
SimFG 2022-06-17 10:22:11 +08:00 committed by GitHub
parent 0f87763682
commit 2008f60e50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 18 deletions

View File

@ -28,6 +28,7 @@ import (
"net/http"
"reflect"
"strconv"
"strings"
"time"
"go.uber.org/zap"
@ -243,32 +244,22 @@ func GetAvailablePort() int {
// ToPhysicalChannel get physical channel name from virtual channel name
func ToPhysicalChannel(vchannel string) string {
var idx int
for idx = len(vchannel) - 1; idx >= 0; idx-- {
if vchannel[idx] == '_' {
break
}
}
if idx < 0 {
index := strings.LastIndex(vchannel, "_")
if index < 0 {
return vchannel
}
return vchannel[:idx]
return vchannel[:index]
}
// ConvertChannelName assembles channel name according to parameters.
func ConvertChannelName(chanName string, tokenFrom string, tokenTo string) (string, error) {
chanNameLen := len(chanName)
tokenFromLen := len(tokenFrom)
if chanNameLen < tokenFromLen {
if tokenFrom == "" {
return "", fmt.Errorf("the tokenFrom is empty")
}
if !strings.Contains(chanName, tokenFrom) {
return "", fmt.Errorf("cannot find token '%s' in '%s'", tokenFrom, chanName)
}
for i := 0; i < (chanNameLen - tokenFromLen); i++ {
if chanName[i:i+tokenFromLen] == tokenFrom {
return chanName[0:i] + tokenTo + chanName[i+tokenFromLen:], nil
}
}
return "", fmt.Errorf("cannot find token '%s' in '%s'", tokenFrom, chanName)
return strings.Replace(chanName, tokenFrom, tokenTo, 1), nil
}
func getNumRowsOfScalarField(datas interface{}) uint64 {

View File

@ -311,6 +311,8 @@ func Test_ConvertChannelName(t *testing.T) {
)
_, err := ConvertChannelName("by-dev", tFrom, tTo)
assert.NotNil(t, err)
_, err = ConvertChannelName("by-dev", "", tTo)
assert.NotNil(t, err)
_, err = ConvertChannelName("by-dev_rootcoord-delta_123v0", tFrom, tTo)
assert.NotNil(t, err)
str, err := ConvertChannelName(chanName, tFrom, tTo)