forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_tables.go
274 lines (240 loc) · 6.97 KB
/
memory_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
// 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"
log "github.com/Sirupsen/logrus"
"github.com/juju/errors"
"github.com/petar/GoLLRB/llrb"
"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"
)
type itemKey int64
type itemPair struct {
handle itemKey
data []types.Datum
}
func (r *itemPair) Less(item llrb.Item) bool {
switch x := item.(type) {
case itemKey:
return r.handle < x
case *itemPair:
return r.handle < x.handle
}
log.Errorf("invalid type %T", item)
return true
}
func (k itemKey) Less(item llrb.Item) bool {
switch x := item.(type) {
case itemKey:
return k < x
case *itemPair:
return k < x.handle
}
log.Errorf("invalid type %T", item)
return true
}
// MemoryTable implements table.Table interface.
type MemoryTable struct {
ID int64
Name model.CIStr
Columns []*table.Column
pkHandleCol *table.Column
recordPrefix kv.Key
alloc autoid.Allocator
meta *model.TableInfo
tree *llrb.LLRB
mu sync.RWMutex
}
// MemoryTableFromMeta creates a Table instance from model.TableInfo.
func MemoryTableFromMeta(alloc autoid.Allocator, tblInfo *model.TableInfo) 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 := newMemoryTable(tblInfo.ID, tblInfo.Name.O, columns, alloc)
t.pkHandleCol = pkHandleColumn
t.meta = tblInfo
return t
}
// newMemoryTable constructs a MemoryTable instance.
func newMemoryTable(tableID int64, tableName string, cols []*table.Column, alloc autoid.Allocator) *MemoryTable {
name := model.NewCIStr(tableName)
t := &MemoryTable{
ID: tableID,
Name: name,
alloc: alloc,
Columns: cols,
recordPrefix: tablecodec.GenTableRecordPrefix(tableID),
tree: llrb.New(),
}
return t
}
// Seek seeks the handle
func (t *MemoryTable) Seek(ctx context.Context, handle int64) (int64, bool, error) {
var found bool
var result int64
t.mu.RLock()
t.tree.AscendGreaterOrEqual(itemKey(handle), func(item llrb.Item) bool {
found = true
result = int64(item.(*itemPair).handle)
return false
})
t.mu.RUnlock()
return result, found, nil
}
// Indices implements table.Table Indices interface.
func (t *MemoryTable) Indices() []table.Index {
return nil
}
// WritableIndices implements table.Table WritableIndices interface.
func (t *MemoryTable) WritableIndices() []table.Index {
return nil
}
// DeletableIndices implements table.Table DeletableIndices interface.
func (t *MemoryTable) DeletableIndices() []table.Index {
return nil
}
// Meta implements table.Table Meta interface.
func (t *MemoryTable) Meta() *model.TableInfo {
return t.meta
}
// Cols implements table.Table Cols interface.
func (t *MemoryTable) Cols() []*table.Column {
return t.Columns
}
// WritableCols implements table.Table WritableCols interface.
func (t *MemoryTable) WritableCols() []*table.Column {
return t.Columns
}
// RecordPrefix implements table.Table RecordPrefix interface.
func (t *MemoryTable) RecordPrefix() kv.Key {
return t.recordPrefix
}
// IndexPrefix implements table.Table IndexPrefix interface.
func (t *MemoryTable) IndexPrefix() kv.Key {
return nil
}
// RecordKey implements table.Table RecordKey interface.
func (t *MemoryTable) RecordKey(h int64) kv.Key {
return tablecodec.EncodeRecordKey(t.recordPrefix, h)
}
// FirstKey implements table.Table FirstKey interface.
func (t *MemoryTable) FirstKey() kv.Key {
return t.RecordKey(0)
}
// Truncate drops all data in Memory Table.
func (t *MemoryTable) Truncate() {
t.tree = llrb.New()
}
// UpdateRecord implements table.Table UpdateRecord interface.
func (t *MemoryTable) UpdateRecord(ctx context.Context, h int64, oldData, newData []types.Datum, touched []bool) error {
t.mu.Lock()
defer t.mu.Unlock()
item := t.tree.Get(itemKey(h))
if item == nil {
return table.ErrRowNotFound
}
pair := item.(*itemPair)
pair.data = newData
return nil
}
// AddRecord implements table.Table AddRecord interface.
func (t *MemoryTable) AddRecord(ctx context.Context, r []types.Datum) (recordID int64, err error) {
if t.pkHandleCol != nil {
recordID, err = r[t.pkHandleCol.Offset].ToInt64(ctx.GetSessionVars().StmtCtx)
if err != nil {
return 0, errors.Trace(err)
}
} else {
recordID, err = t.alloc.Alloc(t.ID)
if err != nil {
return 0, errors.Trace(err)
}
}
item := &itemPair{
handle: itemKey(recordID),
data: r,
}
t.mu.Lock()
defer t.mu.Unlock()
if t.tree.Get(itemKey(recordID)) != nil {
return 0, kv.ErrKeyExists
}
t.tree.ReplaceOrInsert(item)
return
}
// RowWithCols implements table.Table RowWithCols interface.
func (t *MemoryTable) RowWithCols(ctx context.Context, h int64, cols []*table.Column) ([]types.Datum, error) {
t.mu.RLock()
defer t.mu.RUnlock()
item := t.tree.Get(itemKey(h))
if item == nil {
return nil, table.ErrRowNotFound
}
row := item.(*itemPair).data
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 *MemoryTable) 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 *MemoryTable) RemoveRecord(ctx context.Context, h int64, r []types.Datum) error {
t.mu.Lock()
t.tree.Delete(itemKey(h))
t.mu.Unlock()
return nil
}
// AllocAutoID implements table.Table AllocAutoID interface.
func (t *MemoryTable) AllocAutoID() (int64, error) {
return t.alloc.Alloc(t.ID)
}
// Allocator implements table.Table Allocator interface.
func (t *MemoryTable) Allocator() autoid.Allocator {
return t.alloc
}
// RebaseAutoID implements table.Table RebaseAutoID interface.
func (t *MemoryTable) RebaseAutoID(newBase int64, isSetStep bool) error {
return t.alloc.Rebase(t.ID, newBase, isSetStep)
}
// IterRecords implements table.Table IterRecords interface.
func (t *MemoryTable) IterRecords(ctx context.Context, startKey kv.Key, cols []*table.Column,
fn table.RecordIterFunc) error {
return nil
}
// Type implements table.Table Type interface.
func (t *MemoryTable) Type() table.Type {
return table.MemoryTable
}