github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/perfschema/perfschema.go (about) 1 // Copyright 2016 PingCAP, 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 "reflect" 18 19 "github.com/insionng/yougam/libraries/juju/errors" 20 "github.com/insionng/yougam/libraries/pingcap/tidb/kv" 21 "github.com/insionng/yougam/libraries/pingcap/tidb/model" 22 "github.com/insionng/yougam/libraries/pingcap/tidb/table" 23 "github.com/insionng/yougam/libraries/pingcap/tidb/terror" 24 ) 25 26 var ( 27 errInvalidPerfSchemaTable = terror.ClassPerfSchema.New(codeInvalidPerfSchemaTable, "invalid perfschema table") 28 errInvalidTimerFlag = terror.ClassPerfSchema.New(codeInvalidTimerFlag, "invalid timer flag") 29 ) 30 31 // StatementInstrument defines the methods for statement instrumentation points 32 type StatementInstrument interface { 33 RegisterStatement(category, name string, elem interface{}) 34 35 StartStatement(sql string, connID uint64, callerName EnumCallerName, elem interface{}) *StatementState 36 37 EndStatement(state *StatementState) 38 } 39 40 // PerfSchema defines the methods to be invoked by the executor 41 type PerfSchema interface { 42 43 // For statement instrumentation only. 44 StatementInstrument 45 46 // GetDBMeta returns db info for PerformanceSchema. 47 GetDBMeta() *model.DBInfo 48 // GetTable returns table instance for name. 49 GetTable(name string) (table.Table, bool) 50 } 51 52 type perfSchema struct { 53 store kv.Storage 54 dbInfo *model.DBInfo 55 tables map[string]*model.TableInfo 56 mTables map[string]table.Table // Memory tables for perfSchema 57 stmtHandles []int64 58 stmtInfos map[reflect.Type]*statementInfo 59 } 60 61 var _ PerfSchema = (*perfSchema)(nil) 62 63 // NewPerfHandle creates a new perfSchema on store. 64 func NewPerfHandle() (PerfSchema, error) { 65 schema := &perfSchema{} 66 err := schema.initialize() 67 if err != nil { 68 return nil, errors.Trace(err) 69 } 70 schema.registerStatements() 71 return schema, nil 72 } 73 74 // perfschema error codes. 75 const ( 76 codeInvalidPerfSchemaTable terror.ErrCode = 1 77 codeInvalidTimerFlag = 2 78 )