summaryrefslogtreecommitdiff
path: root/examples/redis-unstable/src/notify.c
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:40:55 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:40:55 +0100
commit5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda (patch)
tree1acdfa5220cd13b7be43a2a01368e80d306473ca /examples/redis-unstable/src/notify.c
parentc7ab12bba64d9c20ccd79b132dac475f7bc3923e (diff)
downloadcrep-5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda.tar.gz
Add Redis source code for testing
Diffstat (limited to 'examples/redis-unstable/src/notify.c')
-rw-r--r--examples/redis-unstable/src/notify.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/examples/redis-unstable/src/notify.c b/examples/redis-unstable/src/notify.c
new file mode 100644
index 0000000..84cd855
--- /dev/null
+++ b/examples/redis-unstable/src/notify.c
@@ -0,0 +1,129 @@
+/*
+ * Copyright (c) 2013-Present, Redis Ltd.
+ * All rights reserved.
+ *
+ * Licensed under your choice of (a) the Redis Source Available License 2.0
+ * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
+ * GNU Affero General Public License v3 (AGPLv3).
+ */
+
+#include "server.h"
+
+/* This file implements keyspace events notification via Pub/Sub and
+ * described at https://redis.io/docs/latest/develop/use/keyspace-notifications/. */
+
+/* Turn a string representing notification classes into an integer
+ * representing notification classes flags xored.
+ *
+ * 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) {
+ 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;
+ case 't': flags |= NOTIFY_STREAM; break;
+ case 'm': flags |= NOTIFY_KEY_MISS; break;
+ case 'd': flags |= NOTIFY_MODULE; break;
+ case 'n': flags |= NOTIFY_NEW; break;
+ case 'o': flags |= NOTIFY_OVERWRITTEN; break;
+ case 'c': flags |= NOTIFY_TYPE_CHANGED; break;
+ default: return -1;
+ }
+ }
+ return flags;
+}
+
+/* This function does exactly the reverse 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();
+ if ((flags & NOTIFY_ALL) == NOTIFY_ALL) {
+ res = sdscatlen(res,"A",1);
+ } else {
+ 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);
+ if (flags & NOTIFY_STREAM) res = sdscatlen(res,"t",1);
+ if (flags & NOTIFY_MODULE) res = sdscatlen(res,"d",1);
+ if (flags & NOTIFY_NEW) res = sdscatlen(res,"n",1);
+ if (flags & NOTIFY_OVERWRITTEN) res = sdscatlen(res,"o",1);
+ if (flags & NOTIFY_TYPE_CHANGED) res = sdscatlen(res,"c",1);
+ }
+ if (flags & NOTIFY_KEYSPACE) res = sdscatlen(res,"K",1);
+ if (flags & NOTIFY_KEYEVENT) res = sdscatlen(res,"E",1);
+ if (flags & NOTIFY_KEY_MISS) res = sdscatlen(res,"m",1);
+ return res;
+}
+
+/* The API provided to the rest of the Redis core is a simple function:
+ *
+ * notifyKeyspaceEvent(int type, char *event, robj *key, int dbid);
+ *
+ * 'type' is the notification class we define in `server.h`.
+ * 'event' is a C string representing the event name.
+ * 'key' is a Redis object representing the key name.
+ * 'dbid' is the database ID where the key lives. */
+void notifyKeyspaceEvent(int type, const char *event, robj *key, int dbid) {
+ sds chan;
+ robj *chanobj, *eventobj;
+ int len = -1;
+ char buf[24];
+
+ /* If any modules are interested in events, notify the module system now.
+ * This bypasses the notifications configuration, but the module engine
+ * will only call event subscribers if the event type matches the types
+ * they are interested in. */
+ moduleNotifyKeyspaceEvent(type, event, key, dbid);
+
+ /* If notifications for this class of events are off, return ASAP. */
+ if (!(server.notify_keyspace_events & type)) return;
+
+ eventobj = createStringObject(event,strlen(event));
+
+ /* __keyspace@<db>__:<key> <event> notifications. */
+ if (server.notify_keyspace_events & NOTIFY_KEYSPACE) {
+ chan = sdsnewlen("__keyspace@",11);
+ len = ll2string(buf,sizeof(buf),dbid);
+ chan = sdscatlen(chan, buf, len);
+ chan = sdscatlen(chan, "__:", 3);
+ chan = sdscatsds(chan, key->ptr);
+ chanobj = createObject(OBJ_STRING, chan);
+ pubsubPublishMessage(chanobj, eventobj, 0);
+ decrRefCount(chanobj);
+ }
+
+ /* __keyevent@<db>__:<event> <key> notifications. */
+ if (server.notify_keyspace_events & NOTIFY_KEYEVENT) {
+ chan = sdsnewlen("__keyevent@",11);
+ if (len == -1) len = ll2string(buf,sizeof(buf),dbid);
+ chan = sdscatlen(chan, buf, len);
+ chan = sdscatlen(chan, "__:", 3);
+ chan = sdscatsds(chan, eventobj->ptr);
+ chanobj = createObject(OBJ_STRING, chan);
+ pubsubPublishMessage(chanobj, key, 0);
+ decrRefCount(chanobj);
+ }
+ decrRefCount(eventobj);
+}