github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/name.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 "bytes" 19 "fmt" 20 "math/rand" 21 "unsafe" 22 23 "github.com/matrixorigin/matrixone/pkg/container/types" 24 ) 25 26 /* 27 ObjectName is a fixed-length unmodifiable byte array. 28 Layout: SegmentId | Num | NameStr 29 (types.Uuid) (uint16) (string) 30 Size: 16B + 2B + 42B 31 */ 32 type ObjectName []byte 33 type ObjectNameShort [ObjectNameShortLen]byte 34 35 func BuildObjectName(segid *Segmentid, num uint16) ObjectName { 36 var name [ObjectNameLen]byte 37 copy(name[:SegmentIdSize], types.EncodeUuid(segid)) 38 copy(name[FileNumOff:FileNumOff+FileNumLen], types.EncodeUint16(&num)) 39 str := fmt.Sprintf("%v_%05d", segid.ToString(), num) 40 copy(name[NameStringOff:NameStringOff+NameStringLen], str) 41 return unsafe.Slice((*byte)(unsafe.Pointer(&name)), ObjectNameLen) 42 } 43 44 func BuildObjectNameWithObjectID(segid *ObjectId) ObjectName { 45 var name [ObjectNameLen]byte 46 copy(name[:ObjectIDSize], segid[:]) 47 str := fmt.Sprintf("%v_%05d", segid.Segment().ToString(), segid.Offset()) 48 copy(name[NameStringOff:NameStringOff+NameStringLen], str) 49 return unsafe.Slice((*byte)(unsafe.Pointer(&name)), ObjectNameLen) 50 } 51 52 func (s *ObjectNameShort) Segmentid() *Segmentid { 53 return (*Segmentid)(unsafe.Pointer(&s[0])) 54 } 55 56 func (s *ObjectNameShort) Num() uint16 { 57 return types.DecodeUint16(s[SegmentIdSize:]) 58 } 59 60 func (s *ObjectNameShort) Equal(o []byte) bool { 61 return bytes.Equal(s[:], o) 62 } 63 64 func (o ObjectName) String() string { 65 return string(o[NameStringOff : NameStringOff+NameStringLen]) 66 } 67 68 func (o ObjectName) Short() *ObjectNameShort { 69 return (*ObjectNameShort)(unsafe.Pointer(&o[0])) 70 } 71 72 func (o ObjectName) SegmentId() Segmentid { 73 return types.DecodeUuid(o[:SegmentIdSize]) 74 } 75 76 func (o ObjectName) ObjectId() *ObjectId { 77 return (*ObjectId)(unsafe.Pointer(&o[0])) 78 } 79 80 func (o ObjectName) Num() uint16 { 81 return types.DecodeUint16(o[FileNumOff : FileNumOff+FileNumLen]) 82 } 83 84 func ShortName(b *Blockid) *ObjectNameShort { 85 return (*ObjectNameShort)(unsafe.Pointer(&b[0])) 86 } 87 88 func MockObjectName() ObjectName { 89 return BuildObjectName(NewSegmentid(), uint16(rand.Intn(1000))) 90 } 91 92 func MockLocation(name ObjectName) Location { 93 extent := NewExtent(0, uint32(rand.Intn(10000)), uint32(rand.Intn(10000)), uint32(rand.Intn(10000))) 94 return BuildLocation(name, extent, uint32(rand.Intn(8192)), uint16(rand.Intn(300))) 95 } 96 97 func (o ObjectName) Equal(a ObjectName) bool { 98 return bytes.Equal(o, a) 99 } 100 101 var NameQueryResult [NameStringOff]byte 102 var NameCheckPoint [NameStringOff]byte 103 var NameDiskCleaner [NameStringOff]byte 104 var NameETL [NameStringOff]byte 105 var NameNormal [NameStringOff]byte 106 107 func init() { 108 copy(NameQueryResult[:], "Query_ResultXXXX") 109 copy(NameCheckPoint[:], "CheckPoint_MetaX") 110 copy(NameDiskCleaner[:], "Disk_CleanerXXXX") 111 copy(NameETL[:], "Writer_ETLXXXXXX") 112 copy(NameNormal[:], "Writer_NormalXXX") 113 } 114 115 func BuildQueryResultName() ObjectName { 116 return NameQueryResult[:] 117 } 118 119 func BuildCheckpointName() ObjectName { 120 return NameCheckPoint[:] 121 } 122 123 func BuildDiskCleanerName() ObjectName { 124 return NameDiskCleaner[:] 125 } 126 127 func BuildETLName() ObjectName { 128 return NameETL[:] 129 } 130 131 func BuildNormalName() ObjectName { 132 return NameNormal[:] 133 }