github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/catalog/mock.go (about)

     1  // Copyright 2021 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 catalog
    16  
    17  import (
    18  	"sync"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
    27  )
    28  
    29  func MockTxnFactory(catalog *Catalog) txnbase.TxnFactory {
    30  	return func(mgr *txnbase.TxnManager, store txnif.TxnStore, id []byte, start, snapshot types.TS) txnif.AsyncTxn {
    31  		txn := new(mockTxn)
    32  		txn.Txn = txnbase.NewTxn(mgr, store, id, start, snapshot)
    33  		txn.catalog = catalog
    34  		return txn
    35  	}
    36  }
    37  
    38  func MockTxnStoreFactory(catalog *Catalog) txnbase.TxnStoreFactory {
    39  	return func() txnif.TxnStore {
    40  		store := new(mockTxnStore)
    41  		store.catalog = catalog
    42  		store.entries = make(map[txnif.TxnEntry]bool)
    43  		return store
    44  	}
    45  
    46  }
    47  
    48  type mockTxnStore struct {
    49  	txn txnif.TxnReader
    50  	txnbase.NoopTxnStore
    51  	catalog *Catalog
    52  	entries map[txnif.TxnEntry]bool
    53  }
    54  
    55  func (store *mockTxnStore) AddTxnEntry(et txnif.TxnEntryType, entry txnif.TxnEntry) {
    56  	store.entries[entry] = true
    57  }
    58  
    59  func (store *mockTxnStore) BindTxn(txn txnif.AsyncTxn) {
    60  	store.txn = txn
    61  }
    62  
    63  func (store *mockTxnStore) PrepareCommit() error {
    64  	for e := range store.entries {
    65  		err := e.PrepareCommit()
    66  		if err != nil {
    67  			return err
    68  		}
    69  	}
    70  	return nil
    71  }
    72  
    73  func (store *mockTxnStore) ApplyCommit() error {
    74  	for e := range store.entries {
    75  		err := e.ApplyCommit()
    76  		if err != nil {
    77  			return err
    78  		}
    79  	}
    80  	return nil
    81  }
    82  
    83  func (store *mockTxnStore) DropDatabaseByID(id uint64) (handle.Database, error) {
    84  	return nil, nil
    85  }
    86  
    87  type mockDBHandle struct {
    88  	*txnbase.TxnDatabase
    89  	catalog *Catalog
    90  	entry   *DBEntry
    91  }
    92  
    93  type mockObjIt struct {
    94  	sync.RWMutex
    95  }
    96  
    97  type mockTableHandle struct {
    98  	*txnbase.TxnRelation
    99  	catalog *Catalog
   100  	entry   *TableEntry
   101  }
   102  
   103  func (h *mockTableHandle) GetDB() (handle.Database, error) {
   104  	return h.Txn.GetStore().GetDatabase(h.GetMeta().(*TableEntry).GetDB().GetName())
   105  }
   106  
   107  func newMockDBHandle(catalog *Catalog, txn txnif.AsyncTxn, entry *DBEntry) *mockDBHandle {
   108  	return &mockDBHandle{
   109  		TxnDatabase: &txnbase.TxnDatabase{
   110  			Txn: txn,
   111  		},
   112  		catalog: catalog,
   113  		entry:   entry,
   114  	}
   115  }
   116  
   117  func newMockTableHandle(catalog *Catalog, txn txnif.AsyncTxn, entry *TableEntry) *mockTableHandle {
   118  	return &mockTableHandle{
   119  		TxnRelation: &txnbase.TxnRelation{
   120  			Txn: txn,
   121  		},
   122  		catalog: catalog,
   123  		entry:   entry,
   124  	}
   125  }
   126  
   127  func (it *mockObjIt) GetError() error          { return nil }
   128  func (it *mockObjIt) Valid() bool              { return false }
   129  func (it *mockObjIt) Next()                    {}
   130  func (it *mockObjIt) Close() error             { return nil }
   131  func (it *mockObjIt) GetObject() handle.Object { return nil }
   132  
   133  func (h *mockDBHandle) CreateRelation(def any) (rel handle.Relation, err error) {
   134  	schema := def.(*Schema)
   135  	tbl, err := h.entry.CreateTableEntry(schema, h.Txn, nil)
   136  	if err != nil {
   137  		return nil, err
   138  	}
   139  	h.Txn.GetStore().AddTxnEntry(0, tbl)
   140  	rel = newMockTableHandle(h.catalog, h.Txn, tbl)
   141  	return
   142  }
   143  
   144  func (h *mockDBHandle) CreateRelationWithID(def any, id uint64) (rel handle.Relation, err error) {
   145  	schema := def.(*Schema)
   146  	tbl, err := h.entry.CreateTableEntryWithTableId(schema, h.Txn, nil, id)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  	h.Txn.GetStore().AddTxnEntry(0, tbl)
   151  	rel = newMockTableHandle(h.catalog, h.Txn, tbl)
   152  	return
   153  }
   154  
   155  func (h *mockDBHandle) TruncateByName(name string) (rel handle.Relation, err error) {
   156  	panic("not implemented")
   157  }
   158  
   159  func (h *mockDBHandle) TruncateWithID(name string, newTableId uint64) (rel handle.Relation, err error) {
   160  	panic(moerr.NewNYINoCtx("Pls implement me!!"))
   161  }
   162  
   163  func (h *mockDBHandle) TruncateByID(id uint64, newTableId uint64) (rel handle.Relation, err error) {
   164  	panic(moerr.NewNYINoCtx("Pls implement me!!"))
   165  }
   166  
   167  func (h *mockDBHandle) DropRelationByName(name string) (rel handle.Relation, err error) {
   168  	_, entry, err := h.entry.DropTableEntry(name, h.Txn)
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  	h.Txn.GetStore().AddTxnEntry(0, entry)
   173  	rel = newMockTableHandle(h.catalog, h.Txn, entry)
   174  	return
   175  }
   176  
   177  func (h *mockDBHandle) DropRelationByID(id uint64) (rel handle.Relation, err error) {
   178  	return nil, nil
   179  }
   180  
   181  func (h *mockDBHandle) String() string {
   182  	return h.entry.String()
   183  }
   184  
   185  func (h *mockDBHandle) GetRelationByName(name string) (rel handle.Relation, err error) {
   186  	entry, err := h.entry.TxnGetTableEntryByName(name, h.Txn)
   187  	if err != nil {
   188  		return nil, err
   189  	}
   190  	return newMockTableHandle(h.catalog, h.Txn, entry), nil
   191  }
   192  
   193  func (h *mockDBHandle) GetRelationByID(id uint64) (rel handle.Relation, err error) {
   194  	return nil, nil
   195  }
   196  
   197  func (h *mockDBHandle) IsSubscription() bool {
   198  	return h.entry.IsSubscription()
   199  }
   200  
   201  func (h *mockDBHandle) GetCreateSql() string {
   202  	return h.entry.GetCreateSql()
   203  }
   204  
   205  func (h *mockTableHandle) MakeObjectIt() (it handle.ObjectIt) {
   206  	return new(mockObjIt)
   207  }
   208  
   209  func (h *mockTableHandle) MakeObjectItOnSnap() (it handle.ObjectIt) {
   210  	return new(mockObjIt)
   211  }
   212  
   213  func (h *mockTableHandle) String() string {
   214  	return h.entry.String()
   215  }
   216  
   217  type mockTxn struct {
   218  	*txnbase.Txn
   219  	catalog *Catalog
   220  }
   221  
   222  func (txn *mockTxn) CreateDatabase(name, createSql, datTyp string) (handle.Database, error) {
   223  	entry, err := txn.catalog.CreateDBEntry(name, createSql, datTyp, txn)
   224  	if err != nil {
   225  		return nil, err
   226  	}
   227  	txn.Store.AddTxnEntry(0, entry)
   228  	h := newMockDBHandle(txn.catalog, txn, entry)
   229  	return h, nil
   230  }
   231  
   232  func (txn *mockTxn) CreateDatabaseWithID(name, createSql, datTyp string, id uint64) (handle.Database, error) {
   233  	entry, err := txn.catalog.CreateDBEntryWithID(name, createSql, datTyp, id, txn)
   234  	if err != nil {
   235  		return nil, err
   236  	}
   237  	txn.Store.AddTxnEntry(0, entry)
   238  	h := newMockDBHandle(txn.catalog, txn, entry)
   239  	return h, nil
   240  }
   241  
   242  func (txn *mockTxn) CreateDatabaseByDef(def any) (handle.Database, error) {
   243  	panic(moerr.NewNYINoCtx("CreateDatabaseByID is not implemented yet"))
   244  }
   245  
   246  func (txn *mockTxn) GetDatabase(name string) (handle.Database, error) {
   247  	entry, err := txn.catalog.TxnGetDBEntryByName(name, txn)
   248  	if err != nil {
   249  		return nil, err
   250  	}
   251  	return newMockDBHandle(txn.catalog, txn, entry), nil
   252  }
   253  
   254  func (txn *mockTxn) GetDatabaseByID(id uint64) (handle.Database, error) {
   255  	entry, err := txn.catalog.TxnGetDBEntryByID(id, txn)
   256  	if err != nil {
   257  		return nil, err
   258  	}
   259  	return newMockDBHandle(txn.catalog, txn, entry), nil
   260  }
   261  
   262  func (txn *mockTxn) DropDatabase(name string) (handle.Database, error) {
   263  	_, entry, err := txn.catalog.DropDBEntryByName(name, txn)
   264  	if err != nil {
   265  		return nil, err
   266  	}
   267  	txn.Store.AddTxnEntry(0, entry)
   268  	return newMockDBHandle(txn.catalog, txn, entry), nil
   269  }
   270  
   271  func (txn *mockTxn) DropDatabaseByID(id uint64) (handle.Database, error) {
   272  	_, entry, err := txn.catalog.DropDBEntryByID(id, txn)
   273  	if err != nil {
   274  		return nil, err
   275  	}
   276  	txn.Store.AddTxnEntry(0, entry)
   277  	return newMockDBHandle(txn.catalog, txn, entry), nil
   278  }
   279  
   280  func MockBatch(schema *Schema, rows int) *containers.Batch {
   281  	if schema.HasSortKey() {
   282  		sortKey := schema.GetSingleSortKey()
   283  		return containers.MockBatchWithAttrs(schema.Types(), schema.Attrs(), rows, sortKey.Idx, nil)
   284  	} else {
   285  		return containers.MockBatchWithAttrs(schema.Types(), schema.Attrs(), rows, schema.GetPrimaryKey().Idx /*get fake pk*/, nil)
   286  	}
   287  }