github.com/matrixorigin/matrixone@v1.2.0/pkg/testutil/testengine/testengine.go (about)

     1  // Copyright 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 testengine
    16  
    17  import (
    18  	"context"
    19  	"github.com/matrixorigin/matrixone/pkg/catalog"
    20  	"github.com/matrixorigin/matrixone/pkg/defines"
    21  
    22  	"github.com/google/uuid"
    23  	"github.com/matrixorigin/matrixone/pkg/clusterservice"
    24  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    25  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    26  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    27  	"github.com/matrixorigin/matrixone/pkg/pb/timestamp"
    28  	"github.com/matrixorigin/matrixone/pkg/sql/plan"
    29  	"github.com/matrixorigin/matrixone/pkg/txn/client"
    30  	"github.com/matrixorigin/matrixone/pkg/txn/storage/memorystorage"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    32  	"github.com/matrixorigin/matrixone/pkg/vm/engine/memoryengine"
    33  )
    34  
    35  func New(
    36  	ctx context.Context,
    37  ) (
    38  	eng engine.Engine,
    39  	client client.TxnClient,
    40  	compilerContext plan.CompilerContext,
    41  ) {
    42  	ctx = defines.AttachAccountId(ctx, catalog.System_Account)
    43  	runtime.SetupProcessLevelRuntime(runtime.DefaultRuntime())
    44  	ck := runtime.ProcessLevelRuntime().Clock()
    45  	addr := "1"
    46  	services := []metadata.TNService{{
    47  		ServiceID:         uuid.NewString(),
    48  		TxnServiceAddress: "1",
    49  		Shards: []metadata.TNShard{
    50  			{
    51  				TNShardRecord: metadata.TNShardRecord{ShardID: 2},
    52  				ReplicaID:     2,
    53  			},
    54  		},
    55  	}}
    56  	runtime.ProcessLevelRuntime().SetGlobalVariables(runtime.ClusterService,
    57  		clusterservice.NewMOCluster(nil, 0,
    58  			clusterservice.WithDisableRefresh(),
    59  			clusterservice.WithServices(nil, services)))
    60  
    61  	storage, err := memorystorage.NewMemoryStorage(
    62  		mpool.MustNewZeroNoFixed(),
    63  		ck,
    64  		memoryengine.RandomIDGenerator,
    65  	)
    66  	if err != nil {
    67  		panic(err)
    68  	}
    69  
    70  	client = memorystorage.NewStorageTxnClient(
    71  		ck,
    72  		map[string]*memorystorage.Storage{
    73  			addr: storage,
    74  		},
    75  	)
    76  
    77  	e := memoryengine.New(
    78  		ctx,
    79  		memoryengine.NewDefaultShardPolicy(
    80  			mpool.MustNewZeroNoFixed(),
    81  		),
    82  		memoryengine.RandomIDGenerator,
    83  		clusterservice.GetMOCluster(),
    84  	)
    85  
    86  	txnOp, err := client.New(ctx, timestamp.Timestamp{})
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  	eng = e.Bind(txnOp)
    91  
    92  	err = eng.Create(ctx, "test", txnOp)
    93  	if err != nil {
    94  		panic(err)
    95  	}
    96  
    97  	db, err := eng.Database(ctx, "test", txnOp)
    98  	if err != nil {
    99  		panic(err)
   100  	}
   101  
   102  	CreateR(db)
   103  	CreateS(db)
   104  	CreateT(db)
   105  	CreateT1(db)
   106  	CreatePart(db)
   107  	CreateDate(db)
   108  	CreateSupplier(db)
   109  	CreateCustomer(db)
   110  	CreateLineorder(db)
   111  	CreateCompressFileTable(db)
   112  
   113  	if err = txnOp.Commit(ctx); err != nil {
   114  		panic(err)
   115  	}
   116  
   117  	txnOp, err = client.New(ctx, timestamp.Timestamp{})
   118  	if err != nil {
   119  		panic(err)
   120  	}
   121  	compilerContext = e.NewCompilerContext(ctx, "test", txnOp)
   122  	eng = e.Bind(txnOp)
   123  
   124  	return
   125  }