github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/dbs_algorithm.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package dbs
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    20  )
    21  
    22  
    23  type AlterAlgorithm struct {
    24  	// supported MUST causetstore algorithms in the order 'INSTANT, INPLACE, COPY'
    25  	supported []ast.AlgorithmType
    26  	// If the alter algorithm is not given, the defAlgorithm will be used.
    27  	defAlgorithm ast.AlgorithmType
    28  }
    29  
    30  var (
    31  	instantAlgorithm = &AlterAlgorithm{
    32  		supported:    []ast.AlgorithmType{ast.AlgorithmTypeInstant},
    33  		defAlgorithm: ast.AlgorithmTypeInstant,
    34  	}
    35  
    36  	inplaceAlgorithm = &AlterAlgorithm{
    37  		supported:    []ast.AlgorithmType{ast.AlgorithmTypeInplace},
    38  		defAlgorithm: ast.AlgorithmTypeInplace,
    39  	}
    40  )
    41  
    42  func getProperAlgorithm(specify ast.AlgorithmType, algorithm *AlterAlgorithm) (ast.AlgorithmType, error) {
    43  	if specify == ast.AlgorithmTypeDefault {
    44  		return algorithm.defAlgorithm, nil
    45  	}
    46  
    47  	r := ast.AlgorithmTypeDefault
    48  
    49  	for _, a := range algorithm.supported {
    50  		if specify <= a {
    51  			r = a
    52  			break
    53  		}
    54  	}
    55  
    56  	var err error
    57  	if specify != r {
    58  		err = ErrAlterOperationNotSupported.GenWithStackByArgs(fmt.Sprintf("ALGORITHM=%s", specify), fmt.Sprintf("Cannot alter causet by %s", specify), fmt.Sprintf("ALGORITHM=%s", algorithm.defAlgorithm))
    59  	}
    60  	return r, err
    61  }
    62  
    63  // ResolveAlterAlgorithm resolves the algorithm of the alterSpec.
    64  // If specify is the ast.AlterAlgorithmDefault, then the default algorithm of the alter action will be returned.
    65  // If specify algorithm is not supported by the alter action, it will try to find a better algorithm in the order `INSTANT > INPLACE > COPY`, errAlterOperationNotSupported will be returned.
    66  // E.g. INSTANT may be returned if specify=INPLACE
    67  // If failed to choose any valid algorithm, AlgorithmTypeDefault and errAlterOperationNotSupported will be returned
    68  func ResolveAlterAlgorithm(alterSpec *ast.AlterBlockSpec, specify ast.AlgorithmType) (ast.AlgorithmType, error) {
    69  	switch alterSpec.Tp {
    70  	// For now, MilevaDB only support inplace algorithm and instant algorithm.
    71  	case ast.AlterBlockAddConstraint:
    72  		return getProperAlgorithm(specify, inplaceAlgorithm)
    73  	default:
    74  		return getProperAlgorithm(specify, instantAlgorithm)
    75  	}
    76  }