1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/*
** Copyright (C) 2025 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#ifndef MU_LABELS_CACHE_HH
#define MU_LABELS_CACHE_HH
#include <set>
#include <string>
#include <unordered_map>
#include "utils/mu-utils.hh"
#include "utils/mu-option.hh"
#include "message/mu-labels.hh"
#include "mu-config.hh"
namespace Mu {
class Store; // fwd declaration
/**
* The cache keeps track of what labels are being used. This can be used
* for completion etc. and `mu label list`
*/
class LabelsCache {
public:
// maps a label to a number of occurrences
using Map = std::unordered_map<std::string, size_t>;
/**
* CTOR
*
* Deserialize the map from the configuration.
*
* @param serialized serialization string
*/
LabelsCache(Config& config): config_{config},
label_map_{deserialize(config_.get<Config::Id::Labels>())},
dirty_{false}{}
~LabelsCache() {
if (dirty_)
serialize();
};
/**
* Add a label occurrence to the cache
*
* @param label some 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;
++dirty_;
}
/**
* Remove a label occurrence from the cache
*
* Removes the label completely if this was the _last_ occurence.
*
* @param label some 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);
else
--it->second;
++dirty_;
}
}
/**
* Update the cache with the label changes
*
* @param updates a vector of delta-labels
*/
void update(const Labels::DeltaLabelVec& updates) {
for(const auto& [delta, label]: updates) {
switch(delta) {
case Labels::Delta::Add:
increase(label);
break;
case Labels::Delta::Remove:
decrease(label);
break;
}
}
}
/**
* Return a copy of the label-map
*
* @return the label-map
*/
Map label_map() const { return label_map_; }
// serialization/deserialization could be optimized, but is not super
// time-critical
/**
* Serialize the cache into Config if there are changes.
*
*/
void serialize() const;
/**
* 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);
/**
* @return whether the labels map is non-empty
*/
bool empty() const { return label_map_.empty(); }
private:
/**
* Deserialize the cache into a Map
*
* @return serialized cache
*/
Map deserialize(const std::string& serialized) const;
Config& config_;
Map label_map_;
mutable size_t dirty_{};
};
} // namespace Mux
#endif /*MU_LABELS_CACHE_HH*/
|