github.com/matrixorigin/matrixone@v0.7.0/pkg/objectio/index.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  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    19  	"github.com/matrixorigin/matrixone/pkg/compress"
    20  	"github.com/pierrec/lz4"
    21  )
    22  
    23  type IndexDataType uint8
    24  
    25  const (
    26  	ZoneMapType IndexDataType = iota
    27  	BloomFilterType
    28  )
    29  
    30  const ZoneMapMinSize = 32
    31  const ZoneMapMaxSize = 32
    32  
    33  type IndexData interface {
    34  	Write(writer *ObjectWriter, block *Block) error
    35  	GetIdx() uint16
    36  }
    37  
    38  type ZoneMap struct {
    39  	idx uint16
    40  	buf []byte
    41  }
    42  
    43  func NewZoneMap(idx uint16, buf []byte) (IndexData, error) {
    44  	if len(buf) != ZoneMapMinSize+ZoneMapMaxSize {
    45  		return nil, moerr.NewInternalErrorNoCtx("object io: New ZoneMap failed")
    46  	}
    47  	zoneMap := &ZoneMap{
    48  		idx: idx,
    49  		buf: buf,
    50  	}
    51  	return zoneMap, nil
    52  }
    53  
    54  func (z *ZoneMap) GetIdx() uint16 {
    55  	return z.idx
    56  }
    57  
    58  func (z *ZoneMap) Write(_ *ObjectWriter, block *Block) error {
    59  	var err error
    60  	block.columns[z.idx].(*ColumnBlock).meta.zoneMap = *z
    61  	return err
    62  }
    63  
    64  func (z *ZoneMap) GetData() []byte {
    65  	return z.buf
    66  }
    67  
    68  type BloomFilter struct {
    69  	idx uint16
    70  	alg uint8
    71  	buf []byte
    72  }
    73  
    74  func NewBloomFilter(idx uint16, alg uint8, buf []byte) IndexData {
    75  	bloomFilter := &BloomFilter{
    76  		idx: idx,
    77  		alg: alg,
    78  		buf: buf,
    79  	}
    80  	return bloomFilter
    81  }
    82  
    83  func (b *BloomFilter) GetIdx() uint16 {
    84  	return b.idx
    85  }
    86  
    87  func (b *BloomFilter) GetData() []byte {
    88  	return b.buf
    89  }
    90  
    91  func (b *BloomFilter) Write(writer *ObjectWriter, block *Block) error {
    92  	var err error
    93  	data := make([]byte, lz4.CompressBlockBound(len(b.buf)))
    94  	if data, err = compress.Compress(b.buf, data, compress.Lz4); err != nil {
    95  		return err
    96  	}
    97  	offset, length, err := writer.buffer.Write(data)
    98  	if err != nil {
    99  		return err
   100  	}
   101  	block.columns[b.idx].(*ColumnBlock).meta.bloomFilter.offset = uint32(offset)
   102  	block.columns[b.idx].(*ColumnBlock).meta.bloomFilter.length = uint32(length)
   103  	block.columns[b.idx].(*ColumnBlock).meta.bloomFilter.originSize = uint32(len(b.buf))
   104  	return err
   105  }