summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2025-08-29 21:10:34 +0300
committerDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2025-08-31 22:36:33 +0300
commita87aa44fb4daa18ac435b595b647ba8043ee08a6 (patch)
tree9c6d78f29cd8b8478232eee66c7c64508fb75c44
parentdabfcf58a423b941427210d0b89f6a0b2c5370ce (diff)
store: improve label handling
In particular, clear_labels And some cosmetics
-rw-r--r--lib/message/mu-labels.cc5
-rw-r--r--lib/mu-store-labels.hh18
-rw-r--r--lib/mu-store.cc19
-rw-r--r--lib/mu-store.hh14
4 files changed, 29 insertions, 27 deletions
diff --git a/lib/message/mu-labels.cc b/lib/message/mu-labels.cc
index 3254f97..c3822dd 100644
--- a/lib/message/mu-labels.cc
+++ b/lib/message/mu-labels.cc
@@ -89,7 +89,6 @@ Mu::Labels::parse_delta_label(const std::string &expr)
return Ok(DeltaLabel{std::move(delta), std::move(label)});
}
-
Result<DeltaLabelVec>
Mu::Labels::parse_delta_labels(const std::string& exprs,
const std::string sepa)
@@ -106,10 +105,6 @@ Mu::Labels::parse_delta_labels(const std::string& exprs,
return Ok(std::move(deltas));
}
-
-
-
-
struct cmp_delta_label { // can not yet be a λ in C++17
bool operator()(const DeltaLabel& dl1, const DeltaLabel& dl2) const {
return dl1.second < dl2.second;
diff --git a/lib/mu-store-labels.hh b/lib/mu-store-labels.hh
index 61363f8..b36c9bd 100644
--- a/lib/mu-store-labels.hh
+++ b/lib/mu-store-labels.hh
@@ -52,20 +52,22 @@ public:
/**
* Add a label occurrence to the cache
*
- * @param label
+ * @param label some label
*/
- void add(const std::string& label) {
+ void increase(const std::string& label) {
if (auto it = label_map_.find(label); it == label_map_.end())
label_map_.insert({label, 1});
else
++it->second;
}
/**
- * Remove label occurrence from the cache
+ * Remove a label occurrence from the cache
*
- * @param label
+ * Removes the label completely if this was the _last_ occurence.
+ *
+ * @param label some label
*/
- void remove(const std::string& label) {
+ void decrease(const std::string& label) {
if (auto it = label_map_.find(label); it != label_map_.end()) {
if (it->second == 1)
label_map_.erase(it);
@@ -83,10 +85,10 @@ public:
for(const auto& [delta, label]: updates) {
switch(delta) {
case Labels::Delta::Add:
- add(label);
+ increase(label);
break;
case Labels::Delta::Remove:
- remove(label);
+ decrease(label);
break;
}
}
@@ -99,7 +101,6 @@ public:
*/
Map label_map() const { return label_map_; }
-
// serialization/deserialization could be optimized, but is not super
// time-critical
@@ -116,7 +117,6 @@ public:
return s;
}
-
/**
* Deserialize the cache into a Map
*
diff --git a/lib/mu-store.cc b/lib/mu-store.cc
index b8526e9..8d0f121 100644
--- a/lib/mu-store.cc
+++ b/lib/mu-store.cc
@@ -626,13 +626,12 @@ Result<Labels::DeltaLabelVec>
Store::update_labels(Message& message, const Labels::DeltaLabelVec& labels_delta)
{
std::unique_lock lock{priv_->lock_};
- // i.e. the set of effective labels. and the set up updates, the "diff"
+ // i.e. the set of effective labels. and the set up updates, the "diff"
auto updates{updated_labels(message.labels(), labels_delta)};
if (updates.second.empty())
return Ok(std::move(updates.second)); // nothing to do
-
message.set_labels(updates.first);
auto res{priv_->update_message_unlocked(message, message.docid())};
if (!res)
@@ -643,24 +642,30 @@ Store::update_labels(Message& message, const Labels::DeltaLabelVec& labels_delta
return Ok(std::move(updates.second));
}
-Result<void>
+Result<Labels::DeltaLabelVec>
Store::clear_labels(Message& message)
{
+ using namespace Labels;
+
std::unique_lock lock{priv_->lock_};
+ DeltaLabelVec updates;
+
const auto labels{message.labels()};
if (labels.empty())
- return Ok(); // nothing to do
+ return Ok(std::move(updates)); // nothing to do
message.set_labels({}); // clear all
auto res{priv_->update_message_unlocked(message, message.docid())};
if (!res)
return Err(res.error());
- for (auto label: labels)
- priv_->labels_cache_.remove(label);
+ for (auto label: labels) {
+ updates.emplace_back(DeltaLabel{Delta::Remove, label});
+ priv_->labels_cache_.decrease(label);
+ }
- return Ok();
+ return Ok(std::move(updates));
}
LabelsCache::Map
diff --git a/lib/mu-store.hh b/lib/mu-store.hh
index 6c1fd5b..e23978b 100644
--- a/lib/mu-store.hh
+++ b/lib/mu-store.hh
@@ -347,18 +347,20 @@ public:
* @param message some message
* @param labels_delta the set of changes
*
- * @return the effective changes for this message
+ * @return the effective changes for this message or an error
*/
- Result<Labels::DeltaLabelVec> update_labels(Message& message, const Labels::DeltaLabelVec& labels_delta);
-
+ Result<Labels::DeltaLabelVec> update_labels(Message& message,
+ const Labels::DeltaLabelVec& labels_delta);
/**
- * Clear all labels from message
+ * Clear all labels from a message
+ *
+ * Update the message in the store, and update the labels-cache
*
* @param message some message
*
- * @return Ok or some error
+ * @return the effective changes for this message or an error
*/
- Result<void> clear_labels(Message& message);
+ Result<Labels::DeltaLabelVec> clear_labels(Message& message);
/**
* Get a copy of the map of labels in use.