perf: stream diary export instead of loading all entries into memory #5
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
diary/export_diary.rscallsget_user_history()which loads the entire diary into aVec<DiaryEntry>before serialization. For users with thousands of entries this holds everything in RAM at once.Current flow:
DiaryRepository::get_user_history()→Vec<DiaryEntry>(all in memory)DiaryExporter::serialize_entries(&[DiaryEntry], format)→Vec<u8>(serialized bytes, also in memory)Vec<u8>Proposed fix
Change the
DiaryExporterport to accept a stream/writer instead of a slice, and have the repository yield rows lazily:DiaryRepository::stream_user_history()→BoxStream<DiaryEntry>DiaryExporter::serialize_entries_stream(stream, format, writer)— writes directly to anAsyncWriteor returns aBoxStream<Bytes>Body::from_stream)This avoids holding the full dataset in memory at any point.
Affected files
crates/domain/src/ports.rs—DiaryExporterport signaturecrates/adapters/export/src/lib.rs— serialization implementationcrates/application/src/diary/export_diary.rs— use casecrates/presentation/src/handlers/diary.rs— HTTP handlercrates/adapters/sqlite/src/lib.rs—get_user_historyquerycrates/adapters/postgres/src/lib.rs— sameConstraint: the CSV column order (
title,year,director,rating,comment,watched_at,external_metadata_id) must not change. Export and import are designed to be a round-trip — users can dump their diary and re-import it. Any streaming rewrite must preserve this exact format.