github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/txn/txnimpl/sysdb.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 txnimpl 16 17 import ( 18 pkgcatalog "github.com/matrixorigin/matrixone/pkg/catalog" 19 "github.com/matrixorigin/matrixone/pkg/common/moerr" 20 "github.com/matrixorigin/matrixone/pkg/util/metric" 21 "github.com/matrixorigin/matrixone/pkg/util/trace/impl/motrace" 22 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle" 23 ) 24 25 var sysTableNames map[string]bool 26 var sysTableIds map[uint64]bool 27 28 // any tenant is able to see these db, but access them is required to be upgraded as Sys tenant in a tricky way. 29 // this can be done in frontend 30 var sysSharedDBNames map[string]bool 31 32 func init() { 33 sysTableNames = make(map[string]bool) 34 sysTableNames[pkgcatalog.MO_COLUMNS] = true 35 sysTableNames[pkgcatalog.MO_TABLES] = true 36 sysTableNames[pkgcatalog.MO_DATABASE] = true 37 38 sysTableIds = make(map[uint64]bool) 39 sysTableIds[pkgcatalog.MO_TABLES_ID] = true 40 sysTableIds[pkgcatalog.MO_DATABASE_ID] = true 41 sysTableIds[pkgcatalog.MO_COLUMNS_ID] = true 42 43 sysSharedDBNames = make(map[string]bool) 44 sysSharedDBNames[pkgcatalog.MO_CATALOG] = true 45 sysSharedDBNames[metric.MetricDBConst] = true 46 sysSharedDBNames[motrace.SystemDBConst] = true 47 } 48 49 func isSysTable(name string) bool { 50 return sysTableNames[name] 51 } 52 53 func isSysTableId(id uint64) bool { 54 return sysTableIds[id] 55 } 56 57 func isSysSharedDB(name string) bool { 58 return sysSharedDBNames[name] 59 } 60 61 func buildDB(db *txnDB) handle.Database { 62 if db.entry.IsSystemDB() { 63 return newSysDB(db) 64 } 65 return newDatabase(db) 66 } 67 68 type txnSysDB struct { 69 *txnDatabase 70 } 71 72 func newSysDB(db *txnDB) *txnSysDB { 73 sysDB := &txnSysDB{ 74 txnDatabase: newDatabase(db), 75 } 76 return sysDB 77 } 78 79 func (db *txnSysDB) DropRelationByName(name string) (rel handle.Relation, err error) { 80 if isSysTable(name) { 81 err = moerr.NewInternalErrorNoCtx("drop relation %s is not permitted", name) 82 return 83 } 84 return db.txnDatabase.DropRelationByName(name) 85 } 86 87 func (db *txnSysDB) DropRelationByID(id uint64) (rel handle.Relation, err error) { 88 if isSysTableId(id) { 89 err = moerr.NewInternalErrorNoCtx("drop relation %d is not permitted", id) 90 return 91 } 92 return db.txnDatabase.DropRelationByID(id) 93 } 94 95 func (db *txnSysDB) TruncateByName(name string) (rel handle.Relation, err error) { 96 if isSysTable(name) { 97 err = moerr.NewInternalErrorNoCtx("truncate relation %s is not permitted", name) 98 return 99 } 100 return db.txnDatabase.TruncateByName(name) 101 } 102 103 func (db *txnSysDB) TruncateWithID(name string, newTableId uint64) (rel handle.Relation, err error) { 104 if isSysTable(name) { 105 err = moerr.NewInternalErrorNoCtx("truncate relation %s is not permitted", name) 106 return 107 } 108 return db.txnDatabase.TruncateWithID(name, newTableId) 109 } 110 111 func (db *txnSysDB) TruncateByID(id uint64, newTableId uint64) (rel handle.Relation, err error) { 112 if isSysTableId(id) { 113 err = moerr.NewInternalErrorNoCtx("truncate relation %d is not permitted", id) 114 return 115 } 116 return db.txnDatabase.TruncateByID(id, newTableId) 117 } 118 119 func (db *txnSysDB) IsSubscription() bool { 120 return db.txnDatabase.IsSubscription() 121 } 122 123 func (db *txnSysDB) GetCreateSql() string { 124 return db.txnDatabase.GetCreateSql() 125 }