forked from pingcap/br
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
252 lines (208 loc) · 6.58 KB
/
session.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2019 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 kv
import (
"context"
"fmt"
"strconv"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
)
// Pair is a pair of key and value.
type Pair struct {
// Key is the key of the KV pair
Key []byte
// Val is the value of the KV pair
Val []byte
// IsDelete represents whether we should remove this KV pair.
IsDelete bool
}
// invalidIterator is a trimmed down Iterator type which is invalid.
type invalidIterator struct {
kv.Iterator
}
// TableHasAutoRowID return whether table has auto generated row id.
func TableHasAutoRowID(info *model.TableInfo) bool {
return !info.PKIsHandle && !info.IsCommonHandle
}
// Valid implements the kv.Iterator interface.
func (*invalidIterator) Valid() bool {
return false
}
// Close implements the kv.Iterator interface.
func (*invalidIterator) Close() {
}
type kvMemBuf struct {
kv.MemBuffer
kvPairs []Pair
size int
}
func (mb *kvMemBuf) Set(k kv.Key, v []byte) error {
mb.kvPairs = append(mb.kvPairs, Pair{
Key: k.Clone(),
Val: append([]byte{}, v...),
})
mb.size += len(k) + len(v)
return nil
}
func (mb *kvMemBuf) SetWithFlags(k kv.Key, v []byte, ops ...kv.FlagsOp) error {
return mb.Set(k, v)
}
func (mb *kvMemBuf) Delete(k kv.Key) error {
mb.kvPairs = append(mb.kvPairs, Pair{
Key: k.Clone(),
Val: []byte{},
IsDelete: true,
})
mb.size += len(k)
return nil
}
func (mb *kvMemBuf) DeleteWithFlags(k kv.Key, ops ...kv.FlagsOp) error {
return mb.Delete(k)
}
// Release publish all modifications in the latest staging buffer to upper level.
func (mb *kvMemBuf) Release(h kv.StagingHandle) {
}
func (mb *kvMemBuf) Staging() kv.StagingHandle {
return 0
}
// Cleanup cleanup the resources referenced by the StagingHandle.
// If the changes are not published by `Release`, they will be discarded.
func (mb *kvMemBuf) Cleanup(h kv.StagingHandle) {}
// Size returns sum of keys and values length.
func (mb *kvMemBuf) Size() int {
return mb.size
}
// Len returns the number of entries in the DB.
func (t *transaction) Len() int {
return t.GetMemBuffer().Len()
}
type kvUnionStore struct {
kvMemBuf
}
func (s *kvUnionStore) GetMemBuffer() kv.MemBuffer {
return &s.kvMemBuf
}
func (s *kvUnionStore) GetIndexName(tableID, indexID int64) string {
panic("Unsupported Operation")
}
func (s *kvUnionStore) CacheIndexName(tableID, indexID int64, name string) {
}
func (s *kvUnionStore) CacheTableInfo(id int64, info *model.TableInfo) {
}
// transaction is a trimmed down Transaction type which only supports adding a
// new KV pair.
type transaction struct {
kv.Transaction
kvUnionStore
}
func (t *transaction) GetMemBuffer() kv.MemBuffer {
return &t.kvUnionStore.kvMemBuf
}
func (t *transaction) Discard() {
// do nothing
}
func (t *transaction) Flush() (int, error) {
// do nothing
return 0, nil
}
// Reset implements the kv.MemBuffer interface.
func (t *transaction) Reset() {}
// Get implements the kv.Retriever interface.
func (t *transaction) Get(ctx context.Context, key kv.Key) ([]byte, error) {
return nil, kv.ErrNotExist
}
// Iter implements the kv.Retriever interface.
func (t *transaction) Iter(k kv.Key, upperBound kv.Key) (kv.Iterator, error) {
return &invalidIterator{}, nil
}
// Set implements the kv.Mutator interface.
func (t *transaction) Set(k kv.Key, v []byte) error {
return t.kvMemBuf.Set(k, v)
}
// Delete implements the kv.Mutator interface.
func (t *transaction) Delete(k kv.Key) error {
return t.kvMemBuf.Delete(k)
}
// GetTableInfo implements the kv.Transaction interface.
func (t *transaction) GetTableInfo(id int64) *model.TableInfo {
return nil
}
// CacheTableInfo implements the kv.Transaction interface.
func (t *transaction) CacheTableInfo(id int64, info *model.TableInfo) {
}
// session is a trimmed down Session type which only wraps our own trimmed-down
// transaction type and provides the session variables to the TiDB library
// optimized for Lightning.
type session struct {
sessionctx.Context
txn transaction
vars *variable.SessionVars
// currently, we only set `CommonAddRecordCtx`
values map[fmt.Stringer]interface{}
}
// SessionOptions is the initial configuration of the session.
type SessionOptions struct {
SQLMode mysql.SQLMode
Timestamp int64
RowFormatVersion string
}
func newSession(options *SessionOptions) *session {
sqlMode := options.SQLMode
vars := variable.NewSessionVars()
vars.SkipUTF8Check = true
vars.StmtCtx.InInsertStmt = true
vars.StmtCtx.BatchCheck = true
vars.StmtCtx.BadNullAsWarning = !sqlMode.HasStrictMode()
vars.StmtCtx.TruncateAsWarning = !sqlMode.HasStrictMode()
vars.StmtCtx.OverflowAsWarning = !sqlMode.HasStrictMode()
vars.StmtCtx.AllowInvalidDate = sqlMode.HasAllowInvalidDatesMode()
vars.StmtCtx.IgnoreZeroInDate = !sqlMode.HasStrictMode() || sqlMode.HasAllowInvalidDatesMode()
vars.StmtCtx.TimeZone = vars.Location()
_ = vars.SetSystemVar("timestamp", strconv.FormatInt(options.Timestamp, 10))
_ = vars.SetSystemVar(variable.TiDBRowFormatVersion, options.RowFormatVersion)
vars.TxnCtx = nil
s := &session{
vars: vars,
values: make(map[fmt.Stringer]interface{}, 1),
}
return s
}
func (se *session) takeKvPairs() ([]Pair, int) {
pairs := se.txn.kvMemBuf.kvPairs
size := se.txn.kvMemBuf.Size()
se.txn.kvMemBuf.kvPairs = make([]Pair, 0, len(pairs))
se.txn.kvMemBuf.size = 0
return pairs, size
}
// Txn implements the sessionctx.Context interface.
func (se *session) Txn(active bool) (kv.Transaction, error) {
return &se.txn, nil
}
// GetSessionVars implements the sessionctx.Context interface.
func (se *session) GetSessionVars() *variable.SessionVars {
return se.vars
}
// SetValue saves a value associated with this context for key.
func (se *session) SetValue(key fmt.Stringer, value interface{}) {
se.values[key] = value
}
// Value returns the value associated with this context for key.
func (se *session) Value(key fmt.Stringer) interface{} {
return se.values[key]
}
// StmtAddDirtyTableOP implements the sessionctx.Context interface.
func (se *session) StmtAddDirtyTableOP(op int, physicalID int64, handle kv.Handle) {}