Skip to content

Commit

Permalink
Merge pull request #35 from dachcom-digital/backports
Browse files Browse the repository at this point in the history
Use generators to index data
  • Loading branch information
solverat authored Oct 23, 2023
2 parents 44c804b + 722f181 commit 514596e
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeception.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Codeception
on:
push:
branches: [ 'master' ]
branches: [ '2.x' ]
pull_request:
branches: [ 'master' ]
branches: [ '2.x' ]

jobs:
codeception:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ecs.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Easy Coding Standards
on:
push:
branches: [ 'master' ]
branches: [ '2.x' ]
pull_request:
branches: [ 'master' ]
branches: [ '2.x' ]

jobs:
ecs:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/php-stan.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: PHP Stan
on:
push:
branches: [ 'master' ]
branches: [ '2.x' ]
pull_request:
branches: [ 'master' ]
branches: [ '2.x' ]

jobs:
stan:
Expand Down
3 changes: 3 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Upgrade Notes

## 2.0.3
- [BACKPORT][IMPROVEMENT] Use Generator to index data which will increase processing speed significant

## 2.0.2
- [BUGFIX] `object_relations_getter_extractor` respect arguments (#23)
- [FEATURE] `object_getter_extractor` more flexibility (#24)
Expand Down
8 changes: 6 additions & 2 deletions src/DsTrinityDataBundle/Service/Builder/AssetListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ public function __construct(Connection $db, EventDispatcherInterface $eventDispa
$this->eventDispatcher = $eventDispatcher;
}

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 Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ public function __construct(Connection $db, EventDispatcherInterface $eventDispa
$this->eventDispatcher = $eventDispatcher;
}

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 Down
8 changes: 6 additions & 2 deletions src/DsTrinityDataBundle/Service/Builder/ObjectListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ public function __construct(Connection $db, EventDispatcherInterface $eventDispa
$this->eventDispatcher = $eventDispatcher;
}

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 Down
19 changes: 13 additions & 6 deletions src/DsTrinityDataBundle/Service/DataProviderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,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 514596e

Please sign in to comment.