@@ -389,6 +389,39 @@ class CommandJsonArrPop : public Commander {
389
389
int64_t index_ = -1 ;
390
390
};
391
391
392
+ class CommandJsonObjLen : public Commander {
393
+ public:
394
+ Status Execute (Server *svr, Connection *conn, std::string *output) override {
395
+ redis::Json json (svr->storage , conn->GetNamespace ());
396
+
397
+ std::string path = " $" ;
398
+ if (args_.size () == 3 ) {
399
+ path = args_[2 ];
400
+ } else if (args_.size () > 3 ) {
401
+ return {Status::RedisExecErr, " The number of arguments is more than expected" };
402
+ }
403
+
404
+ std::vector<std::optional<uint64_t >> results;
405
+ auto s = json.ObjLen (args_[1 ], path, results);
406
+ if (s.IsNotFound ()) {
407
+ *output = redis::NilString ();
408
+ return Status::OK ();
409
+ }
410
+ if (!s.ok ()) return {Status::RedisExecErr, s.ToString ()};
411
+
412
+ *output = redis::MultiLen (results.size ());
413
+ for (const auto &len : results) {
414
+ if (len.has_value ()) {
415
+ *output += redis::Integer (len.value ());
416
+ } else {
417
+ *output += redis::NilString ();
418
+ }
419
+ }
420
+
421
+ return Status::OK ();
422
+ }
423
+ };
424
+
392
425
class CommandJsonArrTrim : public Commander {
393
426
public:
394
427
Status Parse (const std::vector<std::string> &args) override {
@@ -608,6 +641,7 @@ REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandJsonSet>("json.set", 4, "write", 1, 1
608
641
MakeCmdAttr<CommandJsonDel>(" json.forget" , -2 , " write" , 1 , 1 , 1 ),
609
642
MakeCmdAttr<CommandJsonNumIncrBy>(" json.numincrby" , 4 , " write" , 1 , 1 , 1 ),
610
643
MakeCmdAttr<CommandJsonNumMultBy>(" json.nummultby" , 4 , " write" , 1 , 1 , 1 ),
644
+ MakeCmdAttr<CommandJsonObjLen>(" json.objlen" , -2 , " read-only" , 1 , 1 , 1 ),
611
645
MakeCmdAttr<CommandJsonStrAppend>(" json.strappend" , -3 , " write" , 1 , 1 , 1 ),
612
646
MakeCmdAttr<CommandJsonStrLen>(" json.strlen" , -2 , " read-only" , 1 , 1 , 1 ), );
613
647
0 commit comments