MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / pubsubCommand

Function pubsubCommand

src/pubsub.cpp:435–489  ·  view source on GitHub ↗

PUBSUB command for Pub/Sub introspection. */

Source from the content-addressed store, hash-verified

433
434/* PUBSUB command for Pub/Sub introspection. */
435void pubsubCommand(client *c) {
436 if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),"help")) {
437 const char *help[] = {
438"CHANNELS [<pattern>]",
439" Return the currently active channels matching a <pattern> (default: '*').",
440"NUMPAT",
441" Return number of subscriptions to patterns.",
442"NUMSUB [<channel> ...]",
443" Return the number of subscribers for the specified channels, excluding",
444" pattern subscriptions(default: no channels).",
445NULL
446 };
447 addReplyHelp(c, help);
448 } else if (!strcasecmp(szFromObj(c->argv[1]),"channels") &&
449 (c->argc == 2 || c->argc == 3))
450 {
451 /* PUBSUB CHANNELS [<pattern>] */
452 sds pat = (c->argc == 2) ? NULL : szFromObj(c->argv[2]);
453 dictIterator *di = dictGetIterator(g_pserver->pubsub_channels);
454 dictEntry *de;
455 long mblen = 0;
456 void *replylen;
457
458 replylen = addReplyDeferredLen(c);
459 while((de = dictNext(di)) != NULL) {
460 robj *cobj = (robj*)dictGetKey(de);
461 sds channel = szFromObj(cobj);
462
463 if (!pat || stringmatchlen(pat, sdslen(pat),
464 channel, sdslen(channel),0))
465 {
466 addReplyBulk(c,cobj);
467 mblen++;
468 }
469 }
470 dictReleaseIterator(di);
471 setDeferredArrayLen(c,replylen,mblen);
472 } else if (!strcasecmp(szFromObj(c->argv[1]),"numsub") && c->argc >= 2) {
473 /* PUBSUB NUMSUB [Channel_1 ... Channel_N] */
474 int j;
475
476 addReplyArrayLen(c,(c->argc-2)*2);
477 for (j = 2; j < c->argc; j++) {
478 list *l = (list*)dictFetchValue(g_pserver->pubsub_channels,c->argv[j]);
479
480 addReplyBulk(c,c->argv[j]);
481 addReplyLongLong(c,l ? listLength(l) : 0);
482 }
483 } else if (!strcasecmp(szFromObj(c->argv[1]),"numpat") && c->argc == 2) {
484 /* PUBSUB NUMPAT */
485 addReplyLongLong(c,dictSize(g_pserver->pubsub_patterns));
486 } else {
487 addReplySubcommandSyntaxError(c);
488 }
489}

Callers

nothing calls this directly

Calls 14

szFromObjFunction · 0.85
addReplyHelpFunction · 0.85
addReplyDeferredLenFunction · 0.85
stringmatchlenFunction · 0.85
sdslenFunction · 0.85
addReplyBulkFunction · 0.85
setDeferredArrayLenFunction · 0.85
addReplyArrayLenFunction · 0.85
dictFetchValueFunction · 0.85
addReplyLongLongFunction · 0.85
dictGetIteratorFunction · 0.70

Tested by

no test coverage detected