Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use generators to index data #34

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading