From 2dd7ff6da867a621cd69884785a072bff82bf0d2 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Tue, 4 Jun 2024 13:52:48 +0200 Subject: [PATCH] core: introduce register_dive() function There was a weird asymmetry, where the undo-commands would register the fulltext index of the dive, but the core would unregister the fulltext index in the "unregister_dive()" function. To make this more logical, create a "register_dive()" function in core that does registers the fulltext index. Signed-off-by: Berthold Stoeger --- commands/command_divelist.cpp | 13 +------------ core/divelist.cpp | 18 ++++++++++++++++++ core/divelist.h | 2 ++ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/commands/command_divelist.cpp b/commands/command_divelist.cpp index eaa9b06e3..f233e4bc7 100644 --- a/commands/command_divelist.cpp +++ b/commands/command_divelist.cpp @@ -72,18 +72,7 @@ dive *DiveListBase::addDive(DiveToAdd &d) d.site->add_dive(d.dive.get()); diveSiteCountChanged(d.site); } - dive *res = d.dive.release(); // Give up ownership of dive - - // When we add dives, we start in hidden-by-filter status. Once all - // dives have been added, their status will be updated. - res->hidden_by_filter = true; - - int idx = dive_table_get_insertion_index(divelog.dives.get(), res); - fulltext_register(res); // Register the dive's fulltext cache - add_to_dive_table(divelog.dives.get(), idx, res); // Return ownership to backend - invalidate_dive_cache(res); // Ensure that dive is written in git_save() - - return res; + return register_dive(std::move(d.dive)); // Transfer ownership to core and update fulltext index } // Some signals are sent in batches per trip. To avoid writing the same loop diff --git a/core/divelist.cpp b/core/divelist.cpp index e2fe647ea..f20aaa5ee 100644 --- a/core/divelist.cpp +++ b/core/divelist.cpp @@ -745,6 +745,24 @@ struct dive *unregister_dive(int idx) return dive; } +/* Add a dive to the global dive table. + * Index it in the fulltext cache and make sure that it is written + * in git_save(). + */ +struct dive *register_dive(std::unique_ptr d) +{ + // When we add dives, we start in hidden-by-filter status. Once all + // dives have been added, their status will be updated. + d->hidden_by_filter = true; + + int idx = dive_table_get_insertion_index(divelog.dives.get(), d.get()); + fulltext_register(d.get()); // Register the dive's fulltext cache + invalidate_dive_cache(d.get()); // Ensure that dive is written in git_save() + add_to_dive_table(divelog.dives.get(), idx, d.get()); + + return d.release(); +} + void process_loaded_dives() { sort_dive_table(divelog.dives.get()); diff --git a/core/divelist.h b/core/divelist.h index b5b006f73..58000c146 100644 --- a/core/divelist.h +++ b/core/divelist.h @@ -3,6 +3,7 @@ #define DIVELIST_H #include "units.h" +#include #include struct dive; @@ -57,6 +58,7 @@ int get_dive_id_closest_to(timestamp_t when); void clear_dive_file_data(); void clear_dive_table(struct dive_table *table); struct dive *unregister_dive(int idx); +struct dive *register_dive(std::unique_ptr d); extern bool has_dive(unsigned int deviceid, unsigned int diveid); #endif // DIVELIST_H