Skip to content

Commit 0ce0f45

Browse files
Added functionality to retrieve ids of all docs that match a given filter
1 parent e2ec385 commit 0ce0f45

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/DocumentStore.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,11 @@ public function getDoc(string $collectionName, string $docId): ?array;
136136
* @throws UnknownCollection
137137
*/
138138
public function filterDocs(string $collectionName, Filter $filter, int $skip = null, int $limit = null, OrderBy $orderBy = null): \Traversable;
139+
140+
/**
141+
* @param string $collectionName
142+
* @param Filter $filter
143+
* @return array
144+
*/
145+
public function filterDocIds(string $collectionName, Filter $filter): array;
139146
}

src/InMemoryDocumentStore.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,27 @@ public function filterDocs(
323323
return new \ArrayIterator($filteredDocs);
324324
}
325325

326+
/**
327+
* @param string $collectionName
328+
* @param Filter $filter
329+
* @return array
330+
*/
331+
public function filterDocIds(
332+
string $collectionName,
333+
Filter $filter
334+
): array {
335+
$this->assertHasCollection($collectionName);
336+
337+
$docIds = [];
338+
foreach ($this->inMemoryConnection['documents'][$collectionName] as $docId => $doc) {
339+
if ($filter->match($doc, (string)$docId)) {
340+
$docIds[] = $docId;
341+
}
342+
}
343+
344+
return $docIds;
345+
}
346+
326347
private function hasDoc(string $collectionName, string $docId): bool
327348
{
328349
if (! $this->hasCollection($collectionName)) {

tests/InMemoryDocumentStoreTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
namespace EventEngineTest\DocumentStore;
55

66
use EventEngine\DocumentStore\FieldIndex;
7+
use EventEngine\DocumentStore\Filter\AndFilter;
78
use EventEngine\DocumentStore\Filter\AnyFilter;
89
use EventEngine\DocumentStore\Filter\EqFilter;
10+
use EventEngine\DocumentStore\Filter\GtFilter;
11+
use EventEngine\DocumentStore\Filter\LtFilter;
12+
use EventEngine\DocumentStore\Filter\OrFilter;
913
use EventEngine\DocumentStore\InMemoryDocumentStore;
1014
use EventEngine\DocumentStore\MultiFieldIndex;
1115
use EventEngine\Persistence\InMemoryConnection;
@@ -105,6 +109,25 @@ public function it_updates_a_subset_of_a_doc()
105109
$this->assertEquals(42, $filteredDocs[0]['some']['other']['nested']);
106110
}
107111

112+
/**
113+
* @test
114+
*/
115+
public function it_retrieves_doc_ids_by_filter()
116+
{
117+
$this->store->addCollection('test');
118+
119+
$this->store->addDoc('test', 'a', ['number' => 10]);
120+
$this->store->addDoc('test', 'b', ['number' => 20]);
121+
$this->store->addDoc('test', 'c', ['number' => 30]);
122+
123+
$result = $this->store->filterDocIds('test', new OrFilter(
124+
new GtFilter('number', 21),
125+
new LtFilter('number', 19)
126+
));
127+
128+
$this->assertEquals(['a', 'c'], $result);
129+
}
130+
108131
/**
109132
* @test
110133
*/

0 commit comments

Comments
 (0)