Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.44% |
51 / 54 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
EntityReindexer | |
94.44% |
51 / 54 |
|
83.33% |
5 / 6 |
9.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getIndexClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
updateIndex | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
recalculateIdfForAll | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
createUpdateQuery | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
2 | |||
recalculateIdf | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | namespace Apie\DoctrineEntityDatalayer; |
3 | |
4 | use Apie\Core\Context\ApieContext; |
5 | use Apie\Core\Entities\EntityInterface; |
6 | use Apie\Core\Indexing\Indexer; |
7 | use Apie\StorageMetadataBuilder\Interfaces\HasIndexInterface; |
8 | use Doctrine\DBAL\ArrayParameterType; |
9 | use Doctrine\DBAL\Platforms\SqlitePlatform; |
10 | use ReflectionClass; |
11 | |
12 | final class EntityReindexer |
13 | { |
14 | public function __construct(private readonly OrmBuilder $ormBuilder, private readonly Indexer $indexer) |
15 | { |
16 | } |
17 | |
18 | /** |
19 | * @param ReflectionClass<HasIndexInterface> $doctrineEntity |
20 | * @return class-string<object> |
21 | */ |
22 | private function getIndexClass(ReflectionClass $doctrineEntity): string |
23 | { |
24 | return $doctrineEntity->getMethod('getIndexTable')->invoke(null)->name; |
25 | } |
26 | |
27 | /** |
28 | * Should be called after storing a doctrine entity from a domain entity. It recalculates the search terms |
29 | * for the entity. For searching we use TF IDF and recalculate the TF of the entity. The IDF needs to be |
30 | * recalculated in a separate function with an update query. |
31 | * |
32 | * @see https://en.wikipedia.org/wiki/Tf%E2%80%93idf |
33 | */ |
34 | public function updateIndex( |
35 | HasIndexInterface $doctrineEntity, |
36 | EntityInterface $entity, |
37 | bool $skipIdf = false |
38 | ): void { |
39 | $entityManager = $this->ormBuilder->createEntityManager(); |
40 | $newIndexes = $this->indexer->getIndexesForObject( |
41 | $entity, |
42 | new ApieContext() |
43 | ); |
44 | $doctrineEntity->replaceIndexes($newIndexes); |
45 | $termsToUpdate = array_keys($newIndexes); |
46 | $entityManager->persist($doctrineEntity); |
47 | $entityManager->flush(); |
48 | if (!$skipIdf) { |
49 | $this->recalculateIdf($doctrineEntity, $termsToUpdate); |
50 | } |
51 | } |
52 | |
53 | /** |
54 | * @param ReflectionClass<HasIndexInterface> $doctrineEntity |
55 | */ |
56 | public function recalculateIdfForAll(ReflectionClass $doctrineEntity): void |
57 | { |
58 | $query = $this->createUpdateQuery($doctrineEntity); |
59 | $entityManager = $this->ormBuilder->createEntityManager(); |
60 | $entityManager->getConnection()->executeQuery($query); |
61 | } |
62 | |
63 | /** |
64 | * @param ReflectionClass<HasIndexInterface> $doctrineEntity |
65 | */ |
66 | private function createUpdateQuery(ReflectionClass $doctrineEntity): string |
67 | { |
68 | $entityManager = $this->ormBuilder->createEntityManager(); |
69 | $tableName = (new ReflectionClass($this->getIndexClass($doctrineEntity)))->getShortName(); |
70 | $columnName = 'ref_' . $doctrineEntity->getShortName() . '_id'; |
71 | $totalDocumentQuery = sprintf( |
72 | '(SELECT total_documents FROM (SELECT COUNT(DISTINCT %s) AS total_documents FROM %s WHERE %s IS NOT NULL) AS sub1)', |
73 | $columnName, |
74 | $tableName, |
75 | $columnName |
76 | ); |
77 | $documentWithTermQuery = sprintf( |
78 | 'SELECT documents_with_term FROM (SELECT text, COUNT(DISTINCT %s) AS documents_with_term FROM %s WHERE %s IS NOT NULL GROUP BY text) AS sub WHERE sub.text', |
79 | $columnName, |
80 | $tableName, |
81 | $columnName |
82 | ); |
83 | $connection = $entityManager->getConnection(); |
84 | $query = sprintf( |
85 | 'UPDATE %s AS t |
86 | SET idf = COALESCE(%s((%s)/(%s = t.text LIMIT 1)), 1) |
87 | WHERE %s IS NOT NULL AND EXISTS (SELECT 1 FROM (SELECT text, COUNT(DISTINCT %s) AS documents_with_term FROM %s GROUP BY text) AS sub WHERE sub.text = t.text LIMIT 1);', |
88 | $tableName, |
89 | // @phpstan-ignore class.notFound |
90 | $connection->getDatabasePlatform() instanceof SqlitePlatform ? '' : 'log', |
91 | $totalDocumentQuery, |
92 | $documentWithTermQuery, |
93 | $columnName, |
94 | $columnName, |
95 | $tableName |
96 | ); |
97 | |
98 | return $query; |
99 | } |
100 | |
101 | /** |
102 | * @param array<int, string> $termsToUpdate |
103 | */ |
104 | private function recalculateIdf(HasIndexInterface $doctrineEntity, array $termsToUpdate): void |
105 | { |
106 | if (empty($termsToUpdate)) { |
107 | return; |
108 | } |
109 | $query = $this->createUpdateQuery(new ReflectionClass($doctrineEntity)); |
110 | $query = preg_replace('#LIMIT 1\);$#', 'AND t.text IN (:terms) LIMIT 1);', $query); |
111 | $entityManager = $this->ormBuilder->createEntityManager(); |
112 | $entityManager->getConnection()->executeQuery( |
113 | $query, |
114 | ['terms' => array_values($termsToUpdate)], |
115 | ['terms' => ArrayParameterType::STRING] |
116 | ); |
117 | } |
118 | } |