github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/vector/hnsw/maintenance_test.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package hnsw
    13  
    14  import (
    15  	"testing"
    16  
    17  	"github.com/sirupsen/logrus/hooks/test"
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/stretchr/testify/require"
    20  	"github.com/weaviate/weaviate/adapters/repos/db/vector/cache"
    21  )
    22  
    23  func Test_growIndexToAccomodateNode(t *testing.T) {
    24  	createVertexSlice := func(size int) []*vertex {
    25  		index := make([]*vertex, size)
    26  		for i := 0; i < len(index); i++ {
    27  			index[i] = &vertex{id: uint64(i)}
    28  		}
    29  		return index
    30  	}
    31  	type args struct {
    32  		index []*vertex
    33  		id    uint64
    34  	}
    35  	tests := []struct {
    36  		name          string
    37  		args          args
    38  		wantIndexSize int
    39  		changed       bool
    40  		err           error
    41  	}{
    42  		{
    43  			name: "is one before the initial size",
    44  			args: args{
    45  				id:    cache.InitialSize - 1,
    46  				index: createVertexSlice(cache.InitialSize),
    47  			},
    48  			wantIndexSize: 0,
    49  			changed:       false,
    50  		},
    51  		{
    52  			name: "exactly equals the initial size",
    53  			args: args{
    54  				id:    cache.InitialSize,
    55  				index: createVertexSlice(cache.InitialSize),
    56  			},
    57  			wantIndexSize: cache.InitialSize + cache.MinimumIndexGrowthDelta,
    58  			changed:       true,
    59  		},
    60  		{
    61  			name: "is one after the initial size",
    62  			args: args{
    63  				id:    cache.InitialSize + 1,
    64  				index: createVertexSlice(cache.InitialSize),
    65  			},
    66  			wantIndexSize: cache.InitialSize + cache.MinimumIndexGrowthDelta,
    67  			changed:       true,
    68  		},
    69  		{
    70  			name: "4 times the initial size minus 1",
    71  			args: args{
    72  				id:    4*cache.InitialSize - 1,
    73  				index: createVertexSlice(cache.InitialSize),
    74  			},
    75  			wantIndexSize: 4*cache.InitialSize - 1 + cache.MinimumIndexGrowthDelta,
    76  			changed:       true,
    77  		},
    78  		{
    79  			name: "4 times the initial size",
    80  			args: args{
    81  				id:    4 * cache.InitialSize,
    82  				index: createVertexSlice(cache.InitialSize),
    83  			},
    84  			wantIndexSize: 4*cache.InitialSize + cache.MinimumIndexGrowthDelta,
    85  			changed:       true,
    86  		},
    87  		{
    88  			name: "4 times the initial size plus 1",
    89  			args: args{
    90  				id:    4*cache.InitialSize + 1,
    91  				index: createVertexSlice(cache.InitialSize),
    92  			},
    93  			wantIndexSize: 4*cache.InitialSize + 1 + cache.MinimumIndexGrowthDelta,
    94  			changed:       true,
    95  		},
    96  		{
    97  			name: "14160016 case",
    98  			args: args{
    99  				id:    uint64(14160016),
   100  				index: createVertexSlice(14160016),
   101  			},
   102  			wantIndexSize: int(14160016 * indexGrowthRate),
   103  			changed:       true,
   104  		},
   105  		{
   106  			name: "panic case",
   107  			args: args{
   108  				id:    uint64(cache.InitialSize + cache.MinimumIndexGrowthDelta + 1),
   109  				index: createVertexSlice(cache.InitialSize + 1),
   110  			},
   111  			wantIndexSize: cache.InitialSize + 1 + 2*cache.MinimumIndexGrowthDelta,
   112  			changed:       true,
   113  		},
   114  	}
   115  	for _, tt := range tests {
   116  		logger, _ := test.NewNullLogger()
   117  		t.Run(tt.name, func(t *testing.T) {
   118  			newNodes, changed, err := growIndexToAccomodateNode(tt.args.index, tt.args.id, logger)
   119  			assert.Len(t, newNodes, tt.wantIndexSize)
   120  			assert.Equal(t, tt.changed, changed)
   121  			if err != nil {
   122  				require.NotNil(t, tt.err)
   123  				assert.EqualError(t, err, tt.err.Error())
   124  			}
   125  			// check the newly grown index
   126  			index := tt.args.index
   127  			if changed {
   128  				index = newNodes
   129  			}
   130  			assert.Greater(t, len(index), int(tt.args.id))
   131  		})
   132  	}
   133  }