store: improve label handling

In particular, clear_labels

And some cosmetics
This commit is contained in:
Dirk-Jan C. Binnema
2025-08-29 21:10:34 +03:00
committed by Seth Ladygo
parent a2b1a7cc31
commit c074c138f8
4 changed files with 29 additions and 27 deletions

View File

@ -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;

View File

@ -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
*

View File

@ -632,7 +632,6 @@ Store::update_labels(Message& message, const Labels::DeltaLabelVec& 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

View File

@ -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.