github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/logstore/entry/desc.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 entry
    16  
    17  import (
    18  	"encoding/binary"
    19  	"io"
    20  	"unsafe"
    21  )
    22  
    23  const (
    24  	PayloadSizeOffset = int(unsafe.Sizeof(ETInvalid))
    25  	InfoSizeOffset    = int(unsafe.Sizeof(ETInvalid) + unsafe.Sizeof(uint32(0)))
    26  	DescriptorSize    = int(unsafe.Sizeof(ETInvalid) + 2*unsafe.Sizeof(uint32(0)))
    27  )
    28  
    29  // type u16, payloadsize u32, infosize u32
    30  type descriptor struct {
    31  	descBuf []byte
    32  }
    33  
    34  func newDescriptor() *descriptor {
    35  	return &descriptor{
    36  		descBuf: make([]byte, DescriptorSize),
    37  	}
    38  }
    39  
    40  func (desc *descriptor) IsFlush() bool {
    41  	return desc.GetType() == ETFlush
    42  }
    43  
    44  func (desc *descriptor) IsCheckpoint() bool {
    45  	return desc.GetType() == ETCheckpoint
    46  }
    47  
    48  func (desc *descriptor) SetType(t Type) {
    49  	binary.BigEndian.PutUint16(desc.descBuf, t)
    50  }
    51  
    52  func (desc *descriptor) SetPayloadSize(size int) {
    53  	binary.BigEndian.PutUint32(desc.descBuf[PayloadSizeOffset:], uint32(size))
    54  }
    55  
    56  func (desc *descriptor) SetInfoSize(size int) {
    57  	binary.BigEndian.PutUint32(desc.descBuf[InfoSizeOffset:], uint32(size))
    58  }
    59  
    60  func (desc *descriptor) reset() {
    61  	desc.SetType(ETInvalid)
    62  	desc.SetPayloadSize(0)
    63  	desc.SetInfoSize(0)
    64  }
    65  
    66  func (desc *descriptor) GetMetaBuf() []byte {
    67  	return desc.descBuf
    68  }
    69  
    70  func (desc *descriptor) GetType() Type {
    71  	return binary.BigEndian.Uint16(desc.descBuf)
    72  }
    73  
    74  func (desc *descriptor) GetPayloadSize() int {
    75  	return int(binary.BigEndian.Uint32(desc.descBuf[PayloadSizeOffset:]))
    76  }
    77  
    78  func (desc *descriptor) GetInfoSize() int {
    79  	return int(binary.BigEndian.Uint32(desc.descBuf[InfoSizeOffset:]))
    80  }
    81  
    82  func (desc *descriptor) GetMetaSize() int {
    83  	return DescriptorSize
    84  }
    85  func (desc *descriptor) TotalSize() int {
    86  	return DescriptorSize + desc.GetPayloadSize() + desc.GetInfoSize()
    87  }
    88  
    89  func (desc *descriptor) TotalSizeExpectMeta() int {
    90  	return desc.GetPayloadSize() + desc.GetInfoSize()
    91  }
    92  
    93  func (desc *descriptor) WriteTo(w io.Writer) (int64, error) {
    94  	n, err := w.Write(desc.descBuf)
    95  	return int64(n), err
    96  }
    97  
    98  func (desc *descriptor) ReadMeta(r io.Reader) (int, error) {
    99  	return r.Read(desc.descBuf)
   100  }