1
Fork 0
mirror of https://github.com/thegeneralist01/archivr synced 2026-07-21 18:55:36 +02:00

feat: precompute cached_bytes per entry in DB (#13)

Store how many bytes of each entry's artifacts are already on disk from
an earlier entry (content-addressed blob deduplication means shared
blobs are only stored once).

Design
------
- Add `cached_bytes INTEGER NOT NULL DEFAULT 0` to `archived_entries`
- Precompute at capture time via `database::refresh_entry_cached_bytes`
  called after all artifacts are saved for every capture path
  (web page, generic URL, tweet, yt-dlp/local)
- One-time migration in `initialize_schema`: detects missing column via
  PRAGMA table_info, ALTERs the table, then back-fills all existing rows
  with the correlated subquery
- `database::cascade_cached_bytes_after_delete` ready for when entry
  deletion is implemented; designed to run asynchronously after the
  delete is acknowledged to the user
- `cached_bytes` included in `EntrySummary` and all four SELECT paths
  (list_root_entries, search_entries, list_entries_for_collection,
  entries_for_tag) via the shared ENTRY_SELECT_COLS constant

Frontend
--------
- `EntryRow` shows a `% cached` sub-line under the size when non-zero,
  with a tooltip showing the raw cached byte count
- No separate API endpoint or extra fetch — value rides in the existing
  entries list response at zero extra query cost per read
This commit is contained in:
TheGeneralist 2026-07-03 11:56:55 +02:00 committed by GitHub
parent c85cce579b
commit 90d17c9d25
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 161 additions and 21 deletions

View file

@ -751,7 +751,7 @@ pub fn perform_capture(
let _ = fs::remove_dir_all(store_path.join("temp").join(&timestamp));
}
record_media_entry(
let entry = record_media_entry(
&conn,
store_path,
user_id,
@ -765,6 +765,7 @@ pub fn perform_capture(
byte_size,
title_hint,
)?;
database::refresh_entry_cached_bytes(&conn, entry.id)?;
database::finish_archive_run(&conn, run.id)?;
return Ok(CaptureResult {
run_uid: run.run_uid.clone(),
@ -904,6 +905,9 @@ pub fn perform_capture(
}
}
// 7. Store how many bytes this entry gets "for free" from earlier entries.
database::refresh_entry_cached_bytes(&conn, entry.id)?;
database::finish_archive_run(&conn, run.id)?;
return Ok(CaptureResult {
run_uid: run.run_uid.clone(),
@ -942,7 +946,7 @@ pub fn perform_capture(
&timestamp,
) {
Ok(_) => {
record_tweet_entry(
let tweet_entry = record_tweet_entry(
&conn,
store_path,
user_id,
@ -952,6 +956,7 @@ pub fn perform_capture(
source,
&tweet_id,
)?;
database::refresh_entry_cached_bytes(&conn, tweet_entry.id)?;
database::finish_archive_run(&conn, run.id)?;
return Ok(CaptureResult {
run_uid: run.run_uid.clone(),
@ -1042,7 +1047,7 @@ pub fn perform_capture(
let _ = fs::remove_dir_all(store_path.join("temp").join(&timestamp));
}
record_media_entry(
let media_entry = record_media_entry(
&conn,
store_path,
user_id,
@ -1056,6 +1061,7 @@ pub fn perform_capture(
byte_size,
None, // title — populated in a later task
)?;
database::refresh_entry_cached_bytes(&conn, media_entry.id)?;
database::finish_archive_run(&conn, run.id)?;
Ok(CaptureResult {