github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/tables/handle.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tables 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog" 19 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 20 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/data" 21 ) 22 23 type tableHandle struct { 24 table *dataTable 25 object *aobject 26 appender data.ObjectAppender 27 } 28 29 func newHandle(table *dataTable, object *aobject) *tableHandle { 30 h := &tableHandle{ 31 table: table, 32 object: object, 33 } 34 if object != nil { 35 h.appender, _ = object.MakeAppender() 36 } 37 return h 38 } 39 40 func (h *tableHandle) SetAppender(id *common.ID) (appender data.ObjectAppender) { 41 tableMeta := h.table.meta 42 objMeta, _ := tableMeta.GetObjectByID(id.ObjectID()) 43 h.object = objMeta.GetObjectData().(*aobject) 44 h.appender, _ = h.object.MakeAppender() 45 h.object.Ref() 46 return h.appender 47 } 48 49 func (h *tableHandle) ThrowAppenderAndErr() (appender data.ObjectAppender, err error) { 50 err = data.ErrAppendableObjectNotFound 51 h.object = nil 52 h.appender = nil 53 return 54 } 55 56 func (h *tableHandle) GetAppender() (appender data.ObjectAppender, err error) { 57 var objEntry *catalog.ObjectEntry 58 if h.appender == nil { 59 objEntry = h.table.meta.LastAppendableObject() 60 if objEntry == nil { 61 err = data.ErrAppendableObjectNotFound 62 return 63 } 64 h.object = objEntry.GetObjectData().(*aobject) 65 h.appender, err = h.object.MakeAppender() 66 if err != nil { 67 panic(err) 68 } 69 } 70 71 dropped := h.object.meta.HasDropCommitted() 72 if !h.appender.IsAppendable() || !h.object.IsAppendable() || dropped { 73 return h.ThrowAppenderAndErr() 74 } 75 h.object.Ref() 76 // Similar to optimistic locking 77 dropped = h.object.meta.HasDropCommitted() 78 if !h.appender.IsAppendable() || !h.object.IsAppendable() || dropped { 79 h.object.Unref() 80 return h.ThrowAppenderAndErr() 81 } 82 appender = h.appender 83 return 84 }