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 коммит произвёл backportbot[bot]
Родитель f7601878a4
Коммит 914c10054b
7 изменённых файлов: 52 добавлений и 9 удалений

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

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

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

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

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

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

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

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

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

@ -26,13 +26,6 @@ class ResetSessionsBeforeYjs implements IRepairStep {
return; return;
} }
$output->startProgress($this->documentService->countAll()); $this->documentService->clearAll();
foreach ($this->documentService->getAll() as $document) {
$fileId = $document->getId();
$this->documentService->unlock($fileId);
$this->documentService->resetDocument($fileId, true);
$output->advance();
}
$output->finishProgress();
} }
} }

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

@ -59,6 +59,7 @@ use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFile;
use OCP\ICache; use OCP\ICache;
use OCP\ICacheFactory; use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IRequest; use OCP\IRequest;
use OCP\Lock\LockedException; use OCP\Lock\LockedException;
use OCP\PreConditionNotMetException; use OCP\PreConditionNotMetException;
@ -87,8 +88,9 @@ class DocumentService {
private IAppData $appData; private IAppData $appData;
private ILockManager $lockManager; private ILockManager $lockManager;
private IUserMountCache $userMountCache; 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->documentMapper = $documentMapper;
$this->stepMapper = $stepMapper; $this->stepMapper = $stepMapper;
$this->sessionMapper = $sessionMapper; $this->sessionMapper = $sessionMapper;
@ -100,6 +102,7 @@ class DocumentService {
$this->shareManager = $shareManager; $this->shareManager = $shareManager;
$this->lockManager = $lockManager; $this->lockManager = $lockManager;
$this->userMountCache = $userMountCache; $this->userMountCache = $userMountCache;
$this->config = $config;
$token = $request->getParam('token'); $token = $request->getParam('token');
if ($this->userId === null && $token !== null) { if ($this->userId === null && $token !== null) {
try { try {
@ -662,4 +665,24 @@ class DocumentService {
public function countAll(): int { public function countAll(): int {
return $this->documentMapper->countAll(); 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\Lock\ILockManager;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
use OCP\ICacheFactory; use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IRequest; use OCP\IRequest;
use OCP\Share\IManager; use OCP\Share\IManager;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -49,6 +50,7 @@ class DocumentServiceTest extends \PHPUnit\Framework\TestCase {
$this->directManager = $this->createMock(\OCP\DirectEditing\IManager::class); $this->directManager = $this->createMock(\OCP\DirectEditing\IManager::class);
$this->lockManager = $this->createMock(ILockManager::class); $this->lockManager = $this->createMock(ILockManager::class);
$this->userMountCache = $this->createMock(IUserMountCache::class); $this->userMountCache = $this->createMock(IUserMountCache::class);
$config = $this->createMock(IConfig::class);
$this->documentService = new DocumentService( $this->documentService = new DocumentService(
$this->documentMapper, $this->documentMapper,
@ -64,6 +66,7 @@ class DocumentServiceTest extends \PHPUnit\Framework\TestCase {
$this->directManager, $this->directManager,
$this->lockManager, $this->lockManager,
$this->userMountCache, $this->userMountCache,
$config,
); );
} }