vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/vstreamer/local_vschema.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vstreamer
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"vitess.io/vitess/go/vt/log"
    24  	"vitess.io/vitess/go/vt/schema"
    25  	"vitess.io/vitess/go/vt/vtgate/vindexes"
    26  )
    27  
    28  // localVSchema provides vschema behavior specific to vstreamer.
    29  // Tables are searched within keyspace, but vindexes can be referenced
    30  // outside the current keyspace.
    31  type localVSchema struct {
    32  	keyspace string
    33  	vschema  *vindexes.VSchema
    34  }
    35  
    36  func (lvs *localVSchema) FindColVindex(tablename string) (*vindexes.ColumnVindex, error) {
    37  	table, err := lvs.findTable(tablename)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	return vindexes.FindBestColVindex(table)
    42  }
    43  
    44  func (lvs *localVSchema) FindOrCreateVindex(qualifiedName string) (vindexes.Vindex, error) {
    45  	splits := strings.Split(qualifiedName, ".")
    46  	var keyspace, name string
    47  	switch len(splits) {
    48  	case 1:
    49  		name = splits[0]
    50  	case 2:
    51  		keyspace, name = splits[0], splits[1]
    52  	default:
    53  		return nil, fmt.Errorf("invalid vindex name: %v", qualifiedName)
    54  	}
    55  	vindex, err := lvs.vschema.FindVindex(keyspace, name)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	if vindex != nil {
    60  		return vindex, nil
    61  	}
    62  	if keyspace != "" {
    63  		return nil, fmt.Errorf("vindex %v not found", qualifiedName)
    64  	}
    65  	return vindexes.CreateVindex(name, name, map[string]string{})
    66  }
    67  
    68  func (lvs *localVSchema) findTable(tablename string) (*vindexes.Table, error) {
    69  	ks, ok := lvs.vschema.Keyspaces[lvs.keyspace]
    70  	if !ok {
    71  		return nil, fmt.Errorf("keyspace %s not found in vschema", lvs.keyspace)
    72  	}
    73  	table := ks.Tables[tablename]
    74  	if table == nil {
    75  		if schema.IsInternalOperationTableName(tablename) {
    76  			log.Infof("found internal table %s, ignoring in local vschema search", tablename)
    77  		} else {
    78  			return nil, fmt.Errorf("table %s not found", tablename)
    79  		}
    80  	}
    81  	return table, nil
    82  }