-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump.go
135 lines (126 loc) · 4.38 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package statistics
import (
"github.com/juju/errors"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tipb/go-tipb"
)
// JSONTable is used for dumping statistics.
type JSONTable struct {
DatabaseName string `json:"database_name"`
TableName string `json:"table_name"`
Columns map[string]*jsonColumn `json:"columns"`
Indices map[string]*jsonColumn `json:"indices"`
Count int64 `json:"count"`
ModifyCount int64 `json:"modify_count"`
Version uint64 `json:"version"`
}
type jsonColumn struct {
Histogram *tipb.Histogram `json:"histogram"`
CMSketch *tipb.CMSketch `json:"cm_sketch"`
NullCount int64 `json:"null_count"`
TotColSize int64 `json:"tot_col_size"`
LastUpdateVersion uint64 `json:"last_update_version"`
}
func dumpJSONCol(hist *Histogram, CMSketch *CMSketch) *jsonColumn {
jsonCol := &jsonColumn{
Histogram: HistogramToProto(hist),
NullCount: hist.NullCount,
TotColSize: hist.TotColSize,
LastUpdateVersion: hist.LastUpdateVersion,
}
if CMSketch != nil {
jsonCol.CMSketch = CMSketchToProto(CMSketch)
}
return jsonCol
}
// DumpStatsToJSON dumps statistic to json.
func (h *Handle) DumpStatsToJSON(dbName string, tableInfo *model.TableInfo) (*JSONTable, error) {
tbl, err := h.tableStatsFromStorage(tableInfo, true)
if err != nil {
return nil, errors.Trace(err)
}
jsonTbl := &JSONTable{
DatabaseName: dbName,
TableName: tableInfo.Name.L,
Columns: make(map[string]*jsonColumn, len(tbl.Columns)),
Indices: make(map[string]*jsonColumn, len(tbl.Indices)),
Count: tbl.Count,
ModifyCount: tbl.ModifyCount,
Version: tbl.Version,
}
for _, col := range tbl.Columns {
hist, err := col.ConvertTo(new(stmtctx.StatementContext), types.NewFieldType(mysql.TypeBlob))
if err != nil {
return nil, errors.Trace(err)
}
jsonTbl.Columns[col.Info.Name.L] = dumpJSONCol(hist, col.CMSketch)
}
for _, idx := range tbl.Indices {
jsonTbl.Indices[idx.Info.Name.L] = dumpJSONCol(&idx.Histogram, idx.CMSketch)
}
return jsonTbl, nil
}
// LoadStatsFromJSON load statistic from json.
func (h *Handle) LoadStatsFromJSON(tableInfo *model.TableInfo, jsonTbl *JSONTable) (*Table, error) {
tbl := &Table{
TableID: tableInfo.ID,
Columns: make(map[int64]*Column, len(jsonTbl.Columns)),
Indices: make(map[int64]*Index, len(jsonTbl.Indices)),
Count: jsonTbl.Count,
Version: jsonTbl.Version,
ModifyCount: jsonTbl.ModifyCount,
}
for id, jsonIdx := range jsonTbl.Indices {
for _, idxInfo := range tableInfo.Indices {
if idxInfo.Name.L != id {
continue
}
hist := HistogramFromProto(jsonIdx.Histogram)
hist.ID, hist.NullCount, hist.LastUpdateVersion = idxInfo.ID, jsonIdx.NullCount, jsonIdx.LastUpdateVersion
idx := &Index{
Histogram: *hist,
CMSketch: CMSketchFromProto(jsonIdx.CMSketch),
Info: idxInfo,
}
tbl.Indices[idx.ID] = idx
}
}
for id, jsonCol := range jsonTbl.Columns {
for _, colInfo := range tableInfo.Columns {
if colInfo.Name.L != id {
continue
}
hist := HistogramFromProto(jsonCol.Histogram)
count := int64(hist.totalRowCount())
hist, err := hist.ConvertTo(new(stmtctx.StatementContext), &colInfo.FieldType)
if err != nil {
return nil, errors.Trace(err)
}
hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.TotColSize = colInfo.ID, jsonCol.NullCount, jsonCol.LastUpdateVersion, jsonCol.TotColSize
col := &Column{
Histogram: *hist,
CMSketch: CMSketchFromProto(jsonCol.CMSketch),
Info: colInfo,
Count: count,
isHandle: tableInfo.PKIsHandle && mysql.HasPriKeyFlag(colInfo.Flag),
}
tbl.Columns[col.ID] = col
}
}
return tbl, nil
}