Skip to content

Commit

Permalink
Use generators to index data
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed Sep 22, 2023
1 parent ca66f75 commit 56a9cb7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
1 change: 1 addition & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

***

Expand Down
12 changes: 6 additions & 6 deletions src/Service/Builder/AssetListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ public function __construct(
) {
}

public function buildByList(array $options): array
public function buildByList(array $options): \Generator
{
$list = $this->getList($options);

return $list->getAssets();
foreach ($list->loadIdList() as $id) {
if ($asset = Asset::getById($id)) {
yield $asset;
}
}
}

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

$assets = $list->getAssets();

if (!is_array($assets)) {
return null;
}

if (count($assets) === 0) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Builder/DataBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

interface DataBuilderInterface
{
public function buildByList(array $options): array;
public function buildByList(array $options): \Generator;

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

Expand Down
12 changes: 6 additions & 6 deletions src/Service/Builder/DocumentListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ public function __construct(
) {
}

public function buildByList(array $options): array
public function buildByList(array $options): \Generator
{
$list = $this->getList($options);

return $list->getDocuments();
foreach ($list->loadIdList() as $id) {
if ($doc = Document::getById($id)) {
yield $doc;
}
}
}

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

$documents = $list->getDocuments();

if (!is_array($documents)) {
return null;
}

if (count($documents) === 0) {
return null;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Service/Builder/ObjectListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ public function __construct(
) {
}

public function buildByList(array $options): array
public function buildByList(array $options): \Generator
{
$list = $this->getList($options);

return $list->getObjects();
foreach ($list->loadIdList() as $id) {
if ($object = DataObject::getById($id)) {
yield $object;
}
}
}

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

$objects = $list->getObjects();

if (!is_array($objects)) {
return null;
}

if (count($objects) === 0) {
return null;
}
Expand Down
19 changes: 13 additions & 6 deletions src/Service/DataProviderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,24 +136,31 @@ protected function fetchByTypeAndId(string $type, string $providerBehaviour, int

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

$this->dispatchData([$element], $providerBehaviour, $resourceMeta);
if ($element instanceof ElementInterface) {
$this->dispatchElement($element, $providerBehaviour, $resourceMeta);
}
}

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

protected function dispatchData(array $elements, string $providerBehaviour, ?ResourceMetaInterface $resourceMeta = null): void
protected function dispatchData(\Generator $elements, string $providerBehaviour, ?ResourceMetaInterface $resourceMeta = null): void
{
foreach ($elements as $element) {
$newDataEvent = new NewDataEvent($this->contextDispatchType, $this->contextName, $element, $providerBehaviour, $resourceMeta);
$this->eventDispatcher->dispatch($newDataEvent, DynamicSearchEvents::NEW_DATA_AVAILABLE);

$this->dispatchProcessControlSignal();
$this->dispatchElement($element, $providerBehaviour, $resourceMeta);
}
}

protected function dispatchElement(ElementInterface $element, string $providerBehaviour, ?ResourceMetaInterface $resourceMeta = null): void
{
$newDataEvent = new NewDataEvent($this->contextDispatchType, $this->contextName, $element, $providerBehaviour, $resourceMeta);
$this->eventDispatcher->dispatch($newDataEvent, DynamicSearchEvents::NEW_DATA_AVAILABLE);

$this->dispatchProcessControlSignal();
}

protected function addSignalListener(): void
{
if (php_sapi_name() !== 'cli') {
Expand Down

0 comments on commit 56a9cb7

Please sign in to comment.