Merge pull request #5261 from wallabag/fix/performance-exists

Improve performance of REST exists call
This commit is contained in:
Kevin Decherf 2021-08-07 00:11:37 +02:00 committed by GitHub
commit d7a3f7eb01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 21 deletions

View file

@ -67,11 +67,24 @@ class EntryRestController extends WallabagRestController
throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId());
} }
$results = []; $results = array_fill_keys($hashedUrls, null);
foreach ($hashedUrls as $hashedUrlToSearch) { $res = $repo->findByUserIdAndBatchHashedUrls($this->getUser()->getId(), $hashedUrls);
$res = $repo->findByHashedUrlAndUserId($hashedUrlToSearch, $this->getUser()->getId()); foreach ($res as $e) {
$_hashedUrl = array_keys($hashedUrls, 'blah', true);
if ([] !== array_keys($hashedUrls, $e['hashedUrl'], true)) {
$_hashedUrl = $e['hashedUrl'];
} elseif ([] !== array_keys($hashedUrls, $e['hashedGivenUrl'], true)) {
$_hashedUrl = $e['hashedGivenUrl'];
} else {
continue;
}
$results[$_hashedUrl] = $e['id'];
}
$results[$hashedUrlToSearch] = $this->returnExistInformation($res, $returnId); if (false === $returnId) {
$results = array_map(function ($v) {
return null !== $v;
}, $results);
} }
$results = $this->replaceUrlHashes($results, $urlHashMap); $results = $this->replaceUrlHashes($results, $urlHashMap);
@ -840,21 +853,4 @@ class EntryRestController extends WallabagRestController
'origin_url' => $request->request->get('origin_url', ''), 'origin_url' => $request->request->get('origin_url', ''),
]; ];
} }
/**
* Return information about the entry if it exist and depending on the id or not.
*
* @param Entry|bool|null $entry
* @param bool $returnId
*
* @return bool|int
*/
private function returnExistInformation($entry, $returnId)
{
if ($returnId) {
return $entry instanceof Entry ? $entry->getId() : null;
}
return $entry instanceof Entry;
}
} }

View file

@ -421,6 +421,22 @@ class EntryRepository extends EntityRepository
return false; return false;
} }
public function findByUserIdAndBatchHashedUrls($userId, $hashedUrls)
{
$qb = $this->createQueryBuilder('e')->select(['e.id', 'e.hashedUrl', 'e.hashedGivenUrl']);
$res = $qb->where('e.user = :user_id')->setParameter('user_id', $userId)
->andWhere(
$qb->expr()->orX(
$qb->expr()->in('e.hashedUrl', $hashedUrls),
$qb->expr()->in('e.hashedGivenUrl', $hashedUrls)
)
)
->getQuery()
->getResult();
return $res;
}
/** /**
* Count all entries for a user. * Count all entries for a user.
* *