Skip to content

Commit d615486

Browse files
lochmuellerchr-hertel
authored andcommitted
[Store] Change StoreInterface from array to iterable
1 parent c72b187 commit d615486

File tree

42 files changed

+199
-208
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+199
-208
lines changed

src/store/src/Bridge/Azure/SearchStore.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ public function add(VectorDocument ...$documents): void
4444
]);
4545
}
4646

47-
public function query(Vector $vector, array $options = []): array
47+
public function query(Vector $vector, array $options = []): iterable
4848
{
4949
$result = $this->request('search', [
5050
'vectorQueries' => [$this->buildVectorQuery($vector)],
5151
]);
5252

53-
return array_map([$this, 'convertToVectorDocument'], $result['value']);
53+
foreach ($result['value'] as $item) {
54+
yield $this->convertToVectorDocument($item);
55+
}
5456
}
5557

5658
/**

src/store/src/Bridge/ChromaDb/Store.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function add(VectorDocument ...$documents): void
5555
/**
5656
* @param array{where?: array<string, string>, whereDocument?: array<string, mixed>} $options
5757
*/
58-
public function query(Vector $vector, array $options = []): array
58+
public function query(Vector $vector, array $options = []): iterable
5959
{
6060
$collection = $this->client->getOrCreateCollection($this->collectionName);
6161
$queryResponse = $collection->query(
@@ -65,15 +65,12 @@ public function query(Vector $vector, array $options = []): array
6565
whereDocument: $options['whereDocument'] ?? null,
6666
);
6767

68-
$documents = [];
6968
for ($i = 0; $i < \count($queryResponse->metadatas[0]); ++$i) {
70-
$documents[] = new VectorDocument(
69+
yield new VectorDocument(
7170
id: Uuid::fromString($queryResponse->ids[0][$i]),
7271
vector: new Vector($queryResponse->embeddings[0][$i]),
7372
metadata: new Metadata($queryResponse->metadatas[0][$i]),
7473
);
7574
}
76-
77-
return $documents;
7875
}
7976
}

src/store/src/Bridge/ClickHouse/Store.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function add(VectorDocument ...$documents): void
6464
$this->insertBatch($rows);
6565
}
6666

67-
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
67+
public function query(Vector $vector, array $options = [], ?float $minScore = null): iterable
6868
{
6969
$sql = <<<'SQL'
7070
SELECT
@@ -93,17 +93,14 @@ public function query(Vector $vector, array $options = [], ?float $minScore = nu
9393
->toArray()['data']
9494
;
9595

96-
$documents = [];
9796
foreach ($results as $result) {
98-
$documents[] = new VectorDocument(
97+
yield new VectorDocument(
9998
id: Uuid::fromString($result['id']),
10099
vector: new Vector($result['embedding']),
101100
metadata: new Metadata(json_decode($result['metadata'] ?? '{}', true, 512, \JSON_THROW_ON_ERROR)),
102101
score: $result['score'],
103102
);
104103
}
105-
106-
return $documents;
107104
}
108105

109106
/**

src/store/src/Bridge/Cloudflare/Store.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,17 @@ public function add(VectorDocument ...$documents): void
7171
});
7272
}
7373

74-
public function query(Vector $vector, array $options = []): array
74+
public function query(Vector $vector, array $options = []): iterable
7575
{
7676
$results = $this->request('POST', \sprintf('vectorize/v2/indexes/%s/query', $this->index), [
7777
'vector' => $vector->getData(),
7878
'returnValues' => true,
7979
'returnMetadata' => 'all',
8080
]);
8181

82-
return array_map($this->convertToVectorDocument(...), $results['result']['matches']);
82+
foreach ($results['result']['matches'] as $item) {
83+
yield $this->convertToVectorDocument($item);
84+
}
8385
}
8486

8587
/**

src/store/src/Bridge/Local/CacheStore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function add(VectorDocument ...$documents): void
7777
* } $options If maxItems is provided, only the top N results will be returned.
7878
* If filter is provided, only documents matching the filter will be considered.
7979
*/
80-
public function query(Vector $vector, array $options = []): array
80+
public function query(Vector $vector, array $options = []): iterable
8181
{
8282
$documents = $this->cache->get($this->cacheKey, static fn (): array => []);
8383

@@ -91,7 +91,7 @@ public function query(Vector $vector, array $options = []): array
9191
$vectorDocuments = array_values(array_filter($vectorDocuments, $options['filter']));
9292
}
9393

94-
return $this->distanceCalculator->calculate($vectorDocuments, $vector, $options['maxItems'] ?? null);
94+
yield from $this->distanceCalculator->calculate($vectorDocuments, $vector, $options['maxItems'] ?? null);
9595
}
9696

9797
public function drop(): void

src/store/src/Bridge/Local/InMemoryStore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ public function add(VectorDocument ...$documents): void
5353
* } $options If maxItems is provided, only the top N results will be returned.
5454
* If filter is provided, only documents matching the filter will be considered.
5555
*/
56-
public function query(Vector $vector, array $options = []): array
56+
public function query(Vector $vector, array $options = []): iterable
5757
{
5858
$documents = $this->documents;
5959

6060
if (isset($options['filter'])) {
6161
$documents = array_values(array_filter($documents, $options['filter']));
6262
}
6363

64-
return $this->distanceCalculator->calculate($documents, $vector, $options['maxItems'] ?? null);
64+
yield from $this->distanceCalculator->calculate($documents, $vector, $options['maxItems'] ?? null);
6565
}
6666

6767
public function drop(): void

src/store/src/Bridge/Manticore/Store.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function add(VectorDocument ...$documents): void
8282
});
8383
}
8484

85-
public function query(Vector $vector, array $options = []): array
85+
public function query(Vector $vector, array $options = []): iterable
8686
{
8787
$documents = $this->request('search', [
8888
'table' => $this->table,
@@ -94,7 +94,9 @@ public function query(Vector $vector, array $options = []): array
9494
],
9595
]);
9696

97-
return array_map($this->convertToVectorDocument(...), $documents['hits']['hits']);
97+
foreach ($documents['hits']['hits'] as $item) {
98+
yield $this->convertToVectorDocument($item);
99+
}
98100
}
99101

100102
/**

src/store/src/Bridge/MariaDb/Store.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function add(VectorDocument ...$documents): void
141141
* maxScore?: float|null,
142142
* } $options
143143
*/
144-
public function query(Vector $vector, array $options = []): array
144+
public function query(Vector $vector, array $options = []): iterable
145145
{
146146
$where = null;
147147

@@ -183,19 +183,15 @@ public function query(Vector $vector, array $options = []): array
183183
$params['maxScore'] = $maxScore;
184184
}
185185

186-
$documents = [];
187-
188186
$statement->execute($params);
189187

190188
foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $result) {
191-
$documents[] = new VectorDocument(
189+
yield new VectorDocument(
192190
id: Uuid::fromRfc4122($result['id']),
193191
vector: new Vector(json_decode((string) $result['embedding'], true)),
194192
metadata: new Metadata(json_decode($result['metadata'] ?? '{}', true)),
195193
score: $result['score'],
196194
);
197195
}
198-
199-
return $documents;
200196
}
201197
}

src/store/src/Bridge/Meilisearch/Store.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function add(VectorDocument ...$documents): void
7777
);
7878
}
7979

80-
public function query(Vector $vector, array $options = []): array
80+
public function query(Vector $vector, array $options = []): iterable
8181
{
8282
$semanticRatio = $options['semanticRatio'] ?? $this->semanticRatio;
8383

@@ -96,7 +96,9 @@ public function query(Vector $vector, array $options = []): array
9696
],
9797
]);
9898

99-
return array_map($this->convertToVectorDocument(...), $result['hits']);
99+
foreach ($result['hits'] as $item) {
100+
yield $this->convertToVectorDocument($item);
101+
}
100102
}
101103

102104
public function drop(): void

src/store/src/Bridge/Milvus/Store.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function add(VectorDocument ...$documents): void
100100
]);
101101
}
102102

103-
public function query(Vector $vector, array $options = []): array
103+
public function query(Vector $vector, array $options = []): iterable
104104
{
105105
$payload = [
106106
'collectionName' => $this->collection,
@@ -117,7 +117,9 @@ public function query(Vector $vector, array $options = []): array
117117

118118
$documents = $this->request('POST', 'v2/vectordb/entities/search', $payload);
119119

120-
return array_map($this->convertToVectorDocument(...), $documents['data']);
120+
foreach ($documents['data'] as $item) {
121+
yield $this->convertToVectorDocument($item);
122+
}
121123
}
122124

123125
public function drop(): void

0 commit comments

Comments
 (0)