Skip to content

Commit

Permalink
fix(resetSessions): Truncate tables and rename documents folder
Browse files Browse the repository at this point in the history
This is way more performant than iterating over all existing sessions.

Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Jun 17, 2024
1 parent d2d22a1 commit 94230e4
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
6 changes: 6 additions & 0 deletions lib/Command/ResetDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
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) {
Expand Down
6 changes: 6 additions & 0 deletions lib/Db/DocumentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ public function countAll(): int {
$result->closeCursor();
return $count;
}

public function clearAll(): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->executeStatement();
}
}
6 changes: 6 additions & 0 deletions lib/Db/SessionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ public function deleteByDocumentId(int $documentId): int {
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('*')
Expand Down
6 changes: 6 additions & 0 deletions lib/Db/StepMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public function deleteAll(int $documentId): void {
->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();
Expand Down
9 changes: 1 addition & 8 deletions lib/Migration/ResetSessionsBeforeYjs.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ public function run(IOutput $output): void {
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();
}
}
25 changes: 24 additions & 1 deletion lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IRequest;
use OCP\Lock\LockedException;
use OCP\PreConditionNotMetException;
Expand Down Expand Up @@ -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;
Expand All @@ -83,6 +85,7 @@ public function __construct(DocumentMapper $documentMapper, StepMapper $stepMapp
$this->shareManager = $shareManager;
$this->lockManager = $lockManager;
$this->userMountCache = $userMountCache;
$this->config = $config;
$token = $request->getParam('token');
if ($this->userId === null && $token !== null) {
try {
Expand Down Expand Up @@ -646,4 +649,24 @@ public function unlock(int $fileId): void {
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();
}
}
3 changes: 3 additions & 0 deletions tests/unit/Service/DocumentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand Down Expand Up @@ -49,6 +50,7 @@ public function setUp(): void {
$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,
Expand All @@ -64,6 +66,7 @@ public function setUp(): void {
$this->directManager,
$this->lockManager,
$this->userMountCache,
$config,
);
}

Expand Down

0 comments on commit 94230e4

Please sign in to comment.