@@ -33,6 +33,22 @@ private function generateUpserts(int $count): array
3333 return $ upserts ;
3434 }
3535
36+ /**
37+ * @return array<VectorUpsert>
38+ */
39+ private function generatePrefixedUpserts (int $ count ): array
40+ {
41+ $ upserts = [];
42+ for ($ i = 0 ; $ i < $ count ; $ i ++) {
43+ $ upserts [] = new VectorUpsert (
44+ id: sprintf ('prefix-%s ' , $ i ),
45+ vector: createRandomVector (2 ),
46+ );
47+ }
48+
49+ return $ upserts ;
50+ }
51+
3652 public function test_can_range_vectors (): void
3753 {
3854 // Arrange
@@ -157,4 +173,60 @@ public function test_can_range_vectors_using_iterator_can_break_loop(): void
157173 // Assert
158174 $ this ->assertLessThan (1024 * 1024 , $ memory );
159175 }
176+
177+ public function test_can_range_vectors_over_a_prefix (): void
178+ {
179+ // Arrange
180+ $ this ->namespace ->upsertMany ([
181+ ...$ this ->generateUpserts (50 ), // ids: 0, 1, ..., 49
182+ ...$ this ->generatePrefixedUpserts (50 ), // ids: prefix-0, prefix-1, ..., prefix-49
183+ ]);
184+ $ this ->waitForIndex ($ this ->namespace );
185+
186+ $ memory = $ this ->measureMemory (function () {
187+ // Act
188+ $ results = $ this ->namespace ->range (new VectorRange (limit: 20 , prefix: 'prefix- ' ));
189+
190+ // Assert
191+ $ this ->assertCount (20 , $ results );
192+
193+ // Act
194+ $ results = $ this ->namespace ->range (new VectorRange (limit: 50 , cursor: $ results ->nextCursor , prefix: 'prefix- ' ));
195+
196+ // Assert
197+ $ this ->assertCount (30 , $ results ); // fails here with 50 items
198+ });
199+
200+ // Assert
201+ $ this ->assertLessThan (1024 * 1024 , $ memory );
202+ }
203+
204+ public function test_can_range_vectors_over_a_prefix_using_iterator (): void
205+ {
206+ // Arrange
207+ $ this ->namespace ->upsertMany ([
208+ ...$ this ->generateUpserts (50 ), // ids: 0, 1, ..., 49
209+ ...$ this ->generatePrefixedUpserts (50 ), // ids: prefix-0, prefix-1, ..., prefix-49
210+ ]);
211+ $ this ->waitForIndex ($ this ->namespace );
212+
213+ $ memory = $ this ->measureMemory (function () {
214+ // Arrange
215+ $ count = 0 ;
216+
217+ // Act
218+ $ results = $ this ->namespace ->rangeIterator (new VectorRange (limit: 10 , prefix: 'prefix- ' ));
219+
220+ // Increment count
221+ foreach ($ results as $ result ) {
222+ $ count ++;
223+ }
224+
225+ // Assert
226+ $ this ->assertSame (50 , $ count );
227+ });
228+
229+ // Assert
230+ $ this ->assertLessThan (1024 * 1024 , $ memory );
231+ }
160232}
0 commit comments