github.com/matrixorigin/matrixone@v0.7.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, ts types.TS, info []byte) txnif.AsyncTxn { 31 txn := new(mockTxn) 32 txn.Txn = txnbase.NewTxn(mgr, store, id, ts, info) 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(nil) 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 mockSegIt 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 *mockSegIt) GetError() error { return nil } 128 func (it *mockSegIt) Valid() bool { return false } 129 func (it *mockSegIt) Next() {} 130 func (it *mockSegIt) Close() error { return nil } 131 func (it *mockSegIt) GetSegment() handle.Segment { 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 *mockTableHandle) MakeSegmentIt() (it handle.SegmentIt) { 198 return new(mockSegIt) 199 } 200 201 func (h *mockTableHandle) String() string { 202 return h.entry.String() 203 } 204 205 type mockTxn struct { 206 *txnbase.Txn 207 catalog *Catalog 208 } 209 210 func (txn *mockTxn) CreateDatabase(name, createSql string) (handle.Database, error) { 211 entry, err := txn.catalog.CreateDBEntry(name, createSql, txn) 212 if err != nil { 213 return nil, err 214 } 215 txn.Store.AddTxnEntry(0, entry) 216 h := newMockDBHandle(txn.catalog, txn, entry) 217 return h, nil 218 } 219 220 func (txn *mockTxn) CreateDatabaseWithID(name, createSql string, id uint64) (handle.Database, error) { 221 entry, err := txn.catalog.CreateDBEntryWithID(name, createSql, id, txn) 222 if err != nil { 223 return nil, err 224 } 225 txn.Store.AddTxnEntry(0, entry) 226 h := newMockDBHandle(txn.catalog, txn, entry) 227 return h, nil 228 } 229 230 func (txn *mockTxn) CreateDatabaseByDef(def any) (handle.Database, error) { 231 panic(moerr.NewNYINoCtx("CreateDatabaseByID is not implemented yet")) 232 } 233 234 func (txn *mockTxn) GetDatabase(name string) (handle.Database, error) { 235 entry, err := txn.catalog.TxnGetDBEntryByName(name, txn) 236 if err != nil { 237 return nil, err 238 } 239 return newMockDBHandle(txn.catalog, txn, entry), nil 240 } 241 242 func (txn *mockTxn) GetDatabaseByID(id uint64) (handle.Database, error) { 243 entry, err := txn.catalog.TxnGetDBEntryByID(id, txn) 244 if err != nil { 245 return nil, err 246 } 247 return newMockDBHandle(txn.catalog, txn, entry), nil 248 } 249 250 func (txn *mockTxn) DropDatabase(name string) (handle.Database, error) { 251 _, entry, err := txn.catalog.DropDBEntry(name, txn) 252 if err != nil { 253 return nil, err 254 } 255 txn.Store.AddTxnEntry(0, entry) 256 return newMockDBHandle(txn.catalog, txn, entry), nil 257 } 258 259 func (txn *mockTxn) DropDatabaseByID(id uint64) (handle.Database, error) { 260 _, entry, err := txn.catalog.DropDBEntryByID(id, txn) 261 if err != nil { 262 return nil, err 263 } 264 txn.Store.AddTxnEntry(0, entry) 265 return newMockDBHandle(txn.catalog, txn, entry), nil 266 } 267 268 func MockBatch(schema *Schema, rows int) *containers.Batch { 269 if schema.HasSortKey() { 270 sortKey := schema.GetSingleSortKey() 271 return containers.MockBatchWithAttrs(schema.Types(), schema.Attrs(), schema.Nullables(), rows, sortKey.Idx, nil) 272 } else { 273 return containers.MockBatchWithAttrs(schema.Types(), schema.Attrs(), schema.Nullables(), rows, schema.PhyAddrKey.Idx, nil) 274 } 275 }