|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Mage-OS Team. All rights reserved. |
| 4 | + * See LICENSE for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace MageOS\Indexer\Model\ResourceModel; |
| 10 | + |
| 11 | +use MageOS\Indexer\Api\IndexStorageMap; |
| 12 | +use MageOS\Indexer\Api\IndexStorageWriter; |
| 13 | + |
| 14 | +class TableBatchIndexStorageWriter implements IndexStorageWriter |
| 15 | +{ |
| 16 | + private array $parametersByTable = []; |
| 17 | + private array $currentBatchByTable = []; |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + private readonly IndexStorageMap $indexStorageMap, |
| 21 | + private readonly BatchInsertStatementRegistry $batchInsertStatementRegistry, |
| 22 | + private readonly IndexTableStructure $indexTableStructure, |
| 23 | + private readonly int $batchSize |
| 24 | + ) { |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + public function add($row): void |
| 29 | + { |
| 30 | + $tableName = $this->indexStorageMap->getStorageName($row); |
| 31 | + $this->currentBatchByTable[$tableName] = ($this->currentBatchByTable[$tableName] ?? 0) + 1; |
| 32 | + if (!isset($this->parametersByTable[$tableName])) { |
| 33 | + $this->parametersByTable[$tableName] = []; |
| 34 | + } |
| 35 | + |
| 36 | + $this->indexTableStructure->prepareRow($this->parametersByTable[$tableName], $row); |
| 37 | + |
| 38 | + if ($this->currentBatchByTable[$tableName] === $this->batchSize) { |
| 39 | + if (!$this->batchInsertStatementRegistry->hasStatement($tableName, $this->batchSize)) { |
| 40 | + $this->batchInsertStatementRegistry->createStatement( |
| 41 | + $tableName, |
| 42 | + $this->batchSize, |
| 43 | + $this->indexTableStructure->generateInsertOnDuplicate($this->batchSize) |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + $this->batchInsertStatementRegistry->executeStatement( |
| 48 | + $tableName, |
| 49 | + $this->currentBatchByTable[$tableName], |
| 50 | + $this->parametersByTable[$tableName] |
| 51 | + ); |
| 52 | + |
| 53 | + $this->currentBatchByTable[$tableName] = 0; |
| 54 | + $this->parametersByTable[$tableName] = []; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public function finish(): void |
| 59 | + { |
| 60 | + foreach ($this->currentBatchByTable as $tableName => $batchSize) { |
| 61 | + if ($batchSize === 0) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if (!$this->batchInsertStatementRegistry->hasStatement($tableName, $batchSize)) { |
| 66 | + $this->batchInsertStatementRegistry->createStatement( |
| 67 | + $tableName, |
| 68 | + $batchSize, |
| 69 | + $this->indexTableStructure->generateInsertOnDuplicate($batchSize) |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + $this->batchInsertStatementRegistry |
| 74 | + ->executeStatement($tableName, $batchSize, $this->parametersByTable[$tableName]); |
| 75 | + } |
| 76 | + |
| 77 | + $this->currentBatchByTable = []; |
| 78 | + $this->parametersByTable = []; |
| 79 | + } |
| 80 | +} |
0 commit comments