github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/tables/indexwrapper/meta.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 indexwrapper
    16  
    17  import (
    18  	"bytes"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    22  )
    23  
    24  type IndexType uint8
    25  
    26  const (
    27  	InvalidIndexType IndexType = iota
    28  	BlockZoneMapIndex
    29  	SegmentZoneMapIndex
    30  	StaticFilterIndex
    31  	ARTIndex
    32  )
    33  
    34  type IndexMeta struct {
    35  	IdxType     IndexType
    36  	CompType    common.CompressType
    37  	ColIdx      uint16
    38  	InternalIdx uint16
    39  	Size        uint32
    40  	RawSize     uint32
    41  }
    42  
    43  func NewEmptyIndexMeta() *IndexMeta {
    44  	return &IndexMeta{
    45  		IdxType:  InvalidIndexType,
    46  		CompType: common.Plain,
    47  	}
    48  }
    49  
    50  func (meta *IndexMeta) SetIndexType(typ IndexType) {
    51  	meta.IdxType = typ
    52  }
    53  
    54  func (meta *IndexMeta) SetCompressType(typ common.CompressType) {
    55  	meta.CompType = typ
    56  }
    57  
    58  func (meta *IndexMeta) SetIndexedColumn(colIdx uint16) {
    59  	meta.ColIdx = colIdx
    60  }
    61  
    62  //func (meta *IndexMeta) SetPartOffset(offset uint32) {
    63  //	meta.PartOffset = offset
    64  //}
    65  
    66  //func (meta *IndexMeta) SetStartOffset(offset uint32) {
    67  //	meta.StartOffset = offset
    68  //}
    69  
    70  func (meta *IndexMeta) SetInternalIndex(idx uint16) {
    71  	meta.InternalIdx = idx
    72  }
    73  
    74  func (meta *IndexMeta) SetSize(raw, exact uint32) {
    75  	meta.RawSize = raw
    76  	meta.Size = exact
    77  }
    78  
    79  func (meta *IndexMeta) Marshal() ([]byte, error) {
    80  	var buf bytes.Buffer
    81  	buf.Write(types.EncodeFixed(uint8(meta.IdxType)))
    82  	buf.Write(types.EncodeFixed(uint8(meta.CompType)))
    83  	buf.Write(types.EncodeFixed(meta.ColIdx))
    84  	buf.Write(types.EncodeFixed(meta.InternalIdx))
    85  	buf.Write(types.EncodeFixed(meta.Size))
    86  	buf.Write(types.EncodeFixed(meta.RawSize))
    87  	return buf.Bytes(), nil
    88  }
    89  
    90  func (meta *IndexMeta) Unmarshal(buf []byte) error {
    91  	meta.IdxType = IndexType(types.DecodeFixed[uint8](buf[:1]))
    92  	buf = buf[1:]
    93  	meta.CompType = common.CompressType(types.DecodeFixed[uint8](buf[1:]))
    94  	buf = buf[1:]
    95  	meta.ColIdx = types.DecodeFixed[uint16](buf[:2])
    96  	buf = buf[2:]
    97  	meta.InternalIdx = types.DecodeFixed[uint16](buf[:2])
    98  	buf = buf[2:]
    99  	meta.Size = types.DecodeFixed[uint32](buf[:4])
   100  	buf = buf[4:]
   101  	meta.RawSize = types.DecodeFixed[uint32](buf[:4])
   102  	return nil
   103  }
   104  
   105  type IndicesMeta struct {
   106  	Metas []IndexMeta
   107  }
   108  
   109  func NewEmptyIndicesMeta() *IndicesMeta {
   110  	return &IndicesMeta{
   111  		Metas: make([]IndexMeta, 0),
   112  	}
   113  }
   114  
   115  func (metas *IndicesMeta) AddIndex(meta ...IndexMeta) {
   116  	metas.Metas = append(metas.Metas, meta...)
   117  }
   118  
   119  func (metas *IndicesMeta) Marshal() ([]byte, error) {
   120  	var buf bytes.Buffer
   121  	buf.Write(types.EncodeFixed(uint8(len(metas.Metas))))
   122  	for _, meta := range metas.Metas {
   123  		v, err := meta.Marshal()
   124  		if err != nil {
   125  			return nil, err
   126  		}
   127  		buf.Write(types.EncodeFixed(uint32(len(v))))
   128  		buf.Write(v)
   129  	}
   130  	return buf.Bytes(), nil
   131  }
   132  
   133  func (metas *IndicesMeta) Unmarshal(buf []byte) error {
   134  	count := types.DecodeFixed[uint8](buf[:1])
   135  	buf = buf[1:]
   136  	metas.Metas = make([]IndexMeta, 0)
   137  	for i := uint8(0); i < count; i++ {
   138  		size := types.DecodeFixed[uint32](buf[:4])
   139  		buf = buf[4:]
   140  		metaBuf := buf[:size]
   141  		buf = buf[size:]
   142  		var meta IndexMeta
   143  		if err := meta.Unmarshal(metaBuf); err != nil {
   144  			return err
   145  		}
   146  		metas.Metas = append(metas.Metas, meta)
   147  	}
   148  	return nil
   149  }