github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/index/indexwrapper/immutindex.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  	"context"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/objectio"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db/dbutils"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/index"
    27  )
    28  
    29  type ImmutIndex struct {
    30  	zm       index.ZM
    31  	bf       objectio.BloomFilter
    32  	location objectio.Location
    33  }
    34  
    35  func NewImmutIndex(
    36  	zm index.ZM,
    37  	bf objectio.BloomFilter,
    38  	location objectio.Location,
    39  ) ImmutIndex {
    40  	return ImmutIndex{
    41  		zm:       zm,
    42  		bf:       bf,
    43  		location: location,
    44  	}
    45  }
    46  
    47  func (idx ImmutIndex) BatchDedup(
    48  	ctx context.Context,
    49  	keys containers.Vector,
    50  	keysZM index.ZM,
    51  	rt *dbutils.Runtime,
    52  	blkID uint32,
    53  ) (sels *nulls.Bitmap, err error) {
    54  	var exist bool
    55  	if keysZM.Valid() {
    56  		if exist = idx.zm.FastIntersect(keysZM); !exist {
    57  			// all keys are not in [min, max]. definitely not
    58  			return
    59  		}
    60  	} else {
    61  		if exist = idx.zm.FastContainsAny(keys.GetDownstreamVector()); !exist {
    62  			// all keys are not in [min, max]. definitely not
    63  			return
    64  		}
    65  	}
    66  
    67  	// some keys are in [min, max]. check bloomfilter for those keys
    68  
    69  	var buf []byte
    70  	if len(idx.bf) > 0 {
    71  		buf = idx.bf.GetBloomFilter(blkID)
    72  	} else {
    73  		var bf objectio.BloomFilter
    74  		if bf, err = objectio.FastLoadBF(
    75  			ctx,
    76  			idx.location,
    77  			false,
    78  			rt.Fs.Service,
    79  		); err != nil {
    80  			return
    81  		}
    82  		buf = bf.GetBloomFilter(blkID)
    83  	}
    84  
    85  	bfIndex := index.NewEmptyBinaryFuseFilter()
    86  	if err = index.DecodeBloomFilter(bfIndex, buf); err != nil {
    87  		return
    88  	}
    89  
    90  	if exist, sels, err = bfIndex.MayContainsAnyKeys(keys); err != nil {
    91  		// check bloomfilter has some unknown error. return err
    92  		err = TranslateError(err)
    93  		return
    94  	} else if !exist {
    95  		// all keys were checked. definitely not
    96  		return
    97  	}
    98  
    99  	err = moerr.GetOkExpectedPossibleDup()
   100  	return
   101  }
   102  
   103  func (idx ImmutIndex) Dedup(
   104  	ctx context.Context, key any, rt *dbutils.Runtime, blkID uint32,
   105  ) (err error) {
   106  	exist := idx.zm.Contains(key)
   107  	// 1. if not in [min, max], key is definitely not found
   108  	if !exist {
   109  		return
   110  	}
   111  	var buf []byte
   112  	if len(idx.bf) > 0 {
   113  		buf = idx.bf.GetBloomFilter(blkID)
   114  	} else {
   115  		var bf objectio.BloomFilter
   116  		if bf, err = objectio.FastLoadBF(
   117  			ctx,
   118  			idx.location,
   119  			false,
   120  			rt.Fs.Service,
   121  		); err != nil {
   122  			return
   123  		}
   124  		buf = bf.GetBloomFilter(blkID)
   125  	}
   126  
   127  	bfIndex := index.NewEmptyBinaryFuseFilter()
   128  	if err = index.DecodeBloomFilter(bfIndex, buf); err != nil {
   129  		return
   130  	}
   131  
   132  	v := types.EncodeValue(key, idx.zm.GetType())
   133  	exist, err = bfIndex.MayContainsKey(v)
   134  	// 2. check bloomfilter has some error. return err
   135  	if err != nil {
   136  		err = TranslateError(err)
   137  		return
   138  	}
   139  	// 3. all keys were checked. definitely not
   140  	if !exist {
   141  		return
   142  	}
   143  	err = moerr.GetOkExpectedPossibleDup()
   144  	return
   145  }