Skip to content

Commit 52a17e0

Browse files
committed
feat: range vectors using an id prefix
1 parent b899b20 commit 52a17e0

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

src/Iterators/VectorRangeIterator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ private function fetchWithCursor(string $cursor): VectorRangeResult
7676
return $this->operation->range(new VectorRange(
7777
limit: $this->range->limit,
7878
cursor: $cursor,
79+
prefix: $this->range->prefix,
7980
includeMetadata: $this->range->includeMetadata,
8081
includeVectors: $this->range->includeVectors,
8182
includeData: $this->range->includeData,

src/VectorRange.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public function __construct(
1010
public int $limit,
1111
public string $cursor = '0',
12+
public ?string $prefix = null,
1213
public bool $includeMetadata = false,
1314
public bool $includeVectors = false,
1415
public bool $includeData = false,
@@ -18,19 +19,26 @@ public function __construct(
1819
* @return array{
1920
* limit: int,
2021
* cursor: string,
22+
* prefix?: string,
2123
* includeMetadata: bool,
2224
* includeVectors: bool,
2325
* includeData: bool,
2426
* }
2527
*/
2628
public function toArray(): array
2729
{
28-
return [
30+
$data = [
2931
'limit' => $this->limit,
3032
'cursor' => $this->cursor,
3133
'includeMetadata' => $this->includeMetadata,
3234
'includeVectors' => $this->includeVectors,
3335
'includeData' => $this->includeData,
3436
];
37+
38+
if ($this->prefix !== null) {
39+
$data['prefix'] = $this->prefix;
40+
}
41+
42+
return $data;
3543
}
3644
}

tests/Dense/Operations/RangeVectorsTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)