Added logic to resolve crawlable short codes

This commit is contained in:
Alejandro Celaya
2021-05-22 09:34:42 +02:00
parent e6ce84aa14
commit 3ef02d46c0
6 changed files with 88 additions and 4 deletions

View File

@@ -288,4 +288,28 @@ class ShortUrlRepository extends EntitySpecificationRepository implements ShortU
$qb->andWhere($qb->expr()->isNull('s.domain'));
}
}
public function findCrawlableShortCodes(): iterable
{
$blockSize = 1000;
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('DISTINCT s.shortCode')
->from(ShortUrl::class, 's')
->where($qb->expr()->eq('s.crawlable', ':crawlable'))
->setParameter('crawlable', true)
->setMaxResults($blockSize);
$page = 0;
do {
$qbClone = (clone $qb)->setFirstResult($blockSize * $page);
$iterator = $qbClone->getQuery()->toIterable();
$resultsFound = false;
$page++;
foreach ($iterator as ['shortCode' => $shortCode]) {
$resultsFound = true;
yield $shortCode;
}
} while ($resultsFound);
}
}

View File

@@ -41,4 +41,6 @@ interface ShortUrlRepositoryInterface extends ObjectRepository, EntitySpecificat
public function findOneMatching(ShortUrlMeta $meta): ?ShortUrl;
public function findOneByImportedUrl(ImportedShlinkUrl $url): ?ShortUrl;
public function findCrawlableShortCodes(): iterable;
}

View File

@@ -66,11 +66,11 @@ class VisitRepository extends EntitySpecificationRepository implements VisitRepo
do {
$qb = (clone $originalQueryBuilder)->andWhere($qb->expr()->gt('v.id', $lastId));
$iterator = $qb->getQuery()->iterate();
$iterator = $qb->getQuery()->toIterable();
$resultsFound = false;
/** @var Visit $visit */
foreach ($iterator as $key => [$visit]) {
foreach ($iterator as $key => $visit) {
$resultsFound = true;
yield $key => $visit;
}