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