-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dav): introduce paginate with custom headers
Signed-off-by: Benjamin Gaussorgues <[email protected]>
- Loading branch information
Showing
9 changed files
with
348 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\DAV\BackgroundJob; | ||
|
||
use OCA\DAV\Paginate\PaginateCache; | ||
use OCP\BackgroundJob\Job; | ||
|
||
class CleanupPaginateCacheJob extends Job { | ||
/** @var PaginateCache */ | ||
private $cache; | ||
|
||
public function __construct(PaginateCache $cache) { | ||
$this->cache = $cache; | ||
} | ||
|
||
public function run($argument): void { | ||
$this->cache->cleanup(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\DAV\Migration; | ||
|
||
use OCP\DB\ISchemaWrapper; | ||
use OCP\DB\Types; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
|
||
class Version1032Date20241011093632 extends SimpleMigrationStep { | ||
public function name(): string { | ||
return 'Add dav_page_cache table'; | ||
} | ||
|
||
public function description(): string { | ||
return 'Add table to cache webdav multistatus responses for pagination purpose'; | ||
} | ||
|
||
public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { | ||
/** @var ISchemaWrapper $schema */ | ||
$schema = $schemaClosure(); | ||
|
||
if (!$schema->hasTable('dav_page_cache')) { | ||
$table = $schema->createTable('dav_page_cache'); | ||
|
||
$table->addColumn('id', Types::BIGINT, [ | ||
'autoincrement' => true | ||
]); | ||
$table->addColumn('url_hash', Types::STRING, [ | ||
'notnull' => true, | ||
'length' => 32, | ||
]); | ||
$table->addColumn('token', Types::STRING, [ | ||
'notnull' => true, | ||
'length' => 32 | ||
]); | ||
$table->addColumn('result_index', Types::INTEGER, [ | ||
'notnull' => true | ||
]); | ||
$table->addColumn('result_value', Types::TEXT, [ | ||
'notnull' => false, | ||
]); | ||
$table->addColumn('insert_time', Types::DATETIME, [ | ||
'notnull' => true, | ||
]); | ||
|
||
$table->setPrimaryKey(['id'], 'dav_page_cache_id_index'); | ||
$table->addIndex(['token', 'url_hash'], 'dav_page_cache_token_url'); | ||
$table->addUniqueIndex(['token', 'url_hash', 'result_index'], 'dav_page_cache_url_index'); | ||
$table->addIndex(['result_index'], 'dav_page_cache_index'); | ||
$table->addIndex(['insert_time'], 'dav_page_cache_time'); | ||
} | ||
|
||
return $schema; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\DAV\Paginate; | ||
|
||
/** | ||
* Save a copy of the first X items into a separate iterator | ||
* | ||
* This allows us to pass the iterator to the cache while keeping a copy | ||
* of the required items. | ||
*/ | ||
class LimitedCopyIterator extends \AppendIterator { | ||
Check failure on line 18 in apps/dav/lib/Paginate/LimitedCopyIterator.php GitHub Actions / static-code-analysisMissingTemplateParam
|
||
private array $skipped = []; | ||
private array $copy = []; | ||
|
||
public function __construct(\Traversable $iterator, int $count, int $offset = 0) { | ||
parent::__construct(); | ||
|
||
if (!$iterator instanceof \Iterator) { | ||
$iterator = new \IteratorIterator($iterator); | ||
} | ||
$iterator = new \NoRewindIterator($iterator); | ||
|
||
$i = 0; | ||
while ($iterator->valid() && ++$i <= $offset) { | ||
$this->skipped[] = $iterator->current(); | ||
$iterator->next(); | ||
} | ||
|
||
while ($iterator->valid() && count($this->copy) < $count) { | ||
$this->copy[] = $iterator->current(); | ||
$iterator->next(); | ||
} | ||
|
||
$this->append(new \ArrayIterator($this->skipped)); | ||
$this->append($this->getRequestedItems()); | ||
$this->append($iterator); | ||
} | ||
|
||
public function getRequestedItems(): \Iterator { | ||
return new \ArrayIterator($this->copy); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\DAV\Paginate; | ||
|
||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||
use OCP\IDBConnection; | ||
use OCP\Security\ISecureRandom; | ||
|
||
class PaginateCache { | ||
public const TTL = '1 hour'; | ||
|
||
public function __construct( | ||
private IDBConnection $database, | ||
private ISecureRandom $random, | ||
private ITimeFactory $timeFactory, | ||
) { | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param \Iterator $items | ||
* @return array{'token': string, 'count': int} | ||
*/ | ||
public function store(string $uri, \Iterator $items): array { | ||
$token = $this->random->generate(32); | ||
$now = $this->timeFactory->getDateTime(); | ||
|
||
$query = $this->database->getQueryBuilder(); | ||
$query->insert('dav_page_cache') | ||
->values([ | ||
'url_hash' => $query->createNamedParameter(md5($uri), IQueryBuilder::PARAM_STR), | ||
'token' => $query->createNamedParameter($token, IQueryBuilder::PARAM_STR), | ||
'insert_time' => $query->createNamedParameter($now, IQueryBuilder::PARAM_DATETIME_MUTABLE), | ||
'result_index' => $query->createParameter('index'), | ||
'result_value' => $query->createParameter('value'), | ||
]); | ||
|
||
$count = 0; | ||
foreach ($items as $item) { | ||
$value = serialize($item); | ||
$query->setParameter('index', $count, IQueryBuilder::PARAM_INT); | ||
$query->setParameter('value', $value); | ||
$query->executeStatement(); | ||
$count++; | ||
} | ||
|
||
return ['token' => $token, 'count' => $count]; | ||
} | ||
|
||
public function get(string $url, string $token, int $offset, int $count): array { | ||
$query = $this->database->getQueryBuilder(); | ||
$query->select(['result_value']) | ||
->from('dav_page_cache') | ||
->where($query->expr()->eq('token', $query->createNamedParameter($token))) | ||
->andWhere($query->expr()->eq('url_hash', $query->createNamedParameter(md5($url)))) | ||
->andWhere($query->expr()->gte('result_index', $query->createNamedParameter($offset, IQueryBuilder::PARAM_INT))) | ||
->andWhere($query->expr()->lt('result_index', $query->createNamedParameter($offset + $count, IQueryBuilder::PARAM_INT))); | ||
|
||
$result = $query->executeQuery(); | ||
return array_map(function (string $entry) { | ||
return unserialize($entry); | ||
}, $result->fetchAll(\PDO::FETCH_COLUMN)); | ||
} | ||
|
||
public function exists(string $token): bool { | ||
$query = $this->database->getQueryBuilder(); | ||
return (bool)$query->select('id') | ||
->from('dav_page_cache') | ||
->where($query->expr()->eq('token', $query->createNamedParameter($token))) | ||
->setMaxResults(1) | ||
->executeQuery() | ||
->fetchOne(); | ||
} | ||
|
||
public function cleanup(): void { | ||
$now = $this->timeFactory->getDateTime(); | ||
$minDate = $now->sub(\DateInterval::createFromDateString(self::TTL)); | ||
|
||
$query = $this->database->getQueryBuilder(); | ||
$query->delete('dav_page_cache') | ||
->where($query->expr()->lt('insert_time', $query->createNamedParameter($minDate, IQueryBuilder::PARAM_DATETIME_MUTABLE))); | ||
$query->executeStatement(); | ||
} | ||
|
||
public function clear(): void { | ||
$query = $this->database->getQueryBuilder(); | ||
$query->delete('dav_page_cache'); | ||
$query->executeStatement(); | ||
} | ||
} |
Oops, something went wrong.