Skip to content

Commit c74a567

Browse files
committed
add(indexer) Working on Indexation Framework
- Added InsertOnDupicate generator - Added RangeGenerator for finding min/max pairs of entity_id - Added EntityData container for loaders - Added TableBatch IndexStorageWriter implementation for efficient writes to table on re-index
1 parent 82f941e commit c74a567

18 files changed

+790
-0
lines changed

Diff for: Api/IndexStorageMap.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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\Api;
10+
11+
interface IndexStorageMap
12+
{
13+
public function getStorageName(array $row): string;
14+
}

Diff for: Api/IndexStorageWriter.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Api;
10+
11+
interface IndexStorageWriter
12+
{
13+
public function add($row): void;
14+
15+
public function finish(): void;
16+
}

Diff for: LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2023 Mage-OS team
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Diff for: Model/EntityData.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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;
10+
11+
use Traversable;
12+
13+
class EntityData implements \IteratorAggregate
14+
{
15+
private array $data = [];
16+
private array $storeData = [];
17+
18+
public function getIterator(): Traversable
19+
{
20+
foreach ($this->data as $entityId => $item) {
21+
yield $entityId;
22+
}
23+
}
24+
25+
public function add(int $entityId, array $data): void
26+
{
27+
$this->data[$entityId] = $data;
28+
}
29+
30+
public function reset(): void
31+
{
32+
$this->data = [];
33+
$this->storeData = [];
34+
}
35+
36+
public function addStoreValue(int $entityId, int $storeId, string $field, mixed $value): void
37+
{
38+
if (!isset($this->data[$entityId])) {
39+
return;
40+
}
41+
42+
$this->storeData[$entityId][$storeId][$field] = $value;
43+
}
44+
45+
public function getStoreValue(int $entityId, int $storeId, string $field): mixed
46+
{
47+
return $this->storeData[$entityId][$storeId][$field]
48+
?? $this->storeData[$entityId][0][$field]
49+
?? null;
50+
}
51+
52+
public function getValue(int $entityId, string $field): mixed
53+
{
54+
return $this->data[$entityId][$field] ?? null;
55+
}
56+
}

Diff for: Model/ResourceModel/BatchInsertStatementRegistry.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
interface BatchInsertStatementRegistry
12+
{
13+
public function hasStatement(string $tableName, int $batchSize): bool;
14+
15+
public function createStatement(string $tableName, int $batchSize, string $sql): void;
16+
17+
public function executeStatement(string $tableName, int $batchSize, array $params): void;
18+
}

Diff for: Model/ResourceModel/IndexTableStructure.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
interface IndexTableStructure
12+
{
13+
public function generateInsertOnDuplicate(int $batchSize): string;
14+
15+
public function prepareRow(array &$batchParameters, array $row): void;
16+
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
class InsertOnDuplicateSqlGenerator
12+
{
13+
public function generate(string $tableName, array $columns, int $rowCount, $onUpdate = []): string
14+
{
15+
$sqlOnUpdate = '';
16+
17+
if ($onUpdate) {
18+
$sqlOnUpdate = sprintf(
19+
' ON DUPLICATE KEY UPDATE %s',
20+
implode(
21+
',',
22+
array_map(fn ($column) => "`$column` = VALUES(`$column`)", $onUpdate)
23+
)
24+
);
25+
}
26+
27+
$rowLine = rtrim(str_repeat('?,', count($columns)), ',');
28+
$rowLines = str_repeat(
29+
"($rowLine),",
30+
$rowCount
31+
);
32+
33+
$sql = sprintf(
34+
'INSERT INTO `%s` (%s) VALUES %s%s',
35+
$tableName,
36+
implode(',', array_map(fn ($column) => "`$column`", $columns)),
37+
rtrim(
38+
$rowLines,
39+
','
40+
),
41+
$sqlOnUpdate
42+
);
43+
44+
return $sql;
45+
}
46+
}

Diff for: Model/ResourceModel/RangeGenerator.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\DB\Sql\Expression;
13+
14+
class RangeGenerator
15+
{
16+
public function __construct(
17+
private readonly ResourceConnection $resourceConnection,
18+
private readonly string $connectionName
19+
) {
20+
21+
}
22+
23+
public function createRanges(
24+
string $entityTable,
25+
string $entityIdField, int $batchSize): iterable
26+
{
27+
$connection = $this->resourceConnection->getConnection($this->connectionName);
28+
$entityIdField = $connection->quoteIdentifier($entityIdField);
29+
30+
$select = $connection->select()
31+
->from(
32+
$entityTable,
33+
[
34+
'min' => sprintf('MIN(%s)', $entityIdField),
35+
'max' => sprintf('MAX(%s)', $entityIdField)
36+
]
37+
)
38+
->group(
39+
new Expression(sprintf(
40+
'CEIL(%s / %d)', $entityIdField, $batchSize
41+
))
42+
);
43+
44+
45+
foreach ($select->query() as $row) {
46+
yield (int)$row['min'] => (int)$row['max'];
47+
}
48+
}
49+
}

Diff for: Model/ResourceModel/TableBatchIndexStorageWriter.php

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)