github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/index/tree_test.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  	"strconv"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestARTIndexNumeric(t *testing.T) {
    28  	defer testutils.AfterTest(t)()
    29  	testutils.EnsureNoLeak(t)
    30  	typ := types.T_int32.ToType()
    31  	idx := NewSimpleARTMap()
    32  
    33  	var err error
    34  	var rows []uint32
    35  	val := int32(0)
    36  	_, err = idx.Search(types.EncodeInt32(&val))
    37  	require.Error(t, err)
    38  
    39  	var vecs []containers.Vector
    40  	for i := 0; i < 10; i++ {
    41  		vec := containers.MockVector2(typ, 100, i*100)
    42  		vecs = append(vecs, vec)
    43  		defer vec.Close()
    44  	}
    45  
    46  	val = int32(55)
    47  	_, err = idx.Search(types.EncodeInt32(&val))
    48  	require.Error(t, err)
    49  
    50  	err = idx.BatchInsert(vecs[0].GetDownstreamVector(), 0, 100, uint32(0))
    51  	require.NoError(t, err)
    52  
    53  	val = int32(55)
    54  	rows, err = idx.Search(types.EncodeInt32(&val))
    55  	require.NoError(t, err)
    56  	require.Equal(t, uint32(55), rows[0])
    57  
    58  	val = int32(100)
    59  	_, err = idx.Search(types.EncodeInt32(&val))
    60  	require.ErrorIs(t, err, ErrNotFound)
    61  
    62  	err = idx.BatchInsert(vecs[0].GetDownstreamVector(), 0, 100, uint32(100))
    63  	require.NoError(t, err)
    64  
    65  	err = idx.BatchInsert(vecs[1].GetDownstreamVector(), 0, 100, uint32(100))
    66  	require.NoError(t, err)
    67  
    68  	val = int32(123)
    69  	rows, err = idx.Search(types.EncodeInt32(&val))
    70  	require.NoError(t, err)
    71  	require.Equal(t, uint32(123), rows[0])
    72  
    73  	val = int32(233)
    74  	_, err = idx.Search(types.EncodeInt32(&val))
    75  	require.ErrorIs(t, err, ErrNotFound)
    76  
    77  	val = int32(55)
    78  	buf := types.EncodeInt32(&val)
    79  	bufval := make([]byte, len(buf))
    80  	copy(bufval, buf)
    81  	err = idx.Insert(bufval, uint32(55))
    82  	require.NoError(t, err)
    83  
    84  	val = int32(55)
    85  	rows, err = idx.Search(types.EncodeInt32(&val))
    86  	require.NoError(t, err)
    87  	require.Equal(t, uint32(55), rows[0])
    88  
    89  }
    90  
    91  func TestArtIndexString(t *testing.T) {
    92  	defer testutils.AfterTest(t)()
    93  	testutils.EnsureNoLeak(t)
    94  	typ := types.T_varchar.ToType()
    95  	idx := NewSimpleARTMap()
    96  
    97  	var err error
    98  	var rows []uint32
    99  	_, err = idx.Search([]byte(strconv.Itoa(0)))
   100  	require.Error(t, err)
   101  
   102  	var vecs []containers.Vector
   103  	for i := 0; i < 10; i++ {
   104  		vec := containers.MockVector2(typ, 100, i*100)
   105  		vecs = append(vecs, vec)
   106  		defer vec.Close()
   107  	}
   108  
   109  	_, err = idx.Search([]byte(strconv.Itoa(55)))
   110  	require.ErrorIs(t, err, ErrNotFound)
   111  
   112  	err = idx.BatchInsert(vecs[0].GetDownstreamVector(), 0, 100, uint32(0))
   113  	require.NoError(t, err)
   114  	t.Log(idx.String())
   115  
   116  	rows, err = idx.Search([]byte(strconv.Itoa(55)))
   117  	require.NoError(t, err)
   118  	require.Equal(t, uint32(55), rows[0])
   119  
   120  	_, err = idx.Search([]byte(strconv.Itoa(100)))
   121  	require.ErrorIs(t, err, ErrNotFound)
   122  
   123  	err = idx.BatchInsert(vecs[0].GetDownstreamVector(), 0, 100, uint32(100))
   124  	require.NoError(t, err)
   125  
   126  	err = idx.BatchInsert(vecs[1].GetDownstreamVector(), 0, 100, uint32(100))
   127  	require.NoError(t, err)
   128  
   129  	rows, err = idx.Search([]byte(strconv.Itoa(123)))
   130  	require.NoError(t, err)
   131  	require.Equal(t, uint32(123), rows[0])
   132  
   133  	_, err = idx.Search([]byte(strconv.Itoa(233)))
   134  	require.ErrorIs(t, err, ErrNotFound)
   135  }
   136  
   137  func BenchmarkArt(b *testing.B) {
   138  	tr := NewSimpleARTMap()
   139  	b.Run("tree-insert", func(b *testing.B) {
   140  		b.ResetTimer()
   141  		for i := 0; i < b.N; i++ {
   142  			j := uint32(i)
   143  			tr.Insert(types.EncodeUint32(&j), uint32(i))
   144  		}
   145  	})
   146  	buf := []byte("hello")
   147  	b.Run("tree-search", func(b *testing.B) {
   148  		b.ResetTimer()
   149  		for i := 0; i < b.N; i++ {
   150  			tr.Search(buf)
   151  		}
   152  	})
   153  }