github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/common/id.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 common 16 17 import ( 18 "fmt" 19 "unsafe" 20 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 ) 23 24 // ID is the general identifier type shared by different types like 25 // table, object, block, etc. 26 // 27 // We could wrap info from upper level via ID, for instance, get the table id, 28 // object id, and the block id for one block by ID.AsBlockID, which made 29 // the resource management easier. 30 type ID struct { 31 // Internal db id 32 DbID uint64 33 // Internal table id 34 TableID uint64 35 // Internal block id 36 BlockID types.Blockid 37 } 38 39 const ( 40 IDSize int64 = int64(unsafe.Sizeof(ID{})) 41 ) 42 43 func EncodeID(id *ID) []byte { 44 return unsafe.Slice((*byte)(unsafe.Pointer(id)), IDSize) 45 } 46 47 func (id *ID) SegmentID() *types.Segmentid { 48 return id.BlockID.Segment() 49 } 50 51 func (id *ID) SetSegmentID(sid *types.Segmentid) { 52 copy(id.BlockID[:types.UuidSize], sid[:]) 53 } 54 55 func (id *ID) ObjectID() *types.Objectid { 56 return id.BlockID.Object() 57 } 58 59 func (id *ID) SetObjectID(oid *types.Objectid) { 60 copy(id.BlockID[:types.ObjectBytesSize], oid[:]) 61 } 62 63 func (id *ID) SetBlockOffset(blkn uint16) { 64 copy(id.BlockID[types.ObjectBytesSize:], types.EncodeUint16(&blkn)) 65 } 66 67 func (id *ID) AsBlockID() *ID { 68 return &ID{ 69 DbID: id.DbID, 70 TableID: id.TableID, 71 BlockID: id.BlockID, 72 } 73 } 74 75 func (id *ID) String() string { 76 return fmt.Sprintf("<%d-%d-%s>", id.DbID, id.TableID, id.BlockID.String()) 77 } 78 79 func (id *ID) DBString() string { 80 return fmt.Sprintf("DB<%d>", id.DbID) 81 } 82 func (id *ID) TableString() string { 83 return fmt.Sprintf("TBL<%d-%d>", id.DbID, id.TableID) 84 } 85 func (id *ID) ObjectString() string { 86 return fmt.Sprintf("OBJ<%d-%d-%s>", id.DbID, id.TableID, id.BlockID.Object().String()) 87 } 88 89 func (id *ID) BlockString() string { 90 return fmt.Sprintf("BLK<%d-%d-%s>", id.DbID, id.TableID, id.BlockID.String()) 91 } 92 93 func IDArraryString(ids []ID) string { 94 str := "[" 95 for _, id := range ids { 96 str = fmt.Sprintf("%s%s,", str, id.String()) 97 } 98 str = fmt.Sprintf("%s]", str) 99 return str 100 } 101 102 func BlockIDArraryString(ids []ID) string { 103 str := "[" 104 for _, id := range ids { 105 str = fmt.Sprintf("%s%s,", str, id.BlockID.String()) 106 } 107 str = fmt.Sprintf("%s]", str) 108 return str 109 }