@@ -7,6 +7,7 @@ package vecindex
77
88import (
99 "context"
10+ "slices"
1011 "testing"
1112
1213 "github.com/cockroachdb/cockroach/pkg/base"
@@ -89,22 +90,40 @@ func TestSearcher(t *testing.T) {
8990 // Insert two vectors into root partition.
9091 var mutator MutationSearcher
9192 mutator .Init (idx , tx )
92- prefix := keys .MakeFamilyKey (encoding .EncodeVarintAscending ([]byte {}, 100 ), 0 /* famID */ )
93+
94+ // Reuse prefix, key bytes, value bytes and vector memory, to ensure it's
95+ // allowed.
96+ var prefix , keyBytes , valueBytes []byte
97+ var original , randomized vector.T
9398
9499 insertVector := func (vec vector.T , key int64 , val cspann.ValueBytes ) vector.T {
100+ prefix = prefix [:0 ]
101+ keyBytes = keyBytes [:0 ]
102+ randomized = randomized [:0 ]
103+
104+ prefix = encoding .EncodeVarintAscending (prefix , 100 )
95105 require .NoError (t , mutator .SearchForInsert (ctx , prefix , vec ))
106+
96107 partitionKey := cspann .PartitionKey (* mutator .PartitionKey ().(* tree.DInt ))
97- keyBytes := keys .MakeFamilyKey (encoding .EncodeVarintAscending ([]byte {}, key ), 0 /* famID */ )
98- randomizedVec := make (vector.T , len (vec ))
99- idx .RandomizeVector (vec , randomizedVec )
108+ keyBytes = keys .MakeFamilyKey (encoding .EncodeVarintAscending (keyBytes , key ), 0 /* famID */ )
109+
110+ randomized = slices .Grow (randomized , len (vec ))[:len (vec )]
111+ idx .RandomizeVector (vec , randomized )
100112 err = mutator .txn .AddToPartition (ctx , cspann .TreeKey (prefix ), partitionKey , cspann .LeafLevel ,
101- randomizedVec , cspann.ChildKey {KeyBytes : keyBytes }, val )
113+ randomized , cspann.ChildKey {KeyBytes : keyBytes }, val )
102114 require .NoError (t , err )
103- return randomizedVec
115+ return randomized
104116 }
105117
106- insertVector (vector.T {1 , 2 }, 1 , cspann.ValueBytes {1 , 2 })
107- randomizedVec := insertVector (vector.T {5 , 3 }, 2 , cspann.ValueBytes {3 , 4 })
118+ // Reuse vector and value memory, to ensure it's allowed.
119+ original = vector.T {1 , 2 }
120+ valueBytes = []byte {1 , 2 }
121+ insertVector (original , 1 , cspann .ValueBytes (valueBytes ))
122+ original [0 ] = 5
123+ original [1 ] = 3
124+ valueBytes [0 ] = 3
125+ valueBytes [1 ] = 4
126+ randomized = insertVector (original , 2 , cspann .ValueBytes (valueBytes ))
108127
109128 // Validate that search vector was correctly encoded and quantized.
110129 encodedVec := mutator .EncodedVector ()
@@ -113,29 +132,52 @@ func TestSearcher(t *testing.T) {
113132 []byte (* encodedVec .(* tree.DBytes )), & vecSet )
114133 require .NoError (t , err )
115134 require .Empty (t , remainder )
116- require .Equal (t , randomizedVec , vecSet .Vectors .At (0 ))
135+ require .Equal (t , randomized , vecSet .Vectors .At (0 ))
117136
118- // Use the Searcher.
137+ // Search for a vector that doesn't exist in the tree (reuse memory).
138+ prefix = prefix [:0 ]
139+ prefix = encoding .EncodeVarintAscending (prefix , 200 )
140+ original [0 ] = 1
141+ original [1 ] = 1
119142 var searcher Searcher
120143 searcher .Init (idx , tx , 8 /* baseBeamSize */ , 2 /* maxResults */ )
121- require .NoError (t , searcher .Search (ctx , prefix , vector.T {1 , 1 }))
144+ require .NoError (t , searcher .Search (ctx , prefix , original ))
145+ require .Nil (t , searcher .NextResult ())
146+
147+ // Search for a vector that does exist (reuse memory).
148+ prefix = prefix [:0 ]
149+ prefix = encoding .EncodeVarintAscending (prefix , 100 )
150+ require .NoError (t , searcher .Search (ctx , prefix , original ))
122151 res := searcher .NextResult ()
123152 require .InDelta (t , float32 (1 ), res .QuerySquaredDistance , 0.01 )
124153 res = searcher .NextResult ()
125154 require .InDelta (t , float32 (20 ), res .QuerySquaredDistance , 0.01 )
126155 require .Nil (t , searcher .NextResult ())
127156
128- // Search for a vector to delete.
129- keyBytes := keys .MakeFamilyKey (encoding .EncodeVarintAscending ([]byte {}, 1 ), 0 /* famID */ )
130- require .NoError (t , mutator .SearchForDelete (ctx , prefix , vector.T {1 , 2 }, keyBytes ))
131- require .Equal (t , tree .NewDInt (tree .DInt (1 )), mutator .PartitionKey ())
157+ // Search for a vector to delete that doesn't exist (reuse memory).
158+ keyBytes = keyBytes [:0 ]
159+ original [0 ] = 1
160+ original [1 ] = 2
161+ keyBytes = keys .MakeFamilyKey (encoding .EncodeVarintAscending (keyBytes , 123 ), 0 /* famID */ )
162+ require .NoError (t , mutator .SearchForDelete (ctx , prefix , original , keyBytes ))
163+ require .Equal (t , tree .DNull , mutator .PartitionKey ())
132164 require .Nil (t , mutator .EncodedVector ())
133165
134- // Search for a vector to delete that doesn't exist.
135- keyBytes = keys .MakeFamilyKey (encoding .EncodeVarintAscending ([]byte {}, 123 ), 0 /* famID */ )
136- require .NoError (t , mutator .SearchForDelete (ctx , prefix , vector.T {1 , 2 }, keyBytes ))
166+ // Search for a vector to delete that doesn't exist in the tree (reuse memory).
167+ prefix = prefix [:0 ]
168+ prefix = encoding .EncodeVarintAscending (prefix , 200 )
169+ require .NoError (t , mutator .SearchForDelete (ctx , prefix , original , keyBytes ))
137170 require .Equal (t , tree .DNull , mutator .PartitionKey ())
138171 require .Nil (t , mutator .EncodedVector ())
139172
173+ // Search for a vector to delete (reuse memory).
174+ prefix = prefix [:0 ]
175+ prefix = encoding .EncodeVarintAscending (prefix , 100 )
176+ keyBytes = keyBytes [:0 ]
177+ keyBytes = keys .MakeFamilyKey (encoding .EncodeVarintAscending (keyBytes , 1 ), 0 /* famID */ )
178+ require .NoError (t , mutator .SearchForDelete (ctx , prefix , original , keyBytes ))
179+ require .Equal (t , tree .NewDInt (tree .DInt (1 )), mutator .PartitionKey ())
180+ require .Nil (t , mutator .EncodedVector ())
181+
140182 require .NoError (t , tx .Commit (ctx ))
141183}
0 commit comments