github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/schemareplicant/perfschema/init.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package perfschema 15 16 import ( 17 "fmt" 18 "sync" 19 20 "github.com/whtcorpsinc/BerolinaSQL" 21 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 22 "github.com/whtcorpsinc/BerolinaSQL/ast" 23 "github.com/whtcorpsinc/BerolinaSQL/perceptron" 24 "github.com/whtcorpsinc/milevadb/dbs" 25 "github.com/whtcorpsinc/milevadb/memex" 26 "github.com/whtcorpsinc/milevadb/schemareplicant" 27 "github.com/whtcorpsinc/milevadb/soliton" 28 "github.com/whtcorpsinc/milevadb/spacetime/autoid" 29 ) 30 31 var once sync.Once 32 33 // Init register the PERFORMANCE_SCHEMA virtual blocks. 34 // It should be init(), and the ideal usage should be: 35 // 36 // import _ "github.com/whtcorpsinc/milevadb/perfschema" 37 // 38 // This function depends on plan/embedded.init(), which initialize the memex.EvalAstExpr function. 39 // The initialize order is a problem if init() is used as the function name. 40 func Init() { 41 initOnce := func() { 42 p := BerolinaSQL.New() 43 tbls := make([]*perceptron.BlockInfo, 0) 44 dbID := autoid.PerformanceSchemaDBID 45 for _, allegrosql := range perfSchemaBlocks { 46 stmt, err := p.ParseOneStmt(allegrosql, "", "") 47 if err != nil { 48 panic(err) 49 } 50 spacetime, err := dbs.BuildBlockInfoFromAST(stmt.(*ast.CreateBlockStmt)) 51 if err != nil { 52 panic(err) 53 } 54 tbls = append(tbls, spacetime) 55 var ok bool 56 spacetime.ID, ok = blockIDMap[spacetime.Name.O] 57 if !ok { 58 panic(fmt.Sprintf("get performance_schema causet id failed, unknown system causet `%v`", spacetime.Name.O)) 59 } 60 for i, c := range spacetime.DeferredCausets { 61 c.ID = int64(i) + 1 62 } 63 } 64 dbInfo := &perceptron.DBInfo{ 65 ID: dbID, 66 Name: soliton.PerformanceSchemaName, 67 Charset: allegrosql.DefaultCharset, 68 DefCauslate: allegrosql.DefaultDefCauslationName, 69 Blocks: tbls, 70 } 71 schemareplicant.RegisterVirtualBlock(dbInfo, blockFromMeta) 72 } 73 if memex.EvalAstExpr != nil { 74 once.Do(initOnce) 75 } 76 }