github.com/matrixorigin/matrixone@v1.2.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  	"io"
    19  	"unsafe"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/objectio"
    23  )
    24  
    25  // type    uint16
    26  // version uint16
    27  // payload size uint32
    28  // info size uint32
    29  const (
    30  	VersionOffset     = int(unsafe.Sizeof(uint16(0)))
    31  	PayloadSizeOffset = VersionOffset + int(unsafe.Sizeof(IOET_WALEntry_Invalid))
    32  	InfoSizeOffset    = VersionOffset + int(unsafe.Sizeof(IOET_WALEntry_Invalid)+unsafe.Sizeof(uint32(0)))
    33  	DescriptorSize    = VersionOffset + int(unsafe.Sizeof(IOET_WALEntry_Invalid)+2*unsafe.Sizeof(uint32(0)))
    34  )
    35  
    36  const (
    37  	IOET_WALEntry_V1              uint16 = 1
    38  	IOET_WALEntry_Invalid         uint16 = 2000
    39  	IOET_WALEntry_Checkpoint      uint16 = 2001
    40  	IOET_WALEntry_PostCommit      uint16 = 2002
    41  	IOET_WALEntry_Uncommitted     uint16 = 2003
    42  	IOET_WALEntry_Txn             uint16 = 2004
    43  	IOET_WALEntry_Test            uint16 = 2005
    44  	IOET_WALEntry_CustomizedStart uint16 = 2006
    45  
    46  	IOET_WALEntry_CurrVer = IOET_WALEntry_V1
    47  )
    48  
    49  func init() {
    50  	objectio.RegisterIOEnrtyCodec(
    51  		objectio.IOEntryHeader{
    52  			Type:    IOET_WALEntry_Checkpoint,
    53  			Version: IOET_WALEntry_V1,
    54  		}, nil, UnmarshalEntry,
    55  	)
    56  	objectio.RegisterIOEnrtyCodec(
    57  		objectio.IOEntryHeader{
    58  			Type:    IOET_WALEntry_PostCommit,
    59  			Version: IOET_WALEntry_V1,
    60  		}, nil, UnmarshalEntry,
    61  	)
    62  	objectio.RegisterIOEnrtyCodec(
    63  		objectio.IOEntryHeader{
    64  			Type:    IOET_WALEntry_Uncommitted,
    65  			Version: IOET_WALEntry_V1,
    66  		}, nil, UnmarshalEntry,
    67  	)
    68  	objectio.RegisterIOEnrtyCodec(
    69  		objectio.IOEntryHeader{
    70  			Type:    IOET_WALEntry_Test,
    71  			Version: IOET_WALEntry_V1,
    72  		}, nil, UnmarshalEntry,
    73  	)
    74  	objectio.RegisterIOEnrtyCodec(
    75  		objectio.IOEntryHeader{
    76  			Type:    IOET_WALEntry_Txn,
    77  			Version: IOET_WALEntry_V1,
    78  		}, nil, UnmarshalEntry,
    79  	)
    80  }
    81  
    82  func UnmarshalEntry(b []byte) (any, error) {
    83  	info := NewEmptyInfo()
    84  	err := info.Unmarshal(b)
    85  	return info, err
    86  }
    87  
    88  // type u16, version u16, payloadsize u32, infosize u32
    89  type descriptor struct {
    90  	descBuf []byte
    91  }
    92  
    93  func newDescriptor() *descriptor {
    94  	return &descriptor{
    95  		descBuf: make([]byte, DescriptorSize),
    96  	}
    97  }
    98  
    99  func (desc *descriptor) SetVersion(t uint16) {
   100  	copy(desc.descBuf[VersionOffset:PayloadSizeOffset], types.EncodeUint16(&t))
   101  }
   102  
   103  func (desc *descriptor) SetType(t Type) {
   104  	copy(desc.descBuf[:VersionOffset], types.EncodeUint16(&t))
   105  }
   106  
   107  func (desc *descriptor) SetPayloadSize(size int) {
   108  	s := uint32(size)
   109  	copy(desc.descBuf[PayloadSizeOffset:InfoSizeOffset], types.EncodeUint32(&s))
   110  }
   111  
   112  func (desc *descriptor) SetInfoSize(size int) {
   113  	s := uint32(size)
   114  	copy(desc.descBuf[InfoSizeOffset:], types.EncodeUint32(&s))
   115  }
   116  
   117  func (desc *descriptor) reset() {
   118  	desc.SetType(IOET_WALEntry_Invalid)
   119  	desc.SetPayloadSize(0)
   120  	desc.SetInfoSize(0)
   121  }
   122  
   123  func (desc *descriptor) GetMetaBuf() []byte {
   124  	return desc.descBuf
   125  }
   126  
   127  func (desc *descriptor) GetVersion() uint16 {
   128  	return types.DecodeUint16(desc.descBuf[VersionOffset:PayloadSizeOffset])
   129  }
   130  
   131  func (desc *descriptor) GetType() Type {
   132  	return types.DecodeUint16(desc.descBuf[:VersionOffset])
   133  }
   134  
   135  func (desc *descriptor) GetPayloadSize() int {
   136  	return int(types.DecodeUint32(desc.descBuf[PayloadSizeOffset:InfoSizeOffset]))
   137  }
   138  
   139  func (desc *descriptor) GetInfoSize() int {
   140  	return int(types.DecodeUint32(desc.descBuf[InfoSizeOffset:]))
   141  }
   142  
   143  func (desc *descriptor) GetMetaSize() int {
   144  	return DescriptorSize
   145  }
   146  func (desc *descriptor) TotalSize() int {
   147  	return DescriptorSize + desc.GetPayloadSize() + desc.GetInfoSize()
   148  }
   149  
   150  func (desc *descriptor) TotalSizeExpectMeta() int {
   151  	return desc.GetPayloadSize() + desc.GetInfoSize()
   152  }
   153  
   154  func (desc *descriptor) WriteTo(w io.Writer) (int64, error) {
   155  	n, err := w.Write(desc.descBuf)
   156  	return int64(n), err
   157  }
   158  
   159  func (desc *descriptor) ReadMeta(r io.Reader) (int, error) {
   160  	return r.Read(desc.descBuf)
   161  }