github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/disttae/logtailreplay/primary_key_utils.go (about)

     1  // Copyright 2023 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 logtailreplay
    16  
    17  import (
    18  	"bytes"
    19  	"math"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  )
    23  
    24  func (p *PartitionState) PKExistInMemBetween(
    25  	from types.TS,
    26  	to types.TS,
    27  	keys [][]byte,
    28  ) (bool, bool) {
    29  	iter := p.primaryIndex.Copy().Iter()
    30  	pivot := RowEntry{
    31  		Time: types.BuildTS(math.MaxInt64, math.MaxUint32),
    32  	}
    33  	idxEntry := &PrimaryIndexEntry{}
    34  	defer iter.Release()
    35  
    36  	for _, key := range keys {
    37  
    38  		idxEntry.Bytes = key
    39  
    40  		for ok := iter.Seek(idxEntry); ok; ok = iter.Next() {
    41  
    42  			entry := iter.Item()
    43  
    44  			if !bytes.Equal(entry.Bytes, key) {
    45  				break
    46  			}
    47  
    48  			if entry.Time.GreaterEq(&from) {
    49  				return true, false
    50  			}
    51  
    52  			//some legacy deletion entries may not be indexed since old TN maybe
    53  			//don't take pk in log tail when delete row , so check all rows for changes.
    54  			pivot.BlockID = entry.BlockID
    55  			pivot.RowID = entry.RowID
    56  			rowIter := p.rows.Iter()
    57  			seek := false
    58  			for {
    59  				if !seek {
    60  					seek = true
    61  					if !rowIter.Seek(pivot) {
    62  						break
    63  					}
    64  				} else {
    65  					if !rowIter.Next() {
    66  						break
    67  					}
    68  				}
    69  				row := rowIter.Item()
    70  				if row.BlockID.Compare(entry.BlockID) != 0 {
    71  					break
    72  				}
    73  				if !row.RowID.Equal(entry.RowID) {
    74  					break
    75  				}
    76  				if row.Time.GreaterEq(&from) {
    77  					rowIter.Release()
    78  					return true, false
    79  				}
    80  			}
    81  			rowIter.Release()
    82  		}
    83  
    84  		iter.First()
    85  	}
    86  
    87  	p.shared.Lock()
    88  	lastFlushTimestamp := p.shared.lastFlushTimestamp
    89  	p.shared.Unlock()
    90  	if lastFlushTimestamp.LessEq(&from) {
    91  		return false, false
    92  	}
    93  	return false, true
    94  }