github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/catalog/metamvccnode.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 catalog 16 17 import ( 18 "fmt" 19 "io" 20 "unsafe" 21 22 "github.com/matrixorigin/matrixone/pkg/container/types" 23 "github.com/matrixorigin/matrixone/pkg/objectio" 24 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 25 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers" 26 ) 27 28 type MetadataMVCCNode struct { 29 MetaLoc objectio.Location 30 DeltaLoc objectio.Location 31 32 // For deltaloc from CN, it needs to ensure that deleteChain is empty. 33 NeedCheckDeleteChainWhenCommit bool 34 } 35 36 func NewEmptyMetadataMVCCNode() *MetadataMVCCNode { 37 return &MetadataMVCCNode{} 38 } 39 40 func (e *MetadataMVCCNode) CloneAll() *MetadataMVCCNode { 41 node := &MetadataMVCCNode{ 42 MetaLoc: e.MetaLoc, 43 DeltaLoc: e.DeltaLoc, 44 } 45 return node 46 } 47 48 func (e *MetadataMVCCNode) CloneData() *MetadataMVCCNode { 49 return &MetadataMVCCNode{ 50 MetaLoc: e.MetaLoc, 51 DeltaLoc: e.DeltaLoc, 52 } 53 } 54 55 func (e *MetadataMVCCNode) String() string { 56 57 return fmt.Sprintf("[MetaLoc=\"%s\",DeltaLoc=\"%s\"]", 58 e.MetaLoc.String(), 59 e.DeltaLoc.String()) 60 } 61 62 // for create drop in one txn 63 func (e *MetadataMVCCNode) Update(un *MetadataMVCCNode) { 64 if !un.MetaLoc.IsEmpty() { 65 e.MetaLoc = un.MetaLoc 66 } 67 if !un.DeltaLoc.IsEmpty() { 68 e.DeltaLoc = un.DeltaLoc 69 } 70 } 71 func (e *MetadataMVCCNode) IdempotentUpdate(un *MetadataMVCCNode) { 72 if !un.MetaLoc.IsEmpty() { 73 e.MetaLoc = un.MetaLoc 74 } 75 if !un.DeltaLoc.IsEmpty() { 76 e.DeltaLoc = un.DeltaLoc 77 } 78 } 79 func (e *MetadataMVCCNode) WriteTo(w io.Writer) (n int64, err error) { 80 var sn int64 81 if sn, err = objectio.WriteBytes(e.MetaLoc, w); err != nil { 82 return 83 } 84 n += sn 85 if sn, err = objectio.WriteBytes(e.DeltaLoc, w); err != nil { 86 return 87 } 88 n += sn 89 return 90 } 91 92 func (e *MetadataMVCCNode) ReadFromWithVersion(r io.Reader, ver uint16) (n int64, err error) { 93 var sn int64 94 if e.MetaLoc, sn, err = objectio.ReadBytes(r); err != nil { 95 return 96 } 97 n += sn 98 if e.DeltaLoc, sn, err = objectio.ReadBytes(r); err != nil { 99 return 100 } 101 n += sn 102 return 103 } 104 105 type ObjectMVCCNode struct { 106 objectio.ObjectStats 107 } 108 109 func NewEmptyObjectMVCCNode() *ObjectMVCCNode { 110 return &ObjectMVCCNode{ 111 ObjectStats: *objectio.NewObjectStats(), 112 } 113 } 114 115 func NewObjectInfoWithMetaLocation(metaLoc objectio.Location, id *objectio.ObjectId) *ObjectMVCCNode { 116 node := NewEmptyObjectMVCCNode() 117 if metaLoc.IsEmpty() { 118 objectio.SetObjectStatsObjectName(&node.ObjectStats, objectio.BuildObjectNameWithObjectID(id)) 119 return node 120 } 121 objectio.SetObjectStatsObjectName(&node.ObjectStats, metaLoc.Name()) 122 objectio.SetObjectStatsExtent(&node.ObjectStats, metaLoc.Extent()) 123 return node 124 } 125 126 func NewObjectInfoWithObjectID(id *objectio.ObjectId) *ObjectMVCCNode { 127 node := NewEmptyObjectMVCCNode() 128 objectio.SetObjectStatsObjectName(&node.ObjectStats, objectio.BuildObjectNameWithObjectID(id)) 129 return node 130 } 131 132 func NewObjectInfoWithObjectStats(stats *objectio.ObjectStats) *ObjectMVCCNode { 133 return &ObjectMVCCNode{ 134 ObjectStats: *stats.Clone(), 135 } 136 } 137 138 func (e *ObjectMVCCNode) CloneAll() *ObjectMVCCNode { 139 return &ObjectMVCCNode{ 140 ObjectStats: *e.ObjectStats.Clone(), 141 } 142 } 143 func (e *ObjectMVCCNode) CloneData() *ObjectMVCCNode { 144 return &ObjectMVCCNode{ 145 ObjectStats: *e.ObjectStats.Clone(), 146 } 147 } 148 func (e *ObjectMVCCNode) String() string { 149 if e == nil || e.IsEmpty() { 150 return "empty" 151 } 152 return e.ObjectStats.String() 153 } 154 func (e *ObjectMVCCNode) Update(vun *ObjectMVCCNode) { 155 e.ObjectStats = *vun.ObjectStats.Clone() 156 } 157 func (e *ObjectMVCCNode) IdempotentUpdate(vun *ObjectMVCCNode) { 158 if e.ObjectStats.IsZero() { 159 e.ObjectStats = *vun.ObjectStats.Clone() 160 } else { 161 if e.IsEmpty() && !vun.IsEmpty() { 162 e.ObjectStats = *vun.ObjectStats.Clone() 163 164 } 165 } 166 } 167 func (e *ObjectMVCCNode) WriteTo(w io.Writer) (n int64, err error) { 168 var sn int 169 if sn, err = w.Write(e.ObjectStats[:]); err != nil { 170 return 171 } 172 n += int64(sn) 173 return 174 } 175 func (e *ObjectMVCCNode) ReadFromWithVersion(r io.Reader, ver uint16) (n int64, err error) { 176 var sn int 177 if sn, err = r.Read(e.ObjectStats[:]); err != nil { 178 return 179 } 180 n += int64(sn) 181 return 182 } 183 184 func (e *ObjectMVCCNode) IsEmpty() bool { 185 return e.Size() == 0 186 } 187 188 func (e *ObjectMVCCNode) AppendTuple(sid *types.Objectid, batch *containers.Batch) { 189 if e == nil || e.IsEmpty() { 190 objectio.SetObjectStatsObjectName(&e.ObjectStats, objectio.BuildObjectNameWithObjectID(sid)) // when replay, sid is get from object name 191 } 192 batch.GetVectorByName(ObjectAttr_ObjectStats).Append(e.ObjectStats[:], false) 193 } 194 195 func ReadObjectInfoTuple(bat *containers.Batch, row int) (e *ObjectMVCCNode) { 196 buf := bat.GetVectorByName(ObjectAttr_ObjectStats).Get(row).([]byte) 197 e = &ObjectMVCCNode{ 198 ObjectStats: (objectio.ObjectStats)(buf), 199 } 200 return 201 } 202 203 type ObjectNode struct { 204 state EntryState 205 IsLocal bool // this object is hold by a localobject 206 SortHint uint64 // sort object by create time, make iteration on object determined 207 sorted bool // deprecated 208 209 remainingRows common.FixedSampleIII[int] 210 } 211 212 const ( 213 BlockNodeSize int64 = int64(unsafe.Sizeof(BlockNode{})) 214 ) 215 216 func (node *ObjectNode) ReadFrom(r io.Reader) (n int64, err error) { 217 _, err = r.Read(types.EncodeInt8((*int8)(&node.state))) 218 if err != nil { 219 return 220 } 221 n += 1 222 _, err = r.Read(types.EncodeBool(&node.IsLocal)) 223 if err != nil { 224 return 225 } 226 n += 1 227 _, err = r.Read(types.EncodeUint64(&node.SortHint)) 228 if err != nil { 229 return 230 } 231 n += 8 232 _, err = r.Read(types.EncodeBool(&node.sorted)) 233 if err != nil { 234 return 235 } 236 n += 1 237 return 238 } 239 240 func (node *ObjectNode) WriteTo(w io.Writer) (n int64, err error) { 241 _, err = w.Write(types.EncodeInt8((*int8)(&node.state))) 242 if err != nil { 243 return 244 } 245 n += 1 246 _, err = w.Write(types.EncodeBool(&node.IsLocal)) 247 if err != nil { 248 return 249 } 250 n += 1 251 _, err = w.Write(types.EncodeUint64(&node.SortHint)) 252 if err != nil { 253 return 254 } 255 n += 8 256 _, err = w.Write(types.EncodeBool(&node.sorted)) 257 if err != nil { 258 return 259 } 260 n += 1 261 return 262 } 263 func (node *ObjectNode) String() string { 264 sorted := "US" 265 if node.sorted { 266 sorted = "S" 267 } 268 return fmt.Sprintf("%s/%d", sorted, node.SortHint) 269 } 270 271 type BlockNode struct { 272 state EntryState 273 } 274 275 func EncodeBlockNode(node *BlockNode) []byte { 276 return unsafe.Slice((*byte)(unsafe.Pointer(node)), BlockNodeSize) 277 } 278 279 func (node *BlockNode) ReadFrom(r io.Reader) (n int64, err error) { 280 if _, err = r.Read(EncodeBlockNode(node)); err != nil { 281 return 282 } 283 n += BlockNodeSize 284 return 285 } 286 287 func (node *BlockNode) WriteTo(w io.Writer) (n int64, err error) { 288 if _, err = w.Write(EncodeBlockNode(node)); err != nil { 289 return 290 } 291 n += BlockNodeSize 292 return 293 }