-
Notifications
You must be signed in to change notification settings - Fork 265
/
object.go
346 lines (315 loc) · 8.91 KB
/
object.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
// Copyright 2021 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"
"sync"
"github.com/matrixorigin/matrixone/pkg/common/mpool"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/objectio"
"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"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
)
// var newObjectCnt atomic.Int64
// var getObjectCnt atomic.Int64
// var putObjectCnt atomic.Int64
// func GetStatsString() string {
// return fmt.Sprintf(
// "NewObj: %d, GetObj: %d, PutObj: %d, NewBlk: %d, GetBlk: %d, PutBlk: %d",
// newObjectCnt.Load(),
// getObjectCnt.Load(),
// putObjectCnt.Load(),
// newBlockCnt.Load(),
// getBlockCnt.Load(),
// putBlockCnt.Load())
// }
var (
_objPool = sync.Pool{
New: func() any {
// newObjectCnt.Add(1)
return &txnObject{}
},
}
)
type txnObject struct {
txnbase.TxnObject
entry *catalog.ObjectEntry
table *txnTable
}
type ObjectIt struct {
sync.RWMutex
linkIt *common.GenericSortedDListIt[*catalog.ObjectEntry]
curr *catalog.ObjectEntry
table *txnTable
err error
}
type composedObjectIt struct {
*ObjectIt
uncommitted *catalog.ObjectEntry
}
func newObjectItOnSnap(table *txnTable) handle.ObjectIt {
it := &ObjectIt{
linkIt: table.entry.MakeObjectIt(true),
table: table,
}
var err error
var ok bool
for it.linkIt.Valid() {
curr := it.linkIt.Get().GetPayload()
curr.RLock()
ok, err = curr.IsVisibleWithLock(it.table.store.txn, curr.RWMutex)
if err != nil {
curr.RUnlock()
it.err = err
return it
}
if ok {
curr.RUnlock()
it.curr = curr
break
}
curr.RUnlock()
it.linkIt.Next()
}
return it
}
func newObjectIt(table *txnTable) handle.ObjectIt {
it := &ObjectIt{
linkIt: table.entry.MakeObjectIt(true),
table: table,
}
var err error
var ok bool
for it.linkIt.Valid() {
curr := it.linkIt.Get().GetPayload()
curr.RLock()
ok, err = curr.IsVisibleWithLock(it.table.store.txn, curr.RWMutex)
if err != nil {
curr.RUnlock()
it.err = err
return it
}
if ok {
curr.RUnlock()
it.curr = curr
break
}
curr.RUnlock()
it.linkIt.Next()
}
if table.tableSpace != nil {
cit := &composedObjectIt{
ObjectIt: it,
uncommitted: table.tableSpace.entry,
}
return cit
}
return it
}
func (it *ObjectIt) Close() error { return nil }
func (it *ObjectIt) GetError() error { return it.err }
func (it *ObjectIt) Valid() bool {
if it.err != nil {
return false
}
return it.linkIt.Valid()
}
func (it *ObjectIt) Next() {
var err error
var valid bool
for {
it.linkIt.Next()
node := it.linkIt.Get()
if node == nil {
it.curr = nil
break
}
entry := node.GetPayload()
entry.RLock()
valid, err = entry.IsVisibleWithLock(it.table.store.txn, entry.RWMutex)
entry.RUnlock()
if err != nil {
it.err = err
break
}
if valid {
it.curr = entry
break
}
}
}
func (it *ObjectIt) GetObject() handle.Object {
if isSysTableId(it.table.GetID()) {
return newSysObject(it.table, it.curr)
}
return newObject(it.table, it.curr)
}
func (cit *composedObjectIt) GetObject() handle.Object {
if cit.uncommitted != nil {
return newObject(cit.table, cit.uncommitted)
}
return cit.ObjectIt.GetObject()
}
func (cit *composedObjectIt) Valid() bool {
if cit.err != nil {
return false
}
if cit.uncommitted != nil {
return true
}
return cit.ObjectIt.Valid()
}
func (cit *composedObjectIt) Next() {
if cit.uncommitted != nil {
cit.uncommitted = nil
return
}
cit.ObjectIt.Next()
}
func newObject(table *txnTable, meta *catalog.ObjectEntry) *txnObject {
obj := _objPool.Get().(*txnObject)
// getObjectCnt.Add(1)
obj.Txn = table.store.txn
obj.table = table
obj.entry = meta
return obj
}
func (obj *txnObject) reset() {
obj.entry = nil
obj.table = nil
obj.TxnObject.Reset()
}
func buildObject(table *txnTable, meta *catalog.ObjectEntry) handle.Object {
if isSysTableId(meta.GetTable().ID) {
return newSysObject(table, meta)
}
return newObject(table, meta)
}
func (obj *txnObject) Close() (err error) {
obj.reset()
_objPool.Put(obj)
// putObjectCnt.Add(1)
return
}
func (obj *txnObject) GetTotalChanges() int {
return obj.entry.GetObjectData().GetTotalChanges()
}
func (obj *txnObject) RangeDelete(blkID uint16, start, end uint32, dt handle.DeleteType, mp *mpool.MPool) (err error) {
schema := obj.table.GetLocalSchema()
pkDef := schema.GetPrimaryKey()
pkVec := makeWorkspaceVector(pkDef.Type)
defer pkVec.Close()
for row := start; row <= end; row++ {
pkVal, _, err := obj.entry.GetObjectData().GetValue(
obj.table.store.GetContext(), obj.Txn, schema, blkID, int(row), pkDef.Idx, mp,
)
if err != nil {
return err
}
pkVec.Append(pkVal, false)
}
id := obj.entry.AsCommonID()
id.SetBlockOffset(blkID)
return obj.Txn.GetStore().RangeDelete(id, start, end, pkVec, dt)
}
func (obj *txnObject) GetMeta() any { return obj.entry }
func (obj *txnObject) String() string { return obj.entry.String() }
func (obj *txnObject) GetID() *types.Objectid { return &obj.entry.ID }
func (obj *txnObject) BlkCnt() int { return obj.entry.BlockCnt() }
func (obj *txnObject) IsUncommitted() bool {
return obj.entry.IsLocal
}
func (obj *txnObject) IsAppendable() bool { return obj.entry.IsAppendable() }
func (obj *txnObject) SoftDeleteBlock(id types.Blockid) (err error) {
fp := obj.entry.AsCommonID()
fp.BlockID = id
return obj.Txn.GetStore().SoftDeleteBlock(fp)
}
func (obj *txnObject) GetRelation() (rel handle.Relation) {
return newRelation(obj.table)
}
func (obj *txnObject) UpdateStats(stats objectio.ObjectStats) error {
id := obj.entry.AsCommonID()
return obj.Txn.GetStore().UpdateObjectStats(id, &stats)
}
func (obj *txnObject) Prefetch(idxes []int) error {
schema := obj.table.GetLocalSchema()
seqnums := make([]uint16, 0, len(idxes))
for _, idx := range idxes {
seqnums = append(seqnums, schema.ColDefs[idx].SeqNum)
}
if obj.IsUncommitted() {
return obj.table.tableSpace.Prefetch(obj.entry, seqnums)
}
for i := 0; i < obj.entry.BlockCnt(); i++ {
err := obj.entry.GetObjectData().Prefetch(seqnums, uint16(i))
if err != nil {
return err
}
}
return nil
}
func (obj *txnObject) Fingerprint() *common.ID { return obj.entry.AsCommonID() }
func (obj *txnObject) GetByFilter(
ctx context.Context, filter *handle.Filter, mp *mpool.MPool,
) (blkID uint16, offset uint32, err error) {
return obj.entry.GetObjectData().GetByFilter(ctx, obj.table.store.txn, filter, mp)
}
func (obj *txnObject) GetColumnDataById(
ctx context.Context, blkID uint16, colIdx int, mp *mpool.MPool,
) (*containers.ColumnView, error) {
if obj.entry.IsLocal {
return obj.table.tableSpace.GetColumnDataById(ctx, obj.entry, colIdx, mp)
}
return obj.entry.GetObjectData().GetColumnDataById(ctx, obj.Txn, obj.table.GetLocalSchema(), blkID, colIdx, mp)
}
func (obj *txnObject) GetColumnDataByIds(
ctx context.Context, blkID uint16, colIdxes []int, mp *mpool.MPool,
) (*containers.BlockView, error) {
if obj.entry.IsLocal {
return obj.table.tableSpace.GetColumnDataByIds(obj.entry, colIdxes, mp)
}
return obj.entry.GetObjectData().GetColumnDataByIds(ctx, obj.Txn, obj.table.GetLocalSchema(), blkID, colIdxes, mp)
}
func (obj *txnObject) GetColumnDataByName(
ctx context.Context, blkID uint16, attr string, mp *mpool.MPool,
) (*containers.ColumnView, error) {
schema := obj.table.GetLocalSchema()
colIdx := schema.GetColIdx(attr)
if obj.entry.IsLocal {
return obj.table.tableSpace.GetColumnDataById(ctx, obj.entry, colIdx, mp)
}
return obj.entry.GetObjectData().GetColumnDataById(ctx, obj.Txn, schema, blkID, colIdx, mp)
}
func (obj *txnObject) GetColumnDataByNames(
ctx context.Context, blkID uint16, attrs []string, mp *mpool.MPool,
) (*containers.BlockView, error) {
schema := obj.table.GetLocalSchema()
attrIds := make([]int, len(attrs))
for i, attr := range attrs {
attrIds[i] = schema.GetColIdx(attr)
}
if obj.entry.IsLocal {
return obj.table.tableSpace.GetColumnDataByIds(obj.entry, attrIds, mp)
}
return obj.entry.GetObjectData().GetColumnDataByIds(ctx, obj.Txn, schema, blkID, attrIds, mp)
}
func (obj *txnObject) UpdateDeltaLoc(blkID uint16, deltaLoc objectio.Location) error {
id := obj.entry.AsCommonID()
id.SetBlockOffset(blkID)
return obj.table.store.UpdateDeltaLoc(id, deltaLoc)
}