fix(resetSessions): Truncate tables and rename documents folder

This is way more performant than iterating over all existing sessions.

Signed-off-by: Jonas <jonas@freesources.org>
This commit is contained in:
Jonas 2024-06-17 11:40:45 +02:00
Родитель d2d22a16d6
Коммит 94230e4156
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 5262E7FF491049FE
7 изменённых файлов: 52 добавлений и 9 удалений

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

@ -60,6 +60,12 @@ class ResetDocument extends Command {
return 1;
}
if ($all && $fullReset) {
// Truncate tables and clear document directory
$this->documentService->clearAll();
return 0;
}
if ($all) {
$fileIds = [];
foreach ($this->documentService->getAll() as $document) {

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

@ -62,4 +62,10 @@ class DocumentMapper extends QBMapper {
$result->closeCursor();
return $count;
}
public function clearAll(): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->executeStatement();
}
}

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

@ -148,6 +148,12 @@ class SessionMapper extends QBMapper {
return $qb->executeStatement();
}
public function clearAll(): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->executeStatement();
}
public function isUserInDocument(int $documentId, string $userId): bool {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('*')

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

@ -62,6 +62,12 @@ class StepMapper extends QBMapper {
->executeStatement();
}
public function clearAll(): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->executeStatement();
}
// not in use right now
public function deleteBeforeVersion(int $documentId, int $version): int {
$qb = $this->db->getQueryBuilder();

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

@ -31,13 +31,6 @@ class ResetSessionsBeforeYjs implements IRepairStep {
return;
}
$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();
}
$output->finishProgress();
$this->documentService->clearAll();
}
}

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

@ -42,6 +42,7 @@ use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IRequest;
use OCP\Lock\LockedException;
use OCP\PreConditionNotMetException;
@ -70,8 +71,9 @@ class DocumentService {
private IAppData $appData;
private ILockManager $lockManager;
private IUserMountCache $userMountCache;
private IConfig $config;
public function __construct(DocumentMapper $documentMapper, StepMapper $stepMapper, SessionMapper $sessionMapper, IAppData $appData, ?string $userId, IRootFolder $rootFolder, ICacheFactory $cacheFactory, LoggerInterface $logger, ShareManager $shareManager, IRequest $request, IManager $directManager, ILockManager $lockManager, IUserMountCache $userMountCache) {
public function __construct(DocumentMapper $documentMapper, StepMapper $stepMapper, SessionMapper $sessionMapper, IAppData $appData, ?string $userId, IRootFolder $rootFolder, ICacheFactory $cacheFactory, LoggerInterface $logger, ShareManager $shareManager, IRequest $request, IManager $directManager, ILockManager $lockManager, IUserMountCache $userMountCache, IConfig $config) {
$this->documentMapper = $documentMapper;
$this->stepMapper = $stepMapper;
$this->sessionMapper = $sessionMapper;
@ -83,6 +85,7 @@ class DocumentService {
$this->shareManager = $shareManager;
$this->lockManager = $lockManager;
$this->userMountCache = $userMountCache;
$this->config = $config;
$token = $request->getParam('token');
if ($this->userId === null && $token !== null) {
try {
@ -646,4 +649,24 @@ class DocumentService {
public function countAll(): int {
return $this->documentMapper->countAll();
}
private function getFullAppFolder(): Folder {
$appFolder = $this->rootFolder->get('appdata_' . $this->config->getSystemValueString('instanceid', '') . '/text');
if (!$appFolder instanceof Folder) {
throw new NotFoundException('Folder not found');
}
return $appFolder;
}
public function clearAll(): void {
$this->stepMapper->clearAll();
$this->sessionMapper->clearAll();
$this->documentMapper->clearAll();
try {
$appFolder = $this->getFullAppFolder();
$appFolder->get('documents')->move($appFolder->getPath() . '/documents_old_' . time());
} catch (NotFoundException) {
}
$this->ensureDocumentsFolder();
}
}

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

@ -14,6 +14,7 @@ use OCP\Files\IRootFolder;
use OCP\Files\Lock\ILockManager;
use OCP\Files\NotPermittedException;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IRequest;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;
@ -49,6 +50,7 @@ class DocumentServiceTest extends \PHPUnit\Framework\TestCase {
$this->directManager = $this->createMock(\OCP\DirectEditing\IManager::class);
$this->lockManager = $this->createMock(ILockManager::class);
$this->userMountCache = $this->createMock(IUserMountCache::class);
$config = $this->createMock(IConfig::class);
$this->documentService = new DocumentService(
$this->documentMapper,
@ -64,6 +66,7 @@ class DocumentServiceTest extends \PHPUnit\Framework\TestCase {
$this->directManager,
$this->lockManager,
$this->userMountCache,
$config,
);
}