github.com/matrixorigin/matrixone@v0.7.0/pkg/txn/storage/memorystorage/memtable/index_iter.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 memtable
    16  
    17  import (
    18  	"database/sql"
    19  	"errors"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/txn/storage/memorystorage/memorytable"
    22  	"github.com/tidwall/btree"
    23  )
    24  
    25  type IndexIter[
    26  	K memorytable.Ordered[K],
    27  	V any,
    28  ] struct {
    29  	iter     btree.GenericIter[*IndexEntry[K, V]]
    30  	rows     *btree.BTreeG[*PhysicalRow[K, V]]
    31  	readTime Time
    32  	tx       *Transaction
    33  	min      Tuple
    34  	max      Tuple
    35  }
    36  
    37  func (t *Table[K, V, R]) NewIndexIter(tx *Transaction, min Tuple, max Tuple) *IndexIter[K, V] {
    38  	state := t.state.Load()
    39  	return t.newIndexIter(
    40  		state.indexes.Copy().Iter(),
    41  		state.rows,
    42  		tx,
    43  		min,
    44  		max,
    45  	)
    46  }
    47  
    48  func (t *Table[K, V, R]) newIndexIter(
    49  	iter btree.GenericIter[*IndexEntry[K, V]],
    50  	rows *btree.BTreeG[*PhysicalRow[K, V]],
    51  	tx *Transaction,
    52  	min Tuple,
    53  	max Tuple,
    54  ) *IndexIter[K, V] {
    55  	return &IndexIter[K, V]{
    56  		iter:     iter,
    57  		rows:     rows,
    58  		readTime: tx.Time,
    59  		tx:       tx,
    60  		min:      min,
    61  		max:      max,
    62  	}
    63  }
    64  
    65  func (i *IndexIter[K, V]) First() bool {
    66  	if !i.iter.First() {
    67  		return false
    68  	}
    69  	if !i.iter.Seek(&IndexEntry[K, V]{
    70  		Index: i.min,
    71  	}) {
    72  		return false
    73  	}
    74  	return i.seekToValid()
    75  }
    76  
    77  func (i *IndexIter[K, V]) seekToValid() bool {
    78  	for {
    79  		entry := i.iter.Item()
    80  		// check range
    81  		if entry.Index.Less(i.min) {
    82  			return false
    83  		}
    84  		if i.max.Less(entry.Index) {
    85  			return false
    86  		}
    87  		// check physical row
    88  		physicalRow := getRowByKey(i.rows, entry.Key)
    89  		if physicalRow == nil {
    90  			if !i.iter.Next() {
    91  				return false
    92  			}
    93  			continue
    94  		}
    95  		// check current version
    96  		currentVersion, err := physicalRow.readVersion(i.readTime, i.tx)
    97  		if err != nil {
    98  			if errors.Is(err, sql.ErrNoRows) {
    99  				if !i.iter.Next() {
   100  					return false
   101  				}
   102  				continue
   103  			}
   104  			panic(err)
   105  		}
   106  		if currentVersion.ID != entry.VersionID {
   107  			if !i.iter.Next() {
   108  				return false
   109  			}
   110  			continue
   111  		}
   112  		return true
   113  	}
   114  }
   115  
   116  func (i *IndexIter[K, V]) Next() bool {
   117  	if !i.iter.Next() {
   118  		return false
   119  	}
   120  	return i.seekToValid()
   121  }
   122  
   123  func (i *IndexIter[K, V]) Close() error {
   124  	i.iter.Release()
   125  	return nil
   126  }
   127  
   128  func (i *IndexIter[K, V]) Item() *IndexEntry[K, V] {
   129  	return i.iter.Item()
   130  }