-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
schema.go
134 lines (108 loc) · 3.33 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
/*
Copyright 2019 The Vitess Authors.
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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package schema
import (
"sync"
"time"
"vitess.io/vitess/go/vt/sqlparser"
querypb "vitess.io/vitess/go/vt/proto/query"
)
// Table types
const (
NoType = iota
Sequence
Message
)
// TypeNames allows to fetch a the type name for a table.
// Count must match the number of table types.
var TypeNames = []string{
"none",
"sequence",
"message",
}
// Table contains info about a table.
type Table struct {
Name sqlparser.TableIdent
Fields []*querypb.Field
PKColumns []int
Type int
// SequenceInfo contains info for sequence tables.
SequenceInfo *SequenceInfo
// MessageInfo contains info for message tables.
MessageInfo *MessageInfo
FileSize uint64
AllocatedSize uint64
}
// SequenceInfo contains info specific to sequence tabels.
// It must be locked before accessing the values inside.
// If CurVal==LastVal, we have to cache new values.
// When the schema is first loaded, the values are all 0,
// which will trigger caching on first use.
type SequenceInfo struct {
sync.Mutex
NextVal int64
LastVal int64
}
// MessageInfo contains info specific to message tables.
type MessageInfo struct {
// Fields stores the field info to be
// returned for subscribers.
Fields []*querypb.Field
// AckWaitDuration specifies how long to wait after
// the message was first sent. The back-off doubles
// every attempt.
AckWaitDuration time.Duration
// PurgeAfterDuration specifies the time after which
// a successfully acked message can be deleted.
PurgeAfterDuration time.Duration
// BatchSize specifies the max number of events to
// send per response.
BatchSize int
// CacheSize specifies the number of messages to keep
// in cache. Anything that cannot fit in the cache
// is sent as best effort.
CacheSize int
// PollInterval specifies the polling frequency to
// look for messages to be sent.
PollInterval time.Duration
// MinBackoff specifies the shortest duration message manager
// should wait before rescheduling a message
MinBackoff time.Duration
// MaxBackoff specifies the longest duration message manager
// should wait before rescheduling a message
MaxBackoff time.Duration
}
// NewTable creates a new Table.
func NewTable(name string) *Table {
return &Table{
Name: sqlparser.NewTableIdent(name),
}
}
// FindColumn finds a column in the table. It returns the index if found.
// Otherwise, it returns -1.
func (ta *Table) FindColumn(name sqlparser.ColIdent) int {
for i, col := range ta.Fields {
if name.EqualString(col.Name) {
return i
}
}
return -1
}
// GetPKColumn returns the pk column specified by the index.
func (ta *Table) GetPKColumn(index int) *querypb.Field {
return ta.Fields[ta.PKColumns[index]]
}
// HasPrimary returns true if the table has a primary key.
func (ta *Table) HasPrimary() bool {
return len(ta.PKColumns) != 0
}