2013-01-23 10:23:33 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2015-07-26 09:14:57 -04:00
|
|
|
#include "server.h"
|
2013-01-23 10:23:33 -05:00
|
|
|
|
|
|
|
/* This file implements keyspace events notification via Pub/Sub ad
|
2013-01-25 07:19:08 -05:00
|
|
|
* described at http://redis.io/topics/keyspace-events. */
|
|
|
|
|
|
|
|
/* Turn a string representing notification classes into an integer
|
|
|
|
* representing notification classes flags xored.
|
2013-01-23 10:23:33 -05:00
|
|
|
*
|
2013-01-25 07:19:08 -05:00
|
|
|
* The function returns -1 if the input contains characters not mapping to
|
|
|
|
* any class. */
|
|
|
|
int keyspaceEventsStringToFlags(char *classes) {
|
|
|
|
char *p = classes;
|
|
|
|
int c, flags = 0;
|
|
|
|
|
|
|
|
while((c = *p++) != '\0') {
|
|
|
|
switch(c) {
|
2015-07-27 03:41:48 -04:00
|
|
|
case 'A': flags |= NOTIFY_ALL; break;
|
|
|
|
case 'g': flags |= NOTIFY_GENERIC; break;
|
|
|
|
case '$': flags |= NOTIFY_STRING; break;
|
|
|
|
case 'l': flags |= NOTIFY_LIST; break;
|
|
|
|
case 's': flags |= NOTIFY_SET; break;
|
|
|
|
case 'h': flags |= NOTIFY_HASH; break;
|
|
|
|
case 'z': flags |= NOTIFY_ZSET; break;
|
|
|
|
case 'x': flags |= NOTIFY_EXPIRED; break;
|
|
|
|
case 'e': flags |= NOTIFY_EVICTED; break;
|
|
|
|
case 'K': flags |= NOTIFY_KEYSPACE; break;
|
|
|
|
case 'E': flags |= NOTIFY_KEYEVENT; break;
|
2013-01-25 07:19:08 -05:00
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function does exactly the revese of the function above: it gets
|
|
|
|
* as input an integer with the xored flags and returns a string representing
|
|
|
|
* the selected classes. The string returned is an sds string that needs to
|
|
|
|
* be released with sdsfree(). */
|
|
|
|
sds keyspaceEventsFlagsToString(int flags) {
|
|
|
|
sds res;
|
|
|
|
|
|
|
|
res = sdsempty();
|
2015-07-27 03:41:48 -04:00
|
|
|
if ((flags & NOTIFY_ALL) == NOTIFY_ALL) {
|
2014-01-08 11:18:00 -05:00
|
|
|
res = sdscatlen(res,"A",1);
|
|
|
|
} else {
|
2015-07-27 03:41:48 -04:00
|
|
|
if (flags & NOTIFY_GENERIC) res = sdscatlen(res,"g",1);
|
|
|
|
if (flags & NOTIFY_STRING) res = sdscatlen(res,"$",1);
|
|
|
|
if (flags & NOTIFY_LIST) res = sdscatlen(res,"l",1);
|
|
|
|
if (flags & NOTIFY_SET) res = sdscatlen(res,"s",1);
|
|
|
|
if (flags & NOTIFY_HASH) res = sdscatlen(res,"h",1);
|
|
|
|
if (flags & NOTIFY_ZSET) res = sdscatlen(res,"z",1);
|
|
|
|
if (flags & NOTIFY_EXPIRED) res = sdscatlen(res,"x",1);
|
|
|
|
if (flags & NOTIFY_EVICTED) res = sdscatlen(res,"e",1);
|
2014-01-08 11:18:00 -05:00
|
|
|
}
|
2015-07-27 03:41:48 -04:00
|
|
|
if (flags & NOTIFY_KEYSPACE) res = sdscatlen(res,"K",1);
|
|
|
|
if (flags & NOTIFY_KEYEVENT) res = sdscatlen(res,"E",1);
|
2013-01-25 07:19:08 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The API provided to the rest of the Redis core is a simple function:
|
2013-01-23 10:23:33 -05:00
|
|
|
*
|
|
|
|
* notifyKeyspaceEvent(char *event, robj *key, int dbid);
|
|
|
|
*
|
|
|
|
* 'event' is a C string representing the event name.
|
|
|
|
* 'key' is a Redis object representing the key name.
|
2013-01-25 07:19:08 -05:00
|
|
|
* 'dbid' is the database ID where the key lives. */
|
|
|
|
void notifyKeyspaceEvent(int type, char *event, robj *key, int dbid) {
|
|
|
|
sds chan;
|
2013-01-25 11:34:52 -05:00
|
|
|
robj *chanobj, *eventobj;
|
2013-01-25 07:19:08 -05:00
|
|
|
int len = -1;
|
2013-01-23 10:23:33 -05:00
|
|
|
char buf[24];
|
|
|
|
|
2013-01-25 07:19:08 -05:00
|
|
|
/* If notifications for this class of events are off, return ASAP. */
|
|
|
|
if (!(server.notify_keyspace_events & type)) return;
|
2013-01-23 10:23:33 -05:00
|
|
|
|
2013-01-25 11:34:52 -05:00
|
|
|
eventobj = createStringObject(event,strlen(event));
|
|
|
|
|
2013-01-25 07:19:08 -05:00
|
|
|
/* __keyspace@<db>__:<key> <event> notifications. */
|
2015-07-27 03:41:48 -04:00
|
|
|
if (server.notify_keyspace_events & NOTIFY_KEYSPACE) {
|
2013-01-25 07:19:08 -05:00
|
|
|
chan = sdsnewlen("__keyspace@",11);
|
|
|
|
len = ll2string(buf,sizeof(buf),dbid);
|
|
|
|
chan = sdscatlen(chan, buf, len);
|
|
|
|
chan = sdscatlen(chan, "__:", 3);
|
2013-01-25 11:34:52 -05:00
|
|
|
chan = sdscatsds(chan, key->ptr);
|
2015-07-26 09:28:00 -04:00
|
|
|
chanobj = createObject(OBJ_STRING, chan);
|
2013-01-25 07:19:08 -05:00
|
|
|
pubsubPublishMessage(chanobj, eventobj);
|
|
|
|
decrRefCount(chanobj);
|
|
|
|
}
|
2013-01-23 10:23:33 -05:00
|
|
|
|
2013-01-25 07:19:08 -05:00
|
|
|
/* __keyevente@<db>__:<event> <key> notifications. */
|
2015-07-27 03:41:48 -04:00
|
|
|
if (server.notify_keyspace_events & NOTIFY_KEYEVENT) {
|
2013-01-25 07:19:08 -05:00
|
|
|
chan = sdsnewlen("__keyevent@",11);
|
|
|
|
if (len == -1) len = ll2string(buf,sizeof(buf),dbid);
|
|
|
|
chan = sdscatlen(chan, buf, len);
|
|
|
|
chan = sdscatlen(chan, "__:", 3);
|
2013-01-25 11:34:52 -05:00
|
|
|
chan = sdscatsds(chan, eventobj->ptr);
|
2015-07-26 09:28:00 -04:00
|
|
|
chanobj = createObject(OBJ_STRING, chan);
|
2013-01-25 07:19:08 -05:00
|
|
|
pubsubPublishMessage(chanobj, key);
|
|
|
|
decrRefCount(chanobj);
|
|
|
|
}
|
2013-01-25 11:34:52 -05:00
|
|
|
decrRefCount(eventobj);
|
2013-01-23 10:23:33 -05:00
|
|
|
}
|