Skip to content

Commit 0702bb0

Browse files
daverigbyudkyo
authored andcommitted
CBD-6348: [BP] CBStatCollector::formatKey Simplify dynamic format args handling
This is a cherry-pick from trinity As per comments in CBStatCollector::formatKey helper function formatFromMap, the handling of dynamic format arguments is significantly simpler with the introduction of dynamic labelled arguments in fmtlib 8 - see https://fmt.dev/latest/api.html#named-arguments Remove the somewhat complex manual handling of this and delegate to fmtlib. Change-Id: I7c070cf327cb19f4db88d257e1cd768281c0b89a Reviewed-on: https://review.couchbase.org/c/kv_engine/+/234758 Well-Formed: Restriction Checker Tested-by: Trond Norbye <[email protected]> Reviewed-by: Trond Norbye <[email protected]>
1 parent 6d58c2f commit 0702bb0

File tree

1 file changed

+6
-103
lines changed

1 file changed

+6
-103
lines changed

statistics/cbstat_collector.cc

Lines changed: 6 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <statistics/cbstat_collector.h>
1313

14+
#include <fmt/args.h>
1415
#include <hdrhistogram/hdrhistogram.h>
1516
#include <logger/logger.h>
1617
#include <memcached/cookie_iface.h>
@@ -173,108 +174,6 @@ cb::engine_errc CBStatCollector::testPrivilegeForStat(
173174
return cb::engine_errc::failed;
174175
}
175176

176-
/**
177-
* Format a string containing `fmt` replacement specifiers using key-value pairs
178-
* from a map as named arguments
179-
*
180-
* e.g.,
181-
*
182-
* formatFromMap(buf, "{connection_type}:items_remaining", {{"connection_type",
183-
* "replica}});
184-
* ->
185-
* "replica:items_remaining"
186-
*
187-
* Note - this is only required as the currently used version of fmt does not
188-
* support dynamic args. Once a version of fmt with
189-
* fmt::dynamic_format_arg_store
190-
* support is available, this can be simplified.
191-
* Additionally, fmt::format_args is _not_ safe to construct with
192-
* fmt::make_format_args in a similar manner, due to lifetime issues as each
193-
* fmt::arg(...) needs to outlive any usages of the format_args object
194-
* (again, resolved by dynamic_format_arg_store)
195-
*
196-
*/
197-
198-
auto formatFromMap(fmt::memory_buffer& buf,
199-
std::string_view formatStr,
200-
const StatCollector::Labels& labels) {
201-
auto itr = labels.cbegin();
202-
203-
// lambda to create a fmt named arg from the next key-value pair
204-
// from the iterator.
205-
auto getArg = [&itr]() {
206-
const auto& [key, value] = *itr++;
207-
return fmt::arg(key, value);
208-
};
209-
210-
// note, the order of evaluation of the args is unspecified, but isn't
211-
// important anyway. This can be made more succinct, but this is
212-
// straightforward to read and understand.
213-
214-
switch (labels.size()) {
215-
case 0:
216-
return fmt::format_to(std::back_inserter(buf), formatStr);
217-
case 1:
218-
return fmt::format_to(std::back_inserter(buf), formatStr, getArg());
219-
case 2:
220-
return fmt::format_to(
221-
std::back_inserter(buf), formatStr, getArg(), getArg());
222-
case 3:
223-
return fmt::format_to(std::back_inserter(buf),
224-
formatStr,
225-
getArg(),
226-
getArg(),
227-
getArg());
228-
case 4:
229-
return fmt::format_to(std::back_inserter(buf),
230-
formatStr,
231-
getArg(),
232-
getArg(),
233-
getArg(),
234-
getArg());
235-
case 5:
236-
return fmt::format_to(std::back_inserter(buf),
237-
formatStr,
238-
getArg(),
239-
getArg(),
240-
getArg(),
241-
getArg(),
242-
getArg());
243-
case 6:
244-
return fmt::format_to(std::back_inserter(buf),
245-
formatStr,
246-
getArg(),
247-
getArg(),
248-
getArg(),
249-
getArg(),
250-
getArg(),
251-
getArg());
252-
case 7:
253-
return fmt::format_to(std::back_inserter(buf),
254-
formatStr,
255-
getArg(),
256-
getArg(),
257-
getArg(),
258-
getArg(),
259-
getArg(),
260-
getArg(),
261-
getArg());
262-
case 8:
263-
return fmt::format_to(std::back_inserter(buf),
264-
formatStr,
265-
getArg(),
266-
getArg(),
267-
getArg(),
268-
getArg(),
269-
getArg(),
270-
getArg(),
271-
getArg(),
272-
getArg());
273-
}
274-
275-
throw std::runtime_error("makeFormatArgs too many labels provided");
276-
}
277-
278177
std::string CBStatCollector::formatKey(std::string_view key,
279178
const Labels& labels) const {
280179
fmt::memory_buffer buf;
@@ -293,7 +192,11 @@ std::string CBStatCollector::formatKey(std::string_view key,
293192
}
294193
// now format the key itself, it may contain replacement specifiers
295194
// that can only be replaced with the appropriate value at runtime
296-
formatFromMap(buf, key, labels);
195+
fmt::dynamic_format_arg_store<fmt::format_context> store;
196+
for (const auto& label : labels) {
197+
store.push_back(fmt::arg(label.first, label.second));
198+
}
199+
fmt::vformat_to(std::back_inserter(buf), key, store);
297200

298201
return fmt::to_string(buf);
299202

0 commit comments

Comments
 (0)