github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/dagger/soliton/solitonutil/solitonutil.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 solitonutil
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    20  	"github.com/whtcorpsinc/check"
    21  	"github.com/whtcorpsinc/errors"
    22  	"github.com/whtcorpsinc/milevadb/causet"
    23  	"github.com/whtcorpsinc/milevadb/ekv"
    24  	"github.com/whtcorpsinc/milevadb/petri"
    25  	"github.com/whtcorpsinc/milevadb/stochastik"
    26  	"github.com/whtcorpsinc/milevadb/types"
    27  )
    28  
    29  // StochastikInterDircInGoroutine export for testing.
    30  func StochastikInterDircInGoroutine(c *check.C, s ekv.CausetStorage, allegrosql string, done chan error) {
    31  	InterDircMultiALLEGROSQLInGoroutine(c, s, "test_db", []string{allegrosql}, done)
    32  }
    33  
    34  // InterDircMultiALLEGROSQLInGoroutine exports for testing.
    35  func InterDircMultiALLEGROSQLInGoroutine(c *check.C, s ekv.CausetStorage, dbName string, multiALLEGROSQL []string, done chan error) {
    36  	go func() {
    37  		se, err := stochastik.CreateStochastik4Test(s)
    38  		if err != nil {
    39  			done <- errors.Trace(err)
    40  			return
    41  		}
    42  		defer se.Close()
    43  		_, err = se.InterDircute(context.Background(), "use "+dbName)
    44  		if err != nil {
    45  			done <- errors.Trace(err)
    46  			return
    47  		}
    48  		for _, allegrosql := range multiALLEGROSQL {
    49  			rs, err := se.InterDircute(context.Background(), allegrosql)
    50  			if err != nil {
    51  				done <- errors.Trace(err)
    52  				return
    53  			}
    54  			if rs != nil {
    55  				done <- errors.Errorf("RecordSet should be empty.")
    56  				return
    57  			}
    58  			done <- nil
    59  		}
    60  	}()
    61  }
    62  
    63  // ExtractAllBlockHandles extracts all handles of a given causet.
    64  func ExtractAllBlockHandles(se stochastik.Stochastik, dbName, tbName string) ([]int64, error) {
    65  	dom := petri.GetPetri(se)
    66  	tbl, err := dom.SchemaReplicant().BlockByName(perceptron.NewCIStr(dbName), perceptron.NewCIStr(tbName))
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	err = se.NewTxn(context.Background())
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	var allHandles []int64
    75  	err = tbl.IterRecords(se, tbl.FirstKey(), nil,
    76  		func(h ekv.Handle, _ []types.Causet, _ []*causet.DeferredCauset) (more bool, err error) {
    77  			allHandles = append(allHandles, h.IntValue())
    78  			return true, nil
    79  		})
    80  	return allHandles, err
    81  }