github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/extent.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  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  )
    22  
    23  // Alg | Offset | Length | OriginSize
    24  // ----|--------|--------|------------
    25  // 1   | 4      | 4      | 4
    26  // Alg: Specifies the compression algorithm
    27  // Offset: The offset of the compressed data in the file
    28  // Length: The length of the compressed data
    29  // OriginSize: The length of the original data
    30  type Extent []byte
    31  
    32  const (
    33  	extentAlgLen    = 1
    34  	extentOffsetOff = extentAlgLen
    35  	extentOffsetLen = 4
    36  	extentLengthOff = extentOffsetOff + extentOffsetLen
    37  	extentLengthLen = 4
    38  	extentOriginOff = extentLengthOff + extentLengthLen
    39  	extentOriginLen = 4
    40  	ExtentSize      = extentOriginOff + extentOriginLen
    41  )
    42  
    43  func NewExtent(alg uint8, offset, length, originSize uint32) Extent {
    44  	var extent [ExtentSize]byte
    45  	copy(extent[:extentAlgLen], types.EncodeUint8(&alg))
    46  	copy(extent[extentOffsetOff:extentLengthOff], types.EncodeUint32(&offset))
    47  	copy(extent[extentLengthOff:extentOriginOff], types.EncodeUint32(&length))
    48  	copy(extent[extentOriginOff:ExtentSize], types.EncodeUint32(&originSize))
    49  	return extent[:]
    50  }
    51  
    52  func (ex Extent) Alg() uint8 {
    53  	return types.DecodeUint8(ex[:extentAlgLen])
    54  }
    55  
    56  func (ex Extent) SetAlg(alg uint8) {
    57  	copy(ex[:extentAlgLen], types.EncodeUint8(&alg))
    58  }
    59  
    60  func (ex Extent) End() uint32 {
    61  	return ex.Offset() + ex.Length()
    62  }
    63  
    64  func (ex Extent) Offset() uint32 {
    65  	return types.DecodeUint32(ex[extentOffsetOff:extentLengthOff])
    66  }
    67  
    68  func (ex Extent) SetOffset(offset uint32) {
    69  	copy(ex[extentOffsetOff:extentLengthOff], types.EncodeUint32(&offset))
    70  }
    71  
    72  func (ex Extent) Length() uint32 {
    73  	return types.DecodeUint32(ex[extentLengthOff:extentOriginOff])
    74  }
    75  
    76  func (ex Extent) SetLength(length uint32) {
    77  	copy(ex[extentLengthOff:extentOriginOff], types.EncodeUint32(&length))
    78  }
    79  
    80  func (ex Extent) OriginSize() uint32 {
    81  	return types.DecodeUint32(ex[extentOriginOff:ExtentSize])
    82  }
    83  
    84  func (ex Extent) SetOriginSize(originSize uint32) {
    85  	copy(ex[extentOriginOff:ExtentSize], types.EncodeUint32(&originSize))
    86  }
    87  
    88  func (ex Extent) String() string {
    89  	return fmt.Sprintf("%d_%d_%d_%d", ex.Alg(), ex.Offset(), ex.Length(), ex.OriginSize())
    90  }