Skip to content

Commit 4a2c5c4

Browse files
committed
chore(indexer) Refactor and simplify design of indexer
1 parent 95132c5 commit 4a2c5c4

37 files changed

+877
-469
lines changed

Api/IndexAction.php

-32
This file was deleted.

Api/IndexGenerationObserver.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
interface IndexGenerationObserver
1212
{
1313
public function beforeGeneration(IndexScope $scope);
14+
1415
public function afterGeneration(IndexScope $scope);
1516
}

Api/IndexGenerator.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88

99
namespace MageOS\Indexer\Api;
1010

11-
use MageOS\Indexer\Model\ArrayIndexRecordData;
12-
1311
interface IndexGenerator
1412
{
15-
public function generateRecord(
16-
int $entityId,
17-
IndexScope $indexScope,
18-
IndexRecordData $indexRecordData,
19-
IndexStorageWriter $indexStorageWriter
13+
public function process(
14+
IndexScope $scope,
15+
IndexRecord $record,
16+
IndexWriter $writer
2017
);
2118
}

Api/IndexRecord.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 IndexRecord
12+
{
13+
/**
14+
* Lists all entity within index record
15+
*
16+
* @return int[]
17+
*/
18+
public function listEntityIds(): iterable;
19+
20+
/**
21+
* Retrieves entity field in specific scope
22+
*
23+
* Should return value from scopeId 0 if it does not exist for current scope
24+
*/
25+
public function getInScope(int $entityId, int $scopeId, string $field): mixed;
26+
27+
/**
28+
* Retrieves entity field in global scope
29+
*/
30+
public function get(int $entityId, string $field): mixed;
31+
}

Api/IndexRecordData.php

-16
This file was deleted.

Api/IndexRecordDataLoader.php renamed to Api/IndexRecordLoader.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
namespace MageOS\Indexer\Api;
1010

11-
interface IndexRecordDataLoader
11+
interface IndexRecordLoader
1212
{
1313
/**
1414
* Loads data into index record by entity id range
1515
*/
1616
public function loadByRange(
17-
IndexScope $indexScope,
18-
IndexRecordMutableData $data,
19-
int $minEntityId,
20-
int $maxEntityId
17+
IndexScope $indexScope,
18+
IndexRecordMutable $data,
19+
int $minEntityId,
20+
int $maxEntityId
2121
): void;
2222

2323
/**
@@ -26,8 +26,8 @@ public function loadByRange(
2626
* @param int[] $entityIds
2727
*/
2828
public function loadByIds(
29-
IndexScope $indexScope,
30-
IndexRecordMutableData $data,
31-
array $entityIds
29+
IndexScope $indexScope,
30+
IndexRecordMutable $data,
31+
array $entityIds
3232
): void;
3333
}

Api/IndexRecordMutable.php

+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\Api;
10+
11+
/**
12+
* Mutable API of index record
13+
*
14+
* It is used in loaders and is separated to make sure index generator
15+
* does not rely on mutations
16+
*/
17+
interface IndexRecordMutable extends IndexRecord
18+
{
19+
/**
20+
* Sets data for entity record
21+
*
22+
* If set was not called, rest of the calls should be ignored
23+
*/
24+
public function set(int $entityId, array $data): void;
25+
26+
/**
27+
* Adds field value for existing entity record
28+
*
29+
* If no entity data is available, it should be ignored
30+
*/
31+
public function add(int $entityId, string $field, mixed $value): void;
32+
33+
/**
34+
* Appends record to array entity field
35+
*
36+
* If no entity data is available, it should be ignored
37+
*/
38+
public function append(int $entityId, string $field, string $key, mixed $value): void;
39+
40+
/**
41+
* Adds field value in scope for entity
42+
*
43+
* If no entity data is available, it should be ignored
44+
*/
45+
public function addInScope(int $entityId, int $storeId, string $field, mixed $value): void;
46+
}

Api/IndexRecordMutableData.php

-20
This file was deleted.

Api/IndexScopeProvider.php

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
namespace MageOS\Indexer\Api;
1010

11+
/**
12+
* Provider of independent scope for indexer
13+
*
14+
*
15+
*/
1116
interface IndexScopeProvider
1217
{
1318
/**

Api/IndexStorageMap.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@
88

99
namespace MageOS\Indexer\Api;
1010

11+
/**
12+
* Mapper of storages to scope
13+
*/
1114
interface IndexStorageMap
1215
{
13-
public function getStorageName(array $row): string;
16+
/**
17+
* Returns index storage name based on current row
18+
*/
19+
public function getStorageByRow(array $row): string;
20+
21+
/**
22+
* Returns all index storage tables that match current index scope
23+
*
24+
* @return string[]
25+
*/
26+
public function getStorageListByScope(IndexScope $scope): iterable;
1427
}

Api/IndexStorageWriter.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88

99
namespace MageOS\Indexer\Api;
1010

11-
interface IndexStorageWriter
11+
interface IndexStorageWriter extends IndexWriter
1212
{
13-
public function add($row): void;
13+
/**
14+
* Clear index storage by entity ids
15+
*
16+
* @param int[] $entityIds
17+
*/
18+
public function clear(array $entityIds): void;
1419

20+
/**
21+
* Finalize indexation
22+
*/
1523
public function finish(): void;
1624
}

Api/IndexWriter.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+
/**
12+
* Writer for index generator
13+
*
14+
* Should store generated index rows into items
15+
*/
16+
interface IndexWriter
17+
{
18+
public function add($row): void;
19+
}

Model/ArrayIndexRecordData.php renamed to Model/ArrayIndexRecord.php

+17-17
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,17 @@
88

99
namespace MageOS\Indexer\Model;
1010

11-
use MageOS\Indexer\Api\IndexRecordMutableData;
12-
11+
use MageOS\Indexer\Api\IndexRecordMutable;
1312
use Traversable;
1413

15-
class ArrayIndexRecordData implements \IteratorAggregate, IndexRecordMutableData
14+
class ArrayIndexRecord implements IndexRecordMutable
1615
{
1716
public function __construct(
1817
private array $data = [],
1918
private array $scopeData = []
20-
) {
21-
22-
}
23-
24-
public function getIterator(): Traversable
19+
)
2520
{
26-
foreach ($this->data as $entityId => $item) {
27-
yield $entityId;
28-
}
21+
2922
}
3023

3124
public function reset(): void
@@ -34,22 +27,22 @@ public function reset(): void
3427
$this->scopeData = [];
3528
}
3629

37-
public function setValue(int $entityId, array $data): void
30+
public function set(int $entityId, array $data): void
3831
{
3932
$this->data[$entityId] = $data;
4033
}
4134

42-
public function addValue(int $entityId, string $field, mixed $value): void
35+
public function add(int $entityId, string $field, mixed $value): void
4336
{
4437
$this->data[$entityId][$field] = $value;
4538
}
4639

47-
public function extendValue(int $entityId, string $field, string $key, mixed $value): void
40+
public function append(int $entityId, string $field, string $key, mixed $value): void
4841
{
4942
$this->data[$entityId][$field][$key] = $value;
5043
}
5144

52-
public function addScopeValue(int $entityId, int $storeId, string $field, mixed $value): void
45+
public function addInScope(int $entityId, int $storeId, string $field, mixed $value): void
5346
{
5447
if (!isset($this->data[$entityId])) {
5548
return;
@@ -58,15 +51,22 @@ public function addScopeValue(int $entityId, int $storeId, string $field, mixed
5851
$this->scopeData[$entityId][$storeId][$field] = $value;
5952
}
6053

61-
public function getScopeValue(int $entityId, int $scopeId, string $field): mixed
54+
public function getInScope(int $entityId, int $scopeId, string $field): mixed
6255
{
6356
return $this->scopeData[$entityId][$scopeId][$field]
6457
?? $this->scopeData[$entityId][0][$field]
6558
?? null;
6659
}
6760

68-
public function getValue(int $entityId, string $field): mixed
61+
public function get(int $entityId, string $field): mixed
6962
{
7063
return $this->data[$entityId][$field] ?? null;
7164
}
65+
66+
public function listEntityIds(): iterable
67+
{
68+
foreach ($this->data as $entityId => $item) {
69+
yield $entityId;
70+
}
71+
}
7272
}

Model/ArrayIndexRecordFactory.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
class ArrayIndexRecordFactory
12+
{
13+
public function create(): ArrayIndexRecord
14+
{
15+
return new ArrayIndexRecord();
16+
}
17+
}

0 commit comments

Comments
 (0)