github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/tables/evictable/bf.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 evictable
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/objectio"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/buffer"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/buffer/base"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/index"
    26  )
    27  
    28  type BfNode struct {
    29  	*buffer.Node
    30  
    31  	Bf index.StaticFilter
    32  
    33  	metaKey, bfKey string
    34  	mgr            base.INodeManager
    35  	colMetaFactory EvictableNodeFactory
    36  }
    37  
    38  func NewBfNode(
    39  	idx uint16,
    40  	typ types.Type,
    41  	metaloc string,
    42  	bfKey, metaKey string,
    43  	mgr base.INodeManager,
    44  	fs *objectio.ObjectFS,
    45  ) (node *BfNode, err error) {
    46  	node = &BfNode{
    47  		bfKey:   bfKey,
    48  		metaKey: metaKey,
    49  		mgr:     mgr,
    50  		colMetaFactory: func() (base.INode, error) {
    51  			return NewColumnMetaNode(
    52  				idx,
    53  				typ,
    54  				metaloc,
    55  				metaKey,
    56  				mgr,
    57  				fs), nil
    58  		},
    59  	}
    60  
    61  	h, err := PinEvictableNode(mgr, metaKey, node.colMetaFactory)
    62  	if err != nil {
    63  		return
    64  	}
    65  	defer h.Close()
    66  	meta := h.GetNode().(*ColumnMetaNode)
    67  	size := meta.GetMeta().GetBloomFilter().OriginSize()
    68  	node.Node = buffer.NewNode(node, mgr, bfKey, uint64(size))
    69  	node.LoadFunc = node.onLoad
    70  	node.UnloadFunc = node.onUnload
    71  	node.HardEvictableFunc = func() bool { return true }
    72  	return
    73  }
    74  
    75  func (n *BfNode) onLoad() {
    76  	var h base.INodeHandle
    77  	var err error
    78  	h, err = PinEvictableNode(n.mgr, n.metaKey, n.colMetaFactory)
    79  	if err != nil {
    80  		panic(err)
    81  	}
    82  	metaNode := h.GetNode().(*ColumnMetaNode)
    83  	h.Close()
    84  
    85  	stat := metaNode.GetMeta()
    86  	compressTyp := stat.GetAlg()
    87  	// Do IO, fetch bloomfilter buf
    88  	fsData, err := metaNode.GetIndex(context.Background(), objectio.BloomFilterType, nil)
    89  	if err != nil {
    90  		panic(err)
    91  	}
    92  	rawSize := stat.GetBloomFilter().OriginSize()
    93  	buf := make([]byte, rawSize)
    94  	data := fsData.(*objectio.BloomFilter).GetData()
    95  	if err = common.Decompress(data, buf, common.CompressType(compressTyp)); err != nil {
    96  		panic(err)
    97  	}
    98  	n.Bf, err = index.NewBinaryFuseFilterFromSource(buf)
    99  	if err != nil {
   100  		panic(err)
   101  	}
   102  }
   103  
   104  func (n *BfNode) onUnload() {
   105  	n.Bf = nil
   106  }