github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/block_info.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 objectio
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"unsafe"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  )
    24  
    25  type ObjectLocation [LocationLen]byte
    26  
    27  const (
    28  	BlockInfoType = uint16(1)
    29  
    30  	V1 = uint16(1)
    31  )
    32  
    33  type InfoHeader struct {
    34  	Type, Version uint16
    35  }
    36  
    37  func EncodeInfoHeader(h InfoHeader) uint32 {
    38  	return uint32(h.Type)<<16 | uint32(h.Version)
    39  }
    40  
    41  func DecodeInfoHeader(h uint32) InfoHeader {
    42  	return InfoHeader{
    43  		Type:    uint16(h >> 16),
    44  		Version: uint16(h),
    45  	}
    46  }
    47  
    48  var (
    49  	EmptyBlockInfo      = BlockInfo{}
    50  	EmptyBlockInfoBytes = EncodeBlockInfo(EmptyBlockInfo)
    51  )
    52  
    53  const (
    54  	BlockInfoSize = int(unsafe.Sizeof(EmptyBlockInfo))
    55  )
    56  
    57  type BlockInfo struct {
    58  	BlockID    types.Blockid
    59  	EntryState bool
    60  	Sorted     bool
    61  	MetaLoc    ObjectLocation
    62  	DeltaLoc   ObjectLocation
    63  	CommitTs   types.TS
    64  	SegmentID  types.Uuid
    65  
    66  	//TODO:: putting them here is a bad idea, remove
    67  	//this block can be distributed to remote nodes.
    68  	CanRemote    bool
    69  	PartitionNum int
    70  }
    71  
    72  func (b *BlockInfo) MetaLocation() Location {
    73  	return b.MetaLoc[:]
    74  }
    75  
    76  func (b *BlockInfo) SetMetaLocation(metaLoc Location) {
    77  	b.MetaLoc = *(*[LocationLen]byte)(unsafe.Pointer(&metaLoc[0]))
    78  }
    79  
    80  func (b *BlockInfo) DeltaLocation() Location {
    81  	return b.DeltaLoc[:]
    82  }
    83  
    84  func (b *BlockInfo) SetDeltaLocation(deltaLoc Location) {
    85  	b.DeltaLoc = *(*[LocationLen]byte)(unsafe.Pointer(&deltaLoc[0]))
    86  }
    87  
    88  // XXX info is passed in by value.   The use of unsafe here will cost
    89  // an allocation and copy.  BlockInfo is not small therefore this is
    90  // not exactly cheap.   However, caller of this function will keep a
    91  // reference to the buffer.  See txnTable.rangesOnePart.
    92  // ranges is *[][]byte.
    93  func EncodeBlockInfo(info BlockInfo) []byte {
    94  	return unsafe.Slice((*byte)(unsafe.Pointer(&info)), BlockInfoSize)
    95  }
    96  
    97  func DecodeBlockInfo(buf []byte) *BlockInfo {
    98  	return (*BlockInfo)(unsafe.Pointer(&buf[0]))
    99  }
   100  
   101  type BlockInfoSlice []byte
   102  
   103  func (s *BlockInfoSlice) Get(i int) *BlockInfo {
   104  	return DecodeBlockInfo((*s)[i*BlockInfoSize:])
   105  }
   106  
   107  func (s *BlockInfoSlice) GetBytes(i int) []byte {
   108  	return (*s)[i*BlockInfoSize : (i+1)*BlockInfoSize]
   109  }
   110  
   111  func (s *BlockInfoSlice) Set(i int, info *BlockInfo) {
   112  	copy((*s)[i*BlockInfoSize:], EncodeBlockInfo(*info))
   113  }
   114  
   115  func (s *BlockInfoSlice) Len() int {
   116  	return len(*s) / BlockInfoSize
   117  }
   118  
   119  func (s *BlockInfoSlice) Size() int {
   120  	return len(*s)
   121  }
   122  
   123  func (s *BlockInfoSlice) Slice(i, j int) []byte {
   124  	return (*s)[i*BlockInfoSize : j*BlockInfoSize]
   125  }
   126  
   127  func (s *BlockInfoSlice) Append(bs []byte) {
   128  	*s = append(*s, bs...)
   129  }
   130  
   131  func (s *BlockInfoSlice) AppendBlockInfo(info BlockInfo) {
   132  	*s = append(*s, EncodeBlockInfo(info)...)
   133  }
   134  
   135  func (s *BlockInfoSlice) SetBytes(bs []byte) {
   136  	*s = bs
   137  }
   138  
   139  func (s *BlockInfoSlice) GetAllBytes() []byte {
   140  	return *s
   141  }
   142  
   143  func (s *BlockInfoSlice) String() string {
   144  	var buf bytes.Buffer
   145  	buf.WriteString(fmt.Sprintf("BlockInfoSlice[Len=%d]:\n", s.Len()))
   146  	for i := 0; i < s.Len(); i++ {
   147  		buf.WriteString(s.Get(i).BlockID.String())
   148  		buf.WriteByte('\n')
   149  	}
   150  	return buf.String()
   151  }
   152  
   153  type BackupObject struct {
   154  	Location Location
   155  	CrateTS  types.TS
   156  	DropTS   types.TS
   157  	NeedCopy bool
   158  }