github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/txn/txnimpl/txn.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 txnimpl 16 17 import ( 18 "context" 19 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/defines" 22 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog" 23 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 24 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers" 25 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle" 26 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif" 27 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/entry" 28 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase" 29 ) 30 31 type txnImpl struct { 32 *txnbase.Txn 33 catalog *catalog.Catalog 34 } 35 36 var TxnFactory = func(catalog *catalog.Catalog) txnbase.TxnFactory { 37 return func(mgr *txnbase.TxnManager, store txnif.TxnStore, txnId []byte, 38 start, snapshot types.TS) txnif.AsyncTxn { 39 return newTxnImpl(catalog, mgr, store, txnId, start, snapshot) 40 } 41 } 42 43 func newTxnImpl(catalog *catalog.Catalog, mgr *txnbase.TxnManager, store txnif.TxnStore, 44 txnId []byte, start, snapshot types.TS) *txnImpl { 45 impl := &txnImpl{ 46 Txn: txnbase.NewTxn(mgr, store, txnId, start, snapshot), 47 catalog: catalog, 48 } 49 return impl 50 } 51 52 func (txn *txnImpl) CreateDatabase(name, createSql, datTyp string) (db handle.Database, err error) { 53 return txn.Store.CreateDatabase(name, createSql, datTyp) 54 } 55 56 func (txn *txnImpl) CreateDatabaseWithCtx(ctx context.Context, 57 name, createSql, datTyp string, id uint64) (db handle.Database, err error) { 58 err = txn.bindCtxInfo(ctx) 59 if err != nil { 60 return nil, err 61 } 62 return txn.Store.CreateDatabaseWithID(name, createSql, datTyp, id) 63 } 64 65 func (txn *txnImpl) DropDatabase(name string) (db handle.Database, err error) { 66 return txn.Store.DropDatabase(name) 67 } 68 69 func (txn *txnImpl) DropDatabaseByID(id uint64) (db handle.Database, err error) { 70 return txn.Store.DropDatabaseByID(id) 71 } 72 73 func (txn *txnImpl) UnsafeGetDatabase(id uint64) (db handle.Database, err error) { 74 return txn.Store.UnsafeGetDatabase(id) 75 } 76 77 func (txn *txnImpl) UnsafeGetRelation(dbId, id uint64) (rel handle.Relation, err error) { 78 return txn.Store.UnsafeGetRelation(dbId, id) 79 } 80 81 func (txn *txnImpl) bindCtxInfo(ctx context.Context) (err error) { 82 var tid uint32 83 if ctx == nil { 84 return 85 } 86 tid, err = defines.GetAccountId(ctx) 87 if err != nil { 88 return 89 } 90 91 uid := defines.GetUserId(ctx) 92 rid := defines.GetRoleId(ctx) 93 txn.BindAccessInfo(tid, uid, rid) 94 return err 95 } 96 func (txn *txnImpl) GetDatabaseWithCtx(ctx context.Context, name string) (db handle.Database, err error) { 97 err = txn.bindCtxInfo(ctx) 98 if err != nil { 99 return nil, err 100 } 101 return txn.Store.GetDatabase(name) 102 } 103 func (txn *txnImpl) GetDatabase(name string) (db handle.Database, err error) { 104 return txn.Store.GetDatabase(name) 105 } 106 107 func (txn *txnImpl) GetDatabaseByID(id uint64) (db handle.Database, err error) { 108 return txn.Store.GetDatabaseByID(id) 109 } 110 111 func (txn *txnImpl) DatabaseNames() (names []string) { 112 return txn.Store.DatabaseNames() 113 } 114 115 func (txn *txnImpl) LogTxnEntry(dbId, tableId uint64, entry txnif.TxnEntry, readed []*common.ID) (err error) { 116 return txn.Store.LogTxnEntry(dbId, tableId, entry, readed) 117 } 118 119 func (txn *txnImpl) LogTxnState(sync bool) (logEntry entry.Entry, err error) { 120 return txn.Store.LogTxnState(sync) 121 } 122 123 func makeWorkspaceVector(typ types.Type) containers.Vector { 124 return containers.MakeVector(typ, common.WorkspaceAllocator) 125 }