From b5c9658c12e2bd4159fbd17887df5fdce9231cca Mon Sep 17 00:00:00 2001 From: llama <100429699+iamllama@users.noreply.github.com> Date: Tue, 7 Jan 2025 09:42:20 +0800 Subject: [PATCH 1/5] add Note::set_modified_with_mtime --- rslib/src/notes/mod.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/rslib/src/notes/mod.rs b/rslib/src/notes/mod.rs index 3f0b63e3e51..de0a47f29da 100644 --- a/rslib/src/notes/mod.rs +++ b/rslib/src/notes/mod.rs @@ -220,11 +220,16 @@ impl Note { Ok(()) } - pub(crate) fn set_modified(&mut self, usn: Usn) { - self.mtime = TimestampSecs::now(); + #[inline] + pub(crate) fn set_modified_with_mtime(&mut self, usn: Usn, mtime: TimestampSecs) { + self.mtime = mtime; self.usn = usn; } + pub(crate) fn set_modified(&mut self, usn: Usn) { + self.set_modified_with_mtime(usn, TimestampSecs::now()) + } + pub(crate) fn nonempty_fields<'a>(&self, fields: &'a [NoteField]) -> HashSet<&'a str> { self.fields .iter() From 5b7d00eee56f7eea00b435cfdfe95bc484957920 Mon Sep 17 00:00:00 2001 From: llama <100429699+iamllama@users.noreply.github.com> Date: Tue, 7 Jan 2025 09:44:56 +0800 Subject: [PATCH 2/5] add struct for Collection::update_note_inner_without_cards's args --- rslib/src/notes/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rslib/src/notes/mod.rs b/rslib/src/notes/mod.rs index de0a47f29da..ce4c5dec963 100644 --- a/rslib/src/notes/mod.rs +++ b/rslib/src/notes/mod.rs @@ -351,6 +351,18 @@ fn invalid_char_for_field(c: char) -> bool { c.is_ascii_control() && c != '\n' && c != '\t' } +/// Used when calling [Collection::update_note_inner_without_cards] and +/// [Collection::update_note_inner_without_cards_using_mtime] +pub(crate) struct UpdateNoteInnerWithoutCardsArgs<'a> { + pub(crate) note: &'a mut Note, + pub(crate) original: &'a Note, + pub(crate) notetype: &'a Notetype, + pub(crate) usn: Usn, + pub(crate) mark_note_modified: bool, + pub(crate) normalize_text: bool, + pub(crate) update_tags: bool, +} + impl Collection { pub(crate) fn canonify_note_tags(&mut self, note: &mut Note, usn: Usn) -> Result<()> { if !note.tags.is_empty() { From aae902bfeb22ef7272586dc08d7b4097b301d492 Mon Sep 17 00:00:00 2001 From: llama <100429699+iamllama@users.noreply.github.com> Date: Tue, 7 Jan 2025 10:01:44 +0800 Subject: [PATCH 3/5] refactor Collection::update_note_inner_without_cards to use the arg struct --- .../package/apkg/import/notes.rs | 20 +++++---- rslib/src/notes/mod.rs | 42 +++++++++---------- rslib/src/notetype/schemachange.rs | 29 ++++++------- 3 files changed, 47 insertions(+), 44 deletions(-) diff --git a/rslib/src/import_export/package/apkg/import/notes.rs b/rslib/src/import_export/package/apkg/import/notes.rs index c12b4d2da0c..24b86f6a8cf 100644 --- a/rslib/src/import_export/package/apkg/import/notes.rs +++ b/rslib/src/import_export/package/apkg/import/notes.rs @@ -15,6 +15,7 @@ use crate::import_export::package::UpdateCondition; use crate::import_export::ImportError; use crate::import_export::ImportProgress; use crate::import_export::NoteLog; +use crate::notes::UpdateNoteInnerWithoutCardsArgs; use crate::notetype::ChangeNotetypeInput; use crate::prelude::*; use crate::progress::ThrottlingProgressHandler; @@ -463,15 +464,16 @@ impl<'n> NoteContext<'n> { self.munge_media(&mut note)?; let original = self.get_expected_note(note.id)?; let notetype = self.get_expected_notetype(note.notetype_id)?; - self.target_col.update_note_inner_without_cards( - &mut note, - &original, - ¬etype, - self.usn, - true, - self.normalize_notes, - true, - )?; + self.target_col + .update_note_inner_without_cards(UpdateNoteInnerWithoutCardsArgs { + note: &mut note, + original: &original, + notetype: ¬etype, + usn: self.usn, + mark_note_modified: true, + normalize_text: self.normalize_notes, + update_tags: true, + })?; self.imports.log_updated(note, source_id); Ok(()) } diff --git a/rslib/src/notes/mod.rs b/rslib/src/notes/mod.rs index ce4c5dec963..34f4e074d7e 100644 --- a/rslib/src/notes/mod.rs +++ b/rslib/src/notes/mod.rs @@ -451,29 +451,29 @@ impl Collection { normalize_text: bool, update_tags: bool, ) -> Result<()> { - self.update_note_inner_without_cards( + self.update_note_inner_without_cards(UpdateNoteInnerWithoutCardsArgs { note, original, - ctx.notetype, - ctx.usn, + notetype: ctx.notetype, + usn: ctx.usn, mark_note_modified, normalize_text, update_tags, - )?; + })?; self.generate_cards_for_existing_note(ctx, note) } - // TODO: refactor into struct - #[allow(clippy::too_many_arguments)] pub(crate) fn update_note_inner_without_cards( &mut self, - note: &mut Note, - original: &Note, - notetype: &Notetype, - usn: Usn, - mark_note_modified: bool, - normalize_text: bool, - update_tags: bool, + UpdateNoteInnerWithoutCardsArgs { + note, + original, + notetype, + usn, + mark_note_modified, + normalize_text, + update_tags, + }: UpdateNoteInnerWithoutCardsArgs, ) -> Result<()> { if update_tags { self.canonify_note_tags(note, usn)?; @@ -559,15 +559,15 @@ impl Collection { out.update_tags, )?; } else { - self.update_note_inner_without_cards( - &mut note, - &original, - &nt, + self.update_note_inner_without_cards(UpdateNoteInnerWithoutCardsArgs { + note: &mut note, + original: &original, + notetype: &nt, usn, - out.mark_modified, - norm, - out.update_tags, - )?; + mark_note_modified: out.mark_modified, + normalize_text: norm, + update_tags: out.update_tags, + })?; } changed_notes += 1; diff --git a/rslib/src/notetype/schemachange.rs b/rslib/src/notetype/schemachange.rs index c253afd629c..f7da96b9d85 100644 --- a/rslib/src/notetype/schemachange.rs +++ b/rslib/src/notetype/schemachange.rs @@ -9,6 +9,7 @@ use std::mem; use super::CardGenContext; use super::CardTemplate; use super::Notetype; +use crate::notes::UpdateNoteInnerWithoutCardsArgs; use crate::prelude::*; use crate::search::JoinSearches; use crate::search::TemplateKind; @@ -80,15 +81,15 @@ impl Collection { for nid in nids { let mut note = self.storage.get_note(nid)?.unwrap(); let original = note.clone(); - self.update_note_inner_without_cards( - &mut note, - &original, - nt, + self.update_note_inner_without_cards(UpdateNoteInnerWithoutCardsArgs { + note: &mut note, + original: &original, + notetype: nt, usn, - true, + mark_note_modified: true, normalize_text, - false, - )?; + update_tags: false, + })?; } } else { // nothing to do @@ -104,15 +105,15 @@ impl Collection { let mut note = self.storage.get_note(nid)?.unwrap(); let original = note.clone(); note.reorder_fields(&ords); - self.update_note_inner_without_cards( - &mut note, - &original, - nt, + self.update_note_inner_without_cards(UpdateNoteInnerWithoutCardsArgs { + note: &mut note, + original: &original, + notetype: nt, usn, - true, + mark_note_modified: true, normalize_text, - false, - )?; + update_tags: false, + })?; } Ok(()) } From 738fd6d677a734d9cef3240181632a9296bc05ef Mon Sep 17 00:00:00 2001 From: llama <100429699+iamllama@users.noreply.github.com> Date: Tue, 7 Jan 2025 10:04:03 +0800 Subject: [PATCH 4/5] add Collection::update_note_inner_without_cards_using_mtime --- rslib/src/notes/mod.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/rslib/src/notes/mod.rs b/rslib/src/notes/mod.rs index 34f4e074d7e..851ffaa2581 100644 --- a/rslib/src/notes/mod.rs +++ b/rslib/src/notes/mod.rs @@ -463,7 +463,8 @@ impl Collection { self.generate_cards_for_existing_note(ctx, note) } - pub(crate) fn update_note_inner_without_cards( + #[inline] + pub(crate) fn update_note_inner_without_cards_using_mtime( &mut self, UpdateNoteInnerWithoutCardsArgs { note, @@ -474,17 +475,29 @@ impl Collection { normalize_text, update_tags, }: UpdateNoteInnerWithoutCardsArgs, + mtime: Option, ) -> Result<()> { if update_tags { self.canonify_note_tags(note, usn)?; } note.prepare_for_update(notetype, normalize_text)?; if mark_note_modified { - note.set_modified(usn); + if let Some(mtime) = mtime { + note.set_modified_with_mtime(usn, mtime); + } else { + note.set_modified(usn); + } } self.update_note_undoable(note, original) } + pub(crate) fn update_note_inner_without_cards( + &mut self, + args: UpdateNoteInnerWithoutCardsArgs<'_>, + ) -> Result<()> { + self.update_note_inner_without_cards_using_mtime(args, None) + } + pub(crate) fn remove_notes_inner(&mut self, nids: &[NoteId], usn: Usn) -> Result { let mut card_count = 0; for nid in nids { From 2bd179d0e8596f72bd0e9a7527abc413da25419d Mon Sep 17 00:00:00 2001 From: llama <100429699+iamllama@users.noreply.github.com> Date: Tue, 7 Jan 2025 10:06:35 +0800 Subject: [PATCH 5/5] use incoming note's mtime when updating notes during import --- .../package/apkg/import/notes.rs | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/rslib/src/import_export/package/apkg/import/notes.rs b/rslib/src/import_export/package/apkg/import/notes.rs index 24b86f6a8cf..ba5178a18ae 100644 --- a/rslib/src/import_export/package/apkg/import/notes.rs +++ b/rslib/src/import_export/package/apkg/import/notes.rs @@ -464,16 +464,21 @@ impl<'n> NoteContext<'n> { self.munge_media(&mut note)?; let original = self.get_expected_note(note.id)?; let notetype = self.get_expected_notetype(note.notetype_id)?; + // Preserve the incoming note's mtime to allow imports of successive exports + let incoming_mtime = note.mtime; self.target_col - .update_note_inner_without_cards(UpdateNoteInnerWithoutCardsArgs { - note: &mut note, - original: &original, - notetype: ¬etype, - usn: self.usn, - mark_note_modified: true, - normalize_text: self.normalize_notes, - update_tags: true, - })?; + .update_note_inner_without_cards_using_mtime( + UpdateNoteInnerWithoutCardsArgs { + note: &mut note, + original: &original, + notetype: ¬etype, + usn: self.usn, + mark_note_modified: true, + normalize_text: self.normalize_notes, + update_tags: true, + }, + Some(incoming_mtime), + )?; self.imports.log_updated(note, source_id); Ok(()) }