github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/location.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 objectio 16 17 import ( 18 "fmt" 19 "unsafe" 20 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 ) 23 24 const ( 25 ExtentOff = ObjectNameLen 26 ExtentLen = ExtentSize 27 RowsOff = ExtentOff + ExtentLen 28 RowsLen = 4 29 BlockIDOff = RowsOff + RowsLen 30 BlockIDLen = 2 31 LocationLen = BlockIDOff + BlockIDLen 32 ) 33 34 const ( 35 FileNumOff = SegmentIdSize 36 FileNumLen = 2 37 NameStringOff = FileNumOff + FileNumLen 38 NameStringLen = 42 //uuid[36]+_[1]+filename[5] 39 ObjectNameLen = NameStringOff + NameStringLen 40 ObjectNameShortLen = NameStringOff 41 ) 42 43 /* 44 Location is a fixed-length unmodifiable byte array. 45 Layout: ObjectName | Extent | Rows(uint32) | ID(uint16) 46 */ 47 type Location []byte 48 49 func BuildLocation(name ObjectName, extent Extent, rows uint32, id uint16) Location { 50 var location [LocationLen]byte 51 copy(location[:ObjectNameLen], name) 52 copy(location[ExtentOff:ExtentOff+ExtentSize], extent) 53 copy(location[RowsOff:RowsOff+RowsLen], types.EncodeUint32(&rows)) 54 copy(location[BlockIDOff:BlockIDOff+BlockIDLen], types.EncodeUint16(&id)) 55 return unsafe.Slice((*byte)(unsafe.Pointer(&location)), LocationLen) 56 } 57 58 func (l Location) Name() ObjectName { 59 return ObjectName(l[:ObjectNameLen]) 60 } 61 62 func (l Location) ShortName() *ObjectNameShort { 63 return (*ObjectNameShort)(unsafe.Pointer(&l[0])) 64 } 65 66 func (l Location) Extent() Extent { 67 return Extent(l[ExtentOff : ExtentOff+ExtentLen]) 68 } 69 70 func (l Location) Rows() uint32 { 71 return types.DecodeUint32(l[RowsOff : RowsOff+RowsLen]) 72 } 73 func (l Location) SetRows(rows uint32) { 74 copy(l[RowsOff:RowsOff+RowsLen], types.EncodeUint32(&rows)) 75 } 76 77 func (l Location) ID() uint16 { 78 return types.DecodeUint16(l[BlockIDOff : BlockIDOff+BlockIDLen]) 79 } 80 81 func (l Location) SetID(id uint16) { 82 copy(l[BlockIDOff:BlockIDOff+BlockIDLen], types.EncodeUint16(&id)) 83 } 84 85 func (l Location) IsEmpty() bool { 86 return len(l) < LocationLen || types.DecodeInt64(l[:ObjectNameLen]) == 0 87 } 88 89 func (l Location) String() string { 90 if len(l) == 0 { 91 return "" 92 } 93 if len(l) != LocationLen { 94 return string(l) 95 } 96 return fmt.Sprintf("%v_%v_%d_%d", l.Name().String(), l.Extent(), l.Rows(), l.ID()) 97 }