Skip to content

Commit 95132c5

Browse files
committed
add(indexer) Added new index framework components
- Added Generator API for prodicing index rows based on loader data - Added Loader API for loading data from tables or other storages - Added Index Scope DTO - Added WriteStorageFactory API - Added Mutable Data Record API - Extracted Data Record read API - Extracted Range Generator API - Created Index and Mview Action Adapter for Internal Index Components
1 parent c74a567 commit 95132c5

22 files changed

+1028
-232
lines changed

Api/IndexAction.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 IndexAction
12+
{
13+
/**
14+
* Reindex full index storage for provided index scope
15+
*
16+
*/
17+
public function reindexFull(
18+
IndexScope $scope,
19+
IndexStorageWriter $writer
20+
): void;
21+
22+
/**
23+
* Reindex partial data in live index
24+
*
25+
* @param int[] $entityIds
26+
*/
27+
public function reindexPartial(
28+
IndexScope $scope,
29+
IndexStorageWriter $writer,
30+
array $entityIds
31+
): void;
32+
}

Api/IndexGenerationObserver.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 IndexGenerationObserver
12+
{
13+
public function beforeGeneration(IndexScope $scope);
14+
public function afterGeneration(IndexScope $scope);
15+
}

Api/IndexGenerator.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
use MageOS\Indexer\Model\ArrayIndexRecordData;
12+
13+
interface IndexGenerator
14+
{
15+
public function generateRecord(
16+
int $entityId,
17+
IndexScope $indexScope,
18+
IndexRecordData $indexRecordData,
19+
IndexStorageWriter $indexStorageWriter
20+
);
21+
}

Api/IndexRecordData.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 IndexRecordData
12+
{
13+
public function getScopeValue(int $entityId, int $scopeId, string $field): mixed;
14+
15+
public function getValue(int $entityId, string $field): mixed;
16+
}

Api/IndexRecordDataLoader.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 IndexRecordDataLoader
12+
{
13+
/**
14+
* Loads data into index record by entity id range
15+
*/
16+
public function loadByRange(
17+
IndexScope $indexScope,
18+
IndexRecordMutableData $data,
19+
int $minEntityId,
20+
int $maxEntityId
21+
): void;
22+
23+
/**
24+
* Loads data into index record by entity ids
25+
*
26+
* @param int[] $entityIds
27+
*/
28+
public function loadByIds(
29+
IndexScope $indexScope,
30+
IndexRecordMutableData $data,
31+
array $entityIds
32+
): void;
33+
}

Api/IndexRecordMutableData.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 IndexRecordMutableData extends IndexRecordData
12+
{
13+
public function setValue(int $entityId, array $data): void;
14+
15+
public function addValue(int $entityId, string $field, mixed $value): void;
16+
17+
public function extendValue(int $entityId, string $field, string $key, mixed $value): void;
18+
19+
public function addScopeValue(int $entityId, int $storeId, string $field, mixed $value): void;
20+
}

Api/IndexScope.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
/**
12+
* Current index data scope
13+
*
14+
* Used to slice index generation into multiple clusters like:
15+
* `product_id`, `website_id`, `customer_group_id`
16+
* or
17+
* `product_id`, `store_id`
18+
*/
19+
readonly final class IndexScope
20+
{
21+
22+
private function __construct(
23+
public array $storeIds,
24+
public array $websiteIds,
25+
public array $customerGroupIds
26+
)
27+
{
28+
29+
}
30+
31+
/**
32+
* Creates index scope from provided values
33+
*
34+
* Typecasts non integer values if they exists
35+
*
36+
* @param int[] $storeIds
37+
* @param int[] $websiteIds
38+
* @param int[] $customerGroupIds
39+
*/
40+
public static function create(array $storeIds, array $websiteIds = [], array $customerGroupIds = []): self
41+
{
42+
$storeIds = array_map('intval', $storeIds);
43+
$websiteIds = array_map('intval', $websiteIds);
44+
$customerGroupIds = array_map('intval', $customerGroupIds);
45+
46+
return new self($storeIds, $websiteIds, $customerGroupIds);
47+
}
48+
}

Api/IndexScopeProvider.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 IndexScopeProvider
12+
{
13+
/**
14+
* Returns list of scope slices for an indexer
15+
*
16+
* @return IndexScope[]
17+
*/
18+
public function getScopes(): iterable;
19+
}

Api/IndexStorageWriterFactory.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 IndexStorageWriterFactory
12+
{
13+
/**
14+
* Creates full reindex writer
15+
*
16+
* Should create real index storage for each scope dimension
17+
* Upon completion will switch real index storage with new one
18+
*/
19+
public function createFullReindex(IndexScope $indexScope): IndexStorageWriter;
20+
21+
/**
22+
* Creates partial reindex writer
23+
*
24+
* Should re-use live index storage for each scope dimension
25+
*/
26+
public function createPartialReindex(IndexScope $indexScope): IndexStorageWriter;
27+
}

Model/ArrayIndexRecordData.php

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 MageOS\Indexer\Api\IndexRecordMutableData;
12+
13+
use Traversable;
14+
15+
class ArrayIndexRecordData implements \IteratorAggregate, IndexRecordMutableData
16+
{
17+
public function __construct(
18+
private array $data = [],
19+
private array $scopeData = []
20+
) {
21+
22+
}
23+
24+
public function getIterator(): Traversable
25+
{
26+
foreach ($this->data as $entityId => $item) {
27+
yield $entityId;
28+
}
29+
}
30+
31+
public function reset(): void
32+
{
33+
$this->data = [];
34+
$this->scopeData = [];
35+
}
36+
37+
public function setValue(int $entityId, array $data): void
38+
{
39+
$this->data[$entityId] = $data;
40+
}
41+
42+
public function addValue(int $entityId, string $field, mixed $value): void
43+
{
44+
$this->data[$entityId][$field] = $value;
45+
}
46+
47+
public function extendValue(int $entityId, string $field, string $key, mixed $value): void
48+
{
49+
$this->data[$entityId][$field][$key] = $value;
50+
}
51+
52+
public function addScopeValue(int $entityId, int $storeId, string $field, mixed $value): void
53+
{
54+
if (!isset($this->data[$entityId])) {
55+
return;
56+
}
57+
58+
$this->scopeData[$entityId][$storeId][$field] = $value;
59+
}
60+
61+
public function getScopeValue(int $entityId, int $scopeId, string $field): mixed
62+
{
63+
return $this->scopeData[$entityId][$scopeId][$field]
64+
?? $this->scopeData[$entityId][0][$field]
65+
?? null;
66+
}
67+
68+
public function getValue(int $entityId, string $field): mixed
69+
{
70+
return $this->data[$entityId][$field] ?? null;
71+
}
72+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 MageOS\Indexer\Api\IndexRecordDataLoader;
12+
use MageOS\Indexer\Api\IndexRecordMutableData;
13+
use MageOS\Indexer\Api\IndexScope;
14+
15+
readonly class CompositeIndexRecordDataLoader implements IndexRecordDataLoader
16+
{
17+
/**
18+
* @param IndexRecordDataLoader[] $loaders
19+
*/
20+
public function __construct(private array $loaders) {
21+
22+
}
23+
24+
public function loadByRange(IndexScope $indexScope, IndexRecordMutableData $data, int $minEntityId, int $maxEntityId): void
25+
{
26+
foreach ($this->loaders as $loader) {
27+
$loader->loadByRange($indexScope, $data, $minEntityId, $maxEntityId);
28+
}
29+
}
30+
31+
public function loadByIds(IndexScope $indexScope, IndexRecordMutableData $data, array $entityIds): void
32+
{
33+
foreach ($this->loaders as $loader) {
34+
$loader->loadByIds($indexScope, $data, $entityIds);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)