]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: drop rgw_obj_key::to_str() 49190/head
authorKefu Chai <tchaikov@gmail.com>
Sat, 10 Dec 2022 02:31:21 +0000 (10:31 +0800)
committerKefu Chai <tchaikov@gmail.com>
Sat, 10 Dec 2022 04:08:38 +0000 (12:08 +0800)
rgw_obj_key::to_str() is mainly used by operator<<(ostream&, ..), so
we can just implement it with the specialization of
fmt::formatter<rgw_obj_key>. and let operator<<(ostream&, ..) to
call into fmt::format(..):

1. for better readability and
2. for probably better performance -- we don't need to do deep copy
   for constructing a `std::string` from a `char[]`.
3. for better standard compliance -- we don't need to use variable-length
   array in C++ code. it is a part of C99 standard. but not a C++ standard.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/rgw/driver/rados/rgw_data_sync.cc
src/rgw/rgw_obj_types.h

index 47573b765da842b3b1eadf0b18d0f409021c3832..0655df91f4aca761ab77f2be11de9d260ffbb108 100644 (file)
@@ -3472,7 +3472,7 @@ public:
       status.shards_done_with_gen.resize(marker_mgr.get().size());
       status.incremental_gen = info.latest_gen;
 
-      ldout(cct, 20) << "writing bucket sync status during init. state=" << status.state << ". marker=" << status.full.position.to_str() << dendl;
+      ldout(cct, 20) << "writing bucket sync status during init. state=" << status.state << ". marker=" << status.full.position << dendl;
 
       // write bucket sync status
       using CR = RGWSimpleRadosWriteCR<rgw_bucket_sync_status>;
index c90d450793c7b7498d8ca5bfdd03532fd1051b81..1347a8ad00d473c8bb5a1bcc5d2faaa2fafa6e24 100644 (file)
@@ -381,28 +381,33 @@ struct rgw_obj_key {
   }
   void dump(Formatter *f) const;
   void decode_json(JSONObj *obj);
-
-  std::string to_str() const {
-    if (instance.empty()) {
-      return name;
-    }
-    char buf[name.size() + instance.size() + 16];
-    snprintf(buf, sizeof(buf), "%s[%s]", name.c_str(), instance.c_str());
-    return buf;
-  }
 };
 WRITE_CLASS_ENCODER(rgw_obj_key)
 
-inline std::ostream& operator<<(std::ostream& out, const rgw_obj_key &o) {
-  return out << o.to_str();
-}
-
+#if FMT_VERSION >= 90000
 template<> struct fmt::formatter<rgw_obj_key> : fmt::formatter<std::string_view> {
   template <typename FormatContext>
   auto format(const rgw_obj_key& key, FormatContext& ctx) const {
-    return formatter<std::string_view>::format(key.to_str(), ctx);
+    if (key.instance.empty()) {
+      return formatter<std::string_view>::format(key.name, ctx);
+    } else {
+      return fmt::format_to(ctx.out(), "{}[{}]", key.name, key.instance);
+    }
   }
 };
+#endif
+
+inline std::ostream& operator<<(std::ostream& out, const rgw_obj_key &key) {
+#if FMT_VERSION >= 90000
+  return out << fmt::format("{}", key);
+#else
+  if (key.instance.empty()) {
+    return out << fmt::format("{}", key.name);
+  } else {
+    return out << fmt::format("{}[{}]", key.name, key.instance);
+  }
+#endif
+}
 
 struct rgw_raw_obj {
   rgw_pool pool;