mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-12-02 13:32:43 -05:00
Closes #9574 Changes proposed in this pull request: - `FreshRSS_EntryDAOSQLite::markRead()` now passes arrays of IDs to `FreshRSS_EntryDAO::markRead()`. That method already does a batched `UPDATE … WHERE is_read<>? AND id IN (…)`, split into chunks by `MAX_VARIABLE_NUMBER` (998, which also covers SQLite's parameter limit), then refreshes the unread caches once. For lists of fewer than 6 IDs it still calls back into the existing per-entry SQLite/PostgreSQL path. `FreshRSS_EntryDAOPGSQL` inherits the change. - The `@param` type now matches the parent's `list<numeric-string>` (PHPStan `argument.type`). How to test the feature manually: 1. Use PostgreSQL with some latency (`tc qdisc add dev eth0 root netem delay 25ms` on the database host gave about 75 ms per round trip). 2. Mark several hundred unread articles read in a single Google Reader API `edit-tag` request, or call `FreshRSS_Factory::createEntryDao()->markRead($ids, true)` from a CLI script. 3. Compare elapsed time and check that `SUM(cache_nbUnreads)` still equals the unread count in `_entry`. My results (official `edge` image; the DAO files are identical in 1.30.4): | Backend | Case | Before | After | |---|---|---|---| | PostgreSQL 17, ~75 ms RTT | mark 300 read | 60.5 s | 0.16 s | | PostgreSQL 17, ~75 ms RTT | resend the same 300 (no change) | 37.6 s | 0.08 s | | PostgreSQL 17, ~75 ms RTT | mark 2,500 read / unread (chunked) | not run | 0.49 s / 0.49 s | | SQLite | mark 300 read | 1.48 s | 0.01 s | | SQLite | mark 2,500 read / unread (chunked) | not run | 0.04 s / 0.04 s | Unread caches matched the entry table in every case, including lists under 6 IDs and mark-unread. A production instance (PostgreSQL about 100 ms away) has been running the PostgreSQL variant of this change. Reeder on iOS, which had been timing out on every sync, now syncs normally, and `edit-tag` requests finish in under a second. * SQLite, PostgreSQL: batch markRead for lists of IDs FreshRSS_EntryDAOSQLite::markRead() handled an array of IDs one entry per transaction, and FreshRSS_EntryDAOPGSQL inherits it. Delegate arrays to FreshRSS_EntryDAO::markRead(), whose batched UPDATE ... WHERE is_read<>? AND id IN (...) is portable, is chunked by MAX_VARIABLE_NUMBER, and calls back into the per-entry path for small lists. Fix https://github.com/FreshRSS/FreshRSS/issues/9574 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Reduce comments --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
216 lines
6.7 KiB
PHP
216 lines
6.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
class FreshRSS_EntryDAOSQLite extends FreshRSS_EntryDAO {
|
|
|
|
#[\Override]
|
|
public static function isCompressed(): bool {
|
|
return false;
|
|
}
|
|
|
|
#[\Override]
|
|
public static function hasNativeHex(): bool {
|
|
return false;
|
|
}
|
|
|
|
#[\Override]
|
|
protected static function sqlConcat(string $s1, string $s2): string {
|
|
return $s1 . '||' . $s2;
|
|
}
|
|
|
|
#[\Override]
|
|
public static function sqlHexDecode(string $x): string {
|
|
return $x;
|
|
}
|
|
|
|
#[\Override]
|
|
public static function sqlIgnoreConflict(string $sql): string {
|
|
return str_replace('INSERT INTO ', 'INSERT OR IGNORE INTO ', $sql);
|
|
}
|
|
|
|
#[\Override]
|
|
protected static function sqlLimitAll(): string {
|
|
// https://sqlite.org/lang_select.html#the_limit_clause
|
|
return '-1';
|
|
}
|
|
|
|
#[\Override]
|
|
public static function sqlRandom(): string {
|
|
return 'RANDOM()';
|
|
}
|
|
|
|
#[\Override]
|
|
protected static function sqlRegex(string $expression, string $regex, array &$values): string {
|
|
$values[] = $regex;
|
|
return "{$expression} REGEXP ?";
|
|
}
|
|
|
|
#[\Override]
|
|
protected function registerSqlFunctions(string $sql): void {
|
|
if (!str_contains($sql, ' REGEXP ')) {
|
|
return;
|
|
}
|
|
// https://www.php.net/pdo-sqlite.createfunction
|
|
// https://www.sqlite.org/lang_expr.html#the_like_glob_regexp_match_and_extract_operators
|
|
if (class_exists(\Pdo\Sqlite::class) && $this->pdo instanceof \Pdo\Sqlite) {
|
|
$this->pdo->createFunction('regexp',
|
|
function (string $pattern, string $text): bool {
|
|
return preg_match($pattern, $text) === 1;
|
|
},
|
|
2
|
|
);
|
|
} else { // PHP < 8.4
|
|
$this->pdo->sqliteCreateFunction('regexp',
|
|
function (string $pattern, string $text): bool {
|
|
return preg_match($pattern, $text) === 1;
|
|
},
|
|
2
|
|
);
|
|
}
|
|
}
|
|
|
|
/** @param array{0:string,1:int,2:string} $errorInfo */
|
|
#[\Override]
|
|
protected function autoUpdateDb(array $errorInfo): bool {
|
|
$columns = $this->fetchColumn("PRAGMA table_info('entry')", 1);
|
|
if ($columns !== null) {
|
|
foreach (['attributes', 'lastUserModified', 'lastModified'] as $column) {
|
|
if (!in_array($column, $columns, true)) {
|
|
return $this->addColumn($column);
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
#[\Override]
|
|
public function commitNewEntries(): bool {
|
|
$sql = <<<'SQL'
|
|
DROP TABLE IF EXISTS `tmp`;
|
|
CREATE TEMP TABLE `tmp` AS
|
|
SELECT id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
|
|
FROM `_entrytmp`
|
|
ORDER BY date, id;
|
|
INSERT OR IGNORE INTO `_entry`
|
|
(id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes)
|
|
SELECT rowid + (SELECT MAX(id) - COUNT(*) FROM `tmp`) AS id,
|
|
guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
|
|
FROM `tmp` t
|
|
ORDER BY t.date, t.id;
|
|
DELETE FROM `_entrytmp` WHERE id <= (SELECT MAX(id) FROM `tmp`);
|
|
DROP TABLE IF EXISTS `tmp`;
|
|
SQL;
|
|
$hadTransaction = $this->pdo->inTransaction();
|
|
if (!$hadTransaction) {
|
|
$this->pdo->beginTransaction();
|
|
}
|
|
$result = $this->pdo->exec($sql) !== false;
|
|
if (!$result) {
|
|
Minz_Log::error('SQL error ' . __METHOD__ . json_encode($this->pdo->errorInfo()));
|
|
}
|
|
if (!$hadTransaction) {
|
|
$this->pdo->commit();
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Toggle the read marker on one or more article.
|
|
* Then the cache is updated.
|
|
*
|
|
* @param numeric-string|list<numeric-string> $ids
|
|
* @return int|false affected rows
|
|
*/
|
|
#[\Override]
|
|
public function markRead(array|string $ids, bool $is_read = true): int|false {
|
|
if (is_array($ids)) { //Many IDs at once (used by API)
|
|
return parent::markRead($ids, $is_read);
|
|
} else {
|
|
FreshRSS_UserDAO::touch();
|
|
$this->pdo->beginTransaction();
|
|
$sql = <<<'SQL'
|
|
UPDATE `_entry` SET is_read=:is_read, `lastUserModified` = :last_user_modified
|
|
WHERE id=:id AND is_read=:previous_is_read
|
|
SQL;
|
|
$stm = $this->pdo->prepare($sql);
|
|
if ($stm === false ||
|
|
!$stm->bindValue(':is_read', $is_read ? 1 : 0, PDO::PARAM_INT) ||
|
|
!$stm->bindValue(':last_user_modified', time(), PDO::PARAM_INT) ||
|
|
!$stm->bindValue(':id', $ids, PDO::PARAM_STR) || // TODO: Test PDO::PARAM_INT on 32-bit platform
|
|
!$stm->bindValue(':previous_is_read', $is_read ? 0 : 1, PDO::PARAM_INT) ||
|
|
!$stm->execute()) {
|
|
$info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
|
|
/** @var array{0:string,1:int,2:string} $info */
|
|
if ($this->autoUpdateDb($info)) {
|
|
return $this->markRead($ids, $is_read);
|
|
} else {
|
|
Minz_Log::error('SQL error ' . __METHOD__ . ' A ' . json_encode($info));
|
|
$this->pdo->rollBack();
|
|
return false;
|
|
}
|
|
}
|
|
$affected = $stm->rowCount();
|
|
if ($affected > 0) {
|
|
$delta = $is_read ? '-1' : '+1';
|
|
$sql = <<<SQL
|
|
UPDATE `_feed` SET `cache_nbUnreads`=`cache_nbUnreads` {$delta}
|
|
WHERE id=(SELECT e.id_feed FROM `_entry` e WHERE e.id=:id)
|
|
SQL;
|
|
$stm = $this->pdo->prepare($sql);
|
|
if ($stm === false ||
|
|
!$stm->bindValue(':id', $ids, PDO::PARAM_STR) ||
|
|
!$stm->execute()) {
|
|
$info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
|
|
Minz_Log::error('SQL error ' . __METHOD__ . ' B ' . json_encode($info));
|
|
$this->pdo->rollBack();
|
|
return false;
|
|
}
|
|
}
|
|
$this->pdo->commit();
|
|
if ($affected > 0) {
|
|
Minz_ExtensionManager::callHook(Minz_HookType::EntriesRead, [$ids], $is_read);
|
|
}
|
|
return $affected;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Mark all the articles in a tag as read.
|
|
* @param int $id tag ID, or empty for targeting any tag
|
|
* @param string $idMax max article ID
|
|
* @return int|false affected rows
|
|
*/
|
|
#[\Override]
|
|
public function markReadTag(int $id = 0, string $idMax = '0', ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true): int|false {
|
|
FreshRSS_UserDAO::touch();
|
|
if ($idMax == 0) {
|
|
$idMax = uTimeString();
|
|
Minz_Log::debug('Calling markReadTag(0) is deprecated!');
|
|
}
|
|
|
|
$tagCondition = $id == 0 ? '' : 'WHERE et.id_tag = ?';
|
|
$sql = <<<SQL
|
|
UPDATE `_entry` SET is_read = ?, `lastUserModified` = ? WHERE is_read <> ? AND id <= ?
|
|
AND id IN (SELECT et.id_entry FROM `_entrytag` et {$tagCondition})
|
|
SQL;
|
|
$values = [$is_read ? 1 : 0, time(), $is_read ? 1 : 0, $idMax];
|
|
if ($id != 0) {
|
|
$values[] = $id;
|
|
}
|
|
|
|
[$searchValues, $search] = $this->sqlListEntriesWhere(alias: '', state: $state, filters: $filters);
|
|
|
|
$stm = $this->pdo->prepare($sql . $search);
|
|
if ($stm === false || !$stm->execute(array_merge($values, $searchValues))) {
|
|
$info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
|
|
Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
|
|
return false;
|
|
}
|
|
$affected = $stm->rowCount();
|
|
if (($affected > 0) && (!$this->updateCacheUnreads(null, null))) {
|
|
return false;
|
|
}
|
|
return $affected;
|
|
}
|
|
}
|