@@ -553,6 +553,32 @@ class CommandSort : public Commander {
553553 SortArgument sort_argument_;
554554};
555555
556+ class CommandKMetadata : public Commander {
557+ public:
558+ Status Execute (engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override {
559+ redis::Database redis (srv->storage , conn->GetNamespace ());
560+ std::string &key = args_[1 ];
561+ std::string nskey = redis.AppendNamespacePrefix (key);
562+
563+ Metadata metadata (kRedisNone , false );
564+ auto s = redis.GetMetadata (ctx, RedisTypes::All (), nskey, &metadata);
565+ if (!s.ok ()) return {Status::RedisExecErr, s.ToString ()};
566+
567+ if (metadata.IsSingleKVType ()) {
568+ *output = conn->Map ({{redis::BulkString (" type" ), redis::BulkString (metadata.TypeName ())},
569+ {redis::BulkString (" expire" ), redis::Integer (metadata.expire )},
570+ {redis::BulkString (" flags" ), redis::Integer (metadata.flags )}});
571+ } else {
572+ *output = conn->Map ({{redis::BulkString (" type" ), redis::BulkString (metadata.TypeName ())},
573+ {redis::BulkString (" size" ), redis::Integer (metadata.size )},
574+ {redis::BulkString (" expire" ), redis::Integer (metadata.expire )},
575+ {redis::BulkString (" flags" ), redis::Integer (metadata.flags )},
576+ {redis::BulkString (" version" ), redis::Integer (metadata.version )}});
577+ }
578+ return Status::OK ();
579+ }
580+ };
581+
556582REDIS_REGISTER_COMMANDS (Key, MakeCmdAttr<CommandTTL>(" ttl" , 2 , " read-only" , 1 , 1 , 1 ),
557583 MakeCmdAttr<CommandPTTL>(" pttl" , 2 , " read-only" , 1 , 1 , 1 ),
558584 MakeCmdAttr<CommandType>(" type" , 2 , " read-only" , 1 , 1 , 1 ),
@@ -573,6 +599,7 @@ REDIS_REGISTER_COMMANDS(Key, MakeCmdAttr<CommandTTL>("ttl", 2, "read-only", 1, 1
573599 MakeCmdAttr<CommandRenameNX>(" renamenx" , 3 , " write" , 1 , 2 , 1 ),
574600 MakeCmdAttr<CommandCopy>(" copy" , -3 , " write" , 1 , 2 , 1 ),
575601 MakeCmdAttr<CommandSort<false >>(" sort" , -2 , " write slow" , 1 , 1 , 1 ),
576- MakeCmdAttr<CommandSort<true >>(" sort_ro" , -2 , " read-only slow" , 1 , 1 , 1 ))
602+ MakeCmdAttr<CommandSort<true >>(" sort_ro" , -2 , " read-only slow" , 1 , 1 , 1 ),
603+ MakeCmdAttr<CommandKMetadata>(" kmetadata" , 2 , " read-only" , 1 , 1 , 1 ))
577604
578605} // namespace redis
0 commit comments