Răsfoiți Sursa

SQLite, PostgreSQL: batch markRead for lists of IDs (#9575)

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>
Corey Quinn 11 ore în urmă
părinte
comite
d6c907bc04
1 a modificat fișierele cu 2 adăugiri și 8 ștergeri
  1. 2 8
      app/Models/EntryDAOSQLite.php

+ 2 - 8
app/Models/EntryDAOSQLite.php

@@ -118,19 +118,13 @@ class FreshRSS_EntryDAOSQLite extends FreshRSS_EntryDAO {
 	 * Toggle the read marker on one or more article.
 	 * Then the cache is updated.
 	 *
-	 * @param numeric-string|array<numeric-string> $ids
+	 * @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)
-			//if (true) {	//Speed heuristics	//TODO: Not implemented yet for SQLite (so always call IDs one by one)
-			$affected = 0;
-			foreach ($ids as $id) {
-				$affected += ($this->markRead($id, $is_read) ?: 0);
-			}
-			return $affected;
-			//}
+			return parent::markRead($ids, $is_read);
 		} else {
 			FreshRSS_UserDAO::touch();
 			$this->pdo->beginTransaction();