check-in the first batch of files

This commit is contained in:
JesseQu 2024-08-21 15:51:27 +08:00
parent 7f1d21f0ec
commit a9bbc8c401
27 changed files with 4911 additions and 0 deletions

View File

@ -0,0 +1,97 @@
#pragma once
// clang-format off
#include <sys/types.h>
#include <stdint.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------------------------------------
// bigredis extensions
#define REDISMODULE_METADATA_NOT_ON_SWAP 0x80
/* Notification that a key's value is added to ram (from swap or otherwise).
* swap_key_flags has 4 bits that the module can write / read.
* when swap_key_metadata is NOT_ON_SWAP, it means the key is not loaded from swap. */
typedef void (*RedisModuleTypeKeyAddedToDbDictFunc)(RedisModuleCtx *ctx, RedisModuleString *key, void *value, int swap_key_metadata);
/* Notification that a key's value is removed from ram (may still exist on swap).
* when swap_key_metadata is NOT_ON_SWAP it means the key does not exist on swap.
* return swap_key_metadata or NOT_ON_SWAP if key is to be deleted (and not to be written). */
typedef int (*RedisModuleTypeRemovingKeyFromDbDictFunc)(RedisModuleCtx *ctx, RedisModuleString *key, void *value, int swap_key_metadata, int writing_to_swap);
/* return swap_key_metadata, 0 indicates nothing to write. when out_min_expire is -1 it indicates nothing to write. */
typedef int (*RedisModuleTypeGetKeyMetadataForRdbFunc)(RedisModuleCtx *ctx, RedisModuleString *key, void *value, long long *out_min_expire, long long *out_max_expire);
#define REDISMODULE_TYPE_EXT_METHOD_VERSION 1
typedef struct RedisModuleTypeExtMethods {
uint64_t version;
RedisModuleTypeKeyAddedToDbDictFunc key_added_to_db_dict;
RedisModuleTypeRemovingKeyFromDbDictFunc removing_key_from_db_dict;
RedisModuleTypeGetKeyMetadataForRdbFunc get_key_metadata_for_rdb;
} RedisModuleTypeExtMethods;
typedef void (*RedisModuleSwapPrefetchCB)(RedisModuleCtx *ctx, RedisModuleString *key, void* user_data);
/* APIs */
REDISMODULE_API int (*RedisModule_SetDataTypeExtensions)(RedisModuleCtx *ctx, RedisModuleType *mt, RedisModuleTypeExtMethods *typemethods) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_SwapPrefetchKey)(RedisModuleCtx *ctx, RedisModuleString *keyname, RedisModuleSwapPrefetchCB fn, void *user_data, int flags) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_GetSwapKeyMetadata)(RedisModuleCtx *ctx, RedisModuleString *key) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_SetSwapKeyMetadata)(RedisModuleCtx *ctx, RedisModuleString *key, int module_metadata) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_IsKeyInRam)(RedisModuleCtx *ctx, RedisModuleString *key) REDISMODULE_ATTR;
//---------------------------------------------------------------------------------------------
/* Keyspace changes notification classes. Every class is associated with a
* character for configuration purposes.
* NOTE: These have to be in sync with NOTIFY_* in server.h */
#define REDISMODULE_NOTIFY_TRIMMED (1<<30) /* trimmed by reshard trimming enterprise only event */
/* Server events definitions.
* Those flags should not be used directly by the module, instead
* the module should use RedisModuleEvent_* variables */
#define REDISMODULE_EVENT_SHARDING 1000
static const RedisModuleEvent
RedisModuleEvent_Sharding = {
REDISMODULE_EVENT_SHARDING,
1
};
/* Those are values that are used for the 'subevent' callback argument. */
#define REDISMODULE_SUBEVENT_SHARDING_SLOT_RANGE_CHANGED 0
#define REDISMODULE_SUBEVENT_SHARDING_TRIMMING_STARTED 1
#define REDISMODULE_SUBEVENT_SHARDING_TRIMMING_ENDED 2
/* APIs */
REDISMODULE_API int (*RedisModule_ShardingGetKeySlot)(RedisModuleString *keyname) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_ShardingGetSlotRange)(int *first_slot, int *last_slot) REDISMODULE_ATTR;
//---------------------------------------------------------------------------------------------
#define REDISMODULE_RLEC_API_DEFS \
REDISMODULE_GET_API(ShardingGetKeySlot); \
REDISMODULE_GET_API(ShardingGetSlotRange); \
REDISMODULE_GET_API(SetDataTypeExtensions); \
REDISMODULE_GET_API(SwapPrefetchKey); \
REDISMODULE_GET_API(GetSwapKeyMetadata); \
REDISMODULE_GET_API(SetSwapKeyMetadata); \
REDISMODULE_GET_API(IsKeyInRam); \
/**/
//---------------------------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif

1582
include/redismodule.h Normal file

File diff suppressed because it is too large Load Diff

32
rmutil/alloc.c Normal file
View File

@ -0,0 +1,32 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "alloc.h"
/* A patched implementation of strdup that will use our patched calloc */
char *rmalloc_strndup(const char *s, size_t n) {
char *ret = calloc(n + 1, sizeof(char));
if (ret)
memcpy(ret, s, n);
return ret;
}
/*
* Re-patching RedisModule_Alloc and friends to the original malloc functions
*
* This function should be called if you are working with malloc-patched code
* outside of redis, usually for unit tests. Call it once when entering your unit
* tests' main().
*
* Since including "alloc.h" while defining REDIS_MODULE_TARGET
* replaces all malloc functions in redis with the RM_Alloc family of functions,
* when running that code outside of redis, your app will crash. This function
* patches the RM_Alloc functions back to the original mallocs. */
void RMUTil_InitAlloc() {
RedisModule_Alloc = malloc;
RedisModule_Realloc = realloc;
RedisModule_Calloc = calloc;
RedisModule_Free = free;
RedisModule_Strdup = strdup;
}

51
rmutil/alloc.h Normal file
View File

@ -0,0 +1,51 @@
#ifndef __RMUTIL_ALLOC__
#define __RMUTIL_ALLOC__
/* Automatic Redis Module Allocation functions monkey-patching.
*
* Including this file while REDIS_MODULE_TARGET is defined, will explicitly
* override malloc, calloc, realloc & free with RedisModule_Alloc,
* RedisModule_Callc, etc implementations, that allow Redis better control and
* reporting over allocations per module.
*
* You should include this file in all c files AS THE LAST INCLUDED FILE
*
* This only has effect when when compiling with the macro REDIS_MODULE_TARGET
* defined. The idea is that for unit tests it will not be defined, but for the
* module build target it will be.
*
*/
#include <stdlib.h>
#include <redismodule.h>
char *rmalloc_strndup(const char *s, size_t n);
#ifdef REDIS_MODULE_TARGET /* Set this when compiling your code as a module */
#define malloc(size) RedisModule_Alloc(size)
#define calloc(count, size) RedisModule_Calloc(count, size)
#define realloc(ptr, size) RedisModule_Realloc(ptr, size)
#define free(ptr) RedisModule_Free(ptr)
#ifdef strdup
#undef strdup
#endif
#define strdup(ptr) RedisModule_Strdup(ptr)
/* More overriding */
// needed to avoid calling strndup->malloc
#ifdef strndup
#undef strndup
#endif
#define strndup(s, n) rmalloc_strndup(s, n)
#else
#endif /* REDIS_MODULE_TARGET */
/* This function should be called if you are working with malloc-patched code
* outside of redis, usually for unit tests. Call it once when entering your unit
* tests' main() */
void RMUTil_InitAlloc();
#endif /* __RMUTIL_ALLOC__ */

107
rmutil/heap.c Normal file
View File

@ -0,0 +1,107 @@
#include "heap.h"
/* Byte-wise swap two items of size SIZE. */
#define SWAP(a, b, size) \
do \
{ \
register size_t __size = (size); \
register char *__a = (a), *__b = (b); \
do \
{ \
char __tmp = *__a; \
*__a++ = *__b; \
*__b++ = __tmp; \
} while (--__size > 0); \
} while (0)
inline char *__vector_GetPtr(Vector *v, size_t pos) {
return v->data + (pos * v->elemSize);
}
void __sift_up(Vector *v, size_t first, size_t last, int (*cmp)(void *, void *)) {
size_t len = last - first;
if (len > 1) {
len = (len - 2) / 2;
size_t ptr = first + len;
if (cmp(__vector_GetPtr(v, ptr), __vector_GetPtr(v, --last)) < 0) {
char t[v->elemSize];
memcpy(t, __vector_GetPtr(v, last), v->elemSize);
do {
memcpy(__vector_GetPtr(v, last), __vector_GetPtr(v, ptr), v->elemSize);
last = ptr;
if (len == 0)
break;
len = (len - 1) / 2;
ptr = first + len;
} while (cmp(__vector_GetPtr(v, ptr), t) < 0);
memcpy(__vector_GetPtr(v, last), t, v->elemSize);
}
}
}
void __sift_down(Vector *v, size_t first, size_t last, int (*cmp)(void *, void *), size_t start) {
// left-child of __start is at 2 * __start + 1
// right-child of __start is at 2 * __start + 2
size_t len = last - first;
size_t child = start - first;
if (len < 2 || (len - 2) / 2 < child)
return;
child = 2 * child + 1;
if ((child + 1) < len && cmp(__vector_GetPtr(v, first + child), __vector_GetPtr(v, first + child + 1)) < 0) {
// right-child exists and is greater than left-child
++child;
}
// check if we are in heap-order
if (cmp(__vector_GetPtr(v, first + child), __vector_GetPtr(v, start)) < 0)
// we are, __start is larger than it's largest child
return;
char top[v->elemSize];
memcpy(top, __vector_GetPtr(v, start), v->elemSize);
do {
// we are not in heap-order, swap the parent with it's largest child
memcpy(__vector_GetPtr(v, start), __vector_GetPtr(v, first + child), v->elemSize);
start = first + child;
if ((len - 2) / 2 < child)
break;
// recompute the child based off of the updated parent
child = 2 * child + 1;
if ((child + 1) < len && cmp(__vector_GetPtr(v, first + child), __vector_GetPtr(v, first + child + 1)) < 0) {
// right-child exists and is greater than left-child
++child;
}
// check if we are in heap-order
} while (cmp(__vector_GetPtr(v, first + child), top) >= 0);
memcpy(__vector_GetPtr(v, start), top, v->elemSize);
}
void Make_Heap(Vector *v, size_t first, size_t last, int (*cmp)(void *, void *)) {
if (last - first > 1) {
// start from the first parent, there is no need to consider children
for (int start = (last - first - 2) / 2; start >= 0; --start) {
__sift_down(v, first, last, cmp, first + start);
}
}
}
inline void Heap_Push(Vector *v, size_t first, size_t last, int (*cmp)(void *, void *)) {
__sift_up(v, first, last, cmp);
}
inline void Heap_Pop(Vector *v, size_t first, size_t last, int (*cmp)(void *, void *)) {
if (last - first > 1) {
SWAP(__vector_GetPtr(v, first), __vector_GetPtr(v, --last), v->elemSize);
__sift_down(v, first, last, cmp, first);
}
}

38
rmutil/heap.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef __HEAP_H__
#define __HEAP_H__
#include "vector.h"
/* Make heap from range
* Rearranges the elements in the range [first,last) in such a way that they form a heap.
* A heap is a way to organize the elements of a range that allows for fast retrieval of the element with the highest
* value at any moment (with pop_heap), even repeatedly, while allowing for fast insertion of new elements (with
* push_heap).
* The element with the highest value is always pointed by first. The order of the other elements depends on the
* particular implementation, but it is consistent throughout all heap-related functions of this header.
* The elements are compared using cmp.
*/
void Make_Heap(Vector *v, size_t first, size_t last, int (*cmp)(void *, void *));
/* Push element into heap range
* Given a heap in the range [first,last-1), this function extends the range considered a heap to [first,last) by
* placing the value in (last-1) into its corresponding location within it.
* A range can be organized into a heap by calling make_heap. After that, its heap properties are preserved if elements
* are added and removed from it using push_heap and pop_heap, respectively.
*/
void Heap_Push(Vector *v, size_t first, size_t last, int (*cmp)(void *, void *));
/* Pop element from heap range
* Rearranges the elements in the heap range [first,last) in such a way that the part considered a heap is shortened
* by one: The element with the highest value is moved to (last-1).
* While the element with the highest value is moved from first to (last-1) (which now is out of the heap), the other
* elements are reorganized in such a way that the range [first,last-1) preserves the properties of a heap.
* A range can be organized into a heap by calling make_heap. After that, its heap properties are preserved if elements
* are added and removed from it using push_heap and pop_heap, respectively.
*/
void Heap_Pop(Vector *v, size_t first, size_t last, int (*cmp)(void *, void *));
#endif //__HEAP_H__

11
rmutil/logging.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef __RMUTIL_LOGGING_H__
#define __RMUTIL_LOGGING_H__
/* Convenience macros for redis logging */
#define RM_LOG_DEBUG(ctx, ...) RedisModule_Log(ctx, "debug", __VA_ARGS__)
#define RM_LOG_VERBOSE(ctx, ...) RedisModule_Log(ctx, "verbose", __VA_ARGS__)
#define RM_LOG_NOTICE(ctx, ...) RedisModule_Log(ctx, "notice", __VA_ARGS__)
#define RM_LOG_WARNING(ctx, ...) RedisModule_Log(ctx, "warning", __VA_ARGS__)
#endif

88
rmutil/periodic.c Normal file
View File

@ -0,0 +1,88 @@
#define REDISMODULE_EXPERIMENTAL_API
#include "periodic.h"
#include <pthread.h>
#include <stdlib.h>
#include <errno.h>
typedef struct RMUtilTimer {
RMutilTimerFunc cb;
RMUtilTimerTerminationFunc onTerm;
void *privdata;
struct timespec interval;
pthread_t thread;
pthread_mutex_t lock;
pthread_cond_t cond;
} RMUtilTimer;
static struct timespec timespecAdd(struct timespec *a, struct timespec *b) {
struct timespec ret;
ret.tv_sec = a->tv_sec + b->tv_sec;
long long ns = a->tv_nsec + b->tv_nsec;
ret.tv_sec += ns / 1000000000;
ret.tv_nsec = ns % 1000000000;
return ret;
}
static void *rmutilTimer_Loop(void *ctx) {
RMUtilTimer *tm = ctx;
int rc = ETIMEDOUT;
struct timespec ts;
pthread_mutex_lock(&tm->lock);
while (rc != 0) {
clock_gettime(CLOCK_REALTIME, &ts);
struct timespec timeout = timespecAdd(&ts, &tm->interval);
if ((rc = pthread_cond_timedwait(&tm->cond, &tm->lock, &timeout)) == ETIMEDOUT) {
// Create a thread safe context if we're running inside redis
RedisModuleCtx *rctx = NULL;
if (RedisModule_GetThreadSafeContext) rctx = RedisModule_GetThreadSafeContext(NULL);
// call our callback...
tm->cb(rctx, tm->privdata);
// If needed - free the thread safe context.
// It's up to the user to decide whether automemory is active there
if (rctx) RedisModule_FreeThreadSafeContext(rctx);
}
if (rc == EINVAL) {
perror("Error waiting for condition");
break;
}
}
// call the termination callback if needed
if (tm->onTerm != NULL) {
tm->onTerm(tm->privdata);
}
// free resources associated with the timer
pthread_cond_destroy(&tm->cond);
free(tm);
return NULL;
}
/* set a new frequency for the timer. This will take effect AFTER the next trigger */
void RMUtilTimer_SetInterval(struct RMUtilTimer *t, struct timespec newInterval) {
t->interval = newInterval;
}
RMUtilTimer *RMUtil_NewPeriodicTimer(RMutilTimerFunc cb, RMUtilTimerTerminationFunc onTerm,
void *privdata, struct timespec interval) {
RMUtilTimer *ret = malloc(sizeof(*ret));
*ret = (RMUtilTimer){
.privdata = privdata, .interval = interval, .cb = cb, .onTerm = onTerm,
};
pthread_cond_init(&ret->cond, NULL);
pthread_mutex_init(&ret->lock, NULL);
pthread_create(&ret->thread, NULL, rmutilTimer_Loop, ret);
return ret;
}
int RMUtilTimer_Terminate(struct RMUtilTimer *t) {
return pthread_cond_signal(&t->cond);
}

46
rmutil/periodic.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef RMUTIL_PERIODIC_H_
#define RMUTIL_PERIODIC_H_
#include <time.h>
#include <redismodule.h>
/** periodic.h - Utility periodic timer running a task repeatedly every given time interval */
/* RMUtilTimer - opaque context for the timer */
struct RMUtilTimer;
/* RMutilTimerFunc - callback type for timer tasks. The ctx is a thread-safe redis module context
* that should be locked/unlocked by the callback when running stuff against redis. privdata is
* pre-existing private data */
typedef void (*RMutilTimerFunc)(RedisModuleCtx *ctx, void *privdata);
typedef void (*RMUtilTimerTerminationFunc)(void *privdata);
/* Create and start a new periodic timer. Each timer has its own thread and can only be run and
* stopped once. The timer runs `cb` every `interval` with `privdata` passed to the callback. */
struct RMUtilTimer *RMUtil_NewPeriodicTimer(RMutilTimerFunc cb, RMUtilTimerTerminationFunc onTerm,
void *privdata, struct timespec interval);
/* set a new frequency for the timer. This will take effect AFTER the next trigger */
void RMUtilTimer_SetInterval(struct RMUtilTimer *t, struct timespec newInterval);
/* Stop the timer loop, call the termination callbck to free up any resources linked to the timer,
* and free the timer after stopping.
*
* This function doesn't wait for the thread to terminate, as it may cause a race condition if the
* timer's callback is waiting for the redis global lock.
* Instead you should make sure any resources are freed by the callback after the thread loop is
* finished.
*
* The timer is freed automatically, so the callback doesn't need to do anything about it.
* The callback gets the timer's associated privdata as its argument.
*
* If no callback is specified we do not free up privdata. If privdata is NULL we still call the
* callback, as it may log stuff or free global resources.
*/
int RMUtilTimer_Terminate(struct RMUtilTimer *t);
/* DEPRECATED - do not use this function (well now you can't), use terminate instead
Free the timer context. The caller should be responsible for freeing the private data at this
* point */
// void RMUtilTimer_Free(struct RMUtilTimer *t);
#endif

36
rmutil/priority_queue.c Normal file
View File

@ -0,0 +1,36 @@
#include "priority_queue.h"
#include "heap.h"
PriorityQueue *__newPriorityQueueSize(size_t elemSize, size_t cap, int (*cmp)(void *, void *)) {
PriorityQueue *pq = malloc(sizeof(PriorityQueue));
pq->v = __newVectorSize(elemSize, cap);
pq->cmp = cmp;
return pq;
}
inline size_t Priority_Queue_Size(PriorityQueue *pq) {
return Vector_Size(pq->v);
}
inline int Priority_Queue_Top(PriorityQueue *pq, void *ptr) {
return Vector_Get(pq->v, 0, ptr);
}
inline size_t __priority_Queue_PushPtr(PriorityQueue *pq, void *elem) {
size_t top = __vector_PushPtr(pq->v, elem);
Heap_Push(pq->v, 0, top, pq->cmp);
return top;
}
inline void Priority_Queue_Pop(PriorityQueue *pq) {
if (pq->v->top == 0) {
return;
}
Heap_Pop(pq->v, 0, pq->v->top, pq->cmp);
pq->v->top--;
}
void Priority_Queue_Free(PriorityQueue *pq) {
Vector_Free(pq->v);
free(pq);
}

55
rmutil/priority_queue.h Normal file
View File

@ -0,0 +1,55 @@
#ifndef __PRIORITY_QUEUE_H__
#define __PRIORITY_QUEUE_H__
#include "vector.h"
/* Priority queue
* Priority queues are designed such that its first element is always the greatest of the elements it contains.
* This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be
* retrieved (the one at the top in the priority queue).
* Priority queues are implemented as Vectors. Elements are popped from the "back" of Vector, which is known as the top
* of the priority queue.
*/
typedef struct {
Vector *v;
int (*cmp)(void *, void *);
} PriorityQueue;
/* Construct priority queue
* Constructs a priority_queue container adaptor object.
*/
PriorityQueue *__newPriorityQueueSize(size_t elemSize, size_t cap, int (*cmp)(void *, void *));
#define NewPriorityQueue(type, cap, cmp) __newPriorityQueueSize(sizeof(type), cap, cmp)
/* Return size
* Returns the number of elements in the priority_queue.
*/
size_t Priority_Queue_Size(PriorityQueue *pq);
/* Access top element
* Copy the top element in the priority_queue to ptr.
* The top element is the element that compares higher in the priority_queue.
*/
int Priority_Queue_Top(PriorityQueue *pq, void *ptr);
/* Insert element
* Inserts a new element in the priority_queue.
*/
size_t __priority_Queue_PushPtr(PriorityQueue *pq, void *elem);
#define Priority_Queue_Push(pq, elem) __priority_Queue_PushPtr(pq, &(typeof(elem)){elem})
/* Remove top element
* Removes the element on top of the priority_queue, effectively reducing its size by one. The element removed is the
* one with the highest value.
* The value of this element can be retrieved before being popped by calling Priority_Queue_Top.
*/
void Priority_Queue_Pop(PriorityQueue *pq);
/* free the priority queue and the underlying data. Does not release its elements if
* they are pointers */
void Priority_Queue_Free(PriorityQueue *pq);
#endif //__PRIORITY_QUEUE_H__

1274
rmutil/sds.c Normal file

File diff suppressed because it is too large Load Diff

273
rmutil/sds.h Normal file
View File

@ -0,0 +1,273 @@
/* SDSLib 2.0 -- A C dynamic strings library
*
* Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2015, Oran Agra
* Copyright (c) 2015, Redis Labs, Inc
* 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.
*/
#ifndef __SDS_H
#define __SDS_H
#define SDS_MAX_PREALLOC (1024*1024)
#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
typedef char *sds;
/* Note: sdshdr5 is never used, we just access the flags byte directly.
* However is here to document the layout of type 5 SDS strings. */
struct __attribute__ ((__packed__)) sdshdr5 {
unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
char buf[];
};
struct __attribute__ ((__packed__)) sdshdr8 {
uint8_t len; /* used */
uint8_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
struct __attribute__ ((__packed__)) sdshdr16 {
uint16_t len; /* used */
uint16_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
struct __attribute__ ((__packed__)) sdshdr32 {
uint32_t len; /* used */
uint32_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
struct __attribute__ ((__packed__)) sdshdr64 {
uint64_t len; /* used */
uint64_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
#define SDS_TYPE_5 0
#define SDS_TYPE_8 1
#define SDS_TYPE_16 2
#define SDS_TYPE_32 3
#define SDS_TYPE_64 4
#define SDS_TYPE_MASK 7
#define SDS_TYPE_BITS 3
#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (void*)((s)-(sizeof(struct sdshdr##T)));
#define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))))
#define SDS_TYPE_5_LEN(f) ((f)>>SDS_TYPE_BITS)
static inline size_t sdslen(const sds s) {
unsigned char flags = s[-1];
switch(flags&SDS_TYPE_MASK) {
case SDS_TYPE_5:
return SDS_TYPE_5_LEN(flags);
case SDS_TYPE_8:
return SDS_HDR(8,s)->len;
case SDS_TYPE_16:
return SDS_HDR(16,s)->len;
case SDS_TYPE_32:
return SDS_HDR(32,s)->len;
case SDS_TYPE_64:
return SDS_HDR(64,s)->len;
}
return 0;
}
static inline size_t sdsavail(const sds s) {
unsigned char flags = s[-1];
switch(flags&SDS_TYPE_MASK) {
case SDS_TYPE_5: {
return 0;
}
case SDS_TYPE_8: {
SDS_HDR_VAR(8,s);
return sh->alloc - sh->len;
}
case SDS_TYPE_16: {
SDS_HDR_VAR(16,s);
return sh->alloc - sh->len;
}
case SDS_TYPE_32: {
SDS_HDR_VAR(32,s);
return sh->alloc - sh->len;
}
case SDS_TYPE_64: {
SDS_HDR_VAR(64,s);
return sh->alloc - sh->len;
}
}
return 0;
}
static inline void sdssetlen(sds s, size_t newlen) {
unsigned char flags = s[-1];
switch(flags&SDS_TYPE_MASK) {
case SDS_TYPE_5:
{
unsigned char *fp = ((unsigned char*)s)-1;
*fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);
}
break;
case SDS_TYPE_8:
SDS_HDR(8,s)->len = newlen;
break;
case SDS_TYPE_16:
SDS_HDR(16,s)->len = newlen;
break;
case SDS_TYPE_32:
SDS_HDR(32,s)->len = newlen;
break;
case SDS_TYPE_64:
SDS_HDR(64,s)->len = newlen;
break;
}
}
static inline void sdsinclen(sds s, size_t inc) {
unsigned char flags = s[-1];
switch(flags&SDS_TYPE_MASK) {
case SDS_TYPE_5:
{
unsigned char *fp = ((unsigned char*)s)-1;
unsigned char newlen = SDS_TYPE_5_LEN(flags)+inc;
*fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);
}
break;
case SDS_TYPE_8:
SDS_HDR(8,s)->len += inc;
break;
case SDS_TYPE_16:
SDS_HDR(16,s)->len += inc;
break;
case SDS_TYPE_32:
SDS_HDR(32,s)->len += inc;
break;
case SDS_TYPE_64:
SDS_HDR(64,s)->len += inc;
break;
}
}
/* sdsalloc() = sdsavail() + sdslen() */
static inline size_t sdsalloc(const sds s) {
unsigned char flags = s[-1];
switch(flags&SDS_TYPE_MASK) {
case SDS_TYPE_5:
return SDS_TYPE_5_LEN(flags);
case SDS_TYPE_8:
return SDS_HDR(8,s)->alloc;
case SDS_TYPE_16:
return SDS_HDR(16,s)->alloc;
case SDS_TYPE_32:
return SDS_HDR(32,s)->alloc;
case SDS_TYPE_64:
return SDS_HDR(64,s)->alloc;
}
return 0;
}
static inline void sdssetalloc(sds s, size_t newlen) {
unsigned char flags = s[-1];
switch(flags&SDS_TYPE_MASK) {
case SDS_TYPE_5:
/* Nothing to do, this type has no total allocation info. */
break;
case SDS_TYPE_8:
SDS_HDR(8,s)->alloc = newlen;
break;
case SDS_TYPE_16:
SDS_HDR(16,s)->alloc = newlen;
break;
case SDS_TYPE_32:
SDS_HDR(32,s)->alloc = newlen;
break;
case SDS_TYPE_64:
SDS_HDR(64,s)->alloc = newlen;
break;
}
}
sds sdsnewlen(const void *init, size_t initlen);
sds sdsnew(const char *init);
sds sdsempty(void);
sds sdsdup(const sds s);
void sdsfree(sds s);
sds sdsgrowzero(sds s, size_t len);
sds sdscatlen(sds s, const void *t, size_t len);
sds sdscat(sds s, const char *t);
sds sdscatsds(sds s, const sds t);
sds sdscpylen(sds s, const char *t, size_t len);
sds sdscpy(sds s, const char *t);
sds sdscatvprintf(sds s, const char *fmt, va_list ap);
#ifdef __GNUC__
sds sdscatprintf(sds s, const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
#else
sds sdscatprintf(sds s, const char *fmt, ...);
#endif
sds sdscatfmt(sds s, char const *fmt, ...);
sds sdstrim(sds s, const char *cset);
void sdsrange(sds s, int start, int end);
void sdsupdatelen(sds s);
void sdsclear(sds s);
int sdscmp(const sds s1, const sds s2);
sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);
void sdsfreesplitres(sds *tokens, int count);
void sdstolower(sds s);
void sdstoupper(sds s);
sds sdsfromlonglong(long long value);
sds sdscatrepr(sds s, const char *p, size_t len);
sds *sdssplitargs(const char *line, int *argc);
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);
sds sdsjoin(char **argv, int argc, char *sep);
sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen);
/* Low level functions exposed to the user API */
sds sdsMakeRoomFor(sds s, size_t addlen);
void sdsIncrLen(sds s, int incr);
sds sdsRemoveFreeSpace(sds s);
size_t sdsAllocSize(sds s);
void *sdsAllocPtr(sds s);
/* Export the allocator used by SDS to the program using SDS.
* Sometimes the program SDS is linked to, may use a different set of
* allocators, but may want to allocate or free things that SDS will
* respectively free or allocate. */
void *sds_malloc(size_t size);
void *sds_realloc(void *ptr, size_t size);
void sds_free(void *ptr);
#ifdef REDIS_TEST
int sdsTest(int argc, char *argv[]);
#endif
#endif

47
rmutil/sdsalloc.h Normal file
View File

@ -0,0 +1,47 @@
/* SDSLib 2.0 -- A C dynamic strings library
*
* Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2015, Redis Labs, Inc
* 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.
*/
/* SDS allocator selection.
*
* This file is used in order to change the SDS allocator at compile time.
* Just define the following defines to what you want to use. Also add
* the include of your alternate allocator if needed (not needed in order
* to use the default libc allocator). */
#if defined(__MACH__)
#include <stdlib.h>
#else
#include <malloc.h>
#endif
//#include "zmalloc.h"
#define s_malloc malloc
#define s_realloc realloc
#define s_free free

81
rmutil/strings.c Normal file
View File

@ -0,0 +1,81 @@
#include <string.h>
#include <sys/param.h>
#include <ctype.h>
#include "strings.h"
#include "alloc.h"
#include "sds.h"
// RedisModuleString *RMUtil_CreateFormattedString(RedisModuleCtx *ctx, const char *fmt, ...) {
// sds s = sdsempty();
// va_list ap;
// va_start(ap, fmt);
// s = sdscatvprintf(s, fmt, ap);
// va_end(ap);
// RedisModuleString *ret = RedisModule_CreateString(ctx, (const char *)s, sdslen(s));
// sdsfree(s);
// return ret;
// }
int RMUtil_StringEquals(RedisModuleString *s1, RedisModuleString *s2) {
const char *c1, *c2;
size_t l1, l2;
c1 = RedisModule_StringPtrLen(s1, &l1);
c2 = RedisModule_StringPtrLen(s2, &l2);
if (l1 != l2) return 0;
return strncmp(c1, c2, l1) == 0;
}
int RMUtil_StringEqualsC(RedisModuleString *s1, const char *s2) {
const char *c1;
size_t l1, l2 = strlen(s2);
c1 = RedisModule_StringPtrLen(s1, &l1);
if (l1 != l2) return 0;
return strncmp(c1, s2, l1) == 0;
}
int RMUtil_StringEqualsCaseC(RedisModuleString *s1, const char *s2) {
const char *c1;
size_t l1, l2 = strlen(s2);
c1 = RedisModule_StringPtrLen(s1, &l1);
if (l1 != l2) return 0;
return strncasecmp(c1, s2, l1) == 0;
}
void RMUtil_StringToLower(RedisModuleString *s) {
size_t l;
char *c = (char *)RedisModule_StringPtrLen(s, &l);
size_t i;
for (i = 0; i < l; i++) {
*c = tolower(*c);
++c;
}
}
void RMUtil_StringToUpper(RedisModuleString *s) {
size_t l;
char *c = (char *)RedisModule_StringPtrLen(s, &l);
size_t i;
for (i = 0; i < l; i++) {
*c = toupper(*c);
++c;
}
}
void RMUtil_StringConvert(RedisModuleString **rs, const char **ss, size_t n, int options) {
for (size_t ii = 0; ii < n; ++ii) {
const char *p = RedisModule_StringPtrLen(rs[ii], NULL);
if (options & RMUTIL_STRINGCONVERT_COPY) {
p = strdup(p);
}
ss[ii] = p;
}
}

38
rmutil/strings.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef __RMUTIL_STRINGS_H__
#define __RMUTIL_STRINGS_H__
#include <redismodule.h>
/*
* Create a new RedisModuleString object from a printf-style format and arguments.
* Note that RedisModuleString objects CANNOT be used as formatting arguments.
*/
// DEPRECATED since it was added to the RedisModule API. Replaced with a macro below
// RedisModuleString *RMUtil_CreateFormattedString(RedisModuleCtx *ctx, const char *fmt, ...);
#define RMUtil_CreateFormattedString RedisModule_CreateStringPrintf
/* Return 1 if the two strings are equal. Case *sensitive* */
int RMUtil_StringEquals(RedisModuleString *s1, RedisModuleString *s2);
/* Return 1 if the string is equal to a C NULL terminated string. Case *sensitive* */
int RMUtil_StringEqualsC(RedisModuleString *s1, const char *s2);
/* Return 1 if the string is equal to a C NULL terminated string. Case *insensitive* */
int RMUtil_StringEqualsCaseC(RedisModuleString *s1, const char *s2);
/* Converts a redis string to lowercase in place without reallocating anything */
void RMUtil_StringToLower(RedisModuleString *s);
/* Converts a redis string to uppercase in place without reallocating anything */
void RMUtil_StringToUpper(RedisModuleString *s);
// If set, copy the strings using strdup rather than simply storing pointers.
#define RMUTIL_STRINGCONVERT_COPY 1
/**
* Convert one or more RedisModuleString objects into `const char*`.
* Both rs and ss are arrays, and should be of <n> length.
* Options may be 0 or `RMUTIL_STRINGCONVERT_COPY`
*/
void RMUtil_StringConvert(RedisModuleString **rs, const char **ss, size_t n, int options);
#endif

69
rmutil/test.h Normal file
View File

@ -0,0 +1,69 @@
#ifndef __TESTUTIL_H__
#define __TESTUTIL_H__
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
static int numTests = 0;
static int numAsserts = 0;
#define TESTFUNC(f) \
printf(" Testing %s\t\t", __STRING(f)); \
numTests++; \
fflush(stdout); \
if (f()) { \
printf(" %s FAILED!\n", __STRING(f)); \
exit(1); \
} else \
printf("[PASS]\n");
#define ASSERTM(expr, ...) \
if (!(expr)) { \
fprintf(stderr, "%s:%d: Assertion '%s' Failed: " __VA_ARGS__ "\n", __FILE__, __LINE__, \
__STRING(expr)); \
return -1; \
} \
numAsserts++;
#define ASSERT(expr) \
if (!(expr)) { \
fprintf(stderr, "%s:%d Assertion '%s' Failed\n", __FILE__, __LINE__, __STRING(expr)); \
return -1; \
} \
numAsserts++;
#define ASSERT_STRING_EQ(s1, s2) ASSERT(!strcmp(s1, s2));
#define ASSERT_EQUAL(x, y, ...) \
if (x != y) { \
fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); \
fprintf(stderr, "%g != %g: " __VA_ARGS__ "\n", (double)x, (double)y); \
return -1; \
} \
numAsserts++;
#define FAIL(fmt, ...) \
{ \
fprintf(stderr, "%s:%d: FAIL: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
return -1; \
}
#define RETURN_TEST_SUCCESS return 0;
#define TEST_CASE(x, block) \
int x { \
block; \
return 0 \
}
#define PRINT_TEST_SUMMARY printf("\nTotal: %d tests and %d assertions OK\n", numTests, numAsserts);
#define TEST_MAIN(body) \
int main(int argc, char **argv) { \
printf("Starting Test '%s'...\n", argv[0]); \
body; \
PRINT_TEST_SUMMARY; \
printf("\n--------------------\n\n"); \
return 0; \
}
#endif

38
rmutil/test_heap.c Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include "heap.h"
#include "assert.h"
int cmp(void *a, void *b) {
int *__a = (int *) a;
int *__b = (int *) b;
return *__a - *__b;
}
int main(int argc, char **argv) {
int myints[] = {10, 20, 30, 5, 15};
Vector *v = NewVector(int, 5);
for (int i = 0; i < 5; i++) {
Vector_Push(v, myints[i]);
}
Make_Heap(v, 0, v->top, cmp);
int n;
Vector_Get(v, 0, &n);
assert(30 == n);
Heap_Pop(v, 0, v->top, cmp);
v->top = 4;
Vector_Get(v, 0, &n);
assert(20 == n);
Vector_Push(v, 99);
Heap_Push(v, 0, v->top, cmp);
Vector_Get(v, 0, &n);
assert(99 == n);
Vector_Free(v);
printf("PASS!\n");
return 0;
}

26
rmutil/test_periodic.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdio.h>
#include <redismodule.h>
#include <unistd.h>
#include "periodic.h"
#include "assert.h"
#include "test.h"
void timerCb(RedisModuleCtx *ctx, void *p) {
int *x = p;
(*x)++;
}
int testPeriodic() {
int x = 0;
struct RMUtilTimer *tm = RMUtil_NewPeriodicTimer(
timerCb, NULL, &x, (struct timespec){.tv_sec = 0, .tv_nsec = 10000000});
sleep(1);
ASSERT_EQUAL(0, RMUtilTimer_Terminate(tm));
ASSERT(x > 0);
ASSERT(x <= 100);
return 0;
}
TEST_MAIN({ TESTFUNC(testPeriodic); });

View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include "assert.h"
#include "priority_queue.h"
int cmp(void* i1, void* i2) {
int *__i1 = (int*) i1;
int *__i2 = (int*) i2;
return *__i1 - *__i2;
}
int main(int argc, char **argv) {
PriorityQueue *pq = NewPriorityQueue(int, 10, cmp);
assert(0 == Priority_Queue_Size(pq));
for (int i = 0; i < 5; i++) {
Priority_Queue_Push(pq, i);
}
assert(5 == Priority_Queue_Size(pq));
Priority_Queue_Pop(pq);
assert(4 == Priority_Queue_Size(pq));
Priority_Queue_Push(pq, 10);
Priority_Queue_Push(pq, 20);
Priority_Queue_Push(pq, 15);
int n;
Priority_Queue_Top(pq, &n);
assert(20 == n);
Priority_Queue_Pop(pq);
Priority_Queue_Top(pq, &n);
assert(15 == n);
Priority_Queue_Free(pq);
printf("PASS!\n");
return 0;
}

70
rmutil/test_util.h Normal file
View File

@ -0,0 +1,70 @@
#ifndef __TEST_UTIL_H__
#define __TEST_UTIL_H__
#include "util.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#define RMUtil_Test(f) \
if (argc < 2 || RMUtil_ArgExists(__STRING(f), argv, argc, 1)) { \
int rc = f(ctx); \
if (rc != REDISMODULE_OK) { \
RedisModule_ReplyWithError(ctx, "Test " __STRING(f) " FAILED"); \
return REDISMODULE_ERR;\
}\
}
#define RMUtil_Assert(expr) if (!(expr)) { fprintf (stderr, "Assertion '%s' Failed\n", __STRING(expr)); return REDISMODULE_ERR; }
#define RMUtil_AssertNullReply(rep) RMUtil_Assert( \
RedisModule_CallReplyType(rep) == REDISMODULE_REPLY_NULL || RedisModule_CreateStringFromCallReply(rep) == NULL)
#define RMUtil_AssertReplyEquals(rep, cstr) RMUtil_Assert( \
RMUtil_StringEquals(RedisModule_CreateStringFromCallReply(rep), RedisModule_CreateString(ctx, cstr, strlen(cstr))) \
)
#
/**
* Create an arg list to pass to a redis command handler manually, based on the format in fmt.
* The accepted format specifiers are:
* c - for null terminated c strings
* s - for RedisModuleString* objects
* l - for longs
*
* Example: RMUtil_MakeArgs(ctx, &argc, "clc", "hello", 1337, "world");
*
* Returns an array of RedisModuleString pointers. The size of the array is store in argcp
*/
RedisModuleString **RMUtil_MakeArgs(RedisModuleCtx *ctx, int *argcp, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
RedisModuleString **argv = calloc(strlen(fmt), sizeof(RedisModuleString*));
int argc = 0;
const char *p = fmt;
while(*p) {
if (*p == 'c') {
char *cstr = va_arg(ap,char*);
argv[argc++] = RedisModule_CreateString(ctx, cstr, strlen(cstr));
} else if (*p == 's') {
argv[argc++] = va_arg(ap,void*);;
} else if (*p == 'l') {
long ll = va_arg(ap,long long);
argv[argc++] = RedisModule_CreateStringFromLongLong(ctx, ll);
} else {
goto fmterr;
}
p++;
}
*argcp = argc;
return argv;
fmterr:
free(argv);
return NULL;
}
#endif

58
rmutil/test_vector.c Normal file
View File

@ -0,0 +1,58 @@
#include "vector.h"
#include <stdio.h>
#include "test.h"
int testVector() {
Vector *v = NewVector(int, 1);
ASSERT(v != NULL);
// Vector_Put(v, 0, 1);
// Vector_Put(v, 1, 3);
for (int i = 0; i < 10; i++) {
Vector_Push(v, i);
}
ASSERT_EQUAL(10, Vector_Size(v));
ASSERT_EQUAL(16, Vector_Cap(v));
for (int i = 0; i < Vector_Size(v); i++) {
int n;
int rc = Vector_Get(v, i, &n);
ASSERT_EQUAL(1, rc);
// printf("%d %d\n", rc, n);
ASSERT_EQUAL(n, i);
}
Vector_Free(v);
v = NewVector(char *, 0);
int N = 4;
char *strings[4] = {"hello", "world", "foo", "bar"};
for (int i = 0; i < N; i++) {
Vector_Push(v, strings[i]);
}
ASSERT_EQUAL(N, Vector_Size(v));
ASSERT(Vector_Cap(v) >= N);
for (int i = 0; i < Vector_Size(v); i++) {
char *x;
int rc = Vector_Get(v, i, &x);
ASSERT_EQUAL(1, rc);
ASSERT_STRING_EQ(x, strings[i]);
}
int rc = Vector_Get(v, 100, NULL);
ASSERT_EQUAL(0, rc);
Vector_Free(v);
return 0;
// Vector_Push(v, "hello");
// Vector_Push(v, "world");
// char *x = NULL;
// int rc = Vector_Getx(v, 0, &x);
// printf("rc: %d got %s\n", rc, x);
}
TEST_MAIN({ TESTFUNC(testVector); });

299
rmutil/util.c Normal file
View File

@ -0,0 +1,299 @@
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <ctype.h>
#include <sys/time.h>
#include <stdarg.h>
#include <limits.h>
#include <string.h>
#define REDISMODULE_EXPERIMENTAL_API
#include <redismodule.h>
#include "util.h"
/**
Check if an argument exists in an argument list (argv,argc), starting at offset.
@return 0 if it doesn't exist, otherwise the offset it exists in
*/
int RMUtil_ArgExists(const char *arg, RedisModuleString **argv, int argc, int offset) {
size_t larg = strlen(arg);
for (; offset < argc; offset++) {
size_t l;
const char *carg = RedisModule_StringPtrLen(argv[offset], &l);
if (l != larg) continue;
if (carg != NULL && strncasecmp(carg, arg, larg) == 0) {
return offset;
}
}
return 0;
}
/**
Check if an argument exists in an argument list (argv,argc)
@return -1 if it doesn't exist, otherwise the offset it exists in
*/
int RMUtil_ArgIndex(const char *arg, RedisModuleString **argv, int argc) {
size_t larg = strlen(arg);
for (int offset = 0; offset < argc; offset++) {
size_t l;
const char *carg = RedisModule_StringPtrLen(argv[offset], &l);
if (l != larg) continue;
if (carg != NULL && strncasecmp(carg, arg, larg) == 0) {
return offset;
}
}
return -1;
}
RMUtilInfo *RMUtil_GetRedisInfo(RedisModuleCtx *ctx) {
RedisModuleCallReply *r = RedisModule_Call(ctx, "INFO", "c", "all");
if (r == NULL || RedisModule_CallReplyType(r) == REDISMODULE_REPLY_ERROR) {
return NULL;
}
int cap = 100; // rough estimate of info lines
RMUtilInfo *info = malloc(sizeof(RMUtilInfo));
info->entries = calloc(cap, sizeof(RMUtilInfoEntry));
int i = 0;
size_t sz;
char *text = (char *)RedisModule_CallReplyStringPtr(r, &sz);
char *line = text;
while (line && line < text + sz) {
char *line = strsep(&text, "\r\n");
if (line == NULL) break;
if (!(*line >= 'a' && *line <= 'z')) { // skip non entry lines
continue;
}
char *key = strsep(&line, ":");
info->entries[i].key = strdup(key);
info->entries[i].val = strdup(line);
i++;
if (i >= cap) {
cap *= 2;
info->entries = realloc(info->entries, cap * sizeof(RMUtilInfoEntry));
}
}
info->numEntries = i;
RedisModule_FreeCallReply(r);
return info;
}
void RMUtilRedisInfo_Free(RMUtilInfo *info) {
for (int i = 0; i < info->numEntries; i++) {
free(info->entries[i].key);
free(info->entries[i].val);
}
free(info->entries);
free(info);
}
int RMUtilInfo_GetInt(RMUtilInfo *info, const char *key, long long *val) {
const char *p = NULL;
if (!RMUtilInfo_GetString(info, key, &p)) {
return 0;
}
*val = strtoll(p, NULL, 10);
if ((errno == ERANGE && (*val == LONG_MAX || *val == LONG_MIN)) || (errno != 0 && *val == 0)) {
*val = -1;
return 0;
}
return 1;
}
int RMUtilInfo_GetString(RMUtilInfo *info, const char *key, const char **str) {
int i;
for (i = 0; i < info->numEntries; i++) {
if (!strcmp(key, info->entries[i].key)) {
*str = info->entries[i].val;
return 1;
}
}
return 0;
}
int RMUtilInfo_GetDouble(RMUtilInfo *info, const char *key, double *d) {
const char *p = NULL;
if (!RMUtilInfo_GetString(info, key, &p)) {
printf("not found %s\n", key);
return 0;
}
*d = strtod(p, NULL);
if ((errno == ERANGE && (*d == HUGE_VAL || *d == -HUGE_VAL)) || (errno != 0 && *d == 0)) {
return 0;
}
return 1;
}
/*
c -- pointer to a Null terminated C string pointer.
b -- pointer to a C buffer, followed by pointer to a size_t for its length
s -- pointer to a RedisModuleString
l -- pointer to Long long integer.
d -- pointer to a Double
* -- do not parse this argument at all
*/
int RMUtil_ParseArgs(RedisModuleString **argv, int argc, int offset, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
int rc = rmutil_vparseArgs(argv, argc, offset, fmt, ap);
va_end(ap);
return rc;
}
// Internal function that parses arguments based on the format described above
int rmutil_vparseArgs(RedisModuleString **argv, int argc, int offset, const char *fmt, va_list ap) {
int i = offset;
char *c = (char *)fmt;
while (*c && i < argc) {
// read c string
if (*c == 'c') {
char **p = va_arg(ap, char **);
*p = (char *)RedisModule_StringPtrLen(argv[i], NULL);
} else if (*c == 'b') {
char **p = va_arg(ap, char **);
size_t *len = va_arg(ap, size_t *);
*p = (char *)RedisModule_StringPtrLen(argv[i], len);
} else if (*c == 's') { // read redis string
RedisModuleString **s = va_arg(ap, void *);
*s = argv[i];
} else if (*c == 'l') { // read long
long long *l = va_arg(ap, long long *);
if (RedisModule_StringToLongLong(argv[i], l) != REDISMODULE_OK) {
return REDISMODULE_ERR;
}
} else if (*c == 'd') { // read double
double *d = va_arg(ap, double *);
if (RedisModule_StringToDouble(argv[i], d) != REDISMODULE_OK) {
return REDISMODULE_ERR;
}
} else if (*c == '*') { // skip current arg
// do nothing
} else {
return REDISMODULE_ERR; // WAT?
}
c++;
i++;
}
// if the format is longer than argc, retun an error
if (*c != 0) {
return REDISMODULE_ERR;
}
return REDISMODULE_OK;
}
int RMUtil_ParseArgsAfter(const char *token, RedisModuleString **argv, int argc, const char *fmt,
...) {
int pos = RMUtil_ArgIndex(token, argv, argc);
if (pos < 0) {
return REDISMODULE_ERR;
}
va_list ap;
va_start(ap, fmt);
int rc = rmutil_vparseArgs(argv, argc, pos + 1, fmt, ap);
va_end(ap);
return rc;
}
RedisModuleCallReply *RedisModule_CallReplyArrayElementByPath(RedisModuleCallReply *rep,
const char *path) {
if (rep == NULL) return NULL;
RedisModuleCallReply *ele = rep;
const char *s = path;
char *e;
long idx;
do {
errno = 0;
idx = strtol(s, &e, 10);
if ((errno == ERANGE && (idx == LONG_MAX || idx == LONG_MIN)) || (errno != 0 && idx == 0) ||
(REDISMODULE_REPLY_ARRAY != RedisModule_CallReplyType(ele)) || (s == e)) {
ele = NULL;
break;
}
s = e;
ele = RedisModule_CallReplyArrayElement(ele, idx - 1);
} while ((ele != NULL) && (*e != '\0'));
return ele;
}
int RedisModule_TryGetValue(RedisModuleKey *key, const RedisModuleType *type, void **out) {
if (key == NULL) {
return RMUTIL_VALUE_MISSING;
}
int keytype = RedisModule_KeyType(key);
if (keytype == REDISMODULE_KEYTYPE_EMPTY) {
return RMUTIL_VALUE_EMPTY;
} else if (keytype == REDISMODULE_KEYTYPE_MODULE && RedisModule_ModuleTypeGetType(key) == type) {
*out = RedisModule_ModuleTypeGetValue(key);
return RMUTIL_VALUE_OK;
} else {
return RMUTIL_VALUE_MISMATCH;
}
}
RedisModuleString **RMUtil_ParseVarArgs(RedisModuleString **argv, int argc, int offset,
const char *keyword, size_t *nargs) {
if (offset > argc) {
return NULL;
}
argv += offset;
argc -= offset;
int ix = RMUtil_ArgIndex(keyword, argv, argc);
if (ix < 0) {
return NULL;
} else if (ix >= argc - 1) {
*nargs = RMUTIL_VARARGS_BADARG;
return argv;
}
argv += (ix + 1);
argc -= (ix + 1);
long long n = 0;
RMUtil_ParseArgs(argv, argc, 0, "l", &n);
if (n > argc - 1 || n < 0) {
*nargs = RMUTIL_VARARGS_BADARG;
return argv;
}
*nargs = n;
return argv + 1;
}
void RMUtil_DefaultAofRewrite(RedisModuleIO *aof, RedisModuleString *key, void *value) {
RedisModuleCtx *ctx = RedisModule_GetThreadSafeContext(NULL);
RedisModuleCallReply *rep = RedisModule_Call(ctx, "DUMP", "s", key);
if (rep != NULL && RedisModule_CallReplyType(rep) == REDISMODULE_REPLY_STRING) {
size_t n;
const char *s = RedisModule_CallReplyStringPtr(rep, &n);
RedisModule_EmitAOF(aof, "RESTORE", "slb", key, 0, s, n);
} else {
RedisModule_Log(RedisModule_GetContextFromIO(aof), "warning", "Failed to emit AOF");
}
if (rep != NULL) {
RedisModule_FreeCallReply(rep);
}
RedisModule_FreeThreadSafeContext(ctx);
}

151
rmutil/util.h Normal file
View File

@ -0,0 +1,151 @@
#ifndef __UTIL_H__
#define __UTIL_H__
#include <redismodule.h>
#include <stdarg.h>
/// make sure the response is not NULL or an error, and if it is sends the error to the client and
/// exit the current function
#define RMUTIL_ASSERT_NOERROR(ctx, r) \
if (r == NULL) { \
return RedisModule_ReplyWithError(ctx, "ERR reply is NULL"); \
} else if (RedisModule_CallReplyType(r) == REDISMODULE_REPLY_ERROR) { \
RedisModule_ReplyWithCallReply(ctx, r); \
return REDISMODULE_ERR; \
}
#define __rmutil_register_cmd(ctx, cmd, f, mode) \
if (RedisModule_CreateCommand(ctx, cmd, f, mode, 1, 1, 1) == REDISMODULE_ERR) \
return REDISMODULE_ERR;
#define RMUtil_RegisterReadCmd(ctx, cmd, f) __rmutil_register_cmd(ctx, cmd, f, "readonly")
#define RMUtil_RegisterWriteCmd(ctx, cmd, f) __rmutil_register_cmd(ctx, cmd, f, "write")
#define RMUtil_RegisterWriteDenyOOMCmd(ctx, cmd, f) __rmutil_register_cmd(ctx, cmd, f, "write deny-oom")
/* RedisModule utilities. */
/** DEPRECATED: Return the offset of an arg if it exists in the arg list, or 0 if it's not there */
int RMUtil_ArgExists(const char *arg, RedisModuleString **argv, int argc, int offset);
/* Same as argExists but returns -1 if not found. Use this, RMUtil_ArgExists is kept for backwards
compatibility. */
int RMUtil_ArgIndex(const char *arg, RedisModuleString **argv, int argc);
/**
Automatically conver the arg list to corresponding variable pointers according to a given format.
You pass it the command arg list and count, the starting offset, a parsing format, and pointers to
the variables.
The format is a string consisting of the following identifiers:
c -- pointer to a Null terminated C string pointer.
s -- pointer to a RedisModuleString
l -- pointer to Long long integer.
d -- pointer to a Double
* -- do not parse this argument at all
Example: If I want to parse args[1], args[2] as a long long and double, I do:
double d;
long long l;
RMUtil_ParseArgs(argv, argc, 1, "ld", &l, &d);
*/
int RMUtil_ParseArgs(RedisModuleString **argv, int argc, int offset, const char *fmt, ...);
/**
Same as RMUtil_ParseArgs, but only parses the arguments after `token`, if it was found.
This is useful for optional stuff like [LIMIT [offset] [limit]]
*/
int RMUtil_ParseArgsAfter(const char *token, RedisModuleString **argv, int argc, const char *fmt,
...);
int rmutil_vparseArgs(RedisModuleString **argv, int argc, int offset, const char *fmt, va_list ap);
#define RMUTIL_VARARGS_BADARG ((size_t)-1)
/**
* Parse arguments in the form of KEYWORD {len} {arg} .. {arg}_len.
* If keyword is present, returns the position within `argv` containing the arguments.
* Returns NULL if the keyword is not found.
* If a parse error has occurred, `nargs` is set to RMUTIL_VARARGS_BADARG, but
* the return value is not NULL.
*/
RedisModuleString **RMUtil_ParseVarArgs(RedisModuleString **argv, int argc, int offset,
const char *keyword, size_t *nargs);
/**
* Default implementation of an AoF rewrite function that simply calls DUMP/RESTORE
* internally. To use this function, pass it as the .aof_rewrite value in
* RedisModuleTypeMethods
*/
void RMUtil_DefaultAofRewrite(RedisModuleIO *aof, RedisModuleString *key, void *value);
// A single key/value entry in a redis info map
typedef struct {
char *key;
char *val;
} RMUtilInfoEntry;
// Representation of INFO command response, as a list of k/v pairs
typedef struct {
RMUtilInfoEntry *entries;
int numEntries;
} RMUtilInfo;
/**
* Get redis INFO result and parse it as RMUtilInfo.
* Returns NULL if something goes wrong.
* The resulting object needs to be freed with RMUtilRedisInfo_Free
*/
RMUtilInfo *RMUtil_GetRedisInfo(RedisModuleCtx *ctx);
/**
* Free an RMUtilInfo object and its entries
*/
void RMUtilRedisInfo_Free(RMUtilInfo *info);
/**
* Get an integer value from an info object. Returns 1 if the value was found and
* is an integer, 0 otherwise. the value is placed in 'val'
*/
int RMUtilInfo_GetInt(RMUtilInfo *info, const char *key, long long *val);
/**
* Get a string value from an info object. The value is placed in str.
* Returns 1 if the key was found, 0 if not
*/
int RMUtilInfo_GetString(RMUtilInfo *info, const char *key, const char **str);
/**
* Get a double value from an info object. Returns 1 if the value was found and is
* a correctly formatted double, 0 otherwise. the value is placed in 'd'
*/
int RMUtilInfo_GetDouble(RMUtilInfo *info, const char *key, double *d);
/*
* Returns a call reply array's element given by a space-delimited path. E.g.,
* the path "1 2 3" will return the 3rd element from the 2 element of the 1st
* element from an array (or NULL if not found)
*/
RedisModuleCallReply *RedisModule_CallReplyArrayElementByPath(RedisModuleCallReply *rep,
const char *path);
/**
* Extract the module type from an opened key.
*/
typedef enum {
RMUTIL_VALUE_OK = 0,
RMUTIL_VALUE_MISSING,
RMUTIL_VALUE_EMPTY,
RMUTIL_VALUE_MISMATCH
} RMUtil_TryGetValueStatus;
/**
* Tries to extract the module-specific type from the value.
* @param key an opened key (may be null)
* @param type the pointer to the type to match to
* @param[out] out if the value is present, will be set to it.
* @return a value in the @ref RMUtil_TryGetValueStatus enum.
*/
int RedisModule_TryGetValue(RedisModuleKey *key, const RedisModuleType *type, void **out);
#endif

88
rmutil/vector.c Normal file
View File

@ -0,0 +1,88 @@
#include "vector.h"
#include <stdio.h>
inline int __vector_PushPtr(Vector *v, void *elem) {
if (v->top == v->cap) {
Vector_Resize(v, v->cap ? v->cap * 2 : 1);
}
__vector_PutPtr(v, v->top, elem);
return v->top;
}
inline int Vector_Get(Vector *v, size_t pos, void *ptr) {
// return 0 if pos is out of bounds
if (pos >= v->top) {
return 0;
}
memcpy(ptr, v->data + (pos * v->elemSize), v->elemSize);
return 1;
}
/* Get the element at the end of the vector, decreasing the size by one */
inline int Vector_Pop(Vector *v, void *ptr) {
if (v->top > 0) {
if (ptr != NULL) {
Vector_Get(v, v->top - 1, ptr);
}
v->top--;
return 1;
}
return 0;
}
inline int __vector_PutPtr(Vector *v, size_t pos, void *elem) {
// resize if pos is out of bounds
if (pos >= v->cap) {
Vector_Resize(v, pos + 1);
}
if (elem) {
memcpy(v->data + pos * v->elemSize, elem, v->elemSize);
} else {
memset(v->data + pos * v->elemSize, 0, v->elemSize);
}
// move the end offset to pos if we grew
if (pos >= v->top) {
v->top = pos + 1;
}
return 1;
}
int Vector_Resize(Vector *v, size_t newcap) {
int oldcap = v->cap;
v->cap = newcap;
v->data = realloc(v->data, v->cap * v->elemSize);
// If we grew:
// put all zeros at the newly realloc'd part of the vector
if (newcap > oldcap) {
int offset = oldcap * v->elemSize;
memset(v->data + offset, 0, v->cap * v->elemSize - offset);
}
return v->cap;
}
Vector *__newVectorSize(size_t elemSize, size_t cap) {
Vector *vec = malloc(sizeof(Vector));
vec->data = calloc(cap, elemSize);
vec->top = 0;
vec->elemSize = elemSize;
vec->cap = cap;
return vec;
}
void Vector_Free(Vector *v) {
free(v->data);
free(v);
}
/* return the used size of the vector, regardless of capacity */
inline int Vector_Size(Vector *v) { return v->top; }
/* return the actual capacity */
inline int Vector_Cap(Vector *v) { return v->cap; }

73
rmutil/vector.h Normal file
View File

@ -0,0 +1,73 @@
#ifndef __VECTOR_H__
#define __VECTOR_H__
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
/*
* Generic resizable vector that can be used if you just want to store stuff
* temporarily.
* Works like C++ std::vector with an underlying resizable buffer
*/
typedef struct {
char *data;
size_t elemSize;
size_t cap;
size_t top;
} Vector;
/* Create a new vector with element size. This should generally be used
* internall by the NewVector macro */
Vector *__newVectorSize(size_t elemSize, size_t cap);
// Put a pointer in the vector. To be used internall by the library
int __vector_PutPtr(Vector *v, size_t pos, void *elem);
/*
* Create a new vector for a given type and a given capacity.
* e.g. NewVector(int, 0) - empty vector of ints
*/
#define NewVector(type, cap) __newVectorSize(sizeof(type), cap)
/*
* get the element at index pos. The value is copied in to ptr. If pos is outside
* the vector capacity, we return 0
* otherwise 1
*/
int Vector_Get(Vector *v, size_t pos, void *ptr);
/* Get the element at the end of the vector, decreasing the size by one */
int Vector_Pop(Vector *v, void *ptr);
//#define Vector_Getx(v, pos, ptr) pos < v->cap ? 1 : 0; *ptr =
//*(typeof(ptr))(v->data + v->elemSize*pos)
/*
* Put an element at pos.
* Note: If pos is outside the vector capacity, we resize it accordingly
*/
#define Vector_Put(v, pos, elem) __vector_PutPtr(v, pos, elem ? &(typeof(elem)){elem} : NULL)
/* Push an element at the end of v, resizing it if needed. This macro wraps
* __vector_PushPtr */
#define Vector_Push(v, elem) __vector_PushPtr(v, elem ? &(typeof(elem)){elem} : NULL)
int __vector_PushPtr(Vector *v, void *elem);
/* resize capacity of v */
int Vector_Resize(Vector *v, size_t newcap);
/* return the used size of the vector, regardless of capacity */
int Vector_Size(Vector *v);
/* return the actual capacity */
int Vector_Cap(Vector *v);
/* free the vector and the underlying data. Does not release its elements if
* they are pointers*/
void Vector_Free(Vector *v);
int __vecotr_PutPtr(Vector *v, size_t pos, void *elem);
#endif

146
source/module.c Normal file
View File

@ -0,0 +1,146 @@
#include "redismodule.h"
#include "../rmutil/util.h"
#include "../rmutil/strings.h"
#include "../rmutil/test_util.h"
/* EXAMPLE.PARSE [SUM <x> <y>] | [PROD <x> <y>]
* Demonstrates the automatic arg parsing utility.
* If the command receives "SUM <x> <y>" it returns their sum
* If it receives "PROD <x> <y>" it returns their product
*/
int ParseCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
// we must have at least 4 args
if (argc < 4) {
return RedisModule_WrongArity(ctx);
}
// init auto memory for created strings
RedisModule_AutoMemory(ctx);
long long x, y;
// If we got SUM - return the sum of 2 consecutive arguments
if (RMUtil_ParseArgsAfter("SUM", argv, argc, "ll", &x, &y) ==
REDISMODULE_OK) {
RedisModule_ReplyWithLongLong(ctx, x + y);
return REDISMODULE_OK;
}
// If we got PROD - return the product of 2 consecutive arguments
if (RMUtil_ParseArgsAfter("PROD", argv, argc, "ll", &x, &y) ==
REDISMODULE_OK) {
RedisModule_ReplyWithLongLong(ctx, x * y);
return REDISMODULE_OK;
}
// something is fishy...
RedisModule_ReplyWithError(ctx, "Invalid arguments");
return REDISMODULE_ERR;
}
/*
* example.HGETSET <key> <element> <value>
* Atomically set a value in a HASH key to <value> and return its value before
* the HSET.
*
* Basically atomic HGET + HSET
*/
int HGetSetCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
// we need EXACTLY 4 arguments
if (argc != 4) {
return RedisModule_WrongArity(ctx);
}
RedisModule_AutoMemory(ctx);
// open the key and make sure it's indeed a HASH and not empty
RedisModuleKey *key =
RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE);
if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_HASH &&
RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_EMPTY) {
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
}
// get the current value of the hash element
RedisModuleCallReply *rep =
RedisModule_Call(ctx, "HGET", "ss", argv[1], argv[2]);
RMUTIL_ASSERT_NOERROR(ctx, rep);
// set the new value of the element
RedisModuleCallReply *srep =
RedisModule_Call(ctx, "HSET", "sss", argv[1], argv[2], argv[3]);
RMUTIL_ASSERT_NOERROR(ctx, srep);
// if the value was null before - we just return null
if (RedisModule_CallReplyType(rep) == REDISMODULE_REPLY_NULL) {
RedisModule_ReplyWithNull(ctx);
return REDISMODULE_OK;
}
// forward the HGET reply to the client
RedisModule_ReplyWithCallReply(ctx, rep);
return REDISMODULE_OK;
}
// Test the the PARSE command
int testParse(RedisModuleCtx *ctx) {
RedisModuleCallReply *r =
RedisModule_Call(ctx, "example.parse", "ccc", "SUM", "5", "2");
RMUtil_Assert(RedisModule_CallReplyType(r) == REDISMODULE_REPLY_INTEGER);
RMUtil_AssertReplyEquals(r, "7");
r = RedisModule_Call(ctx, "example.parse", "ccc", "PROD", "5", "2");
RMUtil_Assert(RedisModule_CallReplyType(r) == REDISMODULE_REPLY_INTEGER);
RMUtil_AssertReplyEquals(r, "10");
return 0;
}
// test the HGETSET command
int testHgetSet(RedisModuleCtx *ctx) {
RedisModuleCallReply *r =
RedisModule_Call(ctx, "example.hgetset", "ccc", "foo", "bar", "baz");
RMUtil_Assert(RedisModule_CallReplyType(r) != REDISMODULE_REPLY_ERROR);
r = RedisModule_Call(ctx, "example.hgetset", "ccc", "foo", "bar", "bag");
RMUtil_Assert(RedisModule_CallReplyType(r) == REDISMODULE_REPLY_STRING);
RMUtil_AssertReplyEquals(r, "baz");
r = RedisModule_Call(ctx, "example.hgetset", "ccc", "foo", "bar", "bang");
RMUtil_AssertReplyEquals(r, "bag");
return 0;
}
// Unit test entry point for the module
int TestModule(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
RedisModule_AutoMemory(ctx);
RMUtil_Test(testParse);
RMUtil_Test(testHgetSet);
RedisModule_ReplyWithSimpleString(ctx, "PASS");
return REDISMODULE_OK;
}
int RedisModule_OnLoad(RedisModuleCtx *ctx) {
// Register the module itself
if (RedisModule_Init(ctx, "example", 1, REDISMODULE_APIVER_1) ==
REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
// register example.parse - the default registration syntax
if (RedisModule_CreateCommand(ctx, "example.parse", ParseCommand, "readonly",
1, 1, 1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
// register example.hgetset - using the shortened utility registration macro
RMUtil_RegisterWriteCmd(ctx, "example.hgetset", HGetSetCommand);
// register the unit test
RMUtil_RegisterWriteCmd(ctx, "example.test", TestModule);
return REDISMODULE_OK;
}