-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move partial index into the typo3 database #138
base: master
Are you sure you want to change the base?
Changes from 13 commits
ceb5eb1
63ac240
3c4f692
4a7ffb9
2b58203
b037818
21db9f5
915265d
85ec743
471fcdf
1d70b86
3145cd4
21e54aa
d52a017
42b231b
0075f64
5993f51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
namespace PAGEmachine\Searchable\Domain\Repository; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Domain may be OK but it's not a repository anymore, so it should be moved somewhere else. |
||
|
||
/* | ||
* This file is part of the PAGEmachine Searchable project. | ||
*/ | ||
|
||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; | ||
use TYPO3\CMS\Core\Database\ConnectionPool; | ||
|
||
final class UpdateQueue | ||
{ | ||
private const TABLE_NAME = 'tx_searchable_domain_model_update'; | ||
|
||
public function __construct(private readonly ConnectionPool $connectionPool) | ||
{ | ||
} | ||
|
||
public function enqueue( | ||
string $type, | ||
string $property, | ||
int $propertyUid, | ||
): void { | ||
try { | ||
$this->connectionPool | ||
->getConnectionForTable(self::TABLE_NAME) | ||
->insert( | ||
self::TABLE_NAME, | ||
[ | ||
'type' => $type, | ||
'property' => $property, | ||
'property_uid' => $propertyUid, | ||
], | ||
); | ||
} catch (UniqueConstraintViolationException) { | ||
// Ignore duplicate entry error | ||
} | ||
} | ||
|
||
public function pendingUpdates(string $type = null): array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I'd do this via foreach ($this->updateQueue as $update) { ... } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As it needs to be filterable by type I think the current implementation is cleaner than the alternatives. Using a Generator or passing the fetchAssociative directly (not a big fan) would be an option. But as it is only used in the preparation step to collect the to update uids and because of needed deduplication it should only a have minor impacts |
||
{ | ||
$queryBuilder = $this->connectionPool | ||
->getConnectionForTable(self::TABLE_NAME) | ||
->createQueryBuilder(); | ||
|
||
$queryBuilder | ||
->select('*') | ||
->from(self::TABLE_NAME); | ||
|
||
if ($type) { | ||
$queryBuilder | ||
->where( | ||
$queryBuilder->expr()->eq('type', $queryBuilder->createNamedParameter($type, \PDO::PARAM_STR)), | ||
); | ||
} | ||
|
||
return $queryBuilder->executeQuery()->fetchAllAssociative(); | ||
} | ||
|
||
public function clear() | ||
{ | ||
foreach ($this->pendingUpdates() as $object) { | ||
$this->connectionPool->getConnectionForTable(self::TABLE_NAME) | ||
->delete( | ||
self::TABLE_NAME, | ||
['uid' => $object['uid']], | ||
); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
<?php | ||
namespace PAGEmachine\Searchable\Query; | ||
|
||
use PAGEmachine\Searchable\Service\ExtconfService; | ||
use PAGEmachine\Searchable\Domain\Repository\UpdateQueue; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
/* | ||
* This file is part of the PAGEmachine Searchable project. | ||
|
@@ -13,13 +14,16 @@ | |
*/ | ||
class UpdateQuery extends AbstractQuery | ||
{ | ||
protected UpdateQueue $updateQueue; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->setIndices([ExtconfService::getInstance()->getUpdateIndex()]); | ||
|
||
$this->updateQueue = GeneralUtility::makeInstance(UpdateQueue::class); | ||
|
||
$this->init(); | ||
} | ||
|
@@ -31,35 +35,18 @@ public function __construct() | |
public function init() | ||
mbrodala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$this->parameters = [ | ||
'index' => implode(',', $this->getIndices()), | ||
'body' => [ | ||
], | ||
'index' => [], | ||
'body' => [], | ||
]; | ||
} | ||
|
||
/** | ||
* Adds a new update query string | ||
* | ||
* @return array | ||
*/ | ||
public function addUpdate($type, $property, $id) | ||
public function addUpdate($type, $property, $id): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above, |
||
{ | ||
// Use querystring hash as id to mark each update only once | ||
$docid = sha1($type . "." . $property . ":" . $id); | ||
|
||
$this->parameters['id'] = $docid; | ||
$this->parameters['type'] = '_doc'; | ||
$this->parameters['body']['type'] = strval($type); | ||
$this->parameters['body']['property'] = $property; | ||
$this->parameters['body']['uid'] = $id; | ||
|
||
try { | ||
$response = $this->client->index($this->getParameters()); | ||
return $response; | ||
} catch (\Exception $e) { | ||
$this->logger->error("Could not track update. Reason: " . $e->getMessage()); | ||
return []; | ||
} | ||
$this->updateQueue->enqueue($type, $property, $id); | ||
} | ||
|
||
/** | ||
|
@@ -69,39 +56,22 @@ public function addUpdate($type, $property, $id) | |
*/ | ||
public function getUpdates($index, $type) | ||
{ | ||
$recordids = []; | ||
|
||
$this->init(); | ||
|
||
$this->parameters['body'] = [ | ||
'query' => [ | ||
'bool' => [ | ||
'filter' => [ | ||
'term' => [ | ||
'type' => $type, | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
|
||
$result = $this->client->search($this->parameters); | ||
$recordids = []; | ||
|
||
if (empty($result['hits']['hits'])) { | ||
return []; | ||
} | ||
$results = $this->updateQueue->pendingUpdates($type); | ||
|
||
$updateParams = []; | ||
|
||
foreach ($result['hits']['hits'] as $hit) { | ||
foreach ($results as $update) { | ||
//If this is a simple toplevel uid check, we can add this id directly to the updated uid list | ||
if ($hit['_source']['property'] == 'uid') { | ||
$recordids[$hit['_source']['uid']] = $hit['_source']['uid']; | ||
if ($update['property'] == 'uid') { | ||
$recordids[$update['property_uid']] = $update['property_uid']; | ||
} else { | ||
$updateParams[] = [ | ||
"term" => [ | ||
$hit['_source']['property'] => $hit['_source']['uid'], | ||
$update['property'] => $update['property_uid'], | ||
], | ||
]; | ||
} | ||
|
@@ -124,7 +94,7 @@ public function getUpdates($index, $type) | |
|
||
if (!empty($result['hits']['hits'])) { | ||
foreach ($result['hits']['hits'] as $hit) { | ||
$recordids[$hit['_id']] = $hit['_id']; | ||
$recordids[$hit['_id']] = (int) $hit['_id']; | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rebase on
master
and drop these changes? #136 was merged. :-)