github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/memoryengine/operation_handler.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 "encoding" 20 "fmt" 21 22 "github.com/matrixorigin/matrixone/pkg/pb/metadata" 23 "github.com/matrixorigin/matrixone/pkg/pb/txn" 24 ) 25 26 type OperationHandler interface { 27 HandleOpenDatabase( 28 ctx context.Context, 29 meta txn.TxnMeta, 30 req *OpenDatabaseReq, 31 resp *OpenDatabaseResp, 32 ) error 33 34 HandleGetDatabases( 35 ctx context.Context, 36 meta txn.TxnMeta, 37 req *GetDatabasesReq, 38 resp *GetDatabasesResp, 39 ) error 40 41 HandleOpenRelation( 42 ctx context.Context, 43 meta txn.TxnMeta, 44 req *OpenRelationReq, 45 resp *OpenRelationResp, 46 ) error 47 48 HandleGetRelations( 49 ctx context.Context, 50 meta txn.TxnMeta, 51 req *GetRelationsReq, 52 resp *GetRelationsResp, 53 ) error 54 55 HandleGetPrimaryKeys( 56 ctx context.Context, 57 meta txn.TxnMeta, 58 req *GetPrimaryKeysReq, 59 resp *GetPrimaryKeysResp, 60 ) error 61 62 HandleGetTableColumns( 63 ctx context.Context, 64 meta txn.TxnMeta, 65 req *GetTableColumnsReq, 66 resp *GetTableColumnsResp, 67 ) error 68 69 HandleGetTableDefs( 70 ctx context.Context, 71 meta txn.TxnMeta, 72 req *GetTableDefsReq, 73 resp *GetTableDefsResp, 74 ) error 75 76 HandleGetHiddenKeys( 77 ctx context.Context, 78 meta txn.TxnMeta, 79 req *GetHiddenKeysReq, 80 resp *GetHiddenKeysResp, 81 ) error 82 83 HandleNewTableIter( 84 ctx context.Context, 85 meta txn.TxnMeta, 86 req *NewTableIterReq, 87 resp *NewTableIterResp, 88 ) error 89 90 HandleRead( 91 ctx context.Context, 92 meta txn.TxnMeta, 93 req *ReadReq, 94 resp *ReadResp, 95 ) error 96 97 HandleCloseTableIter( 98 ctx context.Context, 99 meta txn.TxnMeta, 100 req *CloseTableIterReq, 101 resp *CloseTableIterResp, 102 ) error 103 104 HandleCreateDatabase( 105 ctx context.Context, 106 meta txn.TxnMeta, 107 req *CreateDatabaseReq, 108 resp *CreateDatabaseResp, 109 ) error 110 111 HandleDeleteDatabase( 112 ctx context.Context, 113 meta txn.TxnMeta, 114 req *DeleteDatabaseReq, 115 resp *DeleteDatabaseResp, 116 ) error 117 118 HandleCreateRelation( 119 ctx context.Context, 120 meta txn.TxnMeta, 121 req *CreateRelationReq, 122 resp *CreateRelationResp, 123 ) error 124 125 HandleDeleteRelation( 126 ctx context.Context, 127 meta txn.TxnMeta, 128 req *DeleteRelationReq, 129 resp *DeleteRelationResp, 130 ) error 131 132 HandleTruncateRelation( 133 ctx context.Context, 134 meta txn.TxnMeta, 135 req *TruncateRelationReq, 136 resp *TruncateRelationResp, 137 ) error 138 139 HandleAddTableDef( 140 ctx context.Context, 141 meta txn.TxnMeta, 142 req *AddTableDefReq, 143 resp *AddTableDefResp, 144 ) error 145 146 HandleDelTableDef( 147 ctx context.Context, 148 meta txn.TxnMeta, 149 req *DelTableDefReq, 150 resp *DelTableDefResp, 151 ) error 152 153 HandleDelete( 154 ctx context.Context, 155 meta txn.TxnMeta, 156 req *DeleteReq, 157 resp *DeleteResp, 158 ) error 159 160 HandleUpdate( 161 ctx context.Context, 162 meta txn.TxnMeta, 163 req *UpdateReq, 164 resp *UpdateResp, 165 ) error 166 167 HandleWrite( 168 ctx context.Context, 169 meta txn.TxnMeta, 170 req *WriteReq, 171 resp *WriteResp, 172 ) error 173 174 HandleTableStats( 175 ctx context.Context, 176 meta txn.TxnMeta, 177 req *TableStatsReq, 178 resp *TableStatsResp, 179 ) error 180 } 181 182 type OperationHandlerProvider interface { 183 GetOperationHandler(shard Shard) (OperationHandler, txn.TxnMeta) 184 } 185 186 func handle( 187 ctx context.Context, 188 handler OperationHandler, 189 meta txn.TxnMeta, 190 _ metadata.TNShard, 191 op uint32, 192 req encoding.BinaryMarshaler, 193 resp encoding.BinaryUnmarshaler, 194 ) ( 195 err error, 196 ) { 197 198 switch op { 199 200 case OpCreateDatabase: 201 response := resp.(*CreateDatabaseResp) 202 request := req.(*CreateDatabaseReq) 203 err = handler.HandleCreateDatabase(ctx, meta, request, response) 204 205 case OpOpenDatabase: 206 response := resp.(*OpenDatabaseResp) 207 request := req.(*OpenDatabaseReq) 208 err = handler.HandleOpenDatabase(ctx, meta, request, response) 209 210 case OpGetDatabases: 211 response := resp.(*GetDatabasesResp) 212 request := req.(*GetDatabasesReq) 213 err = handler.HandleGetDatabases(ctx, meta, request, response) 214 215 case OpDeleteDatabase: 216 response := resp.(*DeleteDatabaseResp) 217 request := req.(*DeleteDatabaseReq) 218 err = handler.HandleDeleteDatabase(ctx, meta, request, response) 219 220 case OpCreateRelation: 221 response := resp.(*CreateRelationResp) 222 request := req.(*CreateRelationReq) 223 err = handler.HandleCreateRelation(ctx, meta, request, response) 224 225 case OpDeleteRelation: 226 response := resp.(*DeleteRelationResp) 227 request := req.(*DeleteRelationReq) 228 err = handler.HandleDeleteRelation(ctx, meta, request, response) 229 230 case OpTruncateRelation: 231 response := resp.(*TruncateRelationResp) 232 request := req.(*TruncateRelationReq) 233 handler.HandleTruncateRelation(ctx, meta, request, response) 234 235 case OpOpenRelation: 236 response := resp.(*OpenRelationResp) 237 request := req.(*OpenRelationReq) 238 err = handler.HandleOpenRelation(ctx, meta, request, response) 239 240 case OpGetRelations: 241 response := resp.(*GetRelationsResp) 242 request := req.(*GetRelationsReq) 243 err = handler.HandleGetRelations(ctx, meta, request, response) 244 245 case OpAddTableDef: 246 response := resp.(*AddTableDefResp) 247 request := req.(*AddTableDefReq) 248 err = handler.HandleAddTableDef(ctx, meta, request, response) 249 250 case OpDelTableDef: 251 response := resp.(*DelTableDefResp) 252 request := req.(*DelTableDefReq) 253 err = handler.HandleDelTableDef(ctx, meta, request, response) 254 255 case OpDelete: 256 response := resp.(*DeleteResp) 257 request := req.(*DeleteReq) 258 err = handler.HandleDelete(ctx, meta, request, response) 259 260 case OpGetPrimaryKeys: 261 response := resp.(*GetPrimaryKeysResp) 262 request := req.(*GetPrimaryKeysReq) 263 err = handler.HandleGetPrimaryKeys(ctx, meta, request, response) 264 265 case OpGetTableColumns: 266 response := resp.(*GetTableColumnsResp) 267 request := req.(*GetTableColumnsReq) 268 err = handler.HandleGetTableColumns(ctx, meta, request, response) 269 270 case OpGetTableDefs: 271 response := resp.(*GetTableDefsResp) 272 request := req.(*GetTableDefsReq) 273 err = handler.HandleGetTableDefs(ctx, meta, request, response) 274 275 case OpGetHiddenKeys: 276 response := resp.(*GetHiddenKeysResp) 277 request := req.(*GetHiddenKeysReq) 278 err = handler.HandleGetHiddenKeys(ctx, meta, request, response) 279 280 case OpUpdate: 281 response := resp.(*UpdateResp) 282 request := req.(*UpdateReq) 283 err = handler.HandleUpdate(ctx, meta, request, response) 284 285 case OpWrite, OpPreCommit: 286 response := resp.(*WriteResp) 287 request := req.(*WriteReq) 288 err = handler.HandleWrite(ctx, meta, request, response) 289 290 case OpNewTableIter: 291 response := resp.(*NewTableIterResp) 292 request := req.(*NewTableIterReq) 293 err = handler.HandleNewTableIter(ctx, meta, request, response) 294 295 case OpRead: 296 response := resp.(*ReadResp) 297 request := req.(*ReadReq) 298 err = handler.HandleRead(ctx, meta, request, response) 299 300 case OpCloseTableIter: 301 response := resp.(*CloseTableIterResp) 302 request := req.(*CloseTableIterReq) 303 err = handler.HandleCloseTableIter(ctx, meta, request, response) 304 305 case OpTableStats: 306 response := resp.(*TableStatsResp) 307 request := req.(*TableStatsReq) 308 err = handler.HandleTableStats(ctx, meta, request, response) 309 310 default: 311 panic(fmt.Sprintf("unknown operation %v", op)) 312 } 313 314 return 315 }