github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/memoryengine/database.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 memoryengine
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strings"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/catalog"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    25  	"github.com/matrixorigin/matrixone/pkg/txn/client"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    27  )
    28  
    29  type Database struct {
    30  	id          ID
    31  	name        string
    32  	typ         string
    33  	createSql   string
    34  	engine      *Engine
    35  	txnOperator client.TxnOperator
    36  }
    37  
    38  var _ engine.Database = new(Database)
    39  
    40  func (d *Database) Create(ctx context.Context, relName string, defs []engine.TableDef) error {
    41  
    42  	id, err := d.engine.idGenerator.NewID(ctx)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	// convert interface based engine.TableDef to PB version engine.TableDefPB
    48  	pbDefs := make([]engine.TableDefPB, 0, len(defs))
    49  	for i := 0; i < len(defs); i++ {
    50  		pbDefs = append(pbDefs, defs[i].ToPBVersion())
    51  	}
    52  
    53  	_, err = DoTxnRequest[CreateRelationResp](
    54  		ctx,
    55  		d.txnOperator,
    56  		false,
    57  		d.engine.allShards,
    58  		OpCreateRelation,
    59  		&CreateRelationReq{
    60  			ID:           id,
    61  			DatabaseID:   d.id,
    62  			DatabaseName: d.name,
    63  			Type:         RelationTable,
    64  			Name:         strings.ToLower(relName),
    65  			Defs:         pbDefs,
    66  		},
    67  	)
    68  	if err != nil {
    69  		return nil
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func (d *Database) Truncate(ctx context.Context, relName string) (uint64, error) {
    76  	newId, err := d.engine.idGenerator.NewID(ctx)
    77  	if err != nil {
    78  		return 0, err
    79  	}
    80  	rel, err := d.Relation(ctx, relName, nil)
    81  	if err != nil {
    82  		return 0, err
    83  	}
    84  	oldId := rel.GetTableID(ctx)
    85  
    86  	_, err = DoTxnRequest[TruncateRelationResp](
    87  		ctx,
    88  		d.txnOperator,
    89  		false,
    90  		d.engine.allShards,
    91  		OpTruncateRelation,
    92  		&TruncateRelationReq{
    93  			NewTableID:   newId,
    94  			OldTableID:   ID(oldId),
    95  			DatabaseID:   d.id,
    96  			DatabaseName: d.name,
    97  			Name:         strings.ToLower(relName),
    98  		},
    99  	)
   100  	if err != nil {
   101  		return 0, err
   102  	}
   103  
   104  	return 0, nil
   105  }
   106  
   107  func (d *Database) Delete(ctx context.Context, relName string) error {
   108  
   109  	_, err := DoTxnRequest[DeleteRelationResp](
   110  		ctx,
   111  		d.txnOperator,
   112  		false,
   113  		d.engine.allShards,
   114  		OpDeleteRelation,
   115  		&DeleteRelationReq{
   116  			DatabaseID:   d.id,
   117  			DatabaseName: d.name,
   118  			Name:         strings.ToLower(relName),
   119  		},
   120  	)
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	return nil
   126  }
   127  
   128  func (d *Database) Relation(ctx context.Context, relName string, _ any) (engine.Relation, error) {
   129  	if relName == "" {
   130  		return nil, moerr.NewInvalidInput(ctx, "no table name")
   131  	}
   132  
   133  	resps, err := DoTxnRequest[OpenRelationResp](
   134  		ctx,
   135  		d.txnOperator,
   136  		true,
   137  		d.engine.anyShard,
   138  		OpOpenRelation,
   139  		&OpenRelationReq{
   140  			DatabaseID:   d.id,
   141  			DatabaseName: d.name,
   142  			Name:         relName,
   143  		},
   144  	)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	resp := resps[0]
   150  
   151  	switch resp.Type {
   152  
   153  	case RelationTable, RelationView:
   154  		table := &Table{
   155  			engine:       d.engine,
   156  			txnOperator:  d.txnOperator,
   157  			id:           resp.ID,
   158  			databaseName: resp.DatabaseName,
   159  			tableName:    resp.RelationName,
   160  		}
   161  		return table, nil
   162  
   163  	default:
   164  		panic(moerr.NewInternalError(ctx, "unknown type: %+v", resp.Type))
   165  	}
   166  
   167  }
   168  
   169  func (d *Database) Relations(ctx context.Context) ([]string, error) {
   170  
   171  	resps, err := DoTxnRequest[GetRelationsResp](
   172  		ctx,
   173  		d.txnOperator,
   174  		true,
   175  		d.engine.anyShard,
   176  		OpGetRelations,
   177  		&GetRelationsReq{
   178  			DatabaseID: d.id,
   179  		},
   180  	)
   181  	if err != nil {
   182  		return nil, err
   183  	}
   184  
   185  	return resps[0].Names, nil
   186  }
   187  
   188  func (d *Database) GetDatabaseId(ctx context.Context) string {
   189  	return fmt.Sprintf("%d", d.id)
   190  }
   191  
   192  func (d *Database) IsSubscription(ctx context.Context) bool {
   193  	return d.typ == catalog.SystemDBTypeSubscription
   194  }
   195  func (d *Database) GetCreateSql(ctx context.Context) string {
   196  	return d.createSql
   197  }