Skip to content

Commit 56a9cb7

Browse files
committed
Use generators to index data
1 parent ca66f75 commit 56a9cb7

File tree

6 files changed

+33
-25
lines changed

6 files changed

+33
-25
lines changed

UPGRADE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Global Changes
66
- Recommended folder structure by symfony adopted
7+
- [IMPROVEMENT] Use Generator to index data which will increase processing speed significant
78

89
***
910

src/Service/Builder/AssetListBuilder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ public function __construct(
1717
) {
1818
}
1919

20-
public function buildByList(array $options): array
20+
public function buildByList(array $options): \Generator
2121
{
2222
$list = $this->getList($options);
2323

24-
return $list->getAssets();
24+
foreach ($list->loadIdList() as $id) {
25+
if ($asset = Asset::getById($id)) {
26+
yield $asset;
27+
}
28+
}
2529
}
2630

2731
public function buildByIdList(int $id, array $options): ?ElementInterface
@@ -33,10 +37,6 @@ public function buildByIdList(int $id, array $options): ?ElementInterface
3337

3438
$assets = $list->getAssets();
3539

36-
if (!is_array($assets)) {
37-
return null;
38-
}
39-
4040
if (count($assets) === 0) {
4141
return null;
4242
}

src/Service/Builder/DataBuilderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
interface DataBuilderInterface
88
{
9-
public function buildByList(array $options): array;
9+
public function buildByList(array $options): \Generator;
1010

1111
public function buildByIdList(int $id, array $options): ?ElementInterface;
1212

src/Service/Builder/DocumentListBuilder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ public function __construct(
1818
) {
1919
}
2020

21-
public function buildByList(array $options): array
21+
public function buildByList(array $options): \Generator
2222
{
2323
$list = $this->getList($options);
2424

25-
return $list->getDocuments();
25+
foreach ($list->loadIdList() as $id) {
26+
if ($doc = Document::getById($id)) {
27+
yield $doc;
28+
}
29+
}
2630
}
2731

2832
public function buildByIdList(int $id, array $options): ?ElementInterface
@@ -34,10 +38,6 @@ public function buildByIdList(int $id, array $options): ?ElementInterface
3438

3539
$documents = $list->getDocuments();
3640

37-
if (!is_array($documents)) {
38-
return null;
39-
}
40-
4141
if (count($documents) === 0) {
4242
return null;
4343
}

src/Service/Builder/ObjectListBuilder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ public function __construct(
1717
) {
1818
}
1919

20-
public function buildByList(array $options): array
20+
public function buildByList(array $options): \Generator
2121
{
2222
$list = $this->getList($options);
2323

24-
return $list->getObjects();
24+
foreach ($list->loadIdList() as $id) {
25+
if ($object = DataObject::getById($id)) {
26+
yield $object;
27+
}
28+
}
2529
}
2630

2731
public function buildByIdList(int $id, array $options): ?ElementInterface
@@ -33,10 +37,6 @@ public function buildByIdList(int $id, array $options): ?ElementInterface
3337

3438
$objects = $list->getObjects();
3539

36-
if (!is_array($objects)) {
37-
return null;
38-
}
39-
4040
if (count($objects) === 0) {
4141
return null;
4242
}

src/Service/DataProviderService.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,31 @@ protected function fetchByTypeAndId(string $type, string $providerBehaviour, int
136136

137137
$element = $builder->buildById($id);
138138

139-
$this->dispatchData([$element], $providerBehaviour, $resourceMeta);
139+
if ($element instanceof ElementInterface) {
140+
$this->dispatchElement($element, $providerBehaviour, $resourceMeta);
141+
}
140142
}
141143

142144
protected function log(string $level, string $message): void
143145
{
144146
$this->logger->log($level, $message, DsTrinityDataBundle::PROVIDER_NAME, $this->contextName);
145147
}
146148

147-
protected function dispatchData(array $elements, string $providerBehaviour, ?ResourceMetaInterface $resourceMeta = null): void
149+
protected function dispatchData(\Generator $elements, string $providerBehaviour, ?ResourceMetaInterface $resourceMeta = null): void
148150
{
149151
foreach ($elements as $element) {
150-
$newDataEvent = new NewDataEvent($this->contextDispatchType, $this->contextName, $element, $providerBehaviour, $resourceMeta);
151-
$this->eventDispatcher->dispatch($newDataEvent, DynamicSearchEvents::NEW_DATA_AVAILABLE);
152-
153-
$this->dispatchProcessControlSignal();
152+
$this->dispatchElement($element, $providerBehaviour, $resourceMeta);
154153
}
155154
}
156155

156+
protected function dispatchElement(ElementInterface $element, string $providerBehaviour, ?ResourceMetaInterface $resourceMeta = null): void
157+
{
158+
$newDataEvent = new NewDataEvent($this->contextDispatchType, $this->contextName, $element, $providerBehaviour, $resourceMeta);
159+
$this->eventDispatcher->dispatch($newDataEvent, DynamicSearchEvents::NEW_DATA_AVAILABLE);
160+
161+
$this->dispatchProcessControlSignal();
162+
}
163+
157164
protected function addSignalListener(): void
158165
{
159166
if (php_sapi_name() !== 'cli') {

0 commit comments

Comments
 (0)