perf: Use generator/count query on cleanup tasks

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2024-06-17 09:33:03 +02:00 коммит произвёл backportbot[bot]
Родитель bdc81f5b14
Коммит f7601878a4
7 изменённых файлов: 47 добавлений и 21 удалений

Просмотреть файл

@ -78,9 +78,10 @@ class ResetDocument extends Command {
}
if ($all) {
$fileIds = array_map(static function (Document $document) {
return $document->getId();
}, $this->documentService->getAll());
$fileIds = [];
foreach ($this->documentService->getAll() as $document) {
$fileIds[] = $document->getId();
}
} else {
$fileIds = [$fileId];
}

Просмотреть файл

@ -56,10 +56,8 @@ class Cleanup extends TimedJob {
*/
protected function run($argument): void {
$this->logger->debug('Run cleanup job for text documents');
$documents = $this->documentService->getAll();
foreach ($documents as $document) {
$allSessions = $this->sessionService->getAllSessions($document->getId());
if (count($allSessions) > 0) {
foreach ($this->documentService->getAll() as $document) {
if ($this->sessionService->countAllSessions($document->getId()) > 0) {
// Do not reset if there are any sessions left
// Inactive sessions will get removed further down and will trigger a reset next time
continue;

Просмотреть файл

@ -23,6 +23,7 @@
namespace OCA\Text\Db;
use Generator;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
@ -55,12 +56,27 @@ class DocumentMapper extends QBMapper {
return Document::fromRow($data);
}
public function findAll(): array {
public function findAll(): Generator {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('*')
->from($this->getTableName())
->executeQuery();
try {
while ($row = $result->fetch()) {
yield $this->mapRowToEntity($row);
}
} finally {
$result->closeCursor();
}
}
return $this->findEntities($qb);
public function countAll(): int {
$qb = $this->db->getQueryBuilder();
$qb->select($qb->func()->count('id'))
->from($this->getTableName());
$result = $qb->executeQuery();
$count = (int)$result->fetchOne();
$result->closeCursor();
return $count;
}
}

Просмотреть файл

@ -85,6 +85,17 @@ class SessionMapper extends QBMapper {
return $this->findEntities($qb);
}
public function countAll(int $documentId): int {
$qb = $this->db->getQueryBuilder();
$qb->select('id', 'color', 'document_id', 'last_awareness_message', 'last_contact', 'user_id', 'guest_name')
->from($this->getTableName())
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)));
$result = $qb->executeQuery();
$count = (int)$result->fetchOne();
$result->closeCursor();
return $count;
}
/**
* @return Session[]
*

Просмотреть файл

@ -2,7 +2,6 @@
namespace OCA\Text\Migration;
use OCA\Text\Db\Document;
use OCA\Text\Service\DocumentService;
use OCP\IAppConfig;
use OCP\Migration\IOutput;
@ -27,16 +26,9 @@ class ResetSessionsBeforeYjs implements IRepairStep {
return;
}
$fileIds = array_map(static function (Document $document) {
return $document->getId();
}, $this->documentService->getAll());
if (!$fileIds) {
return;
}
$output->startProgress(count($fileIds));
foreach ($fileIds as $fileId) {
$output->startProgress($this->documentService->countAll());
foreach ($this->documentService->getAll() as $document) {
$fileId = $document->getId();
$this->documentService->unlock($fileId);
$this->documentService->resetDocument($fileId, true);
$output->advance();

Просмотреть файл

@ -444,7 +444,7 @@ class DocumentService {
}
}
public function getAll(): array {
public function getAll(): \Generator {
return $this->documentMapper->findAll();
}
@ -658,4 +658,8 @@ class DocumentService {
} catch (NoLockProviderException | PreConditionNotMetException | NotFoundException $e) {
}
}
public function countAll(): int {
return $this->documentMapper->countAll();
}
}

Просмотреть файл

@ -123,6 +123,10 @@ class SessionService {
}, $sessions);
}
public function countAllSessions(int $documentId): int {
return $this->sessionMapper->countAll($documentId);
}
public function getActiveSessions(int $documentId): array {
$sessions = $this->sessionMapper->findAllActive($documentId);
return array_map(function (Session $session) {