forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
162 lines (143 loc) · 4.13 KB
/
schema.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package schema
// Yes, this sucks. It's a tiny tiny package that needs to be on its own
// It contains a data structure that's shared between sqlparser & tabletserver
import (
"github.com/youtube/vitess/go/cistring"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/sync2"
querypb "github.com/youtube/vitess/go/vt/proto/query"
)
// Table types
const (
NoType = iota
Sequence
)
// TypeNames allows to fetch a the type name for a table.
// Count must match the number of table types.
var TypeNames = []string{
"none",
"sequence",
}
// TableColumn contains info about a table's column.
type TableColumn struct {
Name cistring.CIString
Type querypb.Type
IsAuto bool
Default sqltypes.Value
}
// Table contains info about a table.
type Table struct {
Name string
Columns []TableColumn
Indexes []*Index
PKColumns []int
Type int
// These vars can be accessed concurrently.
TableRows sync2.AtomicInt64
DataLength sync2.AtomicInt64
IndexLength sync2.AtomicInt64
DataFree sync2.AtomicInt64
}
// NewTable creates a new Table.
func NewTable(name string) *Table {
return &Table{
Name: name,
}
}
// AddColumn adds a column to the Table.
func (ta *Table) AddColumn(name string, columnType querypb.Type, defval sqltypes.Value, extra string) {
index := len(ta.Columns)
ta.Columns = append(ta.Columns, TableColumn{Name: cistring.New(name)})
ta.Columns[index].Type = columnType
if extra == "auto_increment" {
ta.Columns[index].IsAuto = true
// Ignore default value, if any
return
}
if defval.IsNull() {
return
}
// Schema values are trusted.
ta.Columns[index].Default = sqltypes.MakeTrusted(ta.Columns[index].Type, defval.Raw())
}
// FindColumn finds a column in the table. It returns the index if found.
// Otherwise, it returns -1.
func (ta *Table) FindColumn(name string) int {
ciName := cistring.New(name)
for i, col := range ta.Columns {
if col.Name.Equal(ciName) {
return i
}
}
return -1
}
// GetPKColumn returns the pk column specified by the index.
func (ta *Table) GetPKColumn(index int) *TableColumn {
return &ta.Columns[ta.PKColumns[index]]
}
// AddIndex adds an index to the table.
func (ta *Table) AddIndex(name string) (index *Index) {
index = NewIndex(name)
ta.Indexes = append(ta.Indexes, index)
return index
}
// SetMysqlStats receives the values found in the mysql information_schema.tables table
func (ta *Table) SetMysqlStats(tr, dl, il, df sqltypes.Value) {
v, _ := tr.ParseInt64()
ta.TableRows.Set(v)
v, _ = dl.ParseInt64()
ta.DataLength.Set(v)
v, _ = il.ParseInt64()
ta.IndexLength.Set(v)
v, _ = df.ParseInt64()
ta.DataFree.Set(v)
}
// Index contains info about a table index.
type Index struct {
Name cistring.CIString
// Columns are the columns comprising the index.
Columns []cistring.CIString
// Cardinality[i] is the number of distinct values of Columns[i] in the
// table.
Cardinality []uint64
// DataColumns are the primary-key columns for secondary indices and
// all the columns for the primary-key index.
DataColumns []cistring.CIString
}
// NewIndex creates a new Index.
func NewIndex(name string) *Index {
return &Index{Name: cistring.New(name)}
}
// AddColumn adds a column to the index.
func (idx *Index) AddColumn(name string, cardinality uint64) {
idx.Columns = append(idx.Columns, cistring.New(name))
if cardinality == 0 {
cardinality = uint64(len(idx.Cardinality) + 1)
}
idx.Cardinality = append(idx.Cardinality, cardinality)
}
// FindColumn finds a column in the index. It returns the index if found.
// Otherwise, it returns -1.
func (idx *Index) FindColumn(name string) int {
ciName := cistring.New(name)
for i, colName := range idx.Columns {
if colName.Equal(ciName) {
return i
}
}
return -1
}
// FindDataColumn finds a data column in the index. It returns the index if found.
// Otherwise, it returns -1.
func (idx *Index) FindDataColumn(name string) int {
ciName := cistring.New(name)
for i, colName := range idx.DataColumns {
if colName.Equal(ciName) {
return i
}
}
return -1
}