forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbounded_tables.go
288 lines (258 loc) · 8.03 KB
/
bounded_tables.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2016 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 tables
import (
"sync/atomic"
"unsafe"
"github.com/juju/errors"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/util/types"
)
const (
// initialRecordID means initial record id, and we want to ensure our initial record id greater than a threshold.
initialRecordID int64 = 123
// invalidRecordID means invalid record id.
// In our case, a valid record will always greater than 0, so we use a
// macro to mark an invalid one.
invalidRecordID int64 = 0
)
type boundedItem struct {
handle int64
data []types.Datum
}
// BoundedTable implements table.Table interface.
type BoundedTable struct {
// cursor must be aligned in i386, or we will meet panic when calling atomic.AddInt64
// https://golang.org/src/sync/atomic/doc.go#L41
cursor int64
ID int64
Name model.CIStr
Columns []*table.Column
pkHandleCol *table.Column
recordPrefix kv.Key
alloc autoid.Allocator
meta *model.TableInfo
records []unsafe.Pointer
capacity int64
}
// BoundedTableFromMeta creates a Table instance from model.TableInfo.
func BoundedTableFromMeta(alloc autoid.Allocator, tblInfo *model.TableInfo, capacity int64) table.Table {
columns := make([]*table.Column, 0, len(tblInfo.Columns))
var pkHandleColumn *table.Column
for _, colInfo := range tblInfo.Columns {
col := table.ToColumn(colInfo)
columns = append(columns, col)
if col.IsPKHandleColumn(tblInfo) {
pkHandleColumn = col
}
}
t := newBoundedTable(tblInfo.ID, tblInfo.Name.O, columns, alloc, capacity)
t.pkHandleCol = pkHandleColumn
t.meta = tblInfo
return t
}
// newBoundedTable constructs a BoundedTable instance.
func newBoundedTable(tableID int64, tableName string, cols []*table.Column, alloc autoid.Allocator, capacity int64) *BoundedTable {
name := model.NewCIStr(tableName)
t := &BoundedTable{
ID: tableID,
Name: name,
alloc: alloc,
Columns: cols,
recordPrefix: tablecodec.GenTableRecordPrefix(tableID),
records: make([]unsafe.Pointer, capacity),
capacity: capacity,
cursor: 0,
}
return t
}
// newBoundedItem constructs a boundedItem instance.
func newBoundedItem(handle int64, data []types.Datum) *boundedItem {
i := &boundedItem{
handle: handle,
data: data,
}
return i
}
// Seek seeks the handle.
func (t *BoundedTable) Seek(ctx context.Context, handle int64) (int64, bool, error) {
result := (*boundedItem)(nil)
if handle < invalidRecordID {
// this is the first seek call.
result = (*boundedItem)(atomic.LoadPointer(&t.records[0]))
} else {
for i := int64(0); i < t.capacity; i++ {
record := (*boundedItem)(atomic.LoadPointer(&t.records[i]))
if record == nil {
break
}
if handle == record.handle {
result = record
break
}
}
}
if result == nil {
// handle not found.
return invalidRecordID, false, nil
}
if result.handle != invalidRecordID {
// this record is valid.
return result.handle, true, nil
}
// this record is invalid.
return invalidRecordID, false, nil
}
// Indices implements table.Table Indices interface.
func (t *BoundedTable) Indices() []table.Index {
return nil
}
// Meta implements table.Table Meta interface.
func (t *BoundedTable) Meta() *model.TableInfo {
return t.meta
}
// Cols implements table.Table Cols interface.
func (t *BoundedTable) Cols() []*table.Column {
return t.Columns
}
// WritableCols implements table.Table WritableCols interface.
func (t *BoundedTable) WritableCols() []*table.Column {
return t.Columns
}
// RecordPrefix implements table.Table RecordPrefix interface.
func (t *BoundedTable) RecordPrefix() kv.Key {
return t.recordPrefix
}
// IndexPrefix implements table.Table IndexPrefix interface.
func (t *BoundedTable) IndexPrefix() kv.Key {
return nil
}
// RecordKey implements table.Table RecordKey interface.
func (t *BoundedTable) RecordKey(h int64) kv.Key {
return tablecodec.EncodeRecordKey(t.recordPrefix, h)
}
// FirstKey implements table.Table FirstKey interface.
func (t *BoundedTable) FirstKey() kv.Key {
return t.RecordKey(0)
}
// Truncate drops all data in BoundedTable.
func (t *BoundedTable) Truncate() {
// just reset everything.
for i := int64(0); i < t.capacity; i++ {
atomic.StorePointer(&t.records[i], unsafe.Pointer(nil))
}
t.cursor = 0
}
// UpdateRecord implements table.Table UpdateRecord interface.
func (t *BoundedTable) UpdateRecord(ctx context.Context, h int64, oldData []types.Datum, newData []types.Datum, touched map[int]bool) error {
for i := int64(0); i < t.capacity; i++ {
record := (*boundedItem)(atomic.LoadPointer(&t.records[i]))
if record == nil {
// A nil record means consecutive nil records.
break
}
if record.handle == h {
newRec := newBoundedItem(h, newData)
atomic.StorePointer(&t.records[i], unsafe.Pointer(newRec))
break
}
}
// UPDATE always succeeds for BoundedTable.
return nil
}
// AddRecord implements table.Table AddRecord interface.
func (t *BoundedTable) AddRecord(ctx context.Context, r []types.Datum) (int64, error) {
var recordID int64
var err error
if t.pkHandleCol != nil {
recordID, err = r[t.pkHandleCol.Offset].ToInt64(ctx.GetSessionVars().StmtCtx)
if err != nil {
return invalidRecordID, errors.Trace(err)
}
} else {
recordID, err = t.alloc.Alloc(t.ID)
if err != nil {
return invalidRecordID, errors.Trace(err)
}
}
cursor := atomic.AddInt64(&t.cursor, 1) - 1
index := int64(cursor % t.capacity)
record := newBoundedItem(recordID+initialRecordID, r)
atomic.StorePointer(&t.records[index], unsafe.Pointer(record))
return recordID + initialRecordID, nil
}
// RowWithCols implements table.Table RowWithCols interface.
func (t *BoundedTable) RowWithCols(ctx context.Context, h int64, cols []*table.Column) ([]types.Datum, error) {
row := []types.Datum(nil)
for i := int64(0); i < t.capacity; i++ {
record := (*boundedItem)(atomic.LoadPointer(&t.records[i]))
if record == nil {
// A nil record means consecutive nil records.
break
}
if record.handle == h {
row = record.data
break
}
}
if row == nil {
return nil, table.ErrRowNotFound
}
v := make([]types.Datum, len(cols))
for i, col := range cols {
if col == nil {
continue
}
v[i] = row[col.Offset]
}
return v, nil
}
// Row implements table.Table Row interface.
func (t *BoundedTable) Row(ctx context.Context, h int64) ([]types.Datum, error) {
r, err := t.RowWithCols(nil, h, t.Cols())
if err != nil {
return nil, errors.Trace(err)
}
return r, nil
}
// RemoveRecord implements table.Table RemoveRecord interface.
func (t *BoundedTable) RemoveRecord(ctx context.Context, h int64, r []types.Datum) error {
// not supported, BoundedTable is TRUNCATE only
return table.ErrUnsupportedOp
}
// AllocAutoID implements table.Table AllocAutoID interface.
func (t *BoundedTable) AllocAutoID() (int64, error) {
recordID, err := t.alloc.Alloc(t.ID)
if err != nil {
return invalidRecordID, errors.Trace(err)
}
return recordID + initialRecordID, nil
}
// Allocator implements table.Table Allocator interface.
func (t *BoundedTable) Allocator() autoid.Allocator {
return t.alloc
}
// RebaseAutoID implements table.Table RebaseAutoID interface.
func (t *BoundedTable) RebaseAutoID(newBase int64, isSetStep bool) error {
return t.alloc.Rebase(t.ID, newBase, isSetStep)
}
// IterRecords implements table.Table IterRecords interface.
func (t *BoundedTable) IterRecords(ctx context.Context, startKey kv.Key, cols []*table.Column,
fn table.RecordIterFunc) error {
return nil
}