github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/index/tree.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 index
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    22  	art "github.com/plar/go-adaptive-radix-tree"
    23  )
    24  
    25  var _ SecondaryIndex = new(simpleARTMap)
    26  
    27  type IndexMVCCChain struct {
    28  	MVCC []uint32
    29  }
    30  
    31  func NewIndexMVCCChain() *IndexMVCCChain {
    32  	return &IndexMVCCChain{
    33  		MVCC: make([]uint32, 0),
    34  	}
    35  }
    36  
    37  func (chain *IndexMVCCChain) Insert(node uint32) {
    38  	chain.MVCC = append(chain.MVCC, node)
    39  }
    40  
    41  func (chain *IndexMVCCChain) GetRows() []uint32 {
    42  	return chain.MVCC
    43  }
    44  
    45  type simpleARTMap struct {
    46  	tree art.Tree
    47  }
    48  
    49  func NewSimpleARTMap() *simpleARTMap {
    50  	return &simpleARTMap{
    51  		tree: art.New(),
    52  	}
    53  }
    54  
    55  func (art *simpleARTMap) Size() int { return art.tree.Size() }
    56  
    57  func (art *simpleARTMap) Insert(key []byte, offset uint32) (err error) {
    58  	chain := NewIndexMVCCChain()
    59  	chain.Insert(offset)
    60  	old, _ := art.tree.Insert(key, chain)
    61  	if old != nil {
    62  		oldChain := old.(*IndexMVCCChain)
    63  		oldChain.Insert(offset)
    64  		art.tree.Insert(key, old)
    65  	}
    66  	return
    67  }
    68  
    69  func (art *simpleARTMap) BatchInsert(
    70  	keys *vector.Vector,
    71  	offset, length int,
    72  	startRow uint32,
    73  ) (err error) {
    74  	op := func(v []byte, _ bool, i int) error {
    75  		chain := NewIndexMVCCChain()
    76  		chain.Insert(startRow)
    77  		old, _ := art.tree.Insert(v, chain)
    78  		if old != nil {
    79  			oldChain := old.(*IndexMVCCChain)
    80  			oldChain.Insert(startRow)
    81  			art.tree.Insert(v, old)
    82  		}
    83  		startRow++
    84  		return nil
    85  	}
    86  	err = containers.ForeachWindowBytes(keys, offset, length, op, nil)
    87  	return
    88  }
    89  
    90  func (art *simpleARTMap) Delete(key any) (old uint32, err error) {
    91  	return
    92  }
    93  
    94  func (art *simpleARTMap) Search(key []byte) ([]uint32, error) {
    95  	v, found := art.tree.Search(key)
    96  	if !found {
    97  		return nil, ErrNotFound
    98  	}
    99  	chain := v.(*IndexMVCCChain)
   100  	return chain.GetRows(), nil
   101  }
   102  
   103  func (art *simpleARTMap) String() string {
   104  	s := fmt.Sprintf("<ART>[Size=%d](\n", art.tree.Size())
   105  	it := art.tree.Iterator()
   106  	for it.HasNext() {
   107  		n, err := it.Next()
   108  		if err != nil {
   109  			break
   110  		}
   111  		s = fmt.Sprintf("%sNode: %v:%v\n", s, n.Key(), n.Value().(*IndexMVCCChain).GetRows())
   112  	}
   113  	s = fmt.Sprintf("%s)", s)
   114  	return s
   115  }