github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/model/codec.go (about)

     1  // Copyright 2022 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 model
    16  
    17  import (
    18  	"encoding/binary"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  )
    22  
    23  // EncodeBlockKeyPrefix [48 Bit (BlockID) + 48 Bit (SegmentID)]
    24  func EncodeBlockKeyPrefix(segmentId, blockId uint64) []byte {
    25  	buf := make([]byte, 12)
    26  	tempBuf := make([]byte, 8)
    27  	binary.BigEndian.PutUint64(tempBuf, segmentId)
    28  	copy(buf[0:], tempBuf[2:])
    29  	binary.BigEndian.PutUint64(tempBuf, blockId)
    30  	copy(buf[6:], tempBuf[2:])
    31  	return buf
    32  }
    33  
    34  func DecodeBlockKeyPrefix(rowid types.Rowid) (segmentId, blockId uint64) {
    35  	tempBuf := make([]byte, 8)
    36  	copy(tempBuf[2:], rowid[0:6])
    37  	segmentId = binary.BigEndian.Uint64(tempBuf)
    38  	copy(tempBuf[2:], rowid[6:12])
    39  	blockId = binary.BigEndian.Uint64(tempBuf)
    40  	return
    41  }
    42  
    43  func EncodePhyAddrKey(segmentId, blockId uint64, offset uint32) types.Rowid {
    44  	prefix := EncodeBlockKeyPrefix(segmentId, blockId)
    45  	return EncodePhyAddrKeyWithPrefix(prefix, offset)
    46  }
    47  
    48  func EncodePhyAddrKeyWithPrefix(prefix []byte, offset uint32) types.Rowid {
    49  	var rowid types.Rowid
    50  	copy(rowid[:12], prefix)
    51  	binary.BigEndian.PutUint32(rowid[12:16], offset)
    52  	return rowid
    53  }
    54  
    55  func DecodePhyAddrKeyFromValue(v any) (segmentId, blockId uint64, offset uint32) {
    56  	rowid := v.(types.Rowid)
    57  	return DecodePhyAddrKey(rowid)
    58  }
    59  
    60  func DecodePhyAddrKey(src types.Rowid) (segmentId, blockId uint64, offset uint32) {
    61  	segmentId, blockId = DecodeBlockKeyPrefix(src)
    62  	offset = binary.BigEndian.Uint32(src[12:])
    63  	return
    64  }