-
Notifications
You must be signed in to change notification settings - Fork 271
/
sysobject.go
402 lines (380 loc) · 13.1 KB
/
sysobject.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2022 Matrix Origin
//
// 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 txnimpl
import (
"context"
"fmt"
pkgcatalog "github.com/matrixorigin/matrixone/pkg/catalog"
"github.com/matrixorigin/matrixone/pkg/common/mpool"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle"
)
type txnSysObject struct {
*txnObject
table *txnTable
catalog *catalog.Catalog
}
func newSysObject(table *txnTable, meta *catalog.ObjectEntry) *txnSysObject {
obj := &txnSysObject{
txnObject: newObject(table, meta),
table: table,
catalog: meta.GetTable().GetCatalog(),
}
return obj
}
func bool2i8(v bool) int8 {
if v {
return int8(1)
} else {
return int8(0)
}
}
func (obj *txnSysObject) isSysTable() bool {
return isSysTable(obj.table.entry.GetLastestSchemaLocked().Name)
}
func (obj *txnSysObject) GetTotalChanges() int {
if obj.isSysTable() {
panic("not supported")
}
return obj.txnObject.GetTotalChanges()
}
func (obj *txnSysObject) RangeDelete(blkID uint16, start, end uint32, dt handle.DeleteType, mp *mpool.MPool) (err error) {
if obj.isSysTable() {
panic("not supported")
}
return obj.txnObject.RangeDelete(blkID, start, end, dt, mp)
}
func (obj *txnSysObject) processDB(fn func(*catalog.DBEntry) error, ignoreErr bool) (err error) {
it := newDBIt(obj.Txn, obj.catalog)
for it.linkIt.Valid() {
if err = it.GetError(); err != nil && !ignoreErr {
break
}
db := it.GetCurr()
if err = fn(db); err != nil && !ignoreErr {
break
}
it.Next()
}
return
}
func (obj *txnSysObject) processTable(entry *catalog.DBEntry, fn func(*catalog.TableEntry) error, ignoreErr bool) (err error) {
txnDB, err := obj.table.store.getOrSetDB(entry.GetID())
if err != nil {
return
}
it := newRelationIt(txnDB)
for it.linkIt.Valid() {
if err = it.GetError(); err != nil && !ignoreErr {
break
}
table := it.GetCurr()
if err = fn(table); err != nil && !ignoreErr {
break
}
it.Next()
}
return
}
func FillColumnRow(table *catalog.TableEntry, node *catalog.MVCCNode[*catalog.TableMVCCNode], attr string, colData containers.Vector) {
schema := node.BaseNode.Schema
tableID := table.GetID()
for i, colDef := range schema.ColDefs {
switch attr {
case pkgcatalog.SystemColAttr_UniqName:
colData.Append([]byte(fmt.Sprintf("%d-%s", tableID, colDef.Name)), false)
case pkgcatalog.SystemColAttr_AccID:
colData.Append(schema.AcInfo.TenantID, false)
case pkgcatalog.SystemColAttr_Name:
colData.Append([]byte(colDef.Name), false)
case pkgcatalog.SystemColAttr_Num:
colData.Append(int32(i+1), false)
case pkgcatalog.SystemColAttr_Type:
//colData.Append(int32(colDef.Type.Oid))
data, _ := types.Encode(&colDef.Type)
colData.Append(data, false)
case pkgcatalog.SystemColAttr_DBID:
colData.Append(table.GetDB().GetID(), false)
case pkgcatalog.SystemColAttr_DBName:
colData.Append([]byte(table.GetDB().GetName()), false)
case pkgcatalog.SystemColAttr_RelID:
colData.Append(tableID, false)
case pkgcatalog.SystemColAttr_RelName:
colData.Append([]byte(schema.Name), false)
case pkgcatalog.SystemColAttr_ConstraintType:
if colDef.Primary {
colData.Append([]byte(pkgcatalog.SystemColPKConstraint), false)
} else {
colData.Append([]byte(pkgcatalog.SystemColNoConstraint), false)
}
case pkgcatalog.SystemColAttr_Length:
colData.Append(int32(colDef.Type.Width), false)
case pkgcatalog.SystemColAttr_NullAbility:
colData.Append(bool2i8(!colDef.NullAbility), false)
case pkgcatalog.SystemColAttr_HasExpr:
colData.Append(bool2i8(len(colDef.Default) > 0), false) // @imlinjunhong says always has Default, expect row_id
case pkgcatalog.SystemColAttr_DefaultExpr:
colData.Append(colDef.Default, false)
case pkgcatalog.SystemColAttr_IsDropped:
colData.Append(int8(0), false)
case pkgcatalog.SystemColAttr_IsHidden:
colData.Append(bool2i8(colDef.Hidden), false)
case pkgcatalog.SystemColAttr_IsUnsigned:
colData.Append(bool2i8(colDef.Type.IsUInt()), false)
case pkgcatalog.SystemColAttr_IsAutoIncrement:
colData.Append(bool2i8(colDef.AutoIncrement), false)
case pkgcatalog.SystemColAttr_Comment:
colData.Append([]byte(colDef.Comment), false)
case pkgcatalog.SystemColAttr_HasUpdate:
colData.Append(bool2i8(len(colDef.OnUpdate) > 0), false)
case pkgcatalog.SystemColAttr_IsClusterBy:
colData.Append(bool2i8(colDef.IsClusterBy()), false)
case pkgcatalog.SystemColAttr_Update:
colData.Append(colDef.OnUpdate, false)
case pkgcatalog.SystemColAttr_Seqnum:
colData.Append(colDef.SeqNum, false)
case pkgcatalog.SystemColAttr_EnumValues:
colData.Append([]byte(colDef.EnumValues), false)
default:
panic("unexpected colname. if add new catalog def, fill it in this switch")
}
}
}
// func (obj *txnSysObject) GetDeltaPersistedTS() types.TS {
// return types.TS{}.Next()
// }
func (obj *txnSysObject) getColumnTableVec(
ts types.TS, colIdx int, mp *mpool.MPool,
) (colData containers.Vector, err error) {
col := catalog.SystemColumnSchema.ColDefs[colIdx]
colData = containers.MakeVector(col.Type, mp)
tableFn := func(table *catalog.TableEntry) error {
table.RLock()
node := table.GetVisibleNodeLocked(obj.Txn)
table.RUnlock()
FillColumnRow(table, node, col.Name, colData)
return nil
}
dbFn := func(db *catalog.DBEntry) error {
return obj.processTable(db, tableFn, false)
}
err = obj.processDB(dbFn, false)
if err != nil {
return
}
return
}
func (obj *txnSysObject) getColumnTableData(
colIdx int, mp *mpool.MPool,
) (view *containers.ColumnView, err error) {
ts := obj.Txn.GetStartTS()
view = containers.NewColumnView(colIdx)
colData, err := obj.getColumnTableVec(ts, colIdx, mp)
view.SetData(colData)
return
}
func FillTableRow(table *catalog.TableEntry, node *catalog.MVCCNode[*catalog.TableMVCCNode], attr string, colData containers.Vector) {
schema := node.BaseNode.Schema
switch attr {
case pkgcatalog.SystemRelAttr_ID:
colData.Append(table.GetID(), false)
case pkgcatalog.SystemRelAttr_Name:
colData.Append([]byte(schema.Name), false)
case pkgcatalog.SystemRelAttr_DBName:
colData.Append([]byte(table.GetDB().GetName()), false)
case pkgcatalog.SystemRelAttr_DBID:
colData.Append(table.GetDB().GetID(), false)
case pkgcatalog.SystemRelAttr_Comment:
colData.Append([]byte(schema.Comment), false)
case pkgcatalog.SystemRelAttr_Partitioned:
colData.Append(schema.Partitioned, false)
case pkgcatalog.SystemRelAttr_Partition:
colData.Append([]byte(schema.Partition), false)
case pkgcatalog.SystemRelAttr_Persistence:
colData.Append([]byte(pkgcatalog.SystemPersistRel), false)
case pkgcatalog.SystemRelAttr_Kind:
colData.Append([]byte(schema.Relkind), false)
case pkgcatalog.SystemRelAttr_CreateSQL:
colData.Append([]byte(schema.Createsql), false)
case pkgcatalog.SystemRelAttr_ViewDef:
colData.Append([]byte(schema.View), false)
case pkgcatalog.SystemRelAttr_Owner:
colData.Append(schema.AcInfo.RoleID, false)
case pkgcatalog.SystemRelAttr_Creator:
colData.Append(schema.AcInfo.UserID, false)
case pkgcatalog.SystemRelAttr_CreateAt:
colData.Append(schema.AcInfo.CreateAt, false)
case pkgcatalog.SystemRelAttr_AccID:
colData.Append(schema.AcInfo.TenantID, false)
case pkgcatalog.SystemRelAttr_Constraint:
colData.Append(schema.Constraint, false)
case pkgcatalog.SystemRelAttr_Version:
colData.Append(schema.Version, false)
case pkgcatalog.SystemRelAttr_CatalogVersion:
colData.Append(schema.CatalogVersion, false)
case catalog.AccountIDDbNameTblName:
packer := types.NewPacker(common.WorkspaceAllocator)
packer.EncodeUint32(schema.AcInfo.TenantID)
packer.EncodeStringType([]byte(table.GetDB().GetName()))
packer.EncodeStringType([]byte(schema.Name))
colData.Append(packer.Bytes(), false)
packer.FreeMem()
default:
panic("unexpected colname. if add new catalog def, fill it in this switch")
}
}
func (obj *txnSysObject) getRelTableVec(ts types.TS, colIdx int, mp *mpool.MPool) (colData containers.Vector, err error) {
colDef := catalog.SystemTableSchema.ColDefs[colIdx]
colData = containers.MakeVector(colDef.Type, mp)
tableFn := func(table *catalog.TableEntry) error {
table.RLock()
node := table.GetVisibleNodeLocked(obj.Txn)
table.RUnlock()
FillTableRow(table, node, colDef.Name, colData)
return nil
}
dbFn := func(db *catalog.DBEntry) error {
return obj.processTable(db, tableFn, false)
}
if err = obj.processDB(dbFn, false); err != nil {
return
}
return
}
func (obj *txnSysObject) getRelTableData(colIdx int, mp *mpool.MPool) (view *containers.ColumnView, err error) {
ts := obj.Txn.GetStartTS()
view = containers.NewColumnView(colIdx)
colData, err := obj.getRelTableVec(ts, colIdx, mp)
view.SetData(colData)
return
}
func FillDBRow(db *catalog.DBEntry, _ *catalog.MVCCNode[*catalog.EmptyMVCCNode], attr string, colData containers.Vector) {
switch attr {
case pkgcatalog.SystemDBAttr_ID:
colData.Append(db.GetID(), false)
case pkgcatalog.SystemDBAttr_Name:
colData.Append([]byte(db.GetName()), false)
case pkgcatalog.SystemDBAttr_CatalogName:
colData.Append([]byte(pkgcatalog.SystemCatalogName), false)
case pkgcatalog.SystemDBAttr_CreateSQL:
colData.Append([]byte(db.GetCreateSql()), false)
case pkgcatalog.SystemDBAttr_Owner:
colData.Append(db.GetRoleID(), false)
case pkgcatalog.SystemDBAttr_Creator:
colData.Append(db.GetUserID(), false)
case pkgcatalog.SystemDBAttr_CreateAt:
colData.Append(db.GetCreateAt(), false)
case pkgcatalog.SystemDBAttr_AccID:
colData.Append(db.GetTenantID(), false)
case pkgcatalog.SystemDBAttr_Type:
colData.Append([]byte(db.GetDatType()), false)
case catalog.AccountIDDbName:
packer := types.NewPacker(common.WorkspaceAllocator)
packer.EncodeUint32(db.GetTenantID())
packer.EncodeStringType([]byte(db.GetName()))
colData.Append(packer.Bytes(), false)
packer.FreeMem()
default:
panic("unexpected colname. if add new catalog def, fill it in this switch")
}
}
func (obj *txnSysObject) getDBTableVec(colIdx int, mp *mpool.MPool) (colData containers.Vector, err error) {
colDef := catalog.SystemDBSchema.ColDefs[colIdx]
colData = containers.MakeVector(colDef.Type, mp)
fn := func(db *catalog.DBEntry) error {
FillDBRow(db, nil, colDef.Name, colData)
return nil
}
if err = obj.processDB(fn, false); err != nil {
return
}
return
}
func (obj *txnSysObject) getDBTableData(
colIdx int, mp *mpool.MPool,
) (view *containers.ColumnView, err error) {
view = containers.NewColumnView(colIdx)
colData, err := obj.getDBTableVec(colIdx, mp)
view.SetData(colData)
return
}
func (obj *txnSysObject) GetColumnDataById(
ctx context.Context, blkID uint16, colIdx int, mp *mpool.MPool,
) (view *containers.ColumnView, err error) {
if !obj.isSysTable() {
return obj.txnObject.GetColumnDataById(ctx, blkID, colIdx, mp)
}
if obj.table.GetID() == pkgcatalog.MO_DATABASE_ID {
return obj.getDBTableData(colIdx, mp)
} else if obj.table.GetID() == pkgcatalog.MO_TABLES_ID {
return obj.getRelTableData(colIdx, mp)
} else if obj.table.GetID() == pkgcatalog.MO_COLUMNS_ID {
return obj.getColumnTableData(colIdx, mp)
} else {
panic("not supported")
}
}
func (obj *txnSysObject) Prefetch(idxes []int) error {
return nil
}
func (obj *txnSysObject) GetColumnDataByName(
ctx context.Context, blkID uint16, attr string, mp *mpool.MPool,
) (view *containers.ColumnView, err error) {
colIdx := obj.entry.GetSchema().GetColIdx(attr)
return obj.GetColumnDataById(ctx, blkID, colIdx, mp)
}
func (obj *txnSysObject) GetColumnDataByNames(
ctx context.Context, blkID uint16, attrs []string, mp *mpool.MPool,
) (view *containers.BlockView, err error) {
if !obj.isSysTable() {
return obj.txnObject.GetColumnDataByNames(ctx, blkID, attrs, mp)
}
view = containers.NewBlockView()
ts := obj.Txn.GetStartTS()
switch obj.table.GetID() {
case pkgcatalog.MO_DATABASE_ID:
for _, attr := range attrs {
colIdx := obj.entry.GetSchema().GetColIdx(attr)
vec, err := obj.getDBTableVec(colIdx, mp)
view.SetData(colIdx, vec)
if err != nil {
return view, err
}
}
case pkgcatalog.MO_TABLES_ID:
for _, attr := range attrs {
colIdx := obj.entry.GetSchema().GetColIdx(attr)
vec, err := obj.getRelTableVec(ts, colIdx, mp)
view.SetData(colIdx, vec)
if err != nil {
return view, err
}
}
case pkgcatalog.MO_COLUMNS_ID:
for _, attr := range attrs {
colIdx := obj.entry.GetSchema().GetColIdx(attr)
vec, err := obj.getColumnTableVec(ts, colIdx, mp)
view.SetData(colIdx, vec)
if err != nil {
return view, err
}
}
default:
panic("not supported")
}
return
}