github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/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 objectio
    16  
    17  import (
    18  	"bytes"
    19  	"hash/fnv"
    20  	"unsafe"
    21  
    22  	"github.com/google/uuid"
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  )
    25  
    26  const (
    27  	SegmentIdSize = types.UuidSize
    28  	ObjectIDSize  = types.ObjectidSize
    29  )
    30  
    31  var emptySegmentId types.Segmentid
    32  var emptyBlockId types.Blockid
    33  
    34  type ObjectId = types.Objectid
    35  type Segmentid = types.Segmentid
    36  type Blockid = types.Blockid
    37  type Rowid = types.Rowid
    38  
    39  func NewSegmentid() *Segmentid {
    40  	id := types.Uuid(uuid.Must(uuid.NewV7()))
    41  	return &id
    42  }
    43  
    44  func NewBlockid(segid *Segmentid, fnum, blknum uint16) *Blockid {
    45  	var id Blockid
    46  	size := SegmentIdSize
    47  	copy(id[:size], segid[:])
    48  	copy(id[size:size+2], types.EncodeUint16(&fnum))
    49  	copy(id[size+2:size+4], types.EncodeUint16(&blknum))
    50  	return &id
    51  }
    52  
    53  func NewObjectid() *ObjectId {
    54  	sid := types.Uuid(uuid.Must(uuid.NewV7()))
    55  	var oid ObjectId
    56  	copy(oid[:types.UuidSize], sid[:])
    57  	return &oid
    58  }
    59  
    60  func NewObjectidWithSegmentIDAndNum(sid *Segmentid, num uint16) *ObjectId {
    61  	var oid ObjectId
    62  	copy(oid[:types.UuidSize], sid[:])
    63  	copy(oid[types.UuidSize:], types.EncodeUint16(&num))
    64  	return &oid
    65  }
    66  
    67  func NewBlockidWithObjectID(segid *ObjectId, blknum uint16) *Blockid {
    68  	var bid Blockid
    69  	size := types.ObjectidSize
    70  	copy(bid[:size], segid[:])
    71  	copy(bid[size:size+2], types.EncodeUint16(&blknum))
    72  	return &bid
    73  }
    74  
    75  func NewRowid(blkid *Blockid, offset uint32) *types.Rowid {
    76  	var rowid types.Rowid
    77  	size := types.BlockidSize
    78  	copy(rowid[:size], blkid[:])
    79  	copy(rowid[size:size+4], types.EncodeUint32(&offset))
    80  	return &rowid
    81  }
    82  
    83  func BuildObjectBlockid(name ObjectName, sequence uint16) *Blockid {
    84  	var id Blockid
    85  	copy(id[:], name[0:NameStringOff])
    86  	copy(id[NameStringOff:], types.EncodeUint16(&sequence))
    87  	return &id
    88  }
    89  
    90  func Str2Blockid(id string) *Blockid {
    91  	var blkid Blockid
    92  	copy(blkid[:], id)
    93  	return &blkid
    94  }
    95  
    96  func ToObjectNameShort(blkID *Blockid) *ObjectNameShort {
    97  	return (*ObjectNameShort)(unsafe.Pointer(&blkID[0]))
    98  }
    99  
   100  func IsBlockInObject(blkID *types.Blockid, objID *ObjectName) bool {
   101  	buf := unsafe.Slice((*byte)(unsafe.Pointer(&blkID[0])), ObjectNameLen)
   102  	return bytes.Equal(buf, *objID)
   103  }
   104  
   105  func ToSegmentId(blkID *Blockid) *Segmentid {
   106  	return (*Segmentid)(unsafe.Pointer(blkID))
   107  }
   108  
   109  func IsEmptySegid(id *Segmentid) bool {
   110  	return bytes.Equal(id[:], emptySegmentId[:])
   111  }
   112  
   113  func IsEmptyBlkid(id *Blockid) bool {
   114  	return bytes.Equal(id[:], emptyBlockId[:])
   115  }
   116  
   117  // some id hacks
   118  
   119  // used only in some special cases
   120  func HackU64ToRowid(v uint64) Rowid {
   121  	var id Rowid
   122  	copy(id[:], types.EncodeUint64(&v))
   123  	return id
   124  }
   125  
   126  // used only in some special cases
   127  func HackRowidToU64(id Rowid) uint64 {
   128  	return types.DecodeUint64(id[:])
   129  }
   130  
   131  // used only in some special cases
   132  func HackBlockid2Rowid(id *Blockid) Rowid {
   133  	var rowid Rowid
   134  	copy(rowid[:], id[:])
   135  	return rowid
   136  }
   137  
   138  // used only in some special cases
   139  func HackSegid2Rowid(id *Segmentid) Rowid {
   140  	var rowid Rowid
   141  	copy(rowid[:], id[:])
   142  	return rowid
   143  }
   144  
   145  // used only in some special cases
   146  func HackObjid2Rowid(id *ObjectId) Rowid {
   147  	var rowid Rowid
   148  	copy(rowid[:], id[:])
   149  	return rowid
   150  }
   151  
   152  // used only in some special cases
   153  func HackBytes2Rowid(bs []byte) Rowid {
   154  	var rowid types.Rowid
   155  	if size := len(bs); size <= types.RowidSize {
   156  		copy(rowid[:size], bs[:size])
   157  	} else {
   158  		hasher := fnv.New128()
   159  		hasher.Write(bs)
   160  		hasher.Sum(rowid[:0])
   161  	}
   162  	return rowid
   163  }