github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/ctl/cmd_flush.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ctl
    16  
    17  import (
    18  	"strconv"
    19  	"strings"
    20  
    21  	"github.com/fagongzi/util/protoc"
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  	pb "github.com/matrixorigin/matrixone/pkg/pb/ctl"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    27  )
    28  
    29  func handleFlush() handleFunc {
    30  	return getDNHandlerFunc(
    31  		pb.CmdMethod_Flush,
    32  		func(_ string) ([]uint64, error) {
    33  			return nil, nil
    34  		},
    35  		func(dnShardID uint64, parameter string, proc *process.Process) ([]byte, error) {
    36  			// parameter should be "DbName.TableName"
    37  			parameters := strings.Split(parameter, ".")
    38  			txnOp := proc.TxnOperator
    39  			if proc.TxnOperator == nil {
    40  				v, err := proc.TxnClient.New()
    41  				if err != nil {
    42  					return nil, err
    43  				}
    44  				txnOp = v
    45  				if err = proc.SessionInfo.StorageEngine.New(proc.Ctx, txnOp); err != nil {
    46  					return nil, err
    47  				}
    48  
    49  				defer func() {
    50  					if err := proc.SessionInfo.StorageEngine.Commit(proc.Ctx, txnOp); err != nil {
    51  						_ = txnOp.Rollback(proc.Ctx)
    52  					} else {
    53  						_ = txnOp.Commit(proc.Ctx)
    54  					}
    55  				}()
    56  			}
    57  			database, err := proc.SessionInfo.StorageEngine.Database(proc.Ctx, parameters[0], txnOp)
    58  			if err != nil {
    59  				return nil, err
    60  			}
    61  			rel, err := database.Relation(proc.Ctx, parameters[1])
    62  			if err != nil {
    63  				return nil, err
    64  			}
    65  			dId := database.GetDatabaseId(proc.Ctx)
    66  			tableId := rel.GetTableID(proc.Ctx)
    67  			dbId, err := strconv.Atoi(dId)
    68  			if err != nil {
    69  				return nil, err
    70  			}
    71  			payload, err := types.Encode(db.FlushTable{
    72  				DatabaseID: uint64(dbId),
    73  				TableID:    tableId,
    74  				AccessInfo: db.AccessInfo{
    75  					AccountID: proc.SessionInfo.AccountId,
    76  					UserID:    proc.SessionInfo.UserId,
    77  					RoleID:    proc.SessionInfo.RoleId,
    78  				},
    79  			})
    80  			if err != nil {
    81  				return nil, moerr.NewInternalError(proc.Ctx, "payload encode err")
    82  			}
    83  			return payload, nil
    84  		},
    85  		func(data []byte) (interface{}, error) {
    86  			resp := pb.DNStringResponse{}
    87  			protoc.MustUnmarshal(&resp, data)
    88  			return resp, nil
    89  		})
    90  }