hanxi commented on code in PR #3294:
URL: https://github.com/apache/kvrocks/pull/3294#discussion_r2620063009


##########
src/server/redis_connection.cc:
##########
@@ -68,9 +68,31 @@ Connection::~Connection() {
 }
 
 std::string Connection::ToString() {
-  return fmt::format("id={} addr={} fd={} name={} age={} idle={} flags={} 
namespace={} qbuf={} obuf={} cmd={}\n", id_,
-                     addr_, bufferevent_getfd(bev_), name_, GetAge(), 
GetIdleTime(), GetFlags(), ns_,
-                     evbuffer_get_length(Input()), 
evbuffer_get_length(Output()), last_cmd_);
+  std::string result;
+
+  // When redis-databases > 0 (SELECT compatibility mode), show db field 
instead of namespace
+  if (srv_->GetConfig()->redis_databases > 0) {
+    // Parse db number from namespace (format: "db0001", "db0002", etc.)
+    int db_num = 0;
+    if (ns_ != kDefaultNamespace && ns_.size() >= 3 && ns_.substr(0, 2) == 
"db") {
+      try {
+        db_num = std::stoi(ns_.substr(2));
+      } catch (...) {
+        // If parsing fails, default to 0
+        db_num = 0;
+      }
+    }
+
+    result = fmt::format("id={} addr={} fd={} name={} age={} idle={} flags={} 
db={} qbuf={} obuf={} cmd={}\n", id_,
+                         addr_, bufferevent_getfd(bev_), name_, GetAge(), 
GetIdleTime(), GetFlags(), db_num,
+                         evbuffer_get_length(Input()), 
evbuffer_get_length(Output()), last_cmd_);
+  } else {
+    result = fmt::format("id={} addr={} fd={} name={} age={} idle={} flags={} 
namespace={} qbuf={} obuf={} cmd={}\n",
+                         id_, addr_, bufferevent_getfd(bev_), name_, GetAge(), 
GetIdleTime(), GetFlags(), ns_,
+                         evbuffer_get_length(Input()), 
evbuffer_get_length(Output()), last_cmd_);

Review Comment:
   Got it, I'll refactor it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to