github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/memoryengine/engine.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 "strings" 20 "time" 21 22 "github.com/matrixorigin/matrixone/pkg/clusterservice" 23 "github.com/matrixorigin/matrixone/pkg/common/moerr" 24 "github.com/matrixorigin/matrixone/pkg/pb/metadata" 25 pb "github.com/matrixorigin/matrixone/pkg/pb/statsinfo" 26 "github.com/matrixorigin/matrixone/pkg/pb/timestamp" 27 "github.com/matrixorigin/matrixone/pkg/sql/plan" 28 "github.com/matrixorigin/matrixone/pkg/txn/client" 29 "github.com/matrixorigin/matrixone/pkg/vm/engine" 30 ) 31 32 // Engine is an engine.Engine impl 33 type Engine struct { 34 shardPolicy ShardPolicy 35 idGenerator IDGenerator 36 cluster clusterservice.MOCluster 37 } 38 39 func New( 40 ctx context.Context, 41 shardPolicy ShardPolicy, 42 idGenerator IDGenerator, 43 cluster clusterservice.MOCluster, 44 ) *Engine { 45 _ = ctx 46 47 engine := &Engine{ 48 shardPolicy: shardPolicy, 49 idGenerator: idGenerator, 50 cluster: cluster, 51 } 52 53 return engine 54 } 55 56 var _ engine.Engine = new(Engine) 57 58 func (e *Engine) New(_ context.Context, _ client.TxnOperator) error { 59 return nil 60 } 61 62 func (e *Engine) Commit(_ context.Context, _ client.TxnOperator) error { 63 return nil 64 } 65 66 func (e *Engine) Rollback(_ context.Context, _ client.TxnOperator) error { 67 return nil 68 } 69 70 func (e *Engine) NewBlockReader(_ context.Context, _ int, _ timestamp.Timestamp, 71 _ *plan.Expr, _ []byte, _ *plan.TableDef, _ any) ([]engine.Reader, error) { 72 return nil, nil 73 } 74 75 func (e *Engine) Create(ctx context.Context, dbName string, txnOperator client.TxnOperator) error { 76 77 id, err := e.idGenerator.NewID(ctx) 78 if err != nil { 79 return err 80 } 81 82 access, err := getAccessInfo(ctx) 83 if err != nil { 84 return err 85 } 86 _, err = DoTxnRequest[CreateDatabaseResp]( 87 ctx, 88 txnOperator, 89 false, 90 e.allShards, 91 OpCreateDatabase, 92 &CreateDatabaseReq{ 93 ID: id, 94 AccessInfo: access, 95 Name: dbName, 96 }, 97 ) 98 if err != nil { 99 return err 100 } 101 102 return nil 103 } 104 105 func (e *Engine) Database(ctx context.Context, dbName string, txnOperator client.TxnOperator) (engine.Database, error) { 106 access, err := getAccessInfo(ctx) 107 if err != nil { 108 return nil, err 109 } 110 resps, err := DoTxnRequest[OpenDatabaseResp]( 111 ctx, 112 txnOperator, 113 true, 114 e.anyShard, 115 OpOpenDatabase, 116 &OpenDatabaseReq{ 117 AccessInfo: access, 118 Name: dbName, 119 }, 120 ) 121 if err != nil { 122 return nil, err 123 } 124 125 resp := resps[0] 126 127 db := &Database{ 128 engine: e, 129 txnOperator: txnOperator, 130 id: resp.ID, 131 name: resp.Name, 132 typ: resp.DatTyp, 133 createSql: resp.CreateSql, 134 } 135 136 return db, nil 137 } 138 139 func (e *Engine) Databases(ctx context.Context, txnOperator client.TxnOperator) ([]string, error) { 140 access, err := getAccessInfo(ctx) 141 if err != nil { 142 return nil, err 143 } 144 resps, err := DoTxnRequest[GetDatabasesResp]( 145 ctx, 146 txnOperator, 147 true, 148 e.anyShard, 149 OpGetDatabases, 150 &GetDatabasesReq{ 151 AccessInfo: access, 152 }, 153 ) 154 if err != nil { 155 return nil, err 156 } 157 158 return resps[0].Names, nil 159 } 160 161 func (e *Engine) Delete(ctx context.Context, dbName string, txnOperator client.TxnOperator) error { 162 access, err := getAccessInfo(ctx) 163 if err != nil { 164 return err 165 } 166 _, err = DoTxnRequest[DeleteDatabaseResp]( 167 ctx, 168 txnOperator, 169 false, 170 e.allShards, 171 OpDeleteDatabase, 172 &DeleteDatabaseReq{ 173 AccessInfo: access, 174 Name: dbName, 175 }, 176 ) 177 if err != nil { 178 return err 179 } 180 181 return nil 182 } 183 184 func (e *Engine) Nodes(isInternal bool, tenant string, _ string, cnLabel map[string]string) (engine.Nodes, error) { 185 var nodes engine.Nodes 186 cluster := clusterservice.GetMOCluster() 187 var selector clusterservice.Selector 188 if isInternal || strings.ToLower(tenant) == "sys" { 189 selector = clusterservice.NewSelector() 190 } else { 191 selector = selector.SelectByLabel(cnLabel, clusterservice.EQ) 192 } 193 cluster.GetCNService(selector, 194 func(c metadata.CNService) bool { 195 nodes = append(nodes, engine.Node{ 196 Mcpu: 1, 197 Id: c.ServiceID, 198 Addr: c.PipelineServiceAddress, 199 }) 200 return true 201 }) 202 return nodes, nil 203 } 204 205 func (e *Engine) Hints() (h engine.Hints) { 206 h.CommitOrRollbackTimeout = time.Minute * 5 207 return 208 } 209 210 func (e *Engine) GetNameById(ctx context.Context, op client.TxnOperator, tableId uint64) (dbName string, tblName string, err error) { 211 return "", "", moerr.NewNYI(ctx, "interface GetNameById is not implemented") 212 } 213 214 func (e *Engine) GetRelationById(ctx context.Context, op client.TxnOperator, tableId uint64) (dbName string, tblName string, rel engine.Relation, err error) { 215 return "", "", nil, moerr.NewNYI(ctx, "interface GetRelationById is not implemented") 216 } 217 218 func (e *Engine) AllocateIDByKey(ctx context.Context, key string) (uint64, error) { 219 id, err := e.idGenerator.NewIDByKey(ctx, key) 220 return uint64(id), err 221 } 222 223 func (e *Engine) TryToSubscribeTable(ctx context.Context, dbID, tbID uint64) error { 224 return nil 225 } 226 227 func (e *Engine) UnsubscribeTable(ctx context.Context, dbID, tbID uint64) error { 228 return nil 229 } 230 231 func (e *Engine) Stats(ctx context.Context, key pb.StatsInfoKey, sync bool) *pb.StatsInfo { 232 return nil 233 } 234 235 func getTNServices(cluster clusterservice.MOCluster) []metadata.TNService { 236 var values []metadata.TNService 237 cluster.GetTNService(clusterservice.NewSelector(), 238 func(d metadata.TNService) bool { 239 values = append(values, d) 240 return true 241 }) 242 return values 243 }