summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2025-09-02 21:08:54 +0300
committerDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2025-09-02 21:08:54 +0300
commit0a2fda4b0b3638fdbd80e9f4b2102f104cc8e192 (patch)
tree4b9c9fae9115e7d26b7239c7f60ab255f6019c86 /lib
parent59717bcfcc0812781f59d60e75aa149affd550bc (diff)
labels: allow restoring cache-map
E.g. with unexpected termination for mu it is possible that the labels-cache (i.e., the one that is used for auto-completion) gets disconnected from reality. Add a --restore option to `mu label list` to restore the actual labels from the labels seen in the store.
Diffstat (limited to 'lib')
-rw-r--r--lib/mu-store-labels.cc19
-rw-r--r--lib/mu-store-labels.hh15
-rw-r--r--lib/mu-store.cc8
-rw-r--r--lib/mu-store.hh12
4 files changed, 52 insertions, 2 deletions
diff --git a/lib/mu-store-labels.cc b/lib/mu-store-labels.cc
index 61666aa..8653c84 100644
--- a/lib/mu-store-labels.cc
+++ b/lib/mu-store-labels.cc
@@ -23,6 +23,25 @@
using namespace Mu;
+Result<void>
+Mu::LabelsCache::restore(const Store& store)
+{
+ const auto res{store.run_query("")};
+ if (!res)
+ return Err(Error{Error::Code::Query,
+ "failed to run query: {}",
+ *res.error().what()});
+ label_map_.clear();
+
+ for (auto&& item: *res) {
+ if (auto &&msg{item.message()}; msg) {
+ for (const auto& label: msg->labels())
+ increase(label);
+ }
+ }
+ return Ok();
+}
+
namespace {
constexpr std::string_view path_key = "path:";
constexpr std::string_view message_id_key = "message-id:";
diff --git a/lib/mu-store-labels.hh b/lib/mu-store-labels.hh
index df4dde0..6104973 100644
--- a/lib/mu-store-labels.hh
+++ b/lib/mu-store-labels.hh
@@ -30,6 +30,8 @@
namespace Mu {
+class Store;
+
/**
* The cache keeps track of what labels are being used. This can be used
* for completion etc. and `mu label list`
@@ -143,6 +145,17 @@ public:
return map;
}
+
+ /**
+ * Restore the labels-cache from the labels seen in the store.
+ *
+ * @param store a store
+ *
+ * @return Ok() or some error
+ */
+ Result<void> restore(const Store& store);
+
+
/**
* Is the cache "dirty"?
*
@@ -159,8 +172,6 @@ private:
mutable bool dirty_{};
};
-class Store;
-
/**
* Export labels to a file
*
diff --git a/lib/mu-store.cc b/lib/mu-store.cc
index 0d4dba6..de148af 100644
--- a/lib/mu-store.cc
+++ b/lib/mu-store.cc
@@ -694,6 +694,14 @@ Store::clear_labels(Message& message)
return Ok(std::move(updates));
}
+Result<void>
+Store::restore_label_map()
+{
+ std::unique_lock lock{priv_->lock_};
+
+ return priv_->labels_cache_.restore(*this);
+}
+
LabelsCache::Map
Store::label_map() const
{
diff --git a/lib/mu-store.hh b/lib/mu-store.hh
index 555f2f0..9aff22c 100644
--- a/lib/mu-store.hh
+++ b/lib/mu-store.hh
@@ -374,6 +374,16 @@ public:
Result<Labels::DeltaLabelVec> clear_labels(Message& message);
/**
+ * Restore label-map from store
+ *
+ * Restore the labels list in the store, i.e., restore the cached list of labels which is
+ * used for e.g. auto-completion in mu4e from the labels in the store.
+ *
+ * @return Ok or some error.
+ */
+ Result<void> restore_label_map();
+
+ /**
* Get a copy of the map of labels in use.
*
* The map maps label-names to their count
@@ -382,6 +392,8 @@ public:
*/
LabelsCache::Map label_map() const;
+
+
/**
* Prototype for the ForEachMessageFunc
*