github.com/matrixorigin/matrixone@v0.7.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  	"math"
    20  	"time"
    21  
    22  	"github.com/google/uuid"
    23  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    24  	logservicepb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    25  	"github.com/matrixorigin/matrixone/pkg/sql/plan"
    26  	"github.com/matrixorigin/matrixone/pkg/txn/client"
    27  	"github.com/matrixorigin/matrixone/pkg/txn/clock"
    28  	"github.com/matrixorigin/matrixone/pkg/txn/storage/memorystorage"
    29  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    30  	"github.com/matrixorigin/matrixone/pkg/vm/engine/memoryengine"
    31  )
    32  
    33  func New(
    34  	ctx context.Context,
    35  ) (
    36  	eng engine.Engine,
    37  	client client.TxnClient,
    38  	compilerContext plan.CompilerContext,
    39  ) {
    40  
    41  	ck := clock.NewHLCClock(func() int64 {
    42  		return time.Now().Unix()
    43  	}, math.MaxInt)
    44  
    45  	shard := logservicepb.DNShardInfo{
    46  		ShardID:   2,
    47  		ReplicaID: 2,
    48  	}
    49  	shards := []logservicepb.DNShardInfo{
    50  		shard,
    51  	}
    52  	dnAddr := "1"
    53  	dnStore := logservicepb.DNStore{
    54  		UUID:           uuid.NewString(),
    55  		ServiceAddress: dnAddr,
    56  		Shards:         shards,
    57  	}
    58  
    59  	storage, err := memorystorage.NewMemoryStorage(
    60  		mpool.MustNewZero(),
    61  		ck,
    62  		memoryengine.RandomIDGenerator,
    63  	)
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  
    68  	client = memorystorage.NewStorageTxnClient(
    69  		ck,
    70  		map[string]*memorystorage.Storage{
    71  			dnAddr: storage,
    72  		},
    73  	)
    74  
    75  	e := memoryengine.New(
    76  		ctx,
    77  		memoryengine.NewDefaultShardPolicy(
    78  			mpool.MustNewZero(),
    79  		),
    80  		func() (logservicepb.ClusterDetails, error) {
    81  			return logservicepb.ClusterDetails{
    82  				DNStores: []logservicepb.DNStore{
    83  					dnStore,
    84  				},
    85  			}, nil
    86  		},
    87  		memoryengine.RandomIDGenerator,
    88  	)
    89  
    90  	txnOp, err := client.New()
    91  	if err != nil {
    92  		panic(err)
    93  	}
    94  	eng = e.Bind(txnOp)
    95  
    96  	err = eng.Create(ctx, "test", txnOp)
    97  	if err != nil {
    98  		panic(err)
    99  	}
   100  
   101  	db, err := eng.Database(ctx, "test", txnOp)
   102  	if err != nil {
   103  		panic(err)
   104  	}
   105  
   106  	CreateR(db)
   107  	CreateS(db)
   108  	CreateT(db)
   109  	CreateT1(db)
   110  	CreatePart(db)
   111  	CreateDate(db)
   112  	CreateSupplier(db)
   113  	CreateCustomer(db)
   114  	CreateLineorder(db)
   115  
   116  	if err = txnOp.Commit(ctx); err != nil {
   117  		panic(err)
   118  	}
   119  
   120  	txnOp, err = client.New()
   121  	if err != nil {
   122  		panic(err)
   123  	}
   124  	compilerContext = e.NewCompilerContext(ctx, "test", txnOp)
   125  	eng = e.Bind(txnOp)
   126  
   127  	return
   128  }