-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
247 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\DataProvider; | ||
|
||
use Setono\SyliusMeilisearchPlugin\Config\Index; | ||
use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface; | ||
|
||
interface DataProviderInterface | ||
{ | ||
/** | ||
* Returns ids of objects of the given entity | ||
* | ||
* @param class-string<IndexableInterface> $entity | ||
* | ||
* @return iterable<array-key, string|int> | ||
*/ | ||
public function getIds(string $entity, Index $index): iterable; | ||
|
||
/** | ||
* @param class-string<IndexableInterface> $entity | ||
* | ||
* @return iterable<array-key, IndexableInterface> | ||
*/ | ||
public function getObjects(string $entity, Index $index): iterable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\DataProvider; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use DoctrineBatchUtils\BatchProcessing\SelectBatchIteratorAggregate; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use Setono\Doctrine\ORMTrait; | ||
use Setono\SyliusMeilisearchPlugin\Config\Index; | ||
use Setono\SyliusMeilisearchPlugin\Event\DataProviderEvent; | ||
use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface; | ||
|
||
final class DefaultDataProvider implements DataProviderInterface | ||
{ | ||
use ORMTrait; | ||
|
||
public function __construct( | ||
ManagerRegistry $managerRegistry, | ||
private readonly EventDispatcherInterface $eventDispatcher, | ||
) { | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
public function getIds(string $entity, Index $index): \Generator | ||
{ | ||
yield from $this->createGenerator($entity, $index, true); | ||
} | ||
|
||
public function getObjects(string $entity, Index $index): \Generator | ||
{ | ||
yield from $this->createGenerator($entity, $index); | ||
} | ||
|
||
/** | ||
* @psalm-suppress MixedReturnTypeCoercion | ||
* | ||
* @param class-string<IndexableInterface> $entity | ||
* | ||
* @return ($onlyId is true ? SelectBatchIteratorAggregate<array-key, string|int> : SelectBatchIteratorAggregate<array-key, IndexableInterface>) | ||
*/ | ||
private function createGenerator(string $entity, Index $index, bool $onlyId = false): SelectBatchIteratorAggregate | ||
{ | ||
$qb = $this | ||
->getManager($entity) | ||
->createQueryBuilder() | ||
->select($onlyId ? 'o.id' : 'o') | ||
->from($entity, 'o') | ||
; | ||
|
||
$this->eventDispatcher->dispatch(new DataProviderEvent($entity, $index, $qb)); | ||
|
||
/** @psalm-suppress MixedReturnTypeCoercion */ | ||
return SelectBatchIteratorAggregate::fromQuery($qb->getQuery(), 100); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Event; | ||
|
||
use Doctrine\ORM\QueryBuilder; | ||
use Setono\SyliusMeilisearchPlugin\Config\Index; | ||
use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface; | ||
|
||
final class DataProviderEvent | ||
{ | ||
public function __construct( | ||
/** @var class-string<IndexableInterface> $entity */ | ||
public readonly string $entity, | ||
public readonly Index $index, | ||
public readonly QueryBuilder $qb, | ||
) { | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
src/Event/EntityBasedQueryBuilderForIndexingCreatedEvent.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Indexer; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @template T | ||
*/ | ||
final class IndexBuffer | ||
{ | ||
private int $count = 0; | ||
|
||
/** @var list<T> */ | ||
private array $buffer = []; | ||
|
||
/** | ||
* @param \Closure(list<T>):void $callback | ||
*/ | ||
public function __construct(private readonly int $bufferSize, private readonly \Closure $callback) | ||
{ | ||
} | ||
|
||
/** | ||
* @param T $item | ||
*/ | ||
public function push(mixed $item): void | ||
{ | ||
$this->buffer[] = $item; | ||
++$this->count; | ||
|
||
if ($this->count >= $this->bufferSize) { | ||
$this->flush(); | ||
} | ||
} | ||
|
||
public function flush(): void | ||
{ | ||
if ($this->count > 0) { | ||
($this->callback)($this->buffer); | ||
$this->buffer = []; | ||
$this->count = 0; | ||
} | ||
} | ||
} |
Oops, something went wrong.