Set up clang-format github action (#538)

Setup clang-format GitHub action to ensure coding style consistency
---------

Signed-off-by: Ping Xie <pingxie@google.com>
This commit is contained in:
Ping Xie 2024-05-28 09:27:51 -07:00 committed by GitHub
parent 4e44f5aae9
commit 84157890fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
36 changed files with 1030 additions and 1120 deletions

48
.github/workflows/clang-format.yml vendored Normal file
View File

@ -0,0 +1,48 @@
name: Clang Format Check
on:
pull_request:
paths:
- 'src/**'
jobs:
clang-format-check:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Set up Clang
run: |
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install software-properties-common -y
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | gpg --dearmor | sudo tee /usr/share/keyrings/llvm-toolchain.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/llvm-toolchain.gpg] http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-18 main" | sudo tee /etc/apt/sources.list.d/llvm.list
sudo apt-get update -y
sudo apt-get install clang-format-18 -y
- name: Run clang-format
id: clang-format
run: |
# Run clang-format and capture the diff
cd src
shopt -s globstar
clang-format-18 -i **/*.c **/*.h
# Capture the diff output
DIFF=$(git diff)
if [ ! -z "$DIFF" ]; then
# Encode the diff in Base64 to ensure it's handled as a single line
ENCODED_DIFF=$(echo "$DIFF" | base64 -w 0)
echo "diff=$ENCODED_DIFF" >> $GITHUB_OUTPUT
fi
shell: bash
- name: Check for formatting changes
if: ${{ steps.clang-format.outputs.diff }}
run: |
echo "Code is not formatted correctly. Here is the diff:"
# Decode the Base64 diff to display it
echo "${{ steps.clang-format.outputs.diff }}" | base64 --decode
exit 1
shell: bash

View File

@ -2428,20 +2428,21 @@ sds ACLLoadFromFile(const char *filename) {
client *c = listNodeValue(ln);
user *original = c->user;
list *channels = NULL;
user *new = ACLGetUserByName(c->user->name, sdslen(c->user->name));
if (new && user_channels) {
if (!raxFind(user_channels, (unsigned char *)(new->name), sdslen(new->name), (void **)&channels)) {
channels = getUpcomingChannelList(new, original);
raxInsert(user_channels, (unsigned char *)(new->name), sdslen(new->name), channels, NULL);
user *new_user = ACLGetUserByName(c->user->name, sdslen(c->user->name));
if (new_user && user_channels) {
if (!raxFind(user_channels, (unsigned char *)(new_user->name), sdslen(new_user->name),
(void **)&channels)) {
channels = getUpcomingChannelList(new_user, original);
raxInsert(user_channels, (unsigned char *)(new_user->name), sdslen(new_user->name), channels, NULL);
}
}
/* When the new channel list is NULL, it means the new user's channel list is a superset of the old user's
* list. */
if (!new || (channels && ACLShouldKillPubsubClient(c, channels))) {
if (!new_user || (channels && ACLShouldKillPubsubClient(c, channels))) {
freeClient(c);
continue;
}
c->user = new;
c->user = new_user;
}
if (user_channels) raxFreeWithCallback(user_channels, (void (*)(void *))listRelease);

View File

@ -987,8 +987,7 @@ int startAppendOnly(void) {
/* If AOF fsync error in bio job, we just ignore it and log the event. */
int aof_bio_fsync_status = atomic_load_explicit(&server.aof_bio_fsync_status, memory_order_relaxed);
if (aof_bio_fsync_status == C_ERR) {
serverLog(LL_WARNING,
"AOF reopen, just ignore the AOF fsync error in bio job");
serverLog(LL_WARNING, "AOF reopen, just ignore the AOF fsync error in bio job");
atomic_store_explicit(&server.aof_bio_fsync_status, C_OK, memory_order_relaxed);
}
@ -1245,8 +1244,7 @@ try_fsync:
server.aof_last_incr_fsync_offset = server.aof_last_incr_size;
server.aof_last_fsync = server.mstime;
atomic_store_explicit(&server.fsynced_reploff_pending, server.master_repl_offset, memory_order_relaxed);
} else if (server.aof_fsync == AOF_FSYNC_EVERYSEC &&
server.mstime - server.aof_last_fsync >= 1000) {
} else if (server.aof_fsync == AOF_FSYNC_EVERYSEC && server.mstime - server.aof_last_fsync >= 1000) {
if (!sync_in_progress) {
aof_background_fsync(server.aof_fd);
server.aof_last_incr_fsync_offset = server.aof_last_incr_size;
@ -2648,7 +2646,8 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal) {
/* Update the fsynced replication offset that just now become valid.
* This could either be the one we took in startAppendOnly, or a
* newer one set by the bio thread. */
long long fsynced_reploff_pending = atomic_load_explicit(&server.fsynced_reploff_pending, memory_order_relaxed);
long long fsynced_reploff_pending =
atomic_load_explicit(&server.fsynced_reploff_pending, memory_order_relaxed);
server.fsynced_reploff = fsynced_reploff_pending;
}

View File

@ -257,9 +257,7 @@ void *bioProcessBackgroundJobs(void *arg) {
/* The fd may be closed by main thread and reused for another
* socket, pipe, or file. We just ignore these errno because
* aof fsync did not really fail. */
if (valkey_fsync(job->fd_args.fd) == -1 &&
errno != EBADF && errno != EINVAL)
{
if (valkey_fsync(job->fd_args.fd) == -1 && errno != EBADF && errno != EINVAL) {
int last_status = atomic_load_explicit(&server.aof_bio_fsync_status, memory_order_relaxed);
atomic_store_explicit(&server.aof_bio_fsync_errno, errno, memory_order_relaxed);

View File

@ -13,8 +13,8 @@ static _Atomic size_t lazyfreed_objects = 0;
void lazyfreeFreeObject(void *args[]) {
robj *o = (robj *)args[0];
decrRefCount(o);
atomic_fetch_sub_explicit(&lazyfree_objects,1,memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects,1,memory_order_relaxed);
atomic_fetch_sub_explicit(&lazyfree_objects, 1, memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects, 1, memory_order_relaxed);
}
/* Release a database from the lazyfree thread. The 'db' pointer is the
@ -27,8 +27,8 @@ void lazyfreeFreeDatabase(void *args[]) {
size_t numkeys = kvstoreSize(da1);
kvstoreRelease(da1);
kvstoreRelease(da2);
atomic_fetch_sub_explicit(&lazyfree_objects,numkeys,memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects,numkeys,memory_order_relaxed);
atomic_fetch_sub_explicit(&lazyfree_objects, numkeys, memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects, numkeys, memory_order_relaxed);
}
/* Release the key tracking table. */
@ -36,8 +36,8 @@ void lazyFreeTrackingTable(void *args[]) {
rax *rt = args[0];
size_t len = rt->numele;
freeTrackingRadixTree(rt);
atomic_fetch_sub_explicit(&lazyfree_objects,len,memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects,len,memory_order_relaxed);
atomic_fetch_sub_explicit(&lazyfree_objects, len, memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects, len, memory_order_relaxed);
}
/* Release the error stats rax tree. */
@ -45,8 +45,8 @@ void lazyFreeErrors(void *args[]) {
rax *errors = args[0];
size_t len = errors->numele;
raxFreeWithCallback(errors, zfree);
atomic_fetch_sub_explicit(&lazyfree_objects,len,memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects,len,memory_order_relaxed);
atomic_fetch_sub_explicit(&lazyfree_objects, len, memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects, len, memory_order_relaxed);
}
/* Release the lua_scripts dict. */
@ -56,8 +56,8 @@ void lazyFreeLuaScripts(void *args[]) {
lua_State *lua = args[2];
long long len = dictSize(lua_scripts);
freeLuaScriptsSync(lua_scripts, lua_scripts_lru_list, lua);
atomic_fetch_sub_explicit(&lazyfree_objects,len,memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects,len,memory_order_relaxed);
atomic_fetch_sub_explicit(&lazyfree_objects, len, memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects, len, memory_order_relaxed);
}
/* Release the functions ctx. */
@ -65,8 +65,8 @@ void lazyFreeFunctionsCtx(void *args[]) {
functionsLibCtx *functions_lib_ctx = args[0];
size_t len = functionsLibCtxFunctionsLen(functions_lib_ctx);
functionsLibCtxFree(functions_lib_ctx);
atomic_fetch_sub_explicit(&lazyfree_objects,len,memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects,len,memory_order_relaxed);
atomic_fetch_sub_explicit(&lazyfree_objects, len, memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects, len, memory_order_relaxed);
}
/* Release replication backlog referencing memory. */
@ -77,24 +77,24 @@ void lazyFreeReplicationBacklogRefMem(void *args[]) {
len += raxSize(index);
listRelease(blocks);
raxFree(index);
atomic_fetch_sub_explicit(&lazyfree_objects,len,memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects,len,memory_order_relaxed);
atomic_fetch_sub_explicit(&lazyfree_objects, len, memory_order_relaxed);
atomic_fetch_add_explicit(&lazyfreed_objects, len, memory_order_relaxed);
}
/* Return the number of currently pending objects to free. */
size_t lazyfreeGetPendingObjectsCount(void) {
size_t aux = atomic_load_explicit(&lazyfree_objects,memory_order_relaxed);
size_t aux = atomic_load_explicit(&lazyfree_objects, memory_order_relaxed);
return aux;
}
/* Return the number of objects that have been freed. */
size_t lazyfreeGetFreedObjectsCount(void) {
size_t aux = atomic_load_explicit(&lazyfreed_objects,memory_order_relaxed);
size_t aux = atomic_load_explicit(&lazyfreed_objects, memory_order_relaxed);
return aux;
}
void lazyfreeResetStats(void) {
atomic_store_explicit(&lazyfreed_objects,0,memory_order_relaxed);
atomic_store_explicit(&lazyfreed_objects, 0, memory_order_relaxed);
}
/* Return the amount of work needed in order to free an object.
@ -174,8 +174,8 @@ void freeObjAsync(robj *key, robj *obj, int dbid) {
* of parts of the server core may call incrRefCount() to protect
* objects, and then call dbDelete(). */
if (free_effort > LAZYFREE_THRESHOLD && obj->refcount == 1) {
atomic_fetch_add_explicit(&lazyfree_objects,1,memory_order_relaxed);
bioCreateLazyFreeJob(lazyfreeFreeObject,1,obj);
atomic_fetch_add_explicit(&lazyfree_objects, 1, memory_order_relaxed);
bioCreateLazyFreeJob(lazyfreeFreeObject, 1, obj);
} else {
decrRefCount(obj);
}
@ -203,8 +203,8 @@ void emptyDbAsync(serverDb *db) {
void freeTrackingRadixTreeAsync(rax *tracking) {
/* Because this rax has only keys and no values so we use numnodes. */
if (tracking->numnodes > LAZYFREE_THRESHOLD) {
atomic_fetch_add_explicit(&lazyfree_objects,tracking->numele,memory_order_relaxed);
bioCreateLazyFreeJob(lazyFreeTrackingTable,1,tracking);
atomic_fetch_add_explicit(&lazyfree_objects, tracking->numele, memory_order_relaxed);
bioCreateLazyFreeJob(lazyFreeTrackingTable, 1, tracking);
} else {
freeTrackingRadixTree(tracking);
}
@ -215,8 +215,8 @@ void freeTrackingRadixTreeAsync(rax *tracking) {
void freeErrorsRadixTreeAsync(rax *errors) {
/* Because this rax has only keys and no values so we use numnodes. */
if (errors->numnodes > LAZYFREE_THRESHOLD) {
atomic_fetch_add_explicit(&lazyfree_objects,errors->numele,memory_order_relaxed);
bioCreateLazyFreeJob(lazyFreeErrors,1,errors);
atomic_fetch_add_explicit(&lazyfree_objects, errors->numele, memory_order_relaxed);
bioCreateLazyFreeJob(lazyFreeErrors, 1, errors);
} else {
raxFreeWithCallback(errors, zfree);
}
@ -226,8 +226,8 @@ void freeErrorsRadixTreeAsync(rax *errors) {
* Close lua interpreter, if there are a lot of lua scripts, close it in async way. */
void freeLuaScriptsAsync(dict *lua_scripts, list *lua_scripts_lru_list, lua_State *lua) {
if (dictSize(lua_scripts) > LAZYFREE_THRESHOLD) {
atomic_fetch_add_explicit(&lazyfree_objects,dictSize(lua_scripts),memory_order_relaxed);
bioCreateLazyFreeJob(lazyFreeLuaScripts,3,lua_scripts,lua_scripts_lru_list,lua);
atomic_fetch_add_explicit(&lazyfree_objects, dictSize(lua_scripts), memory_order_relaxed);
bioCreateLazyFreeJob(lazyFreeLuaScripts, 3, lua_scripts, lua_scripts_lru_list, lua);
} else {
freeLuaScriptsSync(lua_scripts, lua_scripts_lru_list, lua);
}
@ -236,8 +236,9 @@ void freeLuaScriptsAsync(dict *lua_scripts, list *lua_scripts_lru_list, lua_Stat
/* Free functions ctx, if the functions ctx contains enough functions, free it in async way. */
void freeFunctionsAsync(functionsLibCtx *functions_lib_ctx) {
if (functionsLibCtxFunctionsLen(functions_lib_ctx) > LAZYFREE_THRESHOLD) {
atomic_fetch_add_explicit(&lazyfree_objects,functionsLibCtxFunctionsLen(functions_lib_ctx),memory_order_relaxed);
bioCreateLazyFreeJob(lazyFreeFunctionsCtx,1,functions_lib_ctx);
atomic_fetch_add_explicit(&lazyfree_objects, functionsLibCtxFunctionsLen(functions_lib_ctx),
memory_order_relaxed);
bioCreateLazyFreeJob(lazyFreeFunctionsCtx, 1, functions_lib_ctx);
} else {
functionsLibCtxFree(functions_lib_ctx);
}
@ -245,11 +246,9 @@ void freeFunctionsAsync(functionsLibCtx *functions_lib_ctx) {
/* Free replication backlog referencing buffer blocks and rax index. */
void freeReplicationBacklogRefMemAsync(list *blocks, rax *index) {
if (listLength(blocks) > LAZYFREE_THRESHOLD ||
raxSize(index) > LAZYFREE_THRESHOLD)
{
atomic_fetch_add_explicit(&lazyfree_objects,listLength(blocks)+raxSize(index),memory_order_relaxed);
bioCreateLazyFreeJob(lazyFreeReplicationBacklogRefMem,2,blocks,index);
if (listLength(blocks) > LAZYFREE_THRESHOLD || raxSize(index) > LAZYFREE_THRESHOLD) {
atomic_fetch_add_explicit(&lazyfree_objects, listLength(blocks) + raxSize(index), memory_order_relaxed);
bioCreateLazyFreeJob(lazyFreeReplicationBacklogRefMem, 2, blocks, index);
} else {
listRelease(blocks);
raxFree(index);

View File

@ -781,12 +781,12 @@ unsigned char *lpInsert(unsigned char *lp,
unsigned char backlen[LP_MAX_BACKLEN_SIZE];
uint64_t enclen; /* The length of the encoded element. */
int delete = (elestr == NULL && eleint == NULL);
int del_ele = (elestr == NULL && eleint == NULL);
/* when deletion, it is conceptually replacing the element with a
* zero-length element. So whatever we get passed as 'where', set
* it to LP_REPLACE. */
if (delete) where = LP_REPLACE;
if (del_ele) where = LP_REPLACE;
/* If we need to insert after the current element, we just jump to the
* next element (that could be the EOF one) and handle the case of
@ -825,7 +825,7 @@ unsigned char *lpInsert(unsigned char *lp,
/* We need to also encode the backward-parsable length of the element
* and append it to the end: this allows to traverse the listpack from
* the end to the start. */
unsigned long backlen_size = (!delete) ? lpEncodeBacklen(backlen, enclen) : 0;
unsigned long backlen_size = (!del_ele) ? lpEncodeBacklen(backlen, enclen) : 0;
uint64_t old_listpack_bytes = lpGetTotalBytes(lp);
uint32_t replaced_len = 0;
if (where == LP_REPLACE) {
@ -870,9 +870,9 @@ unsigned char *lpInsert(unsigned char *lp,
*newp = dst;
/* In case of deletion, set 'newp' to NULL if the next element is
* the EOF element. */
if (delete && dst[0] == LP_EOF) *newp = NULL;
if (del_ele && dst[0] == LP_EOF) *newp = NULL;
}
if (!delete) {
if (!del_ele) {
if (enctype == LP_ENCODING_INT) {
memcpy(dst, eleint, enclen);
} else if (elestr) {
@ -886,10 +886,10 @@ unsigned char *lpInsert(unsigned char *lp,
}
/* Update header. */
if (where != LP_REPLACE || delete) {
if (where != LP_REPLACE || del_ele) {
uint32_t num_elements = lpGetNumElements(lp);
if (num_elements != LP_HDR_NUMELE_UNKNOWN) {
if (!delete)
if (!del_ele)
lpSetNumElements(lp, num_elements + 1);
else
lpSetNumElements(lp, num_elements - 1);

View File

@ -39,7 +39,7 @@
static ValkeyModuleUser *global;
static uint64_t global_auth_client_id = 0;
/* HELLOACL.REVOKE
/* HELLOACL.REVOKE
* Synchronously revoke access from a user. */
int RevokeCommand_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
VALKEYMODULE_NOT_USED(argv);
@ -49,11 +49,11 @@ int RevokeCommand_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv,
ValkeyModule_DeauthenticateAndCloseClient(ctx, global_auth_client_id);
return ValkeyModule_ReplyWithSimpleString(ctx, "OK");
} else {
return ValkeyModule_ReplyWithError(ctx, "Global user currently not used");
return ValkeyModule_ReplyWithError(ctx, "Global user currently not used");
}
}
/* HELLOACL.RESET
/* HELLOACL.RESET
* Synchronously delete and re-create a module user. */
int ResetCommand_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
VALKEYMODULE_NOT_USED(argv);
@ -68,7 +68,7 @@ int ResetCommand_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv,
return ValkeyModule_ReplyWithSimpleString(ctx, "OK");
}
/* Callback handler for user changes, use this to notify a module of
/* Callback handler for user changes, use this to notify a module of
* changes to users authenticated by the module */
void HelloACL_UserChanged(uint64_t client_id, void *privdata) {
VALKEYMODULE_NOT_USED(privdata);
@ -76,14 +76,14 @@ void HelloACL_UserChanged(uint64_t client_id, void *privdata) {
global_auth_client_id = 0;
}
/* HELLOACL.AUTHGLOBAL
/* HELLOACL.AUTHGLOBAL
* Synchronously assigns a module user to the current context. */
int AuthGlobalCommand_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
if (global_auth_client_id) {
return ValkeyModule_ReplyWithError(ctx, "Global user currently used");
return ValkeyModule_ReplyWithError(ctx, "Global user currently used");
}
ValkeyModule_AuthenticateClientWithUser(ctx, global, HelloACL_UserChanged, NULL, &global_auth_client_id);
@ -102,9 +102,8 @@ int HelloACL_Reply(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
ValkeyModuleString *user_string = ValkeyModule_GetBlockedClientPrivateData(ctx);
const char *name = ValkeyModule_StringPtrLen(user_string, &length);
if (ValkeyModule_AuthenticateClientWithACLUser(ctx, name, length, NULL, NULL, NULL) ==
VALKEYMODULE_ERR) {
return ValkeyModule_ReplyWithError(ctx, "Invalid Username or password");
if (ValkeyModule_AuthenticateClientWithACLUser(ctx, name, length, NULL, NULL, NULL) == VALKEYMODULE_ERR) {
return ValkeyModule_ReplyWithError(ctx, "Invalid Username or password");
}
return ValkeyModule_ReplyWithSimpleString(ctx, "OK");
}
@ -129,20 +128,21 @@ void *HelloACL_ThreadMain(void *args) {
ValkeyModuleString *user = targs[1];
ValkeyModule_Free(targs);
ValkeyModule_UnblockClient(bc,user);
ValkeyModule_UnblockClient(bc, user);
return NULL;
}
/* HELLOACL.AUTHASYNC
/* HELLOACL.AUTHASYNC
* Asynchronously assigns an ACL user to the current context. */
int AuthAsyncCommand_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (argc != 2) return ValkeyModule_WrongArity(ctx);
pthread_t tid;
ValkeyModuleBlockedClient *bc = ValkeyModule_BlockClient(ctx, HelloACL_Reply, HelloACL_Timeout, HelloACL_FreeData, TIMEOUT_TIME);
ValkeyModuleBlockedClient *bc =
ValkeyModule_BlockClient(ctx, HelloACL_Reply, HelloACL_Timeout, HelloACL_FreeData, TIMEOUT_TIME);
void **targs = ValkeyModule_Alloc(sizeof(void*)*2);
void **targs = ValkeyModule_Alloc(sizeof(void *) * 2);
targs[0] = bc;
targs[1] = ValkeyModule_CreateStringFromString(NULL, argv[1]);
@ -160,23 +160,21 @@ int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int arg
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
if (ValkeyModule_Init(ctx,"helloacl",1,VALKEYMODULE_APIVER_1)
== VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_Init(ctx, "helloacl", 1, VALKEYMODULE_APIVER_1) == VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"helloacl.reset",
ResetCommand_ValkeyCommand,"",0,0,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "helloacl.reset", ResetCommand_ValkeyCommand, "", 0, 0, 0) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"helloacl.revoke",
RevokeCommand_ValkeyCommand,"",0,0,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "helloacl.revoke", RevokeCommand_ValkeyCommand, "", 0, 0, 0) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"helloacl.authglobal",
AuthGlobalCommand_ValkeyCommand,"no-auth",0,0,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "helloacl.authglobal", AuthGlobalCommand_ValkeyCommand, "no-auth", 0, 0, 0) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"helloacl.authasync",
AuthAsyncCommand_ValkeyCommand,"no-auth",0,0,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "helloacl.authasync", AuthAsyncCommand_ValkeyCommand, "no-auth", 0, 0, 0) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
global = ValkeyModule_CreateModuleUser("global");

View File

@ -42,14 +42,14 @@ int HelloBlock_Reply(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
int *myint = ValkeyModule_GetBlockedClientPrivateData(ctx);
return ValkeyModule_ReplyWithLongLong(ctx,*myint);
return ValkeyModule_ReplyWithLongLong(ctx, *myint);
}
/* Timeout callback for blocking command HELLO.BLOCK */
int HelloBlock_Timeout(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
return ValkeyModule_ReplyWithSimpleString(ctx,"Request timedout");
return ValkeyModule_ReplyWithSimpleString(ctx, "Request timedout");
}
/* Private data freeing callback for HELLO.BLOCK command. */
@ -69,7 +69,7 @@ void *HelloBlock_ThreadMain(void *arg) {
sleep(delay);
int *r = ValkeyModule_Alloc(sizeof(int));
*r = rand();
ValkeyModule_UnblockClient(bc,r);
ValkeyModule_UnblockClient(bc, r);
return NULL;
}
@ -82,8 +82,7 @@ void *HelloBlock_ThreadMain(void *arg) {
* amount of seconds with a while loop calling sleep(1), so that once we
* detect the client disconnection, we can terminate the thread ASAP. */
void HelloBlock_Disconnected(ValkeyModuleCtx *ctx, ValkeyModuleBlockedClient *bc) {
ValkeyModule_Log(ctx,"warning","Blocked client %p disconnected!",
(void*)bc);
ValkeyModule_Log(ctx, "warning", "Blocked client %p disconnected!", (void *)bc);
/* Here you should cleanup your state / threads, and if possible
* call ValkeyModule_UnblockClient(), or notify the thread that will
@ -98,32 +97,33 @@ int HelloBlock_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, in
long long delay;
long long timeout;
if (ValkeyModule_StringToLongLong(argv[1],&delay) != VALKEYMODULE_OK) {
return ValkeyModule_ReplyWithError(ctx,"ERR invalid count");
if (ValkeyModule_StringToLongLong(argv[1], &delay) != VALKEYMODULE_OK) {
return ValkeyModule_ReplyWithError(ctx, "ERR invalid count");
}
if (ValkeyModule_StringToLongLong(argv[2],&timeout) != VALKEYMODULE_OK) {
return ValkeyModule_ReplyWithError(ctx,"ERR invalid count");
if (ValkeyModule_StringToLongLong(argv[2], &timeout) != VALKEYMODULE_OK) {
return ValkeyModule_ReplyWithError(ctx, "ERR invalid count");
}
pthread_t tid;
ValkeyModuleBlockedClient *bc = ValkeyModule_BlockClient(ctx,HelloBlock_Reply,HelloBlock_Timeout,HelloBlock_FreeData,timeout);
ValkeyModuleBlockedClient *bc =
ValkeyModule_BlockClient(ctx, HelloBlock_Reply, HelloBlock_Timeout, HelloBlock_FreeData, timeout);
/* Here we set a disconnection handler, however since this module will
* block in sleep() in a thread, there is not much we can do in the
* callback, so this is just to show you the API. */
ValkeyModule_SetDisconnectCallback(bc,HelloBlock_Disconnected);
ValkeyModule_SetDisconnectCallback(bc, HelloBlock_Disconnected);
/* Now that we setup a blocking client, we need to pass the control
* to the thread. However we need to pass arguments to the thread:
* the delay and a reference to the blocked client handle. */
void **targ = ValkeyModule_Alloc(sizeof(void*)*2);
void **targ = ValkeyModule_Alloc(sizeof(void *) * 2);
targ[0] = bc;
targ[1] = (void*)(unsigned long) delay;
targ[1] = (void *)(unsigned long)delay;
if (pthread_create(&tid,NULL,HelloBlock_ThreadMain,targ) != 0) {
if (pthread_create(&tid, NULL, HelloBlock_ThreadMain, targ) != 0) {
ValkeyModule_AbortBlock(bc);
return ValkeyModule_ReplyWithError(ctx,"-ERR Can't start thread");
return ValkeyModule_ReplyWithError(ctx, "-ERR Can't start thread");
}
return VALKEYMODULE_OK;
}
@ -141,35 +141,31 @@ void *HelloKeys_ThreadMain(void *arg) {
long long cursor = 0;
size_t replylen = 0;
ValkeyModule_ReplyWithArray(ctx,VALKEYMODULE_POSTPONED_LEN);
ValkeyModule_ReplyWithArray(ctx, VALKEYMODULE_POSTPONED_LEN);
do {
ValkeyModule_ThreadSafeContextLock(ctx);
ValkeyModuleCallReply *reply = ValkeyModule_Call(ctx,
"SCAN","l",(long long)cursor);
ValkeyModuleCallReply *reply = ValkeyModule_Call(ctx, "SCAN", "l", (long long)cursor);
ValkeyModule_ThreadSafeContextUnlock(ctx);
ValkeyModuleCallReply *cr_cursor =
ValkeyModule_CallReplyArrayElement(reply,0);
ValkeyModuleCallReply *cr_keys =
ValkeyModule_CallReplyArrayElement(reply,1);
ValkeyModuleCallReply *cr_cursor = ValkeyModule_CallReplyArrayElement(reply, 0);
ValkeyModuleCallReply *cr_keys = ValkeyModule_CallReplyArrayElement(reply, 1);
ValkeyModuleString *s = ValkeyModule_CreateStringFromCallReply(cr_cursor);
ValkeyModule_StringToLongLong(s,&cursor);
ValkeyModule_FreeString(ctx,s);
ValkeyModule_StringToLongLong(s, &cursor);
ValkeyModule_FreeString(ctx, s);
size_t items = ValkeyModule_CallReplyLength(cr_keys);
for (size_t j = 0; j < items; j++) {
ValkeyModuleCallReply *ele =
ValkeyModule_CallReplyArrayElement(cr_keys,j);
ValkeyModule_ReplyWithCallReply(ctx,ele);
ValkeyModuleCallReply *ele = ValkeyModule_CallReplyArrayElement(cr_keys, j);
ValkeyModule_ReplyWithCallReply(ctx, ele);
replylen++;
}
ValkeyModule_FreeCallReply(reply);
} while (cursor != 0);
ValkeyModule_ReplySetArrayLength(ctx,replylen);
ValkeyModule_ReplySetArrayLength(ctx, replylen);
ValkeyModule_FreeThreadSafeContext(ctx);
ValkeyModule_UnblockClient(bc,NULL);
ValkeyModule_UnblockClient(bc, NULL);
return NULL;
}
@ -186,14 +182,14 @@ int HelloKeys_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int
/* Note that when blocking the client we do not set any callback: no
* timeout is possible since we passed '0', nor we need a reply callback
* because we'll use the thread safe context to accumulate a reply. */
ValkeyModuleBlockedClient *bc = ValkeyModule_BlockClient(ctx,NULL,NULL,NULL,0);
ValkeyModuleBlockedClient *bc = ValkeyModule_BlockClient(ctx, NULL, NULL, NULL, 0);
/* Now that we setup a blocking client, we need to pass the control
* to the thread. However we need to pass arguments to the thread:
* the reference to the blocked client handle. */
if (pthread_create(&tid,NULL,HelloKeys_ThreadMain,bc) != 0) {
if (pthread_create(&tid, NULL, HelloKeys_ThreadMain, bc) != 0) {
ValkeyModule_AbortBlock(bc);
return ValkeyModule_ReplyWithError(ctx,"-ERR Can't start thread");
return ValkeyModule_ReplyWithError(ctx, "-ERR Can't start thread");
}
return VALKEYMODULE_OK;
}
@ -204,14 +200,11 @@ int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int arg
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
if (ValkeyModule_Init(ctx,"helloblock",1,VALKEYMODULE_APIVER_1)
== VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_Init(ctx, "helloblock", 1, VALKEYMODULE_APIVER_1) == VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.block",
HelloBlock_ValkeyCommand,"",0,0,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.block", HelloBlock_ValkeyCommand, "", 0, 0, 0) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.keys",
HelloKeys_ValkeyCommand,"",0,0,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.keys", HelloKeys_ValkeyCommand, "", 0, 0, 0) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
return VALKEYMODULE_OK;

View File

@ -44,7 +44,7 @@ int PingallCommand_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
ValkeyModule_SendClusterMessage(ctx,NULL,MSGTYPE_PING,"Hey",3);
ValkeyModule_SendClusterMessage(ctx, NULL, MSGTYPE_PING, "Hey", 3);
return ValkeyModule_ReplyWithSimpleString(ctx, "OK");
}
@ -54,36 +54,44 @@ int ListCommand_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, i
VALKEYMODULE_NOT_USED(argc);
size_t numnodes;
char **ids = ValkeyModule_GetClusterNodesList(ctx,&numnodes);
char **ids = ValkeyModule_GetClusterNodesList(ctx, &numnodes);
if (ids == NULL) {
return ValkeyModule_ReplyWithError(ctx,"Cluster not enabled");
return ValkeyModule_ReplyWithError(ctx, "Cluster not enabled");
}
ValkeyModule_ReplyWithArray(ctx,numnodes);
ValkeyModule_ReplyWithArray(ctx, numnodes);
for (size_t j = 0; j < numnodes; j++) {
int port;
ValkeyModule_GetClusterNodeInfo(ctx,ids[j],NULL,NULL,&port,NULL);
ValkeyModule_ReplyWithArray(ctx,2);
ValkeyModule_ReplyWithStringBuffer(ctx,ids[j],VALKEYMODULE_NODE_ID_LEN);
ValkeyModule_ReplyWithLongLong(ctx,port);
ValkeyModule_GetClusterNodeInfo(ctx, ids[j], NULL, NULL, &port, NULL);
ValkeyModule_ReplyWithArray(ctx, 2);
ValkeyModule_ReplyWithStringBuffer(ctx, ids[j], VALKEYMODULE_NODE_ID_LEN);
ValkeyModule_ReplyWithLongLong(ctx, port);
}
ValkeyModule_FreeClusterNodesList(ids);
return VALKEYMODULE_OK;
}
/* Callback for message MSGTYPE_PING */
void PingReceiver(ValkeyModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len) {
ValkeyModule_Log(ctx,"notice","PING (type %d) RECEIVED from %.*s: '%.*s'",
type,VALKEYMODULE_NODE_ID_LEN,sender_id,(int)len, payload);
ValkeyModule_SendClusterMessage(ctx,NULL,MSGTYPE_PONG,"Ohi!",4);
void PingReceiver(ValkeyModuleCtx *ctx,
const char *sender_id,
uint8_t type,
const unsigned char *payload,
uint32_t len) {
ValkeyModule_Log(ctx, "notice", "PING (type %d) RECEIVED from %.*s: '%.*s'", type, VALKEYMODULE_NODE_ID_LEN,
sender_id, (int)len, payload);
ValkeyModule_SendClusterMessage(ctx, NULL, MSGTYPE_PONG, "Ohi!", 4);
ValkeyModuleCallReply *reply = ValkeyModule_Call(ctx, "INCR", "c", "pings_received");
ValkeyModule_FreeCallReply(reply);
}
/* Callback for message MSGTYPE_PONG. */
void PongReceiver(ValkeyModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len) {
ValkeyModule_Log(ctx,"notice","PONG (type %d) RECEIVED from %.*s: '%.*s'",
type,VALKEYMODULE_NODE_ID_LEN,sender_id,(int)len, payload);
void PongReceiver(ValkeyModuleCtx *ctx,
const char *sender_id,
uint8_t type,
const unsigned char *payload,
uint32_t len) {
ValkeyModule_Log(ctx, "notice", "PONG (type %d) RECEIVED from %.*s: '%.*s'", type, VALKEYMODULE_NODE_ID_LEN,
sender_id, (int)len, payload);
}
/* This function must be present on each module. It is used in order to
@ -92,15 +100,14 @@ int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int arg
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
if (ValkeyModule_Init(ctx,"hellocluster",1,VALKEYMODULE_APIVER_1)
== VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_Init(ctx, "hellocluster", 1, VALKEYMODULE_APIVER_1) == VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hellocluster.pingall",
PingallCommand_ValkeyCommand,"readonly",0,0,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hellocluster.pingall", PingallCommand_ValkeyCommand, "readonly", 0, 0, 0) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hellocluster.list",
ListCommand_ValkeyCommand,"readonly",0,0,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hellocluster.list", ListCommand_ValkeyCommand, "readonly", 0, 0, 0) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
/* Disable Cluster sharding and redirections. This way every node
@ -109,10 +116,10 @@ int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int arg
* variable. Normally you do that in order for the distributed system
* you create as a module to have total freedom in the keyspace
* manipulation. */
ValkeyModule_SetClusterFlags(ctx,VALKEYMODULE_CLUSTER_FLAG_NO_REDIRECTION);
ValkeyModule_SetClusterFlags(ctx, VALKEYMODULE_CLUSTER_FLAG_NO_REDIRECTION);
/* Register our handlers for different message types. */
ValkeyModule_RegisterClusterMessageReceiver(ctx,MSGTYPE_PING,PingReceiver);
ValkeyModule_RegisterClusterMessageReceiver(ctx,MSGTYPE_PONG,PongReceiver);
ValkeyModule_RegisterClusterMessageReceiver(ctx, MSGTYPE_PING, PingReceiver);
ValkeyModule_RegisterClusterMessageReceiver(ctx, MSGTYPE_PONG, PongReceiver);
return VALKEYMODULE_OK;
}

View File

@ -46,10 +46,10 @@ static ValkeyModuleDict *Keyspace;
* Set the specified key to the specified value. */
int cmd_SET(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (argc != 3) return ValkeyModule_WrongArity(ctx);
ValkeyModule_DictSet(Keyspace,argv[1],argv[2]);
ValkeyModule_DictSet(Keyspace, argv[1], argv[2]);
/* We need to keep a reference to the value stored at the key, otherwise
* it would be freed when this callback returns. */
ValkeyModule_RetainString(NULL,argv[2]);
ValkeyModule_RetainString(NULL, argv[2]);
return ValkeyModule_ReplyWithSimpleString(ctx, "OK");
}
@ -59,7 +59,7 @@ int cmd_SET(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
* is not defined. */
int cmd_GET(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (argc != 2) return ValkeyModule_WrongArity(ctx);
ValkeyModuleString *val = ValkeyModule_DictGet(Keyspace,argv[1],NULL);
ValkeyModuleString *val = ValkeyModule_DictGet(Keyspace, argv[1], NULL);
if (val == NULL) {
return ValkeyModule_ReplyWithNull(ctx);
} else {
@ -76,27 +76,25 @@ int cmd_KEYRANGE(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
/* Parse the count argument. */
long long count;
if (ValkeyModule_StringToLongLong(argv[3],&count) != VALKEYMODULE_OK) {
return ValkeyModule_ReplyWithError(ctx,"ERR invalid count");
if (ValkeyModule_StringToLongLong(argv[3], &count) != VALKEYMODULE_OK) {
return ValkeyModule_ReplyWithError(ctx, "ERR invalid count");
}
/* Seek the iterator. */
ValkeyModuleDictIter *iter = ValkeyModule_DictIteratorStart(
Keyspace, ">=", argv[1]);
ValkeyModuleDictIter *iter = ValkeyModule_DictIteratorStart(Keyspace, ">=", argv[1]);
/* Reply with the matching items. */
char *key;
size_t keylen;
long long replylen = 0; /* Keep track of the emitted array len. */
ValkeyModule_ReplyWithArray(ctx,VALKEYMODULE_POSTPONED_LEN);
while((key = ValkeyModule_DictNextC(iter,&keylen,NULL)) != NULL) {
ValkeyModule_ReplyWithArray(ctx, VALKEYMODULE_POSTPONED_LEN);
while ((key = ValkeyModule_DictNextC(iter, &keylen, NULL)) != NULL) {
if (replylen >= count) break;
if (ValkeyModule_DictCompare(iter,"<=",argv[2]) == VALKEYMODULE_ERR)
break;
ValkeyModule_ReplyWithStringBuffer(ctx,key,keylen);
if (ValkeyModule_DictCompare(iter, "<=", argv[2]) == VALKEYMODULE_ERR) break;
ValkeyModule_ReplyWithStringBuffer(ctx, key, keylen);
replylen++;
}
ValkeyModule_ReplySetArrayLength(ctx,replylen);
ValkeyModule_ReplySetArrayLength(ctx, replylen);
/* Cleanup. */
ValkeyModule_DictIteratorStop(iter);
@ -109,19 +107,15 @@ int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int arg
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
if (ValkeyModule_Init(ctx,"hellodict",1,VALKEYMODULE_APIVER_1)
== VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_Init(ctx, "hellodict", 1, VALKEYMODULE_APIVER_1) == VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hellodict.set",
cmd_SET,"write deny-oom",1,1,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hellodict.set", cmd_SET, "write deny-oom", 1, 1, 0) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hellodict.get",
cmd_GET,"readonly",1,1,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hellodict.get", cmd_GET, "readonly", 1, 1, 0) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hellodict.keyrange",
cmd_KEYRANGE,"readonly",1,1,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hellodict.keyrange", cmd_KEYRANGE, "readonly", 1, 1, 0) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
/* Create our global dictionary. Here we'll set our keys and values. */

View File

@ -37,20 +37,17 @@
#include <string.h>
/* Client state change callback. */
void clientChangeCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
{
void clientChangeCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data) {
VALKEYMODULE_NOT_USED(ctx);
VALKEYMODULE_NOT_USED(e);
ValkeyModuleClientInfo *ci = data;
printf("Client %s event for client #%llu %s:%d\n",
(sub == VALKEYMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED) ?
"connection" : "disconnection",
(unsigned long long)ci->id,ci->addr,ci->port);
(sub == VALKEYMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED) ? "connection" : "disconnection",
(unsigned long long)ci->id, ci->addr, ci->port);
}
void flushdbCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
{
void flushdbCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data) {
VALKEYMODULE_NOT_USED(ctx);
VALKEYMODULE_NOT_USED(e);
@ -58,17 +55,16 @@ void flushdbCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, vo
if (sub == VALKEYMODULE_SUBEVENT_FLUSHDB_START) {
if (fi->dbnum != -1) {
ValkeyModuleCallReply *reply;
reply = ValkeyModule_Call(ctx,"DBSIZE","");
reply = ValkeyModule_Call(ctx, "DBSIZE", "");
long long numkeys = ValkeyModule_CallReplyInteger(reply);
printf("FLUSHDB event of database %d started (%lld keys in DB)\n",
fi->dbnum, numkeys);
printf("FLUSHDB event of database %d started (%lld keys in DB)\n", fi->dbnum, numkeys);
ValkeyModule_FreeCallReply(reply);
} else {
printf("FLUSHALL event started\n");
}
} else {
if (fi->dbnum != -1) {
printf("FLUSHDB event of database %d ended\n",fi->dbnum);
printf("FLUSHDB event of database %d ended\n", fi->dbnum);
} else {
printf("FLUSHALL event ended\n");
}
@ -81,12 +77,9 @@ int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int arg
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
if (ValkeyModule_Init(ctx,"hellohook",1,VALKEYMODULE_APIVER_1)
== VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_Init(ctx, "hellohook", 1, VALKEYMODULE_APIVER_1) == VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
ValkeyModule_SubscribeToServerEvent(ctx,
ValkeyModuleEvent_ClientChange, clientChangeCallback);
ValkeyModule_SubscribeToServerEvent(ctx,
ValkeyModuleEvent_FlushDB, flushdbCallback);
ValkeyModule_SubscribeToServerEvent(ctx, ValkeyModuleEvent_ClientChange, clientChangeCallback);
ValkeyModule_SubscribeToServerEvent(ctx, ValkeyModuleEvent_FlushDB, flushdbCallback);
return VALKEYMODULE_OK;
}

View File

@ -51,8 +51,8 @@ int TimerCommand_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv,
for (int j = 0; j < 10; j++) {
int delay = rand() % 5000;
char *buf = ValkeyModule_Alloc(256);
snprintf(buf,256,"After %d", delay);
ValkeyModuleTimerID tid = ValkeyModule_CreateTimer(ctx,delay,timerHandler,buf);
snprintf(buf, 256, "After %d", delay);
ValkeyModuleTimerID tid = ValkeyModule_CreateTimer(ctx, delay, timerHandler, buf);
VALKEYMODULE_NOT_USED(tid);
}
return ValkeyModule_ReplyWithSimpleString(ctx, "OK");
@ -64,11 +64,10 @@ int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int arg
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
if (ValkeyModule_Init(ctx,"hellotimer",1,VALKEYMODULE_APIVER_1)
== VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_Init(ctx, "hellotimer", 1, VALKEYMODULE_APIVER_1) == VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hellotimer.timer",
TimerCommand_ValkeyCommand,"readonly",0,0,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hellotimer.timer", TimerCommand_ValkeyCommand, "readonly", 0, 0, 0) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
return VALKEYMODULE_OK;

View File

@ -71,7 +71,7 @@ struct HelloTypeObject *createHelloTypeObject(void) {
void HelloTypeInsert(struct HelloTypeObject *o, int64_t ele) {
struct HelloTypeNode *next = o->head, *newnode, *prev = NULL;
while(next && next->value < ele) {
while (next && next->value < ele) {
prev = next;
next = next->next;
}
@ -89,7 +89,7 @@ void HelloTypeInsert(struct HelloTypeObject *o, int64_t ele) {
void HelloTypeReleaseObject(struct HelloTypeObject *o) {
struct HelloTypeNode *cur, *next;
cur = o->head;
while(cur) {
while (cur) {
next = cur->next;
ValkeyModule_Free(cur);
cur = next;
@ -104,34 +104,31 @@ int HelloTypeInsert_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **arg
ValkeyModule_AutoMemory(ctx); /* Use automatic memory management. */
if (argc != 3) return ValkeyModule_WrongArity(ctx);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
int type = ValkeyModule_KeyType(key);
if (type != VALKEYMODULE_KEYTYPE_EMPTY &&
ValkeyModule_ModuleTypeGetType(key) != HelloType)
{
return ValkeyModule_ReplyWithError(ctx,VALKEYMODULE_ERRORMSG_WRONGTYPE);
if (type != VALKEYMODULE_KEYTYPE_EMPTY && ValkeyModule_ModuleTypeGetType(key) != HelloType) {
return ValkeyModule_ReplyWithError(ctx, VALKEYMODULE_ERRORMSG_WRONGTYPE);
}
long long value;
if ((ValkeyModule_StringToLongLong(argv[2],&value) != VALKEYMODULE_OK)) {
return ValkeyModule_ReplyWithError(ctx,"ERR invalid value: must be a signed 64 bit integer");
if ((ValkeyModule_StringToLongLong(argv[2], &value) != VALKEYMODULE_OK)) {
return ValkeyModule_ReplyWithError(ctx, "ERR invalid value: must be a signed 64 bit integer");
}
/* Create an empty value object if the key is currently empty. */
struct HelloTypeObject *hto;
if (type == VALKEYMODULE_KEYTYPE_EMPTY) {
hto = createHelloTypeObject();
ValkeyModule_ModuleTypeSetValue(key,HelloType,hto);
ValkeyModule_ModuleTypeSetValue(key, HelloType, hto);
} else {
hto = ValkeyModule_ModuleTypeGetValue(key);
}
/* Insert the new element. */
HelloTypeInsert(hto,value);
ValkeyModule_SignalKeyAsReady(ctx,argv[1]);
HelloTypeInsert(hto, value);
ValkeyModule_SignalKeyAsReady(ctx, argv[1]);
ValkeyModule_ReplyWithLongLong(ctx,hto->len);
ValkeyModule_ReplyWithLongLong(ctx, hto->len);
ValkeyModule_ReplicateVerbatim(ctx);
return VALKEYMODULE_OK;
}
@ -141,34 +138,28 @@ int HelloTypeRange_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv
ValkeyModule_AutoMemory(ctx); /* Use automatic memory management. */
if (argc != 4) return ValkeyModule_WrongArity(ctx);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
int type = ValkeyModule_KeyType(key);
if (type != VALKEYMODULE_KEYTYPE_EMPTY &&
ValkeyModule_ModuleTypeGetType(key) != HelloType)
{
return ValkeyModule_ReplyWithError(ctx,VALKEYMODULE_ERRORMSG_WRONGTYPE);
if (type != VALKEYMODULE_KEYTYPE_EMPTY && ValkeyModule_ModuleTypeGetType(key) != HelloType) {
return ValkeyModule_ReplyWithError(ctx, VALKEYMODULE_ERRORMSG_WRONGTYPE);
}
long long first, count;
if (ValkeyModule_StringToLongLong(argv[2],&first) != VALKEYMODULE_OK ||
ValkeyModule_StringToLongLong(argv[3],&count) != VALKEYMODULE_OK ||
first < 0 || count < 0)
{
return ValkeyModule_ReplyWithError(ctx,
"ERR invalid first or count parameters");
if (ValkeyModule_StringToLongLong(argv[2], &first) != VALKEYMODULE_OK ||
ValkeyModule_StringToLongLong(argv[3], &count) != VALKEYMODULE_OK || first < 0 || count < 0) {
return ValkeyModule_ReplyWithError(ctx, "ERR invalid first or count parameters");
}
struct HelloTypeObject *hto = ValkeyModule_ModuleTypeGetValue(key);
struct HelloTypeNode *node = hto ? hto->head : NULL;
ValkeyModule_ReplyWithArray(ctx,VALKEYMODULE_POSTPONED_LEN);
ValkeyModule_ReplyWithArray(ctx, VALKEYMODULE_POSTPONED_LEN);
long long arraylen = 0;
while(node && count--) {
ValkeyModule_ReplyWithLongLong(ctx,node->value);
while (node && count--) {
ValkeyModule_ReplyWithLongLong(ctx, node->value);
arraylen++;
node = node->next;
}
ValkeyModule_ReplySetArrayLength(ctx,arraylen);
ValkeyModule_ReplySetArrayLength(ctx, arraylen);
return VALKEYMODULE_OK;
}
@ -177,17 +168,14 @@ int HelloTypeLen_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv,
ValkeyModule_AutoMemory(ctx); /* Use automatic memory management. */
if (argc != 2) return ValkeyModule_WrongArity(ctx);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
int type = ValkeyModule_KeyType(key);
if (type != VALKEYMODULE_KEYTYPE_EMPTY &&
ValkeyModule_ModuleTypeGetType(key) != HelloType)
{
return ValkeyModule_ReplyWithError(ctx,VALKEYMODULE_ERRORMSG_WRONGTYPE);
if (type != VALKEYMODULE_KEYTYPE_EMPTY && ValkeyModule_ModuleTypeGetType(key) != HelloType) {
return ValkeyModule_ReplyWithError(ctx, VALKEYMODULE_ERRORMSG_WRONGTYPE);
}
struct HelloTypeObject *hto = ValkeyModule_ModuleTypeGetValue(key);
ValkeyModule_ReplyWithLongLong(ctx,hto ? hto->len : 0);
ValkeyModule_ReplyWithLongLong(ctx, hto ? hto->len : 0);
return VALKEYMODULE_OK;
}
@ -201,11 +189,9 @@ int HelloBlock_Reply(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
VALKEYMODULE_NOT_USED(argc);
ValkeyModuleString *keyname = ValkeyModule_GetBlockedClientReadyKey(ctx);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx,keyname,VALKEYMODULE_READ);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx, keyname, VALKEYMODULE_READ);
int type = ValkeyModule_KeyType(key);
if (type != VALKEYMODULE_KEYTYPE_MODULE ||
ValkeyModule_ModuleTypeGetType(key) != HelloType)
{
if (type != VALKEYMODULE_KEYTYPE_MODULE || ValkeyModule_ModuleTypeGetType(key) != HelloType) {
ValkeyModule_CloseKey(key);
return VALKEYMODULE_ERR;
}
@ -213,14 +199,14 @@ int HelloBlock_Reply(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
/* In case the key is able to serve our blocked client, let's directly
* use our original command implementation to make this example simpler. */
ValkeyModule_CloseKey(key);
return HelloTypeRange_ValkeyCommand(ctx,argv,argc-1);
return HelloTypeRange_ValkeyCommand(ctx, argv, argc - 1);
}
/* Timeout callback for blocking command HELLOTYPE.BRANGE */
int HelloBlock_Timeout(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
return ValkeyModule_ReplyWithSimpleString(ctx,"Request timedout");
return ValkeyModule_ReplyWithSimpleString(ctx, "Request timedout");
}
/* Private data freeing callback for HELLOTYPE.BRANGE command. */
@ -235,31 +221,28 @@ void HelloBlock_FreeData(ValkeyModuleCtx *ctx, void *privdata) {
int HelloTypeBRange_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (argc != 5) return ValkeyModule_WrongArity(ctx);
ValkeyModule_AutoMemory(ctx); /* Use automatic memory management. */
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
int type = ValkeyModule_KeyType(key);
if (type != VALKEYMODULE_KEYTYPE_EMPTY &&
ValkeyModule_ModuleTypeGetType(key) != HelloType)
{
return ValkeyModule_ReplyWithError(ctx,VALKEYMODULE_ERRORMSG_WRONGTYPE);
if (type != VALKEYMODULE_KEYTYPE_EMPTY && ValkeyModule_ModuleTypeGetType(key) != HelloType) {
return ValkeyModule_ReplyWithError(ctx, VALKEYMODULE_ERRORMSG_WRONGTYPE);
}
/* Parse the timeout before even trying to serve the client synchronously,
* so that we always fail ASAP on syntax errors. */
long long timeout;
if (ValkeyModule_StringToLongLong(argv[4],&timeout) != VALKEYMODULE_OK) {
return ValkeyModule_ReplyWithError(ctx,
"ERR invalid timeout parameter");
if (ValkeyModule_StringToLongLong(argv[4], &timeout) != VALKEYMODULE_OK) {
return ValkeyModule_ReplyWithError(ctx, "ERR invalid timeout parameter");
}
/* Can we serve the reply synchronously? */
if (type != VALKEYMODULE_KEYTYPE_EMPTY) {
return HelloTypeRange_ValkeyCommand(ctx,argv,argc-1);
return HelloTypeRange_ValkeyCommand(ctx, argv, argc - 1);
}
/* Otherwise let's block on the key. */
void *privdata = ValkeyModule_Alloc(100);
ValkeyModule_BlockClientOnKeys(ctx,HelloBlock_Reply,HelloBlock_Timeout,HelloBlock_FreeData,timeout,argv+1,1,privdata);
ValkeyModule_BlockClientOnKeys(ctx, HelloBlock_Reply, HelloBlock_Timeout, HelloBlock_FreeData, timeout, argv + 1, 1,
privdata);
return VALKEYMODULE_OK;
}
@ -272,9 +255,9 @@ void *HelloTypeRdbLoad(ValkeyModuleIO *rdb, int encver) {
}
uint64_t elements = ValkeyModule_LoadUnsigned(rdb);
struct HelloTypeObject *hto = createHelloTypeObject();
while(elements--) {
while (elements--) {
int64_t ele = ValkeyModule_LoadSigned(rdb);
HelloTypeInsert(hto,ele);
HelloTypeInsert(hto, ele);
}
return hto;
}
@ -282,9 +265,9 @@ void *HelloTypeRdbLoad(ValkeyModuleIO *rdb, int encver) {
void HelloTypeRdbSave(ValkeyModuleIO *rdb, void *value) {
struct HelloTypeObject *hto = value;
struct HelloTypeNode *node = hto->head;
ValkeyModule_SaveUnsigned(rdb,hto->len);
while(node) {
ValkeyModule_SaveSigned(rdb,node->value);
ValkeyModule_SaveUnsigned(rdb, hto->len);
while (node) {
ValkeyModule_SaveSigned(rdb, node->value);
node = node->next;
}
}
@ -292,8 +275,8 @@ void HelloTypeRdbSave(ValkeyModuleIO *rdb, void *value) {
void HelloTypeAofRewrite(ValkeyModuleIO *aof, ValkeyModuleString *key, void *value) {
struct HelloTypeObject *hto = value;
struct HelloTypeNode *node = hto->head;
while(node) {
ValkeyModule_EmitAOF(aof,"HELLOTYPE.INSERT","sl",key,node->value);
while (node) {
ValkeyModule_EmitAOF(aof, "HELLOTYPE.INSERT", "sl", key, node->value);
node = node->next;
}
}
@ -303,7 +286,7 @@ void HelloTypeAofRewrite(ValkeyModuleIO *aof, ValkeyModuleString *key, void *val
size_t HelloTypeMemUsage(const void *value) {
const struct HelloTypeObject *hto = value;
struct HelloTypeNode *node = hto->head;
return sizeof(*hto) + sizeof(*node)*hto->len;
return sizeof(*hto) + sizeof(*node) * hto->len;
}
void HelloTypeFree(void *value) {
@ -313,8 +296,8 @@ void HelloTypeFree(void *value) {
void HelloTypeDigest(ValkeyModuleDigest *md, void *value) {
struct HelloTypeObject *hto = value;
struct HelloTypeNode *node = hto->head;
while(node) {
ValkeyModule_DigestAddLongLong(md,node->value);
while (node) {
ValkeyModule_DigestAddLongLong(md, node->value);
node = node->next;
}
ValkeyModule_DigestEndSequence(md);
@ -326,36 +309,33 @@ int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int arg
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
if (ValkeyModule_Init(ctx,"hellotype",1,VALKEYMODULE_APIVER_1)
== VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_Init(ctx, "hellotype", 1, VALKEYMODULE_APIVER_1) == VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
ValkeyModuleTypeMethods tm = {
.version = VALKEYMODULE_TYPE_METHOD_VERSION,
.rdb_load = HelloTypeRdbLoad,
.rdb_save = HelloTypeRdbSave,
.aof_rewrite = HelloTypeAofRewrite,
.mem_usage = HelloTypeMemUsage,
.free = HelloTypeFree,
.digest = HelloTypeDigest
};
ValkeyModuleTypeMethods tm = {.version = VALKEYMODULE_TYPE_METHOD_VERSION,
.rdb_load = HelloTypeRdbLoad,
.rdb_save = HelloTypeRdbSave,
.aof_rewrite = HelloTypeAofRewrite,
.mem_usage = HelloTypeMemUsage,
.free = HelloTypeFree,
.digest = HelloTypeDigest};
HelloType = ValkeyModule_CreateDataType(ctx,"hellotype",0,&tm);
HelloType = ValkeyModule_CreateDataType(ctx, "hellotype", 0, &tm);
if (HelloType == NULL) return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hellotype.insert",
HelloTypeInsert_ValkeyCommand,"write deny-oom",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hellotype.insert", HelloTypeInsert_ValkeyCommand, "write deny-oom", 1, 1, 1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hellotype.range",
HelloTypeRange_ValkeyCommand,"readonly",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hellotype.range", HelloTypeRange_ValkeyCommand, "readonly", 1, 1, 1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hellotype.len",
HelloTypeLen_ValkeyCommand,"readonly",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hellotype.len", HelloTypeLen_ValkeyCommand, "readonly", 1, 1, 1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hellotype.brange",
HelloTypeBRange_ValkeyCommand,"readonly",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hellotype.brange", HelloTypeBRange_ValkeyCommand, "readonly", 1, 1, 1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
return VALKEYMODULE_OK;

View File

@ -48,7 +48,7 @@
int HelloSimple_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
ValkeyModule_ReplyWithLongLong(ctx,ValkeyModule_GetSelectedDb(ctx));
ValkeyModule_ReplyWithLongLong(ctx, ValkeyModule_GetSelectedDb(ctx));
return VALKEYMODULE_OK;
}
@ -58,17 +58,15 @@ int HelloSimple_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, i
*
* You'll find this command to be roughly as fast as the actual RPUSH
* command. */
int HelloPushNative_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
{
int HelloPushNative_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (argc != 3) return ValkeyModule_WrongArity(ctx);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
ValkeyModule_ListPush(key,VALKEYMODULE_LIST_TAIL,argv[2]);
ValkeyModule_ListPush(key, VALKEYMODULE_LIST_TAIL, argv[2]);
size_t newlen = ValkeyModule_ValueLength(key);
ValkeyModule_CloseKey(key);
ValkeyModule_ReplyWithLongLong(ctx,newlen);
ValkeyModule_ReplyWithLongLong(ctx, newlen);
return VALKEYMODULE_OK;
}
@ -77,30 +75,28 @@ int HelloPushNative_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **arg
* approach is useful when you need to call commands that are not
* available as low level APIs, or when you don't need the maximum speed
* possible but instead prefer implementation simplicity. */
int HelloPushCall_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
{
int HelloPushCall_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (argc != 3) return ValkeyModule_WrongArity(ctx);
ValkeyModuleCallReply *reply;
reply = ValkeyModule_Call(ctx,"RPUSH","ss",argv[1],argv[2]);
reply = ValkeyModule_Call(ctx, "RPUSH", "ss", argv[1], argv[2]);
long long len = ValkeyModule_CallReplyInteger(reply);
ValkeyModule_FreeCallReply(reply);
ValkeyModule_ReplyWithLongLong(ctx,len);
ValkeyModule_ReplyWithLongLong(ctx, len);
return VALKEYMODULE_OK;
}
/* HELLO.PUSH.CALL2
* This is exactly as HELLO.PUSH.CALL, but shows how we can reply to the
* client using directly a reply object that Call() returned. */
int HelloPushCall2_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
{
int HelloPushCall2_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (argc != 3) return ValkeyModule_WrongArity(ctx);
ValkeyModuleCallReply *reply;
reply = ValkeyModule_Call(ctx,"RPUSH","ss",argv[1],argv[2]);
ValkeyModule_ReplyWithCallReply(ctx,reply);
reply = ValkeyModule_Call(ctx, "RPUSH", "ss", argv[1], argv[2]);
ValkeyModule_ReplyWithCallReply(ctx, reply);
ValkeyModule_FreeCallReply(reply);
return VALKEYMODULE_OK;
}
@ -108,22 +104,21 @@ int HelloPushCall2_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv
/* HELLO.LIST.SUM.LEN returns the total length of all the items inside
* a list, by using the high level Call() API.
* This command is an example of the array reply access. */
int HelloListSumLen_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
{
int HelloListSumLen_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (argc != 2) return ValkeyModule_WrongArity(ctx);
ValkeyModuleCallReply *reply;
reply = ValkeyModule_Call(ctx,"LRANGE","sll",argv[1],(long long)0,(long long)-1);
reply = ValkeyModule_Call(ctx, "LRANGE", "sll", argv[1], (long long)0, (long long)-1);
size_t strlen = 0;
size_t items = ValkeyModule_CallReplyLength(reply);
size_t j;
for (j = 0; j < items; j++) {
ValkeyModuleCallReply *ele = ValkeyModule_CallReplyArrayElement(reply,j);
ValkeyModuleCallReply *ele = ValkeyModule_CallReplyArrayElement(reply, j);
strlen += ValkeyModule_CallReplyLength(ele);
}
ValkeyModule_FreeCallReply(reply);
ValkeyModule_ReplyWithLongLong(ctx,strlen);
ValkeyModule_ReplyWithLongLong(ctx, strlen);
return VALKEYMODULE_OK;
}
@ -134,43 +129,39 @@ int HelloListSumLen_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **arg
int HelloListSplice_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (argc != 4) return ValkeyModule_WrongArity(ctx);
ValkeyModuleKey *srckey = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *dstkey = ValkeyModule_OpenKey(ctx,argv[2],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *srckey = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
ValkeyModuleKey *dstkey = ValkeyModule_OpenKey(ctx, argv[2], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
/* Src and dst key must be empty or lists. */
if ((ValkeyModule_KeyType(srckey) != VALKEYMODULE_KEYTYPE_LIST &&
ValkeyModule_KeyType(srckey) != VALKEYMODULE_KEYTYPE_EMPTY) ||
(ValkeyModule_KeyType(dstkey) != VALKEYMODULE_KEYTYPE_LIST &&
ValkeyModule_KeyType(dstkey) != VALKEYMODULE_KEYTYPE_EMPTY))
{
ValkeyModule_KeyType(dstkey) != VALKEYMODULE_KEYTYPE_EMPTY)) {
ValkeyModule_CloseKey(srckey);
ValkeyModule_CloseKey(dstkey);
return ValkeyModule_ReplyWithError(ctx,VALKEYMODULE_ERRORMSG_WRONGTYPE);
return ValkeyModule_ReplyWithError(ctx, VALKEYMODULE_ERRORMSG_WRONGTYPE);
}
long long count;
if ((ValkeyModule_StringToLongLong(argv[3],&count) != VALKEYMODULE_OK) ||
(count < 0)) {
if ((ValkeyModule_StringToLongLong(argv[3], &count) != VALKEYMODULE_OK) || (count < 0)) {
ValkeyModule_CloseKey(srckey);
ValkeyModule_CloseKey(dstkey);
return ValkeyModule_ReplyWithError(ctx,"ERR invalid count");
return ValkeyModule_ReplyWithError(ctx, "ERR invalid count");
}
while(count-- > 0) {
while (count-- > 0) {
ValkeyModuleString *ele;
ele = ValkeyModule_ListPop(srckey,VALKEYMODULE_LIST_TAIL);
ele = ValkeyModule_ListPop(srckey, VALKEYMODULE_LIST_TAIL);
if (ele == NULL) break;
ValkeyModule_ListPush(dstkey,VALKEYMODULE_LIST_HEAD,ele);
ValkeyModule_FreeString(ctx,ele);
ValkeyModule_ListPush(dstkey, VALKEYMODULE_LIST_HEAD, ele);
ValkeyModule_FreeString(ctx, ele);
}
size_t len = ValkeyModule_ValueLength(srckey);
ValkeyModule_CloseKey(srckey);
ValkeyModule_CloseKey(dstkey);
ValkeyModule_ReplyWithLongLong(ctx,len);
ValkeyModule_ReplyWithLongLong(ctx, len);
return VALKEYMODULE_OK;
}
@ -181,37 +172,32 @@ int HelloListSpliceAuto_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString *
ValkeyModule_AutoMemory(ctx);
ValkeyModuleKey *srckey = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *dstkey = ValkeyModule_OpenKey(ctx,argv[2],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *srckey = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
ValkeyModuleKey *dstkey = ValkeyModule_OpenKey(ctx, argv[2], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
/* Src and dst key must be empty or lists. */
if ((ValkeyModule_KeyType(srckey) != VALKEYMODULE_KEYTYPE_LIST &&
ValkeyModule_KeyType(srckey) != VALKEYMODULE_KEYTYPE_EMPTY) ||
(ValkeyModule_KeyType(dstkey) != VALKEYMODULE_KEYTYPE_LIST &&
ValkeyModule_KeyType(dstkey) != VALKEYMODULE_KEYTYPE_EMPTY))
{
return ValkeyModule_ReplyWithError(ctx,VALKEYMODULE_ERRORMSG_WRONGTYPE);
ValkeyModule_KeyType(dstkey) != VALKEYMODULE_KEYTYPE_EMPTY)) {
return ValkeyModule_ReplyWithError(ctx, VALKEYMODULE_ERRORMSG_WRONGTYPE);
}
long long count;
if ((ValkeyModule_StringToLongLong(argv[3],&count) != VALKEYMODULE_OK) ||
(count < 0))
{
return ValkeyModule_ReplyWithError(ctx,"ERR invalid count");
if ((ValkeyModule_StringToLongLong(argv[3], &count) != VALKEYMODULE_OK) || (count < 0)) {
return ValkeyModule_ReplyWithError(ctx, "ERR invalid count");
}
while(count-- > 0) {
while (count-- > 0) {
ValkeyModuleString *ele;
ele = ValkeyModule_ListPop(srckey,VALKEYMODULE_LIST_TAIL);
ele = ValkeyModule_ListPop(srckey, VALKEYMODULE_LIST_TAIL);
if (ele == NULL) break;
ValkeyModule_ListPush(dstkey,VALKEYMODULE_LIST_HEAD,ele);
ValkeyModule_ListPush(dstkey, VALKEYMODULE_LIST_HEAD, ele);
}
size_t len = ValkeyModule_ValueLength(srckey);
ValkeyModule_ReplyWithLongLong(ctx,len);
ValkeyModule_ReplyWithLongLong(ctx, len);
return VALKEYMODULE_OK;
}
@ -221,15 +207,14 @@ int HelloListSpliceAuto_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString *
int HelloRandArray_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (argc != 2) return ValkeyModule_WrongArity(ctx);
long long count;
if (ValkeyModule_StringToLongLong(argv[1],&count) != VALKEYMODULE_OK ||
count < 0)
return ValkeyModule_ReplyWithError(ctx,"ERR invalid count");
if (ValkeyModule_StringToLongLong(argv[1], &count) != VALKEYMODULE_OK || count < 0)
return ValkeyModule_ReplyWithError(ctx, "ERR invalid count");
/* To reply with an array, we call ValkeyModule_ReplyWithArray() followed
* by other "count" calls to other reply functions in order to generate
* the elements of the array. */
ValkeyModule_ReplyWithArray(ctx,count);
while(count--) ValkeyModule_ReplyWithLongLong(ctx,rand());
ValkeyModule_ReplyWithArray(ctx, count);
while (count--) ValkeyModule_ReplyWithLongLong(ctx, rand());
return VALKEYMODULE_OK;
}
@ -237,8 +222,7 @@ int HelloRandArray_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv
* in the ValkeyModule_Call() call, the two INCRs get replicated.
* Also note how the ECHO is replicated in an unexpected position (check
* comments the function implementation). */
int HelloRepl1_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
{
int HelloRepl1_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
ValkeyModule_AutoMemory(ctx);
@ -253,14 +237,14 @@ int HelloRepl1_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, in
* ECHO c foo
* EXEC
*/
ValkeyModule_Replicate(ctx,"ECHO","c","foo");
ValkeyModule_Replicate(ctx, "ECHO", "c", "foo");
/* Using the "!" modifier we replicate the command if it
* modified the dataset in some way. */
ValkeyModule_Call(ctx,"INCR","c!","foo");
ValkeyModule_Call(ctx,"INCR","c!","bar");
ValkeyModule_Call(ctx, "INCR", "c!", "foo");
ValkeyModule_Call(ctx, "INCR", "c!", "bar");
ValkeyModule_ReplyWithLongLong(ctx,0);
ValkeyModule_ReplyWithLongLong(ctx, 0);
return VALKEYMODULE_OK;
}
@ -279,26 +263,25 @@ int HelloRepl2_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, in
if (argc != 2) return ValkeyModule_WrongArity(ctx);
ValkeyModule_AutoMemory(ctx); /* Use automatic memory management. */
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
if (ValkeyModule_KeyType(key) != VALKEYMODULE_KEYTYPE_LIST)
return ValkeyModule_ReplyWithError(ctx,VALKEYMODULE_ERRORMSG_WRONGTYPE);
return ValkeyModule_ReplyWithError(ctx, VALKEYMODULE_ERRORMSG_WRONGTYPE);
size_t listlen = ValkeyModule_ValueLength(key);
long long sum = 0;
/* Rotate and increment. */
while(listlen--) {
ValkeyModuleString *ele = ValkeyModule_ListPop(key,VALKEYMODULE_LIST_TAIL);
while (listlen--) {
ValkeyModuleString *ele = ValkeyModule_ListPop(key, VALKEYMODULE_LIST_TAIL);
long long val;
if (ValkeyModule_StringToLongLong(ele,&val) != VALKEYMODULE_OK) val = 0;
if (ValkeyModule_StringToLongLong(ele, &val) != VALKEYMODULE_OK) val = 0;
val++;
sum += val;
ValkeyModuleString *newele = ValkeyModule_CreateStringFromLongLong(ctx,val);
ValkeyModule_ListPush(key,VALKEYMODULE_LIST_HEAD,newele);
ValkeyModuleString *newele = ValkeyModule_CreateStringFromLongLong(ctx, val);
ValkeyModule_ListPush(key, VALKEYMODULE_LIST_HEAD, newele);
}
ValkeyModule_ReplyWithLongLong(ctx,sum);
ValkeyModule_ReplyWithLongLong(ctx, sum);
ValkeyModule_ReplicateVerbatim(ctx);
return VALKEYMODULE_OK;
}
@ -314,20 +297,17 @@ int HelloRepl2_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, in
int HelloToggleCase_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (argc != 2) return ValkeyModule_WrongArity(ctx);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
int keytype = ValkeyModule_KeyType(key);
if (keytype != VALKEYMODULE_KEYTYPE_STRING &&
keytype != VALKEYMODULE_KEYTYPE_EMPTY)
{
if (keytype != VALKEYMODULE_KEYTYPE_STRING && keytype != VALKEYMODULE_KEYTYPE_EMPTY) {
ValkeyModule_CloseKey(key);
return ValkeyModule_ReplyWithError(ctx,VALKEYMODULE_ERRORMSG_WRONGTYPE);
return ValkeyModule_ReplyWithError(ctx, VALKEYMODULE_ERRORMSG_WRONGTYPE);
}
if (keytype == VALKEYMODULE_KEYTYPE_STRING) {
size_t len, j;
char *s = ValkeyModule_StringDMA(key,&len,VALKEYMODULE_WRITE);
char *s = ValkeyModule_StringDMA(key, &len, VALKEYMODULE_WRITE);
for (j = 0; j < len; j++) {
if (isupper(s[j])) {
s[j] = tolower(s[j]);
@ -338,7 +318,7 @@ int HelloToggleCase_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **arg
}
ValkeyModule_CloseKey(key);
ValkeyModule_ReplyWithSimpleString(ctx,"OK");
ValkeyModule_ReplyWithSimpleString(ctx, "OK");
ValkeyModule_ReplicateVerbatim(ctx);
return VALKEYMODULE_OK;
}
@ -353,17 +333,16 @@ int HelloMoreExpire_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **arg
mstime_t addms, expire;
if (ValkeyModule_StringToLongLong(argv[2],&addms) != VALKEYMODULE_OK)
return ValkeyModule_ReplyWithError(ctx,"ERR invalid expire time");
if (ValkeyModule_StringToLongLong(argv[2], &addms) != VALKEYMODULE_OK)
return ValkeyModule_ReplyWithError(ctx, "ERR invalid expire time");
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
expire = ValkeyModule_GetExpire(key);
if (expire != VALKEYMODULE_NO_EXPIRE) {
expire += addms;
ValkeyModule_SetExpire(key,expire);
ValkeyModule_SetExpire(key, expire);
}
return ValkeyModule_ReplyWithSimpleString(ctx,"OK");
return ValkeyModule_ReplyWithSimpleString(ctx, "OK");
}
/* HELLO.ZSUMRANGE key startscore endscore
@ -376,36 +355,34 @@ int HelloZsumRange_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv
double score_start, score_end;
if (argc != 4) return ValkeyModule_WrongArity(ctx);
if (ValkeyModule_StringToDouble(argv[2],&score_start) != VALKEYMODULE_OK ||
ValkeyModule_StringToDouble(argv[3],&score_end) != VALKEYMODULE_OK)
{
return ValkeyModule_ReplyWithError(ctx,"ERR invalid range");
if (ValkeyModule_StringToDouble(argv[2], &score_start) != VALKEYMODULE_OK ||
ValkeyModule_StringToDouble(argv[3], &score_end) != VALKEYMODULE_OK) {
return ValkeyModule_ReplyWithError(ctx, "ERR invalid range");
}
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
if (ValkeyModule_KeyType(key) != VALKEYMODULE_KEYTYPE_ZSET) {
return ValkeyModule_ReplyWithError(ctx,VALKEYMODULE_ERRORMSG_WRONGTYPE);
return ValkeyModule_ReplyWithError(ctx, VALKEYMODULE_ERRORMSG_WRONGTYPE);
}
double scoresum_a = 0;
double scoresum_b = 0;
ValkeyModule_ZsetFirstInScoreRange(key,score_start,score_end,0,0);
while(!ValkeyModule_ZsetRangeEndReached(key)) {
ValkeyModule_ZsetFirstInScoreRange(key, score_start, score_end, 0, 0);
while (!ValkeyModule_ZsetRangeEndReached(key)) {
double score;
ValkeyModuleString *ele = ValkeyModule_ZsetRangeCurrentElement(key,&score);
ValkeyModule_FreeString(ctx,ele);
ValkeyModuleString *ele = ValkeyModule_ZsetRangeCurrentElement(key, &score);
ValkeyModule_FreeString(ctx, ele);
scoresum_a += score;
ValkeyModule_ZsetRangeNext(key);
}
ValkeyModule_ZsetRangeStop(key);
ValkeyModule_ZsetLastInScoreRange(key,score_start,score_end,0,0);
while(!ValkeyModule_ZsetRangeEndReached(key)) {
ValkeyModule_ZsetLastInScoreRange(key, score_start, score_end, 0, 0);
while (!ValkeyModule_ZsetRangeEndReached(key)) {
double score;
ValkeyModuleString *ele = ValkeyModule_ZsetRangeCurrentElement(key,&score);
ValkeyModule_FreeString(ctx,ele);
ValkeyModuleString *ele = ValkeyModule_ZsetRangeCurrentElement(key, &score);
ValkeyModule_FreeString(ctx, ele);
scoresum_b += score;
ValkeyModule_ZsetRangePrev(key);
}
@ -414,9 +391,9 @@ int HelloZsumRange_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv
ValkeyModule_CloseKey(key);
ValkeyModule_ReplyWithArray(ctx,2);
ValkeyModule_ReplyWithDouble(ctx,scoresum_a);
ValkeyModule_ReplyWithDouble(ctx,scoresum_b);
ValkeyModule_ReplyWithArray(ctx, 2);
ValkeyModule_ReplyWithDouble(ctx, scoresum_a);
ValkeyModule_ReplyWithDouble(ctx, scoresum_b);
return VALKEYMODULE_OK;
}
@ -432,28 +409,27 @@ int HelloLexRange_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv,
if (argc != 6) return ValkeyModule_WrongArity(ctx);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
if (ValkeyModule_KeyType(key) != VALKEYMODULE_KEYTYPE_ZSET) {
return ValkeyModule_ReplyWithError(ctx,VALKEYMODULE_ERRORMSG_WRONGTYPE);
return ValkeyModule_ReplyWithError(ctx, VALKEYMODULE_ERRORMSG_WRONGTYPE);
}
if (ValkeyModule_ZsetFirstInLexRange(key,argv[2],argv[3]) != VALKEYMODULE_OK) {
return ValkeyModule_ReplyWithError(ctx,"invalid range");
if (ValkeyModule_ZsetFirstInLexRange(key, argv[2], argv[3]) != VALKEYMODULE_OK) {
return ValkeyModule_ReplyWithError(ctx, "invalid range");
}
int arraylen = 0;
ValkeyModule_ReplyWithArray(ctx,VALKEYMODULE_POSTPONED_LEN);
while(!ValkeyModule_ZsetRangeEndReached(key)) {
ValkeyModule_ReplyWithArray(ctx, VALKEYMODULE_POSTPONED_LEN);
while (!ValkeyModule_ZsetRangeEndReached(key)) {
double score;
ValkeyModuleString *ele = ValkeyModule_ZsetRangeCurrentElement(key,&score);
ValkeyModule_ReplyWithString(ctx,ele);
ValkeyModule_FreeString(ctx,ele);
ValkeyModuleString *ele = ValkeyModule_ZsetRangeCurrentElement(key, &score);
ValkeyModule_ReplyWithString(ctx, ele);
ValkeyModule_FreeString(ctx, ele);
ValkeyModule_ZsetRangeNext(key);
arraylen++;
}
ValkeyModule_ZsetRangeStop(key);
ValkeyModule_ReplySetArrayLength(ctx,arraylen);
ValkeyModule_ReplySetArrayLength(ctx, arraylen);
ValkeyModule_CloseKey(key);
return VALKEYMODULE_OK;
}
@ -469,22 +445,19 @@ int HelloHCopy_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, in
ValkeyModule_AutoMemory(ctx); /* Use automatic memory management. */
if (argc != 4) return ValkeyModule_WrongArity(ctx);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx,argv[1],
VALKEYMODULE_READ|VALKEYMODULE_WRITE);
ValkeyModuleKey *key = ValkeyModule_OpenKey(ctx, argv[1], VALKEYMODULE_READ | VALKEYMODULE_WRITE);
int type = ValkeyModule_KeyType(key);
if (type != VALKEYMODULE_KEYTYPE_HASH &&
type != VALKEYMODULE_KEYTYPE_EMPTY)
{
return ValkeyModule_ReplyWithError(ctx,VALKEYMODULE_ERRORMSG_WRONGTYPE);
if (type != VALKEYMODULE_KEYTYPE_HASH && type != VALKEYMODULE_KEYTYPE_EMPTY) {
return ValkeyModule_ReplyWithError(ctx, VALKEYMODULE_ERRORMSG_WRONGTYPE);
}
/* Get the old field value. */
ValkeyModuleString *oldval;
ValkeyModule_HashGet(key,VALKEYMODULE_HASH_NONE,argv[2],&oldval,NULL);
ValkeyModule_HashGet(key, VALKEYMODULE_HASH_NONE, argv[2], &oldval, NULL);
if (oldval) {
ValkeyModule_HashSet(key,VALKEYMODULE_HASH_NONE,argv[3],oldval,NULL);
ValkeyModule_HashSet(key, VALKEYMODULE_HASH_NONE, argv[3], oldval, NULL);
}
ValkeyModule_ReplyWithLongLong(ctx,oldval != NULL);
ValkeyModule_ReplyWithLongLong(ctx, oldval != NULL);
return VALKEYMODULE_OK;
}
@ -512,9 +485,8 @@ int HelloLeftPad_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv,
if (argc != 4) return ValkeyModule_WrongArity(ctx);
if ((ValkeyModule_StringToLongLong(argv[2],&padlen) != VALKEYMODULE_OK) ||
(padlen< 0)) {
return ValkeyModule_ReplyWithError(ctx,"ERR invalid padding length");
if ((ValkeyModule_StringToLongLong(argv[2], &padlen) != VALKEYMODULE_OK) || (padlen < 0)) {
return ValkeyModule_ReplyWithError(ctx, "ERR invalid padding length");
}
size_t strlen, chlen;
const char *str = ValkeyModule_StringPtrLen(argv[1], &strlen);
@ -522,99 +494,91 @@ int HelloLeftPad_ValkeyCommand(ValkeyModuleCtx *ctx, ValkeyModuleString **argv,
/* If the string is already larger than the target len, just return
* the string itself. */
if (strlen >= (size_t)padlen)
return ValkeyModule_ReplyWithString(ctx,argv[1]);
if (strlen >= (size_t)padlen) return ValkeyModule_ReplyWithString(ctx, argv[1]);
/* Padding must be a single character in this simple implementation. */
if (chlen != 1)
return ValkeyModule_ReplyWithError(ctx,
"ERR padding must be a single char");
if (chlen != 1) return ValkeyModule_ReplyWithError(ctx, "ERR padding must be a single char");
/* Here we use our pool allocator, for our throw-away allocation. */
padlen -= strlen;
char *buf = ValkeyModule_PoolAlloc(ctx,padlen+strlen);
char *buf = ValkeyModule_PoolAlloc(ctx, padlen + strlen);
for (long long j = 0; j < padlen; j++) buf[j] = *ch;
memcpy(buf+padlen,str,strlen);
memcpy(buf + padlen, str, strlen);
ValkeyModule_ReplyWithStringBuffer(ctx,buf,padlen+strlen);
ValkeyModule_ReplyWithStringBuffer(ctx, buf, padlen + strlen);
return VALKEYMODULE_OK;
}
/* This function must be present on each module. It is used in order to
* register the commands into the server. */
int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (ValkeyModule_Init(ctx,"helloworld",1,VALKEYMODULE_APIVER_1)
== VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
if (ValkeyModule_Init(ctx, "helloworld", 1, VALKEYMODULE_APIVER_1) == VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
/* Log the list of parameters passing loading the module. */
for (int j = 0; j < argc; j++) {
const char *s = ValkeyModule_StringPtrLen(argv[j],NULL);
const char *s = ValkeyModule_StringPtrLen(argv[j], NULL);
printf("Module loaded with ARGV[%d] = %s\n", j, s);
}
if (ValkeyModule_CreateCommand(ctx,"hello.simple",
HelloSimple_ValkeyCommand,"readonly",0,0,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.simple", HelloSimple_ValkeyCommand, "readonly", 0, 0, 0) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.push.native",
HelloPushNative_ValkeyCommand,"write deny-oom",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.push.native", HelloPushNative_ValkeyCommand, "write deny-oom", 1, 1,
1) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.push.call",
HelloPushCall_ValkeyCommand,"write deny-oom",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.push.call", HelloPushCall_ValkeyCommand, "write deny-oom", 1, 1, 1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.push.call2",
HelloPushCall2_ValkeyCommand,"write deny-oom",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.push.call2", HelloPushCall2_ValkeyCommand, "write deny-oom", 1, 1, 1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.list.sum.len",
HelloListSumLen_ValkeyCommand,"readonly",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.list.sum.len", HelloListSumLen_ValkeyCommand, "readonly", 1, 1, 1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.list.splice",
HelloListSplice_ValkeyCommand,"write deny-oom",1,2,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.list.splice", HelloListSplice_ValkeyCommand, "write deny-oom", 1, 2,
1) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.list.splice.auto",
HelloListSpliceAuto_ValkeyCommand,
"write deny-oom",1,2,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.list.splice.auto", HelloListSpliceAuto_ValkeyCommand, "write deny-oom",
1, 2, 1) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.rand.array",
HelloRandArray_ValkeyCommand,"readonly",0,0,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.rand.array", HelloRandArray_ValkeyCommand, "readonly", 0, 0, 0) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.repl1",
HelloRepl1_ValkeyCommand,"write",0,0,0) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.repl1", HelloRepl1_ValkeyCommand, "write", 0, 0, 0) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.repl2",
HelloRepl2_ValkeyCommand,"write",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.repl2", HelloRepl2_ValkeyCommand, "write", 1, 1, 1) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.toggle.case",
HelloToggleCase_ValkeyCommand,"write",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.toggle.case", HelloToggleCase_ValkeyCommand, "write", 1, 1, 1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.more.expire",
HelloMoreExpire_ValkeyCommand,"write",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.more.expire", HelloMoreExpire_ValkeyCommand, "write", 1, 1, 1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.zsumrange",
HelloZsumRange_ValkeyCommand,"readonly",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.zsumrange", HelloZsumRange_ValkeyCommand, "readonly", 1, 1, 1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.lexrange",
HelloLexRange_ValkeyCommand,"readonly",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.lexrange", HelloLexRange_ValkeyCommand, "readonly", 1, 1, 1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.hcopy",
HelloHCopy_ValkeyCommand,"write deny-oom",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.hcopy", HelloHCopy_ValkeyCommand, "write deny-oom", 1, 1, 1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
if (ValkeyModule_CreateCommand(ctx,"hello.leftpad",
HelloLeftPad_ValkeyCommand,"",1,1,1) == VALKEYMODULE_ERR)
if (ValkeyModule_CreateCommand(ctx, "hello.leftpad", HelloLeftPad_ValkeyCommand, "", 1, 1, 1) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;
return VALKEYMODULE_OK;

View File

@ -128,8 +128,8 @@ client *createClient(connection *conn) {
connSetPrivateData(conn, c);
}
c->buf = zmalloc_usable(PROTO_REPLY_CHUNK_BYTES, &c->buf_usable_size);
selectDb(c,0);
uint64_t client_id = atomic_fetch_add_explicit(&server.next_client_id,1,memory_order_relaxed);
selectDb(c, 0);
uint64_t client_id = atomic_fetch_add_explicit(&server.next_client_id, 1, memory_order_relaxed);
c->id = client_id;
#ifdef LOG_REQ_RES
reqresReset(c, 0);
@ -1943,7 +1943,7 @@ int _writeToClient(client *c, ssize_t *nwritten) {
* thread safe. */
int writeToClient(client *c, int handler_installed) {
/* Update total number of writes on server */
atomic_fetch_add_explicit(&server.stat_total_writes_processed,1, memory_order_relaxed);
atomic_fetch_add_explicit(&server.stat_total_writes_processed, 1, memory_order_relaxed);
ssize_t nwritten = 0, totwritten = 0;
@ -2611,7 +2611,7 @@ void readQueryFromClient(connection *conn) {
if (postponeClientRead(c)) return;
/* Update total number of reads on server */
atomic_fetch_add_explicit(&server.stat_total_reads_processed,1,memory_order_relaxed);
atomic_fetch_add_explicit(&server.stat_total_reads_processed, 1, memory_order_relaxed);
readlen = PROTO_IOBUF_LEN;
/* If this is a multi bulk request, and we are processing a bulk reply
@ -2677,9 +2677,9 @@ void readQueryFromClient(connection *conn) {
c->lastinteraction = server.unixtime;
if (c->flags & CLIENT_MASTER) {
c->read_reploff += nread;
atomic_fetch_add_explicit(&server.stat_net_repl_input_bytes,nread,memory_order_relaxed);
atomic_fetch_add_explicit(&server.stat_net_repl_input_bytes, nread, memory_order_relaxed);
} else {
atomic_fetch_add_explicit(&server.stat_net_input_bytes,nread,memory_order_relaxed);
atomic_fetch_add_explicit(&server.stat_net_input_bytes, nread, memory_order_relaxed);
}
c->net_input_bytes += nread;
@ -2698,7 +2698,7 @@ void readQueryFromClient(connection *conn) {
sdsfree(ci);
sdsfree(bytes);
freeClientAsync(c);
atomic_fetch_add_explicit(&server.stat_client_qbuf_limit_disconnections,1,memory_order_relaxed);
atomic_fetch_add_explicit(&server.stat_client_qbuf_limit_disconnections, 1, memory_order_relaxed);
goto done;
}
@ -4142,7 +4142,7 @@ pthread_t io_threads[IO_THREADS_MAX_NUM];
pthread_mutex_t io_threads_mutex[IO_THREADS_MAX_NUM];
threads_pending io_threads_pending[IO_THREADS_MAX_NUM];
int io_threads_op;
/* IO_THREADS_OP_IDLE, IO_THREADS_OP_READ or IO_THREADS_OP_WRITE. */ // TODO: should access to this be atomic??!
/* IO_THREADS_OP_IDLE, IO_THREADS_OP_READ or IO_THREADS_OP_WRITE. */ // TODO: should access to this be atomic??!
/* This is the list of clients each thread will serve when threaded I/O is
* used. We spawn io_threads_num-1 threads, since one is the main thread

View File

@ -2889,7 +2889,7 @@ void rdbLoadProgressCallback(rio *r, const void *buf, size_t len) {
processModuleLoadingProgressEvent(0);
}
if (server.repl_state == REPL_STATE_TRANSFER && rioCheckType(r) == RIO_TYPE_CONN) {
atomic_fetch_add_explicit(&server.stat_net_repl_input_bytes,len,memory_order_relaxed);
atomic_fetch_add_explicit(&server.stat_net_repl_input_bytes, len, memory_order_relaxed);
}
}

View File

@ -1376,8 +1376,8 @@ void sendBulkToSlave(connection *conn) {
freeClient(slave);
return;
}
atomic_fetch_add_explicit(&server.stat_net_repl_output_bytes,nwritten,memory_order_relaxed);
sdsrange(slave->replpreamble,nwritten,-1);
atomic_fetch_add_explicit(&server.stat_net_repl_output_bytes, nwritten, memory_order_relaxed);
sdsrange(slave->replpreamble, nwritten, -1);
if (sdslen(slave->replpreamble) == 0) {
sdsfree(slave->replpreamble);
slave->replpreamble = NULL;
@ -1404,7 +1404,7 @@ void sendBulkToSlave(connection *conn) {
return;
}
slave->repldboff += nwritten;
atomic_fetch_add_explicit(&server.stat_net_repl_output_bytes,nwritten,memory_order_relaxed);
atomic_fetch_add_explicit(&server.stat_net_repl_output_bytes, nwritten, memory_order_relaxed);
if (slave->repldboff == slave->repldbsize) {
closeRepldbfd(slave);
connSetWriteHandler(slave->conn, NULL);
@ -1446,7 +1446,7 @@ void rdbPipeWriteHandler(struct connection *conn) {
return;
} else {
slave->repldboff += nwritten;
atomic_fetch_add_explicit(&server.stat_net_repl_output_bytes,nwritten,memory_order_relaxed);
atomic_fetch_add_explicit(&server.stat_net_repl_output_bytes, nwritten, memory_order_relaxed);
if (slave->repldboff < server.rdb_pipe_bufflen) {
slave->repl_last_partial_write = server.unixtime;
return; /* more data to write.. */
@ -1519,7 +1519,7 @@ void rdbPipeReadHandler(struct aeEventLoop *eventLoop, int fd, void *clientData,
/* Note: when use diskless replication, 'repldboff' is the offset
* of 'rdb_pipe_buff' sent rather than the offset of entire RDB. */
slave->repldboff = nwritten;
atomic_fetch_add_explicit(&server.stat_net_repl_output_bytes,nwritten,memory_order_relaxed);
atomic_fetch_add_explicit(&server.stat_net_repl_output_bytes, nwritten, memory_order_relaxed);
}
/* If we were unable to write all the data to one of the replicas,
* setup write handler (and disable pipe read handler, below) */
@ -1827,7 +1827,7 @@ void readSyncBulkPayload(connection *conn) {
} else {
/* nread here is returned by connSyncReadLine(), which calls syncReadLine() and
* convert "\r\n" to '\0' so 1 byte is lost. */
atomic_fetch_add_explicit(&server.stat_net_repl_input_bytes,nread+1,memory_order_relaxed);
atomic_fetch_add_explicit(&server.stat_net_repl_input_bytes, nread + 1, memory_order_relaxed);
}
if (buf[0] == '-') {
@ -1896,7 +1896,7 @@ void readSyncBulkPayload(connection *conn) {
cancelReplicationHandshake(1);
return;
}
atomic_fetch_add_explicit(&server.stat_net_repl_input_bytes,nread,memory_order_relaxed);
atomic_fetch_add_explicit(&server.stat_net_repl_input_bytes, nread, memory_order_relaxed);
/* When a mark is used, we want to detect EOF asap in order to avoid
* writing the EOF mark into the file... */

View File

@ -1258,10 +1258,10 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
long long stat_net_input_bytes, stat_net_output_bytes;
long long stat_net_repl_input_bytes, stat_net_repl_output_bytes;
stat_net_input_bytes = atomic_load_explicit(&server.stat_net_input_bytes,memory_order_relaxed);
stat_net_output_bytes = atomic_load_explicit(&server.stat_net_output_bytes,memory_order_relaxed);
stat_net_repl_input_bytes = atomic_load_explicit(&server.stat_net_repl_input_bytes,memory_order_relaxed);
stat_net_repl_output_bytes = atomic_load_explicit(&server.stat_net_repl_output_bytes,memory_order_relaxed);
stat_net_input_bytes = atomic_load_explicit(&server.stat_net_input_bytes, memory_order_relaxed);
stat_net_output_bytes = atomic_load_explicit(&server.stat_net_output_bytes, memory_order_relaxed);
stat_net_repl_input_bytes = atomic_load_explicit(&server.stat_net_repl_input_bytes, memory_order_relaxed);
stat_net_repl_output_bytes = atomic_load_explicit(&server.stat_net_repl_output_bytes, memory_order_relaxed);
monotime current_time = getMonotonicUs();
long long factor = 1000000; // us
@ -1737,12 +1737,10 @@ void afterSleep(struct aeEventLoop *eventLoop) {
if (moduleCount()) {
mstime_t latency;
latencyStartMonitor(latency);
atomic_store_explicit(&server.module_gil_acquiring,1,memory_order_relaxed);
atomic_store_explicit(&server.module_gil_acquiring, 1, memory_order_relaxed);
moduleAcquireGIL();
atomic_store_explicit(&server.module_gil_acquiring,0,memory_order_relaxed);
moduleFireServerEvent(VALKEYMODULE_EVENT_EVENTLOOP,
VALKEYMODULE_SUBEVENT_EVENTLOOP_AFTER_SLEEP,
NULL);
atomic_store_explicit(&server.module_gil_acquiring, 0, memory_order_relaxed);
moduleFireServerEvent(VALKEYMODULE_EVENT_EVENTLOOP, VALKEYMODULE_SUBEVENT_EVENTLOOP_AFTER_SLEEP, NULL);
latencyEndMonitor(latency);
latencyAddSampleIfNeeded("module-acquire-GIL", latency);
}
@ -1991,7 +1989,7 @@ void initServerConfig(void) {
server.aof_flush_sleep = 0;
server.aof_last_fsync = time(NULL) * 1000;
server.aof_cur_timestamp = 0;
atomic_store_explicit(&server.aof_bio_fsync_status,C_OK,memory_order_relaxed);
atomic_store_explicit(&server.aof_bio_fsync_status, C_OK, memory_order_relaxed);
server.aof_rewrite_time_last = -1;
server.aof_rewrite_time_start = -1;
server.aof_lastbgrewrite_status = C_OK;
@ -2481,10 +2479,10 @@ void resetServerStats(void) {
server.stat_sync_partial_ok = 0;
server.stat_sync_partial_err = 0;
server.stat_io_reads_processed = 0;
atomic_store_explicit(&server.stat_total_reads_processed,0,memory_order_relaxed);
atomic_store_explicit(&server.stat_total_reads_processed, 0, memory_order_relaxed);
server.stat_io_writes_processed = 0;
atomic_store_explicit(&server.stat_total_writes_processed,0,memory_order_relaxed);
atomic_store_explicit(&server.stat_client_qbuf_limit_disconnections,0,memory_order_relaxed);
atomic_store_explicit(&server.stat_total_writes_processed, 0, memory_order_relaxed);
atomic_store_explicit(&server.stat_client_qbuf_limit_disconnections, 0, memory_order_relaxed);
server.stat_client_outbuf_limit_disconnections = 0;
for (j = 0; j < STATS_METRIC_COUNT; j++) {
server.inst_metric[j].idx = 0;
@ -2495,10 +2493,10 @@ void resetServerStats(void) {
server.stat_aof_rewrites = 0;
server.stat_rdb_saves = 0;
server.stat_aofrw_consecutive_failures = 0;
atomic_store_explicit(&server.stat_net_input_bytes,0,memory_order_relaxed);
atomic_store_explicit(&server.stat_net_output_bytes,0,memory_order_relaxed);
atomic_store_explicit(&server.stat_net_repl_input_bytes,0,memory_order_relaxed);
atomic_store_explicit(&server.stat_net_repl_output_bytes,0,memory_order_relaxed);
atomic_store_explicit(&server.stat_net_input_bytes, 0, memory_order_relaxed);
atomic_store_explicit(&server.stat_net_output_bytes, 0, memory_order_relaxed);
atomic_store_explicit(&server.stat_net_repl_input_bytes, 0, memory_order_relaxed);
atomic_store_explicit(&server.stat_net_repl_output_bytes, 0, memory_order_relaxed);
server.stat_unexpected_error_replies = 0;
server.stat_total_error_replies = 0;
server.stat_dump_payload_sanitizations = 0;
@ -5530,7 +5528,7 @@ sds genValkeyInfoString(dict *section_dict, int all_sections, int everything) {
} else if (server.stat_current_save_keys_total) {
fork_perc = ((double)server.stat_current_save_keys_processed / server.stat_current_save_keys_total) * 100;
}
int aof_bio_fsync_status = atomic_load_explicit(&server.aof_bio_fsync_status,memory_order_relaxed);
int aof_bio_fsync_status = atomic_load_explicit(&server.aof_bio_fsync_status, memory_order_relaxed);
/* clang-format off */
info = sdscatprintf(info, "# Persistence\r\n" FMTARGS(
@ -5629,14 +5627,15 @@ sds genValkeyInfoString(dict *section_dict, int all_sections, int everything) {
long long current_active_defrag_time =
server.stat_last_active_defrag_time ? (long long)elapsedUs(server.stat_last_active_defrag_time) : 0;
long long stat_client_qbuf_limit_disconnections;
stat_total_reads_processed = atomic_load_explicit(&server.stat_total_reads_processed, memory_order_relaxed);
stat_total_writes_processed = atomic_load_explicit(&server.stat_total_writes_processed, memory_order_relaxed);
stat_net_input_bytes = atomic_load_explicit(&server.stat_net_input_bytes, memory_order_relaxed);
stat_net_output_bytes = atomic_load_explicit(&server.stat_net_output_bytes, memory_order_relaxed);
stat_net_repl_input_bytes = atomic_load_explicit(&server.stat_net_repl_input_bytes, memory_order_relaxed);
stat_net_repl_output_bytes = atomic_load_explicit(&server.stat_net_repl_output_bytes, memory_order_relaxed);
stat_client_qbuf_limit_disconnections = atomic_load_explicit(&server.stat_client_qbuf_limit_disconnections, memory_order_relaxed);
stat_client_qbuf_limit_disconnections =
atomic_load_explicit(&server.stat_client_qbuf_limit_disconnections, memory_order_relaxed);
if (sections++) info = sdscat(info, "\r\n");
/* clang-format off */

View File

@ -716,7 +716,7 @@ typedef enum {
#define LATENCY_HISTOGRAM_MAX_VALUE 1000000000L /* <= 1 secs */
#define LATENCY_HISTOGRAM_PRECISION \
2 /* Maintain a value precision of 2 significant digits across LATENCY_HISTOGRAM_MIN_VALUE and \
* LATENCY_HISTOGRAM_MAX_VALUE range. Value quantization within the range will thus be no larger than 1/100th \
* LATENCY_HISTOGRAM_MAX_VALUE range. Value quantization within the range will thus be no larger than 1/100th \
* (or 1%) of any value. The total size per histogram should sit around 40 KiB Bytes. */
/* Busy module flags, see busy_module_yield_flags */
@ -1671,13 +1671,13 @@ struct valkeyServer {
uint32_t paused_actions; /* Bitmask of actions that are currently paused */
list *postponed_clients; /* List of postponed clients */
pause_event client_pause_per_purpose[NUM_PAUSE_PURPOSES];
char neterr[ANET_ERR_LEN]; /* Error buffer for anet.c */
dict *migrate_cached_sockets;/* MIGRATE cached sockets */
_Atomic uint64_t next_client_id; /* Next client unique ID. Incremental. */
int protected_mode; /* Don't accept external connections. */
int io_threads_num; /* Number of IO threads to use. */
int io_threads_do_reads; /* Read and parse from IO threads? */
int io_threads_active; /* Is IO threads currently active? */
char neterr[ANET_ERR_LEN]; /* Error buffer for anet.c */
dict *migrate_cached_sockets; /* MIGRATE cached sockets */
_Atomic uint64_t next_client_id; /* Next client unique ID. Incremental. */
int protected_mode; /* Don't accept external connections. */
int io_threads_num; /* Number of IO threads to use. */
int io_threads_do_reads; /* Read and parse from IO threads? */
int io_threads_active; /* Is IO threads currently active? */
long long events_processed_while_blocked; /* processEventsWhileBlocked() */
int enable_protected_configs; /* Enable the modification of protected configs, see PROTECTED_ACTION_ALLOWED_* */
int enable_debug_cmd; /* Enable DEBUG commands, see PROTECTED_ACTION_ALLOWED_* */
@ -1698,61 +1698,64 @@ struct valkeyServer {
long long stat_expiredkeys; /* Number of expired keys */
double stat_expired_stale_perc; /* Percentage of keys probably expired */
long long stat_expired_time_cap_reached_count; /* Early expire cycle stops.*/
long long stat_expire_cycle_time_used; /* Cumulative microseconds used. */
long long stat_evictedkeys; /* Number of evicted keys (maxmemory) */
long long stat_evictedclients; /* Number of evicted clients */
long long stat_evictedscripts; /* Number of evicted lua scripts. */
long long stat_total_eviction_exceeded_time; /* Total time over the memory limit, unit us */
monotime stat_last_eviction_exceeded_time; /* Timestamp of current eviction start, unit us */
long long stat_keyspace_hits; /* Number of successful lookups of keys */
long long stat_keyspace_misses; /* Number of failed lookups of keys */
long long stat_active_defrag_hits; /* number of allocations moved */
long long stat_active_defrag_misses; /* number of allocations scanned but not moved */
long long stat_active_defrag_key_hits; /* number of keys with moved allocations */
long long stat_active_defrag_key_misses;/* number of keys scanned and not moved */
long long stat_active_defrag_scanned; /* number of dictEntries scanned */
long long stat_total_active_defrag_time; /* Total time memory fragmentation over the limit, unit us */
monotime stat_last_active_defrag_time; /* Timestamp of current active defrag start */
size_t stat_peak_memory; /* Max used memory record */
long long stat_aof_rewrites; /* number of aof file rewrites performed */
long long stat_aofrw_consecutive_failures; /* The number of consecutive failures of aofrw */
long long stat_rdb_saves; /* number of rdb saves performed */
long long stat_fork_time; /* Time needed to perform latest fork() */
double stat_fork_rate; /* Fork rate in GB/sec. */
long long stat_total_forks; /* Total count of fork. */
long long stat_rejected_conn; /* Clients rejected because of maxclients */
long long stat_sync_full; /* Number of full resyncs with slaves. */
long long stat_sync_partial_ok; /* Number of accepted PSYNC requests. */
long long stat_sync_partial_err;/* Number of unaccepted PSYNC requests. */
list *slowlog; /* SLOWLOG list of commands */
long long slowlog_entry_id; /* SLOWLOG current entry ID */
long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */
unsigned long slowlog_max_len; /* SLOWLOG max number of items logged */
struct malloc_stats cron_malloc_stats; /* sampled in serverCron(). */
_Atomic long long stat_net_input_bytes; /* Bytes read from network. */
_Atomic long long stat_net_output_bytes; /* Bytes written to network. */
_Atomic long long stat_net_repl_input_bytes; /* Bytes read during replication, added to stat_net_input_bytes in 'info'. */
_Atomic long long stat_net_repl_output_bytes; /* Bytes written during replication, added to stat_net_output_bytes in 'info'. */
size_t stat_current_cow_peak; /* Peak size of copy on write bytes. */
size_t stat_current_cow_bytes; /* Copy on write bytes while child is active. */
monotime stat_current_cow_updated; /* Last update time of stat_current_cow_bytes */
size_t stat_current_save_keys_processed; /* Processed keys while child is active. */
size_t stat_current_save_keys_total; /* Number of keys when child started. */
size_t stat_rdb_cow_bytes; /* Copy on write bytes during RDB saving. */
size_t stat_aof_cow_bytes; /* Copy on write bytes during AOF rewrite. */
size_t stat_module_cow_bytes; /* Copy on write bytes during module fork. */
double stat_module_progress; /* Module save progress. */
size_t stat_clients_type_memory[CLIENT_TYPE_COUNT];/* Mem usage by type */
size_t stat_cluster_links_memory; /* Mem usage by cluster links */
long long stat_unexpected_error_replies; /* Number of unexpected (aof-loading, replica to master, etc.) error replies */
long long stat_expire_cycle_time_used; /* Cumulative microseconds used. */
long long stat_evictedkeys; /* Number of evicted keys (maxmemory) */
long long stat_evictedclients; /* Number of evicted clients */
long long stat_evictedscripts; /* Number of evicted lua scripts. */
long long stat_total_eviction_exceeded_time; /* Total time over the memory limit, unit us */
monotime stat_last_eviction_exceeded_time; /* Timestamp of current eviction start, unit us */
long long stat_keyspace_hits; /* Number of successful lookups of keys */
long long stat_keyspace_misses; /* Number of failed lookups of keys */
long long stat_active_defrag_hits; /* number of allocations moved */
long long stat_active_defrag_misses; /* number of allocations scanned but not moved */
long long stat_active_defrag_key_hits; /* number of keys with moved allocations */
long long stat_active_defrag_key_misses; /* number of keys scanned and not moved */
long long stat_active_defrag_scanned; /* number of dictEntries scanned */
long long stat_total_active_defrag_time; /* Total time memory fragmentation over the limit, unit us */
monotime stat_last_active_defrag_time; /* Timestamp of current active defrag start */
size_t stat_peak_memory; /* Max used memory record */
long long stat_aof_rewrites; /* number of aof file rewrites performed */
long long stat_aofrw_consecutive_failures; /* The number of consecutive failures of aofrw */
long long stat_rdb_saves; /* number of rdb saves performed */
long long stat_fork_time; /* Time needed to perform latest fork() */
double stat_fork_rate; /* Fork rate in GB/sec. */
long long stat_total_forks; /* Total count of fork. */
long long stat_rejected_conn; /* Clients rejected because of maxclients */
long long stat_sync_full; /* Number of full resyncs with slaves. */
long long stat_sync_partial_ok; /* Number of accepted PSYNC requests. */
long long stat_sync_partial_err; /* Number of unaccepted PSYNC requests. */
list *slowlog; /* SLOWLOG list of commands */
long long slowlog_entry_id; /* SLOWLOG current entry ID */
long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */
unsigned long slowlog_max_len; /* SLOWLOG max number of items logged */
struct malloc_stats cron_malloc_stats; /* sampled in serverCron(). */
_Atomic long long stat_net_input_bytes; /* Bytes read from network. */
_Atomic long long stat_net_output_bytes; /* Bytes written to network. */
_Atomic long long
stat_net_repl_input_bytes; /* Bytes read during replication, added to stat_net_input_bytes in 'info'. */
_Atomic long long
stat_net_repl_output_bytes; /* Bytes written during replication, added to stat_net_output_bytes in 'info'. */
size_t stat_current_cow_peak; /* Peak size of copy on write bytes. */
size_t stat_current_cow_bytes; /* Copy on write bytes while child is active. */
monotime stat_current_cow_updated; /* Last update time of stat_current_cow_bytes */
size_t stat_current_save_keys_processed; /* Processed keys while child is active. */
size_t stat_current_save_keys_total; /* Number of keys when child started. */
size_t stat_rdb_cow_bytes; /* Copy on write bytes during RDB saving. */
size_t stat_aof_cow_bytes; /* Copy on write bytes during AOF rewrite. */
size_t stat_module_cow_bytes; /* Copy on write bytes during module fork. */
double stat_module_progress; /* Module save progress. */
size_t stat_clients_type_memory[CLIENT_TYPE_COUNT]; /* Mem usage by type */
size_t stat_cluster_links_memory; /* Mem usage by cluster links */
long long
stat_unexpected_error_replies; /* Number of unexpected (aof-loading, replica to master, etc.) error replies */
long long stat_total_error_replies; /* Total number of issued error replies ( command + rejected errors ) */
long long stat_dump_payload_sanitizations; /* Number deep dump payloads integrity validations. */
long long stat_io_reads_processed; /* Number of read events processed by IO / Main threads */
long long stat_io_writes_processed; /* Number of write events processed by IO / Main threads */
_Atomic long long stat_total_reads_processed; /* Total number of read events processed */
_Atomic long long stat_total_writes_processed; /* Total number of write events processed */
_Atomic long long stat_client_qbuf_limit_disconnections; /* Total number of clients reached query buf length limit */
long long stat_client_outbuf_limit_disconnections; /* Total number of clients reached output buf length limit */
long long stat_dump_payload_sanitizations; /* Number deep dump payloads integrity validations. */
long long stat_io_reads_processed; /* Number of read events processed by IO / Main threads */
long long stat_io_writes_processed; /* Number of write events processed by IO / Main threads */
_Atomic long long stat_total_reads_processed; /* Total number of read events processed */
_Atomic long long stat_total_writes_processed; /* Total number of write events processed */
_Atomic long long stat_client_qbuf_limit_disconnections; /* Total number of clients reached query buf length limit */
long long stat_client_outbuf_limit_disconnections; /* Total number of clients reached output buf length limit */
/* The following two are used to track instantaneous metrics, like
* number of operations per second, network traffic. */
struct {
@ -1812,43 +1815,43 @@ struct valkeyServer {
unsigned int max_new_conns_per_cycle; /* The maximum number of tcp connections that will be accepted during each
invocation of the event loop. */
/* AOF persistence */
int aof_enabled; /* AOF configuration */
int aof_state; /* AOF_(ON|OFF|WAIT_REWRITE) */
int aof_fsync; /* Kind of fsync() policy */
char *aof_filename; /* Basename of the AOF file and manifest file */
char *aof_dirname; /* Name of the AOF directory */
int aof_no_fsync_on_rewrite; /* Don't fsync if a rewrite is in prog. */
int aof_rewrite_perc; /* Rewrite AOF if % growth is > M and... */
off_t aof_rewrite_min_size; /* the AOF file is at least N bytes. */
off_t aof_rewrite_base_size; /* AOF size on latest startup or rewrite. */
off_t aof_current_size; /* AOF current size (Including BASE + INCRs). */
off_t aof_last_incr_size; /* The size of the latest incr AOF. */
off_t aof_last_incr_fsync_offset; /* AOF offset which is already requested to be synced to disk.
* Compare with the aof_last_incr_size. */
int aof_flush_sleep; /* Micros to sleep before flush. (used by tests) */
int aof_rewrite_scheduled; /* Rewrite once BGSAVE terminates. */
sds aof_buf; /* AOF buffer, written before entering the event loop */
int aof_fd; /* File descriptor of currently selected AOF file */
int aof_selected_db; /* Currently selected DB in AOF */
int aof_enabled; /* AOF configuration */
int aof_state; /* AOF_(ON|OFF|WAIT_REWRITE) */
int aof_fsync; /* Kind of fsync() policy */
char *aof_filename; /* Basename of the AOF file and manifest file */
char *aof_dirname; /* Name of the AOF directory */
int aof_no_fsync_on_rewrite; /* Don't fsync if a rewrite is in prog. */
int aof_rewrite_perc; /* Rewrite AOF if % growth is > M and... */
off_t aof_rewrite_min_size; /* the AOF file is at least N bytes. */
off_t aof_rewrite_base_size; /* AOF size on latest startup or rewrite. */
off_t aof_current_size; /* AOF current size (Including BASE + INCRs). */
off_t aof_last_incr_size; /* The size of the latest incr AOF. */
off_t aof_last_incr_fsync_offset; /* AOF offset which is already requested to be synced to disk.
* Compare with the aof_last_incr_size. */
int aof_flush_sleep; /* Micros to sleep before flush. (used by tests) */
int aof_rewrite_scheduled; /* Rewrite once BGSAVE terminates. */
sds aof_buf; /* AOF buffer, written before entering the event loop */
int aof_fd; /* File descriptor of currently selected AOF file */
int aof_selected_db; /* Currently selected DB in AOF */
mstime_t aof_flush_postponed_start; /* mstime of postponed AOF flush */
mstime_t aof_last_fsync; /* mstime of last fsync() */
time_t aof_rewrite_time_last; /* Time used by last AOF rewrite run. */
time_t aof_rewrite_time_start; /* Current AOF rewrite start time. */
time_t aof_cur_timestamp; /* Current record timestamp in AOF */
int aof_timestamp_enabled; /* Enable record timestamp in AOF */
int aof_lastbgrewrite_status; /* C_OK or C_ERR */
unsigned long aof_delayed_fsync; /* delayed AOF fsync() counter */
int aof_rewrite_incremental_fsync;/* fsync incrementally while aof rewriting? */
int rdb_save_incremental_fsync; /* fsync incrementally while rdb saving? */
int aof_last_write_status; /* C_OK or C_ERR */
int aof_last_write_errno; /* Valid if aof write/fsync status is ERR */
int aof_load_truncated; /* Don't stop on unexpected AOF EOF. */
int aof_use_rdb_preamble; /* Specify base AOF to use RDB encoding on AOF rewrites. */
_Atomic int aof_bio_fsync_status; /* Status of AOF fsync in bio job. */
_Atomic int aof_bio_fsync_errno; /* Errno of AOF fsync in bio job. */
aofManifest *aof_manifest; /* Used to track AOFs. */
int aof_disable_auto_gc; /* If disable automatically deleting HISTORY type AOFs?
default no. (for testings). */
time_t aof_rewrite_time_last; /* Time used by last AOF rewrite run. */
time_t aof_rewrite_time_start; /* Current AOF rewrite start time. */
time_t aof_cur_timestamp; /* Current record timestamp in AOF */
int aof_timestamp_enabled; /* Enable record timestamp in AOF */
int aof_lastbgrewrite_status; /* C_OK or C_ERR */
unsigned long aof_delayed_fsync; /* delayed AOF fsync() counter */
int aof_rewrite_incremental_fsync; /* fsync incrementally while aof rewriting? */
int rdb_save_incremental_fsync; /* fsync incrementally while rdb saving? */
int aof_last_write_status; /* C_OK or C_ERR */
int aof_last_write_errno; /* Valid if aof write/fsync status is ERR */
int aof_load_truncated; /* Don't stop on unexpected AOF EOF. */
int aof_use_rdb_preamble; /* Specify base AOF to use RDB encoding on AOF rewrites. */
_Atomic int aof_bio_fsync_status; /* Status of AOF fsync in bio job. */
_Atomic int aof_bio_fsync_errno; /* Errno of AOF fsync in bio job. */
aofManifest *aof_manifest; /* Used to track AOFs. */
int aof_disable_auto_gc; /* If disable automatically deleting HISTORY type AOFs?
default no. (for testings). */
/* RDB persistence */
long long dirty; /* Changes to DB from the last save */
@ -1906,35 +1909,35 @@ struct valkeyServer {
int shutdown_on_sigterm; /* Shutdown flags configured for SIGTERM. */
/* Replication (master) */
char replid[CONFIG_RUN_ID_SIZE+1]; /* My current replication ID. */
char replid2[CONFIG_RUN_ID_SIZE+1]; /* replid inherited from master*/
long long master_repl_offset; /* My current replication offset */
long long second_replid_offset; /* Accept offsets up to this for replid2. */
_Atomic long long fsynced_reploff_pending;/* Largest replication offset to
* potentially have been fsynced, applied to
fsynced_reploff only when AOF state is AOF_ON
(not during the initial rewrite) */
long long fsynced_reploff; /* Largest replication offset that has been confirmed to be fsynced */
int slaveseldb; /* Last SELECTed DB in replication output */
int repl_ping_slave_period; /* Master pings the slave every N seconds */
replBacklog *repl_backlog; /* Replication backlog for partial syncs */
long long repl_backlog_size; /* Backlog circular buffer size */
time_t repl_backlog_time_limit; /* Time without slaves after the backlog
gets released. */
time_t repl_no_slaves_since; /* We have no slaves since that time.
Only valid if server.slaves len is 0. */
int repl_min_slaves_to_write; /* Min number of slaves to write. */
int repl_min_slaves_max_lag; /* Max lag of <count> slaves to write. */
int repl_good_slaves_count; /* Number of slaves with lag <= max_lag. */
int repl_diskless_sync; /* Master send RDB to slaves sockets directly. */
int repl_diskless_load; /* Slave parse RDB directly from the socket.
* see REPL_DISKLESS_LOAD_* enum */
int repl_diskless_sync_delay; /* Delay to start a diskless repl BGSAVE. */
int repl_diskless_sync_max_replicas;/* Max replicas for diskless repl BGSAVE
* delay (start sooner if they all connect). */
size_t repl_buffer_mem; /* The memory of replication buffer. */
list *repl_buffer_blocks; /* Replication buffers blocks list
* (serving replica clients and repl backlog) */
char replid[CONFIG_RUN_ID_SIZE + 1]; /* My current replication ID. */
char replid2[CONFIG_RUN_ID_SIZE + 1]; /* replid inherited from master*/
long long master_repl_offset; /* My current replication offset */
long long second_replid_offset; /* Accept offsets up to this for replid2. */
_Atomic long long fsynced_reploff_pending; /* Largest replication offset to
* potentially have been fsynced, applied to
fsynced_reploff only when AOF state is AOF_ON
(not during the initial rewrite) */
long long fsynced_reploff; /* Largest replication offset that has been confirmed to be fsynced */
int slaveseldb; /* Last SELECTed DB in replication output */
int repl_ping_slave_period; /* Master pings the slave every N seconds */
replBacklog *repl_backlog; /* Replication backlog for partial syncs */
long long repl_backlog_size; /* Backlog circular buffer size */
time_t repl_backlog_time_limit; /* Time without slaves after the backlog
gets released. */
time_t repl_no_slaves_since; /* We have no slaves since that time.
Only valid if server.slaves len is 0. */
int repl_min_slaves_to_write; /* Min number of slaves to write. */
int repl_min_slaves_max_lag; /* Max lag of <count> slaves to write. */
int repl_good_slaves_count; /* Number of slaves with lag <= max_lag. */
int repl_diskless_sync; /* Master send RDB to slaves sockets directly. */
int repl_diskless_load; /* Slave parse RDB directly from the socket.
* see REPL_DISKLESS_LOAD_* enum */
int repl_diskless_sync_delay; /* Delay to start a diskless repl BGSAVE. */
int repl_diskless_sync_max_replicas; /* Max replicas for diskless repl BGSAVE
* delay (start sooner if they all connect). */
size_t repl_buffer_mem; /* The memory of replication buffer. */
list *repl_buffer_blocks; /* Replication buffers blocks list
* (serving replica clients and repl backlog) */
/* Replication (slave) */
char *masteruser; /* AUTH with this user and masterauth with master */
sds masterauth; /* AUTH with this password with master */
@ -2018,13 +2021,13 @@ struct valkeyServer {
int list_max_listpack_size;
int list_compress_depth;
/* time cache */
_Atomic time_t unixtime; /* Unix time sampled every cron cycle. */
time_t timezone; /* Cached timezone. As set by tzset(). */
int daylight_active; /* Currently in daylight saving time. */
mstime_t mstime; /* 'unixtime' in milliseconds. */
ustime_t ustime; /* 'unixtime' in microseconds. */
mstime_t cmd_time_snapshot; /* Time snapshot of the root execution nesting. */
size_t blocking_op_nesting; /* Nesting level of blocking operation, used to reset blocked_last_cron. */
_Atomic time_t unixtime; /* Unix time sampled every cron cycle. */
time_t timezone; /* Cached timezone. As set by tzset(). */
int daylight_active; /* Currently in daylight saving time. */
mstime_t mstime; /* 'unixtime' in milliseconds. */
ustime_t ustime; /* 'unixtime' in microseconds. */
mstime_t cmd_time_snapshot; /* Time snapshot of the root execution nesting. */
size_t blocking_op_nesting; /* Nesting level of blocking operation, used to reset blocked_last_cron. */
long long blocked_last_cron; /* Indicate the mstime of the last time we did cron jobs from a blocking operation */
/* Pubsub */
kvstore *pubsub_channels; /* Map channels to list of subscribed clients */

View File

@ -112,7 +112,7 @@ __attribute__((noinline)) int ThreadsManager_runOnThreads(pid_t *tids, size_t ti
static int test_and_start(void) {
/* atomic_exchange_explicit sets the variable to 1 and returns the previous value */
int prev_state = atomic_exchange_explicit(&g_in_progress,1,memory_order_relaxed);
int prev_state = atomic_exchange_explicit(&g_in_progress, 1, memory_order_relaxed);
/* If prev_state is 1, g_in_progress was on. */
return prev_state;
@ -123,7 +123,7 @@ __attribute__((noinline)) static void invoke_callback(int sig) {
run_on_thread_cb callback = g_callback;
if (callback) {
callback();
atomic_fetch_add_explicit(&g_num_threads_done,1,memory_order_relaxed);
atomic_fetch_add_explicit(&g_num_threads_done, 1, memory_order_relaxed);
} else {
serverLogFromHandler(LL_WARNING, "tid %ld: ThreadsManager g_callback is NULL", syscall(SYS_gettid));
}
@ -145,7 +145,7 @@ static void wait_threads(void) {
/* Sleep a bit to yield to other threads. */
/* usleep isn't listed as signal safe, so we use select instead */
select(0, NULL, NULL, NULL, &tv);
curr_done_count = atomic_load_explicit(&g_num_threads_done,memory_order_relaxed);
curr_done_count = atomic_load_explicit(&g_num_threads_done, memory_order_relaxed);
clock_gettime(CLOCK_REALTIME, &curr_time);
} while (curr_done_count < g_tids_len && curr_time.tv_sec <= timeout_time.tv_sec);
@ -160,7 +160,7 @@ static void ThreadsManager_cleanups(void) {
g_num_threads_done = 0;
/* Lastly, turn off g_in_progress */
atomic_store_explicit(&g_in_progress,0,memory_order_relaxed);
atomic_store_explicit(&g_in_progress, 0, memory_order_relaxed);
}
#else

View File

@ -16,13 +16,13 @@ int test_crc64(int argc, char **argv, int flags) {
TEST_ASSERT_MESSAGE("[calcula]: CRC64 '123456789'", (uint64_t)crc64(0, numbers, 9) == 16845390139448941002ull);
unsigned char li[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
"do eiusmod tempor incididunt ut labore et dolore magna "
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
"ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis "
"aute irure dolor in reprehenderit in voluptate velit esse "
"cillum dolore eu fugiat nulla pariatur. Excepteur sint "
"occaecat cupidatat non proident, sunt in culpa qui officia "
"deserunt mollit anim id est laborum.";
"do eiusmod tempor incididunt ut labore et dolore magna "
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
"ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis "
"aute irure dolor in reprehenderit in voluptate velit esse "
"cillum dolore eu fugiat nulla pariatur. Excepteur sint "
"occaecat cupidatat non proident, sunt in culpa qui officia "
"deserunt mollit anim id est laborum.";
TEST_ASSERT_MESSAGE("[calcula]: CRC64 TEXT'", (uint64_t)_crc64(0, li, sizeof(li)) == 14373597793578550195ull);
TEST_ASSERT_MESSAGE("[calcula]: CRC64 TEXT", (uint64_t)crc64(0, li, sizeof(li)) == 14373597793578550195ull);

View File

@ -20,7 +20,7 @@ long long _ustime(void) {
long long ust;
gettimeofday(&tv, NULL);
ust = ((long long)tv.tv_sec)*1000000;
ust = ((long long)tv.tv_sec) * 1000000;
ust += tv.tv_usec;
return ust;
}
@ -28,18 +28,17 @@ long long _ustime(void) {
static int bench_crc64(unsigned char *data, uint64_t size, long long passes, uint64_t check, char *name, int csv) {
uint64_t min = size, hash = 0;
long long original_start = _ustime(), original_end;
for (long long i=passes; i > 0; i--) {
for (long long i = passes; i > 0; i--) {
hash = crc64(0, data, size);
}
original_end = _ustime();
min = (original_end - original_start) * 1000 / passes;
/* approximate nanoseconds without nstime */
if (csv) {
printf("%s,%" PRIu64 ",%" PRIu64 ",%d\n",
name, size, (1000 * size) / min, hash == check);
printf("%s,%" PRIu64 ",%" PRIu64 ",%d\n", name, size, (1000 * size) / min, hash == check);
} else {
TEST_PRINT_INFO("test size=%" PRIu64 " algorithm=%s %" PRIu64 " M/sec matches=%d",
size, name, (1000 * size) / min, hash == check);
TEST_PRINT_INFO("test size=%" PRIu64 " algorithm=%s %" PRIu64 " M/sec matches=%d", size, name,
(1000 * size) / min, hash == check);
}
return hash != check;
}
@ -49,7 +48,7 @@ const uint64_t BENCH_RPOLY = UINT64_C(0x95ac9329ac4bc9b5);
static void bench_combine(char *label, uint64_t size, uint64_t expect, int csv) {
uint64_t min = size, start = expect, thash = expect ^ (expect >> 17);
long long original_start = _ustime(), original_end;
for (int i=0; i < 1000; i++) {
for (int i = 0; i < 1000; i++) {
crc64_combine(thash, start, size, BENCH_RPOLY, 64);
}
original_end = _ustime();
@ -67,8 +66,8 @@ static void genBenchmarkRandomData(char *data, int count) {
int i = 0;
while (count--) {
state = (state*1103515245+12345);
data[i++] = '0'+((state>>16)&63);
state = (state * 1103515245 + 12345);
data[i++] = '0' + ((state >> 16) & 63);
}
}
@ -84,29 +83,27 @@ int test_crc64combine(int argc, char **argv, int flags) {
int i, lastarg, csv = 0, loop = 0, combine = 0;
again:
for (i = 3; i < argc; i++) {
lastarg = (i == (argc-1));
if (!strcmp(argv[i],"--help")) {
lastarg = (i == (argc - 1));
if (!strcmp(argv[i], "--help")) {
goto usage;
} else if (!strcmp(argv[i],"--csv")) {
} else if (!strcmp(argv[i], "--csv")) {
csv = 1;
} else if (!strcmp(argv[i],"-l")) {
} else if (!strcmp(argv[i], "-l")) {
loop = 1;
} else if (!strcmp(argv[i],"--crc")) {
} else if (!strcmp(argv[i], "--crc")) {
if (lastarg) goto invalid;
crc64_test_size = atoll(argv[++i]);
} else if (!strcmp(argv[i],"--combine")) {
} else if (!strcmp(argv[i], "--combine")) {
combine = 1;
} else {
invalid:
printf("Invalid option \"%s\" or option argument missing\n\n",argv[i]);
usage:
printf(
"Usage: --single test_crc64combine.c [OPTIONS]\n\n"
" --csv Output in CSV format\n"
" -l Loop. Run the tests forever\n"
" --crc <bytes> Benchmark crc64 faster options, using a buffer this big, and quit when done.\n"
" --combine Benchmark crc64 combine value ranges and timings.\n"
);
invalid:
printf("Invalid option \"%s\" or option argument missing\n\n", argv[i]);
usage:
printf("Usage: --single test_crc64combine.c [OPTIONS]\n\n"
" --csv Output in CSV format\n"
" -l Loop. Run the tests forever\n"
" --crc <bytes> Benchmark crc64 faster options, using a buffer this big, and quit when done.\n"
" --combine Benchmark crc64 combine value ranges and timings.\n");
return 1;
}
}
@ -115,11 +112,11 @@ usage:
long long init_start, init_end;
do {
unsigned char* data = NULL;
unsigned char *data = NULL;
uint64_t passes = 0;
if (crc64_test_size) {
data = zmalloc(crc64_test_size);
genBenchmarkRandomData((char*)data, crc64_test_size);
genBenchmarkRandomData((char *)data, crc64_test_size);
/* We want to hash about 1 gig of data in total, looped, to get a good
* idea of our performance.
*/
@ -130,22 +127,22 @@ usage:
crc64_init();
/* warm up the cache */
set_crc64_cutoffs(crc64_test_size+1, crc64_test_size+1);
set_crc64_cutoffs(crc64_test_size + 1, crc64_test_size + 1);
uint64_t expect = crc64(0, data, crc64_test_size);
if (!combine && crc64_test_size) {
if (csv && init_this_loop) printf("algorithm,buffer,performance,crc64_matches\n");
/* get the single-character version for single-byte Redis behavior */
set_crc64_cutoffs(0, crc64_test_size+1);
set_crc64_cutoffs(0, crc64_test_size + 1);
if (bench_crc64(data, crc64_test_size, passes, expect, "crc_1byte", csv)) return 1;
set_crc64_cutoffs(crc64_test_size+1, crc64_test_size+1);
set_crc64_cutoffs(crc64_test_size + 1, crc64_test_size + 1);
/* run with 8-byte "single" path, crcfaster */
if (bench_crc64(data, crc64_test_size, passes, expect, "crcspeed", csv)) return 1;
/* run with dual 8-byte paths */
set_crc64_cutoffs(1, crc64_test_size+1);
set_crc64_cutoffs(1, crc64_test_size + 1);
if (bench_crc64(data, crc64_test_size, passes, expect, "crcdual", csv)) return 1;
/* run with tri 8-byte paths */
@ -161,11 +158,7 @@ usage:
if (combine) {
if (init_this_loop) {
init_start = _ustime();
crc64_combine(
UINT64_C(0xdeadbeefdeadbeef),
UINT64_C(0xfeebdaedfeebdaed),
INIT_SIZE,
BENCH_RPOLY, 64);
crc64_combine(UINT64_C(0xdeadbeefdeadbeef), UINT64_C(0xfeebdaedfeebdaed), INIT_SIZE, BENCH_RPOLY, 64);
init_end = _ustime();
init_end -= init_start;

View File

@ -10,15 +10,15 @@ int test_endianconv(int argc, char *argv[], int flags) {
char buf[32];
snprintf(buf,sizeof(buf),"ciaoroma");
snprintf(buf, sizeof(buf), "ciaoroma");
memrev16(buf);
TEST_ASSERT(!strcmp(buf, "icaoroma"));
snprintf(buf,sizeof(buf),"ciaoroma");
snprintf(buf, sizeof(buf), "ciaoroma");
memrev32(buf);
TEST_ASSERT(!strcmp(buf, "oaicroma"));
snprintf(buf,sizeof(buf),"ciaoroma");
snprintf(buf, sizeof(buf), "ciaoroma");
memrev64(buf);
TEST_ASSERT(!strcmp(buf, "amoroaic"));

View File

@ -1,4 +1,5 @@
/* Do not modify this file, it's automatically generated from utils/generate-unit-test-header.py */
/* clang-format off */
typedef int unitTestProc(int argc, char **argv, int flags);
typedef struct unitTest {

View File

@ -1,5 +1,5 @@
/* A very simple test framework for valkey. See unit/README.me for more information on usage.
*
*
* Example:
*
* int test_example(int argc, char *argv[], int flags) {
@ -16,30 +16,30 @@
#include <stdio.h>
/* The flags are the following:
* --accurate: Runs tests with more iterations.
* --large-memory: Enables tests that consume more than 100mb.
* --single: A flag to indicate a specific test file was executed. */
#define UNIT_TEST_ACCURATE (1<<0)
#define UNIT_TEST_LARGE_MEMORY (1<<1)
#define UNIT_TEST_SINGLE (1<<2)
* --accurate: Runs tests with more iterations.
* --large-memory: Enables tests that consume more than 100mb.
* --single: A flag to indicate a specific test file was executed. */
#define UNIT_TEST_ACCURATE (1 << 0)
#define UNIT_TEST_LARGE_MEMORY (1 << 1)
#define UNIT_TEST_SINGLE (1 << 2)
#define KRED "\33[31m"
#define KGRN "\33[32m"
#define KBLUE "\33[34m"
#define KRED "\33[31m"
#define KGRN "\33[32m"
#define KBLUE "\33[34m"
#define KRESET "\33[0m"
#define TEST_PRINT_ERROR(descr) \
printf("[" KRED "%s - %s:%d" KRESET "] %s\n", __func__, __FILE__, __LINE__, descr)
#define TEST_PRINT_ERROR(descr) printf("[" KRED "%s - %s:%d" KRESET "] %s\n", __func__, __FILE__, __LINE__, descr)
#define TEST_PRINT_INFO(descr, ...) \
printf("[" KBLUE "%s - %s:%d" KRESET "] " descr "\n", __func__, __FILE__, __LINE__, __VA_ARGS__)
#define TEST_PRINT_INFO(descr, ...) \
printf("[" KBLUE "%s - %s:%d" KRESET "] " descr "\n", __func__, __FILE__, __LINE__, __VA_ARGS__)
#define TEST_ASSERT_MESSAGE(descr, _c) do { \
if (!(_c)) { \
TEST_PRINT_ERROR(descr); \
return 1; \
} \
} while(0)
#define TEST_ASSERT_MESSAGE(descr, _c) \
do { \
if (!(_c)) { \
TEST_PRINT_ERROR(descr); \
return 1; \
} \
} while (0)
#define TEST_ASSERT(_c) TEST_ASSERT_MESSAGE("Failed assertion: " #_c, _c)

View File

@ -14,39 +14,39 @@
static long long usec(void) {
struct timeval tv;
gettimeofday(&tv,NULL);
return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
gettimeofday(&tv, NULL);
return (((long long)tv.tv_sec) * 1000000) + tv.tv_usec;
}
static intset *createSet(int bits, int size) {
uint64_t mask = (1<<bits)-1;
uint64_t mask = (1 << bits) - 1;
uint64_t value;
intset *is = intsetNew();
for (int i = 0; i < size; i++) {
if (bits > 32) {
value = (rand()*rand()) & mask;
value = (rand() * rand()) & mask;
} else {
value = rand() & mask;
}
is = intsetAdd(is,value,NULL);
is = intsetAdd(is, value, NULL);
}
return is;
}
static int checkConsistency(intset *is) {
for (uint32_t i = 0; i < (intrev32ifbe(is->length)-1); i++) {
for (uint32_t i = 0; i < (intrev32ifbe(is->length) - 1); i++) {
uint32_t encoding = intrev32ifbe(is->encoding);
if (encoding == INTSET_ENC_INT16) {
int16_t *i16 = (int16_t*)is->contents;
TEST_ASSERT(i16[i] < i16[i+1]);
int16_t *i16 = (int16_t *)is->contents;
TEST_ASSERT(i16[i] < i16[i + 1]);
} else if (encoding == INTSET_ENC_INT32) {
int32_t *i32 = (int32_t*)is->contents;
TEST_ASSERT(i32[i] < i32[i+1]);
int32_t *i32 = (int32_t *)is->contents;
TEST_ASSERT(i32[i] < i32[i + 1]);
} else {
int64_t *i64 = (int64_t*)is->contents;
TEST_ASSERT(i64[i] < i64[i+1]);
int64_t *i64 = (int64_t *)is->contents;
TEST_ASSERT(i64[i] < i64[i + 1]);
}
}
return 1;
@ -65,10 +65,8 @@ int test_intsetValueEncodings(int argc, char **argv, int flags) {
TEST_ASSERT(_intsetValueEncoding(+2147483647) == INTSET_ENC_INT32);
TEST_ASSERT(_intsetValueEncoding(-2147483649) == INTSET_ENC_INT64);
TEST_ASSERT(_intsetValueEncoding(+2147483648) == INTSET_ENC_INT64);
TEST_ASSERT(_intsetValueEncoding(-9223372036854775808ull) ==
INTSET_ENC_INT64);
TEST_ASSERT(_intsetValueEncoding(+9223372036854775807ull) ==
INTSET_ENC_INT64);
TEST_ASSERT(_intsetValueEncoding(-9223372036854775808ull) == INTSET_ENC_INT64);
TEST_ASSERT(_intsetValueEncoding(+9223372036854775807ull) == INTSET_ENC_INT64);
return 0;
}
@ -80,10 +78,14 @@ int test_intsetBasicAdding(int argc, char **argv, int flags) {
intset *is = intsetNew();
uint8_t success;
is = intsetAdd(is,5,&success); TEST_ASSERT(success);
is = intsetAdd(is,6,&success); TEST_ASSERT(success);
is = intsetAdd(is,4,&success); TEST_ASSERT(success);
is = intsetAdd(is,4,&success); TEST_ASSERT(!success);
is = intsetAdd(is, 5, &success);
TEST_ASSERT(success);
is = intsetAdd(is, 6, &success);
TEST_ASSERT(success);
is = intsetAdd(is, 4, &success);
TEST_ASSERT(success);
is = intsetAdd(is, 4, &success);
TEST_ASSERT(!success);
TEST_ASSERT(6 == intsetMax(is));
TEST_ASSERT(4 == intsetMin(is));
zfree(is);
@ -100,7 +102,7 @@ int test_intsetLargeNumberRandomAdd(int argc, char **argv, int flags) {
uint8_t success;
intset *is = intsetNew();
for (int i = 0; i < 1024; i++) {
is = intsetAdd(is,rand()%0x800,&success);
is = intsetAdd(is, rand() % 0x800, &success);
if (success) inserts++;
}
TEST_ASSERT(intrev32ifbe(is->length) == inserts);
@ -115,22 +117,22 @@ int test_intsetUpgradeFromint16Toint32(int argc, char **argv, int flags) {
UNUSED(flags);
intset *is = intsetNew();
is = intsetAdd(is,32,NULL);
is = intsetAdd(is, 32, NULL);
TEST_ASSERT(intrev32ifbe(is->encoding) == INTSET_ENC_INT16);
is = intsetAdd(is,65535,NULL);
is = intsetAdd(is, 65535, NULL);
TEST_ASSERT(intrev32ifbe(is->encoding) == INTSET_ENC_INT32);
TEST_ASSERT(intsetFind(is,32));
TEST_ASSERT(intsetFind(is,65535));
TEST_ASSERT(intsetFind(is, 32));
TEST_ASSERT(intsetFind(is, 65535));
TEST_ASSERT(checkConsistency(is) == 1);
zfree(is);
is = intsetNew();
is = intsetAdd(is,32,NULL);
is = intsetAdd(is, 32, NULL);
TEST_ASSERT(intrev32ifbe(is->encoding) == INTSET_ENC_INT16);
is = intsetAdd(is,-65535,NULL);
is = intsetAdd(is, -65535, NULL);
TEST_ASSERT(intrev32ifbe(is->encoding) == INTSET_ENC_INT32);
TEST_ASSERT(intsetFind(is,32));
TEST_ASSERT(intsetFind(is,-65535));
TEST_ASSERT(intsetFind(is, 32));
TEST_ASSERT(intsetFind(is, -65535));
TEST_ASSERT(checkConsistency(is) == 1);
zfree(is);
@ -143,22 +145,22 @@ int test_intsetUpgradeFromint16Toint64(int argc, char **argv, int flags) {
UNUSED(flags);
intset *is = intsetNew();
is = intsetAdd(is,32,NULL);
is = intsetAdd(is, 32, NULL);
TEST_ASSERT(intrev32ifbe(is->encoding) == INTSET_ENC_INT16);
is = intsetAdd(is,4294967295,NULL);
is = intsetAdd(is, 4294967295, NULL);
TEST_ASSERT(intrev32ifbe(is->encoding) == INTSET_ENC_INT64);
TEST_ASSERT(intsetFind(is,32));
TEST_ASSERT(intsetFind(is,4294967295));
TEST_ASSERT(intsetFind(is, 32));
TEST_ASSERT(intsetFind(is, 4294967295));
TEST_ASSERT(checkConsistency(is) == 1);
zfree(is);
is = intsetNew();
is = intsetAdd(is,32,NULL);
is = intsetAdd(is, 32, NULL);
TEST_ASSERT(intrev32ifbe(is->encoding) == INTSET_ENC_INT16);
is = intsetAdd(is,-4294967295,NULL);
is = intsetAdd(is, -4294967295, NULL);
TEST_ASSERT(intrev32ifbe(is->encoding) == INTSET_ENC_INT64);
TEST_ASSERT(intsetFind(is,32));
TEST_ASSERT(intsetFind(is,-4294967295));
TEST_ASSERT(intsetFind(is, 32));
TEST_ASSERT(intsetFind(is, -4294967295));
TEST_ASSERT(checkConsistency(is) == 1);
zfree(is);
@ -171,22 +173,22 @@ int test_intsetUpgradeFromint32Toint64(int argc, char **argv, int flags) {
UNUSED(flags);
intset *is = intsetNew();
is = intsetAdd(is,65535,NULL);
is = intsetAdd(is, 65535, NULL);
TEST_ASSERT(intrev32ifbe(is->encoding) == INTSET_ENC_INT32);
is = intsetAdd(is,4294967295,NULL);
is = intsetAdd(is, 4294967295, NULL);
TEST_ASSERT(intrev32ifbe(is->encoding) == INTSET_ENC_INT64);
TEST_ASSERT(intsetFind(is,65535));
TEST_ASSERT(intsetFind(is,4294967295));
TEST_ASSERT(intsetFind(is, 65535));
TEST_ASSERT(intsetFind(is, 4294967295));
TEST_ASSERT(checkConsistency(is) == 1);
zfree(is);
is = intsetNew();
is = intsetAdd(is,65535,NULL);
is = intsetAdd(is, 65535, NULL);
TEST_ASSERT(intrev32ifbe(is->encoding) == INTSET_ENC_INT32);
is = intsetAdd(is,-4294967295,NULL);
is = intsetAdd(is, -4294967295, NULL);
TEST_ASSERT(intrev32ifbe(is->encoding) == INTSET_ENC_INT64);
TEST_ASSERT(intsetFind(is,65535));
TEST_ASSERT(intsetFind(is,-4294967295));
TEST_ASSERT(intsetFind(is, 65535));
TEST_ASSERT(intsetFind(is, -4294967295));
TEST_ASSERT(checkConsistency(is) == 1);
zfree(is);
@ -201,13 +203,12 @@ int test_intsetStressLookups(int argc, char **argv, int flags) {
long num = 100000, size = 10000;
int i, bits = 20;
long long start;
intset *is = createSet(bits,size);
intset *is = createSet(bits, size);
TEST_ASSERT(checkConsistency(is) == 1);
start = usec();
for (i = 0; i < num; i++) intsetSearch(is,rand() % ((1<<bits)-1),NULL);
TEST_PRINT_INFO("%ld lookups, %ld element set, %lldusec\n",
num,size,usec()-start);
for (i = 0; i < num; i++) intsetSearch(is, rand() % ((1 << bits) - 1), NULL);
TEST_PRINT_INFO("%ld lookups, %ld element set, %lldusec\n", num, size, usec() - start);
zfree(is);
return 0;
@ -222,12 +223,12 @@ int test_intsetStressAddDelete(int argc, char **argv, int flags) {
intset *is = intsetNew();
for (i = 0; i < 0xffff; i++) {
v1 = rand() % 0xfff;
is = intsetAdd(is,v1,NULL);
TEST_ASSERT(intsetFind(is,v1));
is = intsetAdd(is, v1, NULL);
TEST_ASSERT(intsetFind(is, v1));
v2 = rand() % 0xfff;
is = intsetRemove(is,v2,NULL);
TEST_ASSERT(!intsetFind(is,v2));
is = intsetRemove(is, v2, NULL);
TEST_ASSERT(!intsetFind(is, v2));
}
TEST_ASSERT(checkConsistency(is) == 1);
zfree(is);

View File

@ -2,7 +2,7 @@
#include "test_help.h"
uint64_t hashTestCallback(const void *key) {
return dictGenHashFunction((unsigned char*)key, strlen((char*)key));
return dictGenHashFunction((unsigned char *)key, strlen((char *)key));
}
void freeTestCallback(dict *d, void *val) {
@ -10,23 +10,15 @@ void freeTestCallback(dict *d, void *val) {
zfree(val);
}
dictType KvstoreDictTestType = {
hashTestCallback,
NULL,
NULL,
NULL,
freeTestCallback,
NULL,
NULL
};
dictType KvstoreDictTestType = {hashTestCallback, NULL, NULL, NULL, freeTestCallback, NULL, NULL};
char *stringFromInt(int value) {
char buf[32];
int len;
char *s;
len = snprintf(buf, sizeof(buf), "%d",value);
s = zmalloc(len+1);
len = snprintf(buf, sizeof(buf), "%d", value);
s = zmalloc(len + 1);
memcpy(s, buf, len);
s[len] = '\0';
return s;
@ -80,7 +72,7 @@ int test_kvstoreIteratorRemoveAllKeysNoDeleteEmptyDict(int argc, char **argv, in
}
kvs_it = kvstoreIteratorInit(kvs1);
while((de = kvstoreIteratorNext(kvs_it)) != NULL) {
while ((de = kvstoreIteratorNext(kvs_it)) != NULL) {
curr_slot = kvstoreIteratorGetCurrentDictIndex(kvs_it);
key = dictGetKey(de);
TEST_ASSERT(kvstoreDictDelete(kvs1, curr_slot, key) == DICT_OK);
@ -116,7 +108,7 @@ int test_kvstoreIteratorRemoveAllKeysDeleteEmptyDict(int argc, char **argv, int
}
kvs_it = kvstoreIteratorInit(kvs2);
while((de = kvstoreIteratorNext(kvs_it)) != NULL) {
while ((de = kvstoreIteratorNext(kvs_it)) != NULL) {
curr_slot = kvstoreIteratorGetCurrentDictIndex(kvs_it);
key = dictGetKey(de);
TEST_ASSERT(kvstoreDictDelete(kvs2, curr_slot, key) == DICT_OK);
@ -124,7 +116,8 @@ int test_kvstoreIteratorRemoveAllKeysDeleteEmptyDict(int argc, char **argv, int
kvstoreIteratorRelease(kvs_it);
/* Make sure the dict was removed from the rehashing list. */
while (kvstoreIncrementallyRehash(kvs2, 1000)) {}
while (kvstoreIncrementallyRehash(kvs2, 1000)) {
}
dict *d = kvstoreGetDict(kvs2, didx);
TEST_ASSERT(d == NULL);
@ -154,7 +147,7 @@ int test_kvstoreDictIteratorRemoveAllKeysNoDeleteEmptyDict(int argc, char **argv
}
kvs_di = kvstoreGetDictSafeIterator(kvs1, didx);
while((de = kvstoreDictIteratorNext(kvs_di)) != NULL) {
while ((de = kvstoreDictIteratorNext(kvs_di)) != NULL) {
key = dictGetKey(de);
TEST_ASSERT(kvstoreDictDelete(kvs1, didx, key) == DICT_OK);
}
@ -188,7 +181,7 @@ int test_kvstoreDictIteratorRemoveAllKeysDeleteEmptyDict(int argc, char **argv,
}
kvs_di = kvstoreGetDictSafeIterator(kvs2, didx);
while((de = kvstoreDictIteratorNext(kvs_di)) != NULL) {
while ((de = kvstoreDictIteratorNext(kvs_di)) != NULL) {
key = dictGetKey(de);
TEST_ASSERT(kvstoreDictDelete(kvs2, didx, key) == DICT_OK);
}

View File

@ -33,8 +33,7 @@ int runTestSuite(struct unitTestSuite *test, int argc, char **argv, int flags) {
}
printf("[" KBLUE "END" KRESET "] - %s: ", test->filename);
printf("%d tests, %d passed, %d failed\n", test_num,
test_num - failed_tests, failed_tests);
printf("%d tests, %d passed, %d failed\n", test_num, test_num - failed_tests, failed_tests);
return !failed_tests;
}
@ -43,15 +42,17 @@ int main(int argc, char **argv) {
char *file = NULL;
for (int j = 1; j < argc; j++) {
char *arg = argv[j];
if (!strcasecmp(arg, "--accurate")) flags |= UNIT_TEST_ACCURATE;
else if (!strcasecmp(arg, "--large-memory")) flags |= UNIT_TEST_LARGE_MEMORY;
if (!strcasecmp(arg, "--accurate"))
flags |= UNIT_TEST_ACCURATE;
else if (!strcasecmp(arg, "--large-memory"))
flags |= UNIT_TEST_LARGE_MEMORY;
else if (!strcasecmp(arg, "--single") && (j + 1 < argc)) {
flags |= UNIT_TEST_SINGLE;
file = argv[j + 1];
}
}
int numtests = sizeof(unitTestSuite)/sizeof(struct unitTestSuite);
int numtests = sizeof(unitTestSuite) / sizeof(struct unitTestSuite);
int failed_num = 0, suites_executed = 0;
for (int j = 0; j < numtests; j++) {
if (file && strcasecmp(file, unitTestSuite[j].filename)) continue;
@ -60,8 +61,8 @@ int main(int argc, char **argv) {
}
suites_executed++;
}
printf("%d test suites executed, %d passed, %d failed\n", suites_executed,
suites_executed-failed_num, failed_num);
printf("%d test suites executed, %d passed, %d failed\n", suites_executed, suites_executed - failed_num,
failed_num);
return failed_num == 0 ? 0 : 1;
}

View File

@ -10,9 +10,12 @@ static sds sdsTestTemplateCallback(sds varname, void *arg) {
static const char *_var1 = "variable1";
static const char *_var2 = "variable2";
if (!strcmp(varname, _var1)) return sdsnew("value1");
else if (!strcmp(varname, _var2)) return sdsnew("value2");
else return NULL;
if (!strcmp(varname, _var1))
return sdsnew("value1");
else if (!strcmp(varname, _var2))
return sdsnew("value2");
else
return NULL;
}
int test_sds(int argc, char **argv, int flags) {
@ -22,36 +25,32 @@ int test_sds(int argc, char **argv, int flags) {
sds x = sdsnew("foo"), y;
TEST_ASSERT_MESSAGE("Create a string and obtain the length",
sdslen(x) == 3 && memcmp(x, "foo\0", 4) == 0);
TEST_ASSERT_MESSAGE("Create a string and obtain the length", sdslen(x) == 3 && memcmp(x, "foo\0", 4) == 0);
sdsfree(x);
x = sdsnewlen("foo", 2);
TEST_ASSERT_MESSAGE("Create a string with specified length",
sdslen(x) == 2 && memcmp(x, "fo\0", 3) == 0);
TEST_ASSERT_MESSAGE("Create a string with specified length", sdslen(x) == 2 && memcmp(x, "fo\0", 3) == 0);
x = sdscat(x, "bar");
TEST_ASSERT_MESSAGE("Strings concatenation",
sdslen(x) == 5 && memcmp(x, "fobar\0", 6) == 0);
TEST_ASSERT_MESSAGE("Strings concatenation", sdslen(x) == 5 && memcmp(x, "fobar\0", 6) == 0);
x = sdscpy(x, "a");
TEST_ASSERT_MESSAGE("sdscpy() against an originally longer string",
sdslen(x) == 1 && memcmp(x, "a\0", 2) == 0);
TEST_ASSERT_MESSAGE("sdscpy() against an originally longer string", sdslen(x) == 1 && memcmp(x, "a\0", 2) == 0);
x = sdscpy(x, "xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
TEST_ASSERT_MESSAGE("sdscpy() against an originally shorter string",
sdslen(x) == 33 &&
memcmp(x, "xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0", 33) == 0);
sdslen(x) == 33 && memcmp(x, "xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0", 33) == 0);
sdsfree(x);
x = sdscatprintf(sdsempty(), "%d", 123);
TEST_ASSERT_MESSAGE("sdscatprintf() seems working in the base case",
sdslen(x) == 3 && memcmp(x, "123\0", 4) == 0);
TEST_ASSERT_MESSAGE("sdscatprintf() seems working in the base case", sdslen(x) == 3 && memcmp(x, "123\0", 4) == 0);
sdsfree(x);
x = sdscatprintf(sdsempty(), "a%cb", 0);
TEST_ASSERT_MESSAGE("sdscatprintf() seems working with \\0 inside of result",
sdslen(x) == 3 && memcmp(x, "a\0""b\0", 4) == 0);
TEST_ASSERT_MESSAGE("sdscatprintf() seems working with \\0 inside of result", sdslen(x) == 3 && memcmp(x,
"a\0"
"b\0",
4) == 0);
sdsfree(x);
char etalon[1024 * 1024];
@ -60,112 +59,100 @@ int test_sds(int argc, char **argv, int flags) {
}
x = sdscatprintf(sdsempty(), "%0*d", (int)sizeof(etalon), 0);
TEST_ASSERT_MESSAGE("sdscatprintf() can print 1MB",
sdslen(x) == sizeof(etalon) && memcmp(x, etalon, sizeof(etalon)) == 0);
sdslen(x) == sizeof(etalon) && memcmp(x, etalon, sizeof(etalon)) == 0);
sdsfree(x);
x = sdsnew("--");
x = sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN,LLONG_MAX);
x = sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN, LLONG_MAX);
TEST_ASSERT_MESSAGE("sdscatfmt() seems working in the base case",
sdslen(x) == 60 &&
memcmp(x,"--Hello Hi! World -9223372036854775808,"
"9223372036854775807--",60) == 0);
sdslen(x) == 60 && memcmp(x,
"--Hello Hi! World -9223372036854775808,"
"9223372036854775807--",
60) == 0);
sdsfree(x);
x = sdsnew("--");
x = sdscatfmt(x, "%u,%U--", UINT_MAX, ULLONG_MAX);
TEST_ASSERT_MESSAGE("sdscatfmt() seems working with unsigned numbers",
sdslen(x) == 35 &&
memcmp(x, "--4294967295,18446744073709551615--", 35) == 0);
sdslen(x) == 35 && memcmp(x, "--4294967295,18446744073709551615--", 35) == 0);
sdsfree(x);
x = sdsnew(" x ");
sdstrim(x, " x");
TEST_ASSERT_MESSAGE("sdstrim() works when all chars match",
sdslen(x) == 0);
TEST_ASSERT_MESSAGE("sdstrim() works when all chars match", sdslen(x) == 0);
sdsfree(x);
x = sdsnew(" x ");
sdstrim(x," ");
TEST_ASSERT_MESSAGE("sdstrim() works when a single char remains",
sdslen(x) == 1 && x[0] == 'x');
sdstrim(x, " ");
TEST_ASSERT_MESSAGE("sdstrim() works when a single char remains", sdslen(x) == 1 && x[0] == 'x');
sdsfree(x);
x = sdsnew("xxciaoyyy");
sdstrim(x, "xy");
TEST_ASSERT_MESSAGE("sdstrim() correctly trims characters",
sdslen(x) == 4 && memcmp(x, "ciao\0", 5) == 0);
TEST_ASSERT_MESSAGE("sdstrim() correctly trims characters", sdslen(x) == 4 && memcmp(x, "ciao\0", 5) == 0);
y = sdsdup(x);
sdsrange(y, 1, 1);
TEST_ASSERT_MESSAGE("sdsrange(...,1,1)",
sdslen(y) == 1 && memcmp(y, "i\0", 2) == 0);
TEST_ASSERT_MESSAGE("sdsrange(...,1,1)", sdslen(y) == 1 && memcmp(y, "i\0", 2) == 0);
sdsfree(y);
y = sdsdup(x);
sdsrange(y, 1, -1);
TEST_ASSERT_MESSAGE("sdsrange(...,1,-1)",
sdslen(y) == 3 && memcmp(y, "iao\0", 4) == 0);
TEST_ASSERT_MESSAGE("sdsrange(...,1,-1)", sdslen(y) == 3 && memcmp(y, "iao\0", 4) == 0);
sdsfree(y);
y = sdsdup(x);
sdsrange(y, -2, -1);
TEST_ASSERT_MESSAGE("sdsrange(...,-2,-1)",
sdslen(y) == 2 && memcmp(y, "ao\0", 3) == 0);
TEST_ASSERT_MESSAGE("sdsrange(...,-2,-1)", sdslen(y) == 2 && memcmp(y, "ao\0", 3) == 0);
sdsfree(y);
y = sdsdup(x);
sdsrange(y, 2, 1);
TEST_ASSERT_MESSAGE("sdsrange(...,2,1)",
sdslen(y) == 0 && memcmp(y, "\0", 1) == 0);
TEST_ASSERT_MESSAGE("sdsrange(...,2,1)", sdslen(y) == 0 && memcmp(y, "\0", 1) == 0);
sdsfree(y);
y = sdsdup(x);
sdsrange(y, 1, 100);
TEST_ASSERT_MESSAGE("sdsrange(...,1,100)",
sdslen(y) == 3 && memcmp(y, "iao\0", 4) == 0);
TEST_ASSERT_MESSAGE("sdsrange(...,1,100)", sdslen(y) == 3 && memcmp(y, "iao\0", 4) == 0);
sdsfree(y);
y = sdsdup(x);
sdsrange(y, 100, 100);
TEST_ASSERT_MESSAGE("sdsrange(...,100,100)",
sdslen(y) == 0 && memcmp(y, "\0", 1) == 0);
TEST_ASSERT_MESSAGE("sdsrange(...,100,100)", sdslen(y) == 0 && memcmp(y, "\0", 1) == 0);
sdsfree(y);
y = sdsdup(x);
sdsrange(y,4,6);
TEST_ASSERT_MESSAGE("sdsrange(...,4,6)",
sdslen(y) == 0 && memcmp(y, "\0", 1) == 0);
sdsrange(y, 4, 6);
TEST_ASSERT_MESSAGE("sdsrange(...,4,6)", sdslen(y) == 0 && memcmp(y, "\0", 1) == 0);
sdsfree(y);
y = sdsdup(x);
sdsrange(y, 3, 6);
TEST_ASSERT_MESSAGE("sdsrange(...,3,6)",
sdslen(y) == 1 && memcmp(y, "o\0", 2) == 0);
TEST_ASSERT_MESSAGE("sdsrange(...,3,6)", sdslen(y) == 1 && memcmp(y, "o\0", 2) == 0);
sdsfree(y);
sdsfree(x);
x = sdsnew("foo");
y = sdsnew("foa");
TEST_ASSERT_MESSAGE("sdscmp(foo,foa)", sdscmp(x,y) > 0);
TEST_ASSERT_MESSAGE("sdscmp(foo,foa)", sdscmp(x, y) > 0);
sdsfree(y);
sdsfree(x);
x = sdsnew("bar");
y = sdsnew("bar");
TEST_ASSERT_MESSAGE("sdscmp(bar,bar)", sdscmp(x,y) == 0);
TEST_ASSERT_MESSAGE("sdscmp(bar,bar)", sdscmp(x, y) == 0);
sdsfree(y);
sdsfree(x);
x = sdsnew("aar");
y = sdsnew("bar");
TEST_ASSERT_MESSAGE("sdscmp(bar,bar)", sdscmp(x,y) < 0);
TEST_ASSERT_MESSAGE("sdscmp(bar,bar)", sdscmp(x, y) < 0);
sdsfree(y);
sdsfree(x);
x = sdsnewlen("\a\n\0foo\r", 7);
y = sdscatrepr(sdsempty(), x, sdslen(x));
TEST_ASSERT_MESSAGE("sdscatrepr(...data...)",
memcmp(y, "\"\\a\\n\\x00foo\\r\"", 15) == 0);
TEST_ASSERT_MESSAGE("sdscatrepr(...data...)", memcmp(y, "\"\\a\\n\\x00foo\\r\"", 15) == 0);
unsigned int oldfree;
char *p;
@ -175,8 +162,7 @@ int test_sds(int argc, char **argv, int flags) {
sdsfree(x);
sdsfree(y);
x = sdsnew("0");
TEST_ASSERT_MESSAGE("sdsnew() free/len buffers",
sdslen(x) == 1 && sdsavail(x) == 0);
TEST_ASSERT_MESSAGE("sdsnew() free/len buffers", sdslen(x) == 1 && sdsavail(x) == 0);
/* Run the test a few times in order to hit the first two
* SDS header types. */
@ -191,22 +177,22 @@ int test_sds(int argc, char **argv, int flags) {
oldfree = sdsavail(x);
UNUSED(oldfree);
}
p = x+oldlen;
p = x + oldlen;
for (j = 0; j < step; j++) {
p[j] = 'A' + j;
}
sdsIncrLen(x, step);
}
TEST_ASSERT_MESSAGE("sdsMakeRoomFor() content",
memcmp("0ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ", x, 101) == 0);
TEST_ASSERT_MESSAGE("sdsMakeRoomFor() content", memcmp("0ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGH"
"IJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ",
x, 101) == 0);
TEST_ASSERT_MESSAGE("sdsMakeRoomFor() final length", sdslen(x) == 101);
sdsfree(x);
/* Simple template */
x = sdstemplate("v1={variable1} v2={variable2}", sdsTestTemplateCallback, NULL);
TEST_ASSERT_MESSAGE("sdstemplate() normal flow",
memcmp(x, "v1=value1 v2=value2", 19) == 0);
TEST_ASSERT_MESSAGE("sdstemplate() normal flow", memcmp(x, "v1=value1 v2=value2", 19) == 0);
sdsfree(x);
/* Template with callback error */
@ -223,8 +209,7 @@ int test_sds(int argc, char **argv, int flags) {
/* Template with quoting */
x = sdstemplate("v1={{{variable1}} {{} v2={variable2}", sdsTestTemplateCallback, NULL);
TEST_ASSERT_MESSAGE("sdstemplate() with quoting",
memcmp(x, "v1={value1} {} v2=value2", 24) == 0);
TEST_ASSERT_MESSAGE("sdstemplate() with quoting", memcmp(x, "v1={value1} {} v2=value2", 24) == 0);
sdsfree(x);
/* Test sdsResize - extend */

View File

@ -6,20 +6,18 @@
int test_sha1(int argc, char **argv, int flags) {
SHA1_CTX ctx;
unsigned char hash[20], buf[BUFSIZE];
unsigned char expected[20] = {0x15, 0xdd, 0x99, 0xa1, 0x99, 0x1e, 0x0b, 0x38,
0x26, 0xfe, 0xde, 0x3d, 0xef, 0xfc, 0x1f, 0xeb, 0xa4, 0x22, 0x78, 0xe6};
unsigned char expected[20] = {0x15, 0xdd, 0x99, 0xa1, 0x99, 0x1e, 0x0b, 0x38, 0x26, 0xfe,
0xde, 0x3d, 0xef, 0xfc, 0x1f, 0xeb, 0xa4, 0x22, 0x78, 0xe6};
int i;
UNUSED(argc);
UNUSED(argv);
UNUSED(flags);
for(i=0;i<BUFSIZE;i++)
buf[i] = i;
for (i = 0; i < BUFSIZE; i++) buf[i] = i;
SHA1Init(&ctx);
for(i=0;i<1000;i++)
SHA1Update(&ctx, buf, BUFSIZE);
for (i = 0; i < 1000; i++) SHA1Update(&ctx, buf, BUFSIZE);
SHA1Final(hash, &ctx);
TEST_ASSERT(memcmp(hash, expected, 20) == 0);

View File

@ -15,54 +15,54 @@ int test_string2ll(int argc, char **argv, int flags) {
long long v;
/* May not start with +. */
valkey_strlcpy(buf,"+1",sizeof(buf));
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 0);
valkey_strlcpy(buf, "+1", sizeof(buf));
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 0);
/* Leading space. */
valkey_strlcpy(buf," 1",sizeof(buf));
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 0);
valkey_strlcpy(buf, " 1", sizeof(buf));
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 0);
/* Trailing space. */
valkey_strlcpy(buf,"1 ",sizeof(buf));
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 0);
valkey_strlcpy(buf, "1 ", sizeof(buf));
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 0);
/* May not start with 0. */
valkey_strlcpy(buf,"01",sizeof(buf));
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 0);
valkey_strlcpy(buf, "01", sizeof(buf));
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 0);
valkey_strlcpy(buf,"-1",sizeof(buf));
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "-1", sizeof(buf));
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == -1);
valkey_strlcpy(buf,"0",sizeof(buf));
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "0", sizeof(buf));
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == 0);
valkey_strlcpy(buf,"1",sizeof(buf));
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "1", sizeof(buf));
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == 1);
valkey_strlcpy(buf,"99",sizeof(buf));
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "99", sizeof(buf));
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == 99);
valkey_strlcpy(buf,"-99",sizeof(buf));
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "-99", sizeof(buf));
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == -99);
valkey_strlcpy(buf,"-9223372036854775808",sizeof(buf));
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "-9223372036854775808", sizeof(buf));
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == LLONG_MIN);
valkey_strlcpy(buf,"-9223372036854775809",sizeof(buf)); /* overflow */
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 0);
valkey_strlcpy(buf, "-9223372036854775809", sizeof(buf)); /* overflow */
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 0);
valkey_strlcpy(buf,"9223372036854775807",sizeof(buf));
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "9223372036854775807", sizeof(buf));
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == LLONG_MAX);
valkey_strlcpy(buf,"9223372036854775808",sizeof(buf)); /* overflow */
TEST_ASSERT(string2ll(buf,strlen(buf),&v) == 0);
valkey_strlcpy(buf, "9223372036854775808", sizeof(buf)); /* overflow */
TEST_ASSERT(string2ll(buf, strlen(buf), &v) == 0);
return 0;
}
@ -76,47 +76,47 @@ int test_string2l(int argc, char **argv, int flags) {
long v;
/* May not start with +. */
valkey_strlcpy(buf,"+1",sizeof(buf));
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 0);
valkey_strlcpy(buf, "+1", sizeof(buf));
TEST_ASSERT(string2l(buf, strlen(buf), &v) == 0);
/* May not start with 0. */
valkey_strlcpy(buf,"01",sizeof(buf));
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 0);
valkey_strlcpy(buf, "01", sizeof(buf));
TEST_ASSERT(string2l(buf, strlen(buf), &v) == 0);
valkey_strlcpy(buf,"-1",sizeof(buf));
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "-1", sizeof(buf));
TEST_ASSERT(string2l(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == -1);
valkey_strlcpy(buf,"0",sizeof(buf));
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "0", sizeof(buf));
TEST_ASSERT(string2l(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == 0);
valkey_strlcpy(buf,"1",sizeof(buf));
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "1", sizeof(buf));
TEST_ASSERT(string2l(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == 1);
valkey_strlcpy(buf,"99",sizeof(buf));
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "99", sizeof(buf));
TEST_ASSERT(string2l(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == 99);
valkey_strlcpy(buf,"-99",sizeof(buf));
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "-99", sizeof(buf));
TEST_ASSERT(string2l(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == -99);
#if LONG_MAX != LLONG_MAX
valkey_strlcpy(buf,"-2147483648",sizeof(buf));
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "-2147483648", sizeof(buf));
TEST_ASSERT(string2l(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == LONG_MIN);
valkey_strlcpy(buf,"-2147483649",sizeof(buf)); /* overflow */
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 0);
valkey_strlcpy(buf, "-2147483649", sizeof(buf)); /* overflow */
TEST_ASSERT(string2l(buf, strlen(buf), &v) == 0);
valkey_strlcpy(buf,"2147483647",sizeof(buf));
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 1);
valkey_strlcpy(buf, "2147483647", sizeof(buf));
TEST_ASSERT(string2l(buf, strlen(buf), &v) == 1);
TEST_ASSERT(v == LONG_MAX);
valkey_strlcpy(buf,"2147483648",sizeof(buf)); /* overflow */
TEST_ASSERT(string2l(buf,strlen(buf),&v) == 0);
valkey_strlcpy(buf, "2147483648", sizeof(buf)); /* overflow */
TEST_ASSERT(string2l(buf, strlen(buf), &v) == 0);
#endif
return 0;
@ -202,14 +202,14 @@ int test_fixedpoint_d2string(int argc, char **argv, int flags) {
TEST_ASSERT(sz == 3);
TEST_ASSERT(!strcmp(buf, "0.0"));
/* set junk in buffer */
memset(buf,'A',32);
memset(buf, 'A', 32);
v = 0.0001;
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
TEST_ASSERT(sz == 6);
TEST_ASSERT(buf[sz] == '\0');
TEST_ASSERT(!strcmp(buf, "0.0001"));
/* set junk in buffer */
memset(buf,'A',32);
memset(buf, 'A', 32);
v = 6.0642951598391699e-05;
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
TEST_ASSERT(sz == 6);
@ -226,7 +226,7 @@ int test_fixedpoint_d2string(int argc, char **argv, int flags) {
sz = fixedpoint_d2string(buf, sizeof buf, v, 4);
TEST_ASSERT(sz == 7);
TEST_ASSERT(!strcmp(buf, "-0.0100"));
v = -0.1;
v = -0.1;
sz = fixedpoint_d2string(buf, sizeof buf, v, 1);
TEST_ASSERT(sz == 4);
TEST_ASSERT(!strcmp(buf, "-0.1"));
@ -279,7 +279,7 @@ static int cache_exist(int fd) {
munmap(m, 4096);
/* the least significant bit of the byte will be set if the corresponding
* page is currently resident in memory */
return flag&1;
return flag & 1;
}
#endif
@ -290,7 +290,7 @@ int test_reclaimFilePageCache(int argc, char **argv, int flags) {
#if defined(__linux__)
char *tmpfile = "/tmp/redis-reclaim-cache-test";
int fd = open(tmpfile, O_RDWR|O_CREAT, 0644);
int fd = open(tmpfile, O_RDWR | O_CREAT, 0644);
TEST_ASSERT(fd >= 0);
/* test write file */

View File

@ -6,10 +6,10 @@
static unsigned char *createList(void) {
unsigned char *zl = ziplistNew();
zl = ziplistPush(zl, (unsigned char *) "foo", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "quux", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "hello", 5, ZIPLIST_HEAD);
zl = ziplistPush(zl, (unsigned char *) "1024", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"foo", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"quux", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"hello", 5, ZIPLIST_HEAD);
zl = ziplistPush(zl, (unsigned char *)"1024", 4, ZIPLIST_TAIL);
return zl;
}
@ -18,24 +18,24 @@ static unsigned char *createIntList(void) {
char buf[32];
snprintf(buf, sizeof(buf), "100");
zl = ziplistPush(zl, (unsigned char *) buf, strlen(buf), ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, strlen(buf), ZIPLIST_TAIL);
snprintf(buf, sizeof(buf), "128000");
zl = ziplistPush(zl, (unsigned char *) buf, strlen(buf), ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, strlen(buf), ZIPLIST_TAIL);
snprintf(buf, sizeof(buf), "-100");
zl = ziplistPush(zl, (unsigned char *) buf, strlen(buf), ZIPLIST_HEAD);
zl = ziplistPush(zl, (unsigned char *)buf, strlen(buf), ZIPLIST_HEAD);
snprintf(buf, sizeof(buf), "4294967296");
zl = ziplistPush(zl, (unsigned char *) buf, strlen(buf), ZIPLIST_HEAD);
zl = ziplistPush(zl, (unsigned char *)buf, strlen(buf), ZIPLIST_HEAD);
snprintf(buf, sizeof(buf), "non integer");
zl = ziplistPush(zl, (unsigned char *) buf, strlen(buf), ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, strlen(buf), ZIPLIST_TAIL);
snprintf(buf, sizeof(buf), "much much longer non integer");
zl = ziplistPush(zl, (unsigned char *) buf, strlen(buf), ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, strlen(buf), ZIPLIST_TAIL);
return zl;
}
static long long usec(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return (((long long) tv.tv_sec) * 1000000) + tv.tv_usec;
return (((long long)tv.tv_sec) * 1000000) + tv.tv_usec;
}
static void stress(int pos, int num, int maxsize, int dnum) {
@ -44,12 +44,12 @@ static void stress(int pos, int num, int maxsize, int dnum) {
for (i = 0; i < maxsize; i += dnum) {
zl = ziplistNew();
for (j = 0; j < i; j++) {
zl = ziplistPush(zl, (unsigned char *) "quux", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"quux", 4, ZIPLIST_TAIL);
}
/* Do num times a push+pop from pos */
for (k = 0; k < num; k++) {
zl = ziplistPush(zl, (unsigned char *) "quux", 4, pos);
zl = ziplistPush(zl, (unsigned char *)"quux", 4, pos);
zl = ziplistDeleteRange(zl, 0, 1);
}
zfree(zl);
@ -74,24 +74,22 @@ static int randstring(char *target, unsigned int min, unsigned int max) {
int len = min + rand() % (max - min + 1);
int minval, maxval;
switch (rand() % 3) {
case 0:
minval = 0;
maxval = 255;
break;
case 1:
minval = 48;
maxval = 122;
break;
case 2:
minval = 48;
maxval = 52;
break;
default:
assert(NULL);
case 0:
minval = 0;
maxval = 255;
break;
case 1:
minval = 48;
maxval = 122;
break;
case 2:
minval = 48;
maxval = 52;
break;
default: assert(NULL);
}
while (p < len)
target[p++] = minval + rand() % (maxval - minval + 1);
while (p < len) target[p++] = minval + rand() % (maxval - minval + 1);
return len;
}
@ -144,8 +142,7 @@ int iteration;
int test_ziplistCreateIntList(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createIntList();
/* "4294967296", "-100", "100", "128000", "non integer", "much much longer non integer" */
@ -174,8 +171,7 @@ int test_ziplistCreateIntList(int argc, char **argv, int flags) {
int test_ziplistPop(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList(); /* "hello", "foo", "quux", "1024" */
@ -211,8 +207,7 @@ int test_ziplistPop(int argc, char **argv, int flags) {
int test_ziplistGetElementAtIndex3(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList(); /* "hello", "foo", "quux", "1024" */
p = ziplistIndex(zl, 3);
TEST_ASSERT(p != NULL);
@ -223,8 +218,7 @@ int test_ziplistGetElementAtIndex3(int argc, char **argv, int flags) {
int test_ziplistGetElementOutOfRange(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList();
p = ziplistIndex(zl, 4);
TEST_ASSERT(p == NULL);
@ -234,8 +228,7 @@ int test_ziplistGetElementOutOfRange(int argc, char **argv, int flags) {
int test_ziplistGetLastElement(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList(); /* "hello", "foo", "quux", "1024" */
p = ziplistIndex(zl, -1);
TEST_ASSERT(p != NULL);
@ -246,8 +239,7 @@ int test_ziplistGetLastElement(int argc, char **argv, int flags) {
int test_ziplistGetFirstElement(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList(); /* "hello", "foo", "quux", "1024" */
p = ziplistIndex(zl, -4);
TEST_ASSERT(p != NULL);
@ -258,8 +250,7 @@ int test_ziplistGetFirstElement(int argc, char **argv, int flags) {
int test_ziplistGetElementOutOfRangeReverse(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList(); /* "hello", "foo", "quux", "1024" */
p = ziplistIndex(zl, -5);
TEST_ASSERT(p == NULL);
@ -269,8 +260,7 @@ int test_ziplistGetElementOutOfRangeReverse(int argc, char **argv, int flags) {
int test_ziplistIterateThroughFullList(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList();
p = ziplistIndex(zl, 0);
while (ziplistGet(p, &entry, &elen, &value)) {
@ -283,8 +273,7 @@ int test_ziplistIterateThroughFullList(int argc, char **argv, int flags) {
int test_ziplistIterateThroughListFrom1ToEnd(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList();
p = ziplistIndex(zl, 1);
while (ziplistGet(p, &entry, &elen, &value)) {
@ -297,8 +286,7 @@ int test_ziplistIterateThroughListFrom1ToEnd(int argc, char **argv, int flags) {
int test_ziplistIterateThroughListFrom2ToEnd(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList();
p = ziplistIndex(zl, 2);
while (ziplistGet(p, &entry, &elen, &value)) {
@ -311,8 +299,7 @@ int test_ziplistIterateThroughListFrom2ToEnd(int argc, char **argv, int flags) {
int test_ziplistIterateThroughStartOutOfRange(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList();
p = ziplistIndex(zl, 4);
TEST_ASSERT(p == NULL);
@ -322,8 +309,7 @@ int test_ziplistIterateThroughStartOutOfRange(int argc, char **argv, int flags)
int test_ziplistIterateBackToFront(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList();
p = ziplistIndex(zl, -1);
while (ziplistGet(p, &entry, &elen, &value)) {
@ -336,8 +322,7 @@ int test_ziplistIterateBackToFront(int argc, char **argv, int flags) {
int test_ziplistIterateBackToFrontDeletingAllItems(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList();
p = ziplistIndex(zl, -1);
while (ziplistGet(p, &entry, &elen, &value)) {
@ -351,8 +336,7 @@ int test_ziplistIterateBackToFrontDeletingAllItems(int argc, char **argv, int fl
int test_ziplistDeleteInclusiveRange0To0(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList(); /* "hello", "foo", "quux", "1024" */
p = ziplistIndex(zl, 0);
@ -370,8 +354,7 @@ int test_ziplistDeleteInclusiveRange0To0(int argc, char **argv, int flags) {
int test_ziplistDeleteInclusiveRange0To1(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList(); /* "hello", "foo", "quux", "1024" */
p = ziplistIndex(zl, 0);
@ -394,8 +377,7 @@ int test_ziplistDeleteInclusiveRange0To1(int argc, char **argv, int flags) {
int test_ziplistDeleteInclusiveRange1To2(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList(); /* "hello", "foo", "quux", "1024" */
p = ziplistIndex(zl, 1);
@ -416,8 +398,7 @@ int test_ziplistDeleteInclusiveRange1To2(int argc, char **argv, int flags) {
int test_ziplistDeleteWithStartIndexOutOfRange(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList();
int orig_len = ziplistLen(zl);
zl = ziplistDeleteRange(zl, 5, 1);
@ -429,8 +410,7 @@ int test_ziplistDeleteWithStartIndexOutOfRange(int argc, char **argv, int flags)
int test_ziplistDeleteWithNumOverflow(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList(); /* "hello", "foo", "quux", "1024" */
int orig_len = ziplistLen(zl);
@ -443,13 +423,12 @@ int test_ziplistDeleteWithNumOverflow(int argc, char **argv, int flags) {
int test_ziplistDeleteFooWhileIterating(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList(); /* "hello", "foo", "quux", "1024" */
p = ziplistIndex(zl, 0);
while (ziplistGet(p, &entry, &elen, &value)) {
TEST_ASSERT(p != NULL);
if (entry && strncmp("foo", (char *) entry, elen) == 0) {
if (entry && strncmp("foo", (char *)entry, elen) == 0) {
zl = ziplistDelete(zl, &p);
} else {
p = ziplistNext(zl, p);
@ -465,21 +444,22 @@ int test_ziplistDeleteFooWhileIterating(int argc, char **argv, int flags) {
int test_ziplistReplaceWithSameSize(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList(); /* "hello", "foo", "quux", "1024" */
unsigned char *orig_zl = zl;
p = ziplistIndex(zl, 0);
zl = ziplistReplace(zl, p, (unsigned char *) "zoink", 5);
zl = ziplistReplace(zl, p, (unsigned char *)"zoink", 5);
p = ziplistIndex(zl, 3);
zl = ziplistReplace(zl, p, (unsigned char *) "yy", 2);
zl = ziplistReplace(zl, p, (unsigned char *)"yy", 2);
p = ziplistIndex(zl, 1);
zl = ziplistReplace(zl, p, (unsigned char *) "65536", 5);
zl = ziplistReplace(zl, p, (unsigned char *)"65536", 5);
p = ziplistIndex(zl, 0);
TEST_ASSERT(!memcmp((char *) p,
TEST_ASSERT(!memcmp((char *)p,
"\x00\x05zoink"
"\x07\xf0\x00\x00\x01" /* 65536 as int24 */
"\x05\x04quux" "\x06\x02yy" "\xff",
"\x05\x04quux"
"\x06\x02yy"
"\xff",
23));
TEST_ASSERT(zl == orig_zl); /* no reallocations have happened */
zfree(zl);
@ -488,15 +468,17 @@ int test_ziplistReplaceWithSameSize(int argc, char **argv, int flags) {
int test_ziplistReplaceWithDifferentSize(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList(); /* "hello", "foo", "quux", "1024" */
p = ziplistIndex(zl, 1);
zl = ziplistReplace(zl, p, (unsigned char *) "squirrel", 8);
zl = ziplistReplace(zl, p, (unsigned char *)"squirrel", 8);
p = ziplistIndex(zl, 0);
TEST_ASSERT(!strncmp((char *) p,
"\x00\x05hello" "\x07\x08squirrel" "\x0a\x04quux"
"\x06\xc0\x00\x04" "\xff",
TEST_ASSERT(!strncmp((char *)p,
"\x00\x05hello"
"\x07\x08squirrel"
"\x0a\x04quux"
"\x06\xc0\x00\x04"
"\xff",
28));
zfree(zl);
return 0;
@ -504,33 +486,31 @@ int test_ziplistReplaceWithDifferentSize(int argc, char **argv, int flags) {
int test_ziplistRegressionTestForOver255ByteStrings(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
char v1[257] = {0}, v2[257] = {0};
memset(v1, 'x', 256);
memset(v2, 'y', 256);
zl = ziplistNew();
zl = ziplistPush(zl, (unsigned char *) v1, strlen(v1), ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) v2, strlen(v2), ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)v1, strlen(v1), ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)v2, strlen(v2), ZIPLIST_TAIL);
/* Pop values again and compare their value. */
p = ziplistIndex(zl, 0);
TEST_ASSERT(ziplistGet(p, &entry, &elen, &value));
TEST_ASSERT(strncmp(v1, (char *) entry, elen) == 0);
TEST_ASSERT(strncmp(v1, (char *)entry, elen) == 0);
p = ziplistIndex(zl, 1);
TEST_ASSERT(ziplistGet(p, &entry, &elen, &value));
TEST_ASSERT(strncmp(v2, (char *) entry, elen) == 0);
TEST_ASSERT(strncmp(v2, (char *)entry, elen) == 0);
zfree(zl);
return 0;
}
int test_ziplistRegressionTestDeleteNextToLastEntries(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
char v[3][257] = {{0}};
zlentry e[3] = {{.prevrawlensize = 0, .prevrawlen = 0, .lensize = 0,
.len = 0, .headersize = 0, .encoding = 0, .p = NULL}};
zlentry e[3] = {
{.prevrawlensize = 0, .prevrawlen = 0, .lensize = 0, .len = 0, .headersize = 0, .encoding = 0, .p = NULL}};
size_t i;
for (i = 0; i < (sizeof(v) / sizeof(v[0])); i++) {
@ -543,7 +523,7 @@ int test_ziplistRegressionTestDeleteNextToLastEntries(int argc, char **argv, int
zl = ziplistNew();
for (i = 0; i < (sizeof(v) / sizeof(v[0])); i++) {
zl = ziplistPush(zl, (unsigned char *) v[i], strlen(v[i]), ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)v[i], strlen(v[i]), ZIPLIST_TAIL);
}
verify(zl, e);
@ -567,14 +547,13 @@ int test_ziplistRegressionTestDeleteNextToLastEntries(int argc, char **argv, int
int test_ziplistCreateLongListAndCheckIndices(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = ziplistNew();
char buf[32];
int i, len;
for (i = 0; i < 1000; i++) {
len = snprintf(buf, sizeof(buf), "%d", i);
zl = ziplistPush(zl, (unsigned char *) buf, len, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, len, ZIPLIST_TAIL);
}
for (i = 0; i < 1000; i++) {
p = ziplistIndex(zl, i);
@ -591,25 +570,23 @@ int test_ziplistCreateLongListAndCheckIndices(int argc, char **argv, int flags)
int test_ziplistCompareStringWithZiplistEntries(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
zl = createList();
p = ziplistIndex(zl, 0);
TEST_ASSERT(ziplistCompare(p, (unsigned char *) "hello", 5));
TEST_ASSERT(!ziplistCompare(p, (unsigned char *) "hella", 5));
TEST_ASSERT(ziplistCompare(p, (unsigned char *)"hello", 5));
TEST_ASSERT(!ziplistCompare(p, (unsigned char *)"hella", 5));
p = ziplistIndex(zl, 3);
TEST_ASSERT(ziplistCompare(p, (unsigned char *) "1024", 4));
TEST_ASSERT(!ziplistCompare(p, (unsigned char *) "1025", 4));
TEST_ASSERT(ziplistCompare(p, (unsigned char *)"1024", 4));
TEST_ASSERT(!ziplistCompare(p, (unsigned char *)"1025", 4));
zfree(zl);
return 0;
}
int test_ziplistMergeTest(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
/* create list gives us: [hello, foo, quux, 1024] */
zl = createList();
unsigned char *zl2 = createList();
@ -630,28 +607,27 @@ int test_ziplistMergeTest(int argc, char **argv, int flags) {
TEST_ASSERT(ziplistLen(zl2) == 8);
p = ziplistIndex(zl2, 0);
TEST_ASSERT(ziplistCompare(p, (unsigned char *) "hello", 5));
TEST_ASSERT(!ziplistCompare(p, (unsigned char *) "hella", 5));
TEST_ASSERT(ziplistCompare(p, (unsigned char *)"hello", 5));
TEST_ASSERT(!ziplistCompare(p, (unsigned char *)"hella", 5));
p = ziplistIndex(zl2, 3);
TEST_ASSERT(ziplistCompare(p, (unsigned char *) "1024", 4));
TEST_ASSERT(!ziplistCompare(p, (unsigned char *) "1025", 4));
TEST_ASSERT(ziplistCompare(p, (unsigned char *)"1024", 4));
TEST_ASSERT(!ziplistCompare(p, (unsigned char *)"1025", 4));
p = ziplistIndex(zl2, 4);
TEST_ASSERT(ziplistCompare(p, (unsigned char *) "hello", 5));
TEST_ASSERT(!ziplistCompare(p, (unsigned char *) "hella", 5));
TEST_ASSERT(ziplistCompare(p, (unsigned char *)"hello", 5));
TEST_ASSERT(!ziplistCompare(p, (unsigned char *)"hella", 5));
p = ziplistIndex(zl2, 7);
TEST_ASSERT(ziplistCompare(p, (unsigned char *) "1024", 4));
TEST_ASSERT(!ziplistCompare(p, (unsigned char *) "1025", 4));
TEST_ASSERT(ziplistCompare(p, (unsigned char *)"1024", 4));
TEST_ASSERT(!ziplistCompare(p, (unsigned char *)"1025", 4));
zfree(zl);
return 0;
}
int test_ziplistStressWithRandomPayloadsOfDifferentEncoding(int argc, char **argv, int flags) {
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
int accurate = (flags & UNIT_TEST_ACCURATE);
int i, j, len, where;
unsigned char *p;
@ -669,7 +645,7 @@ int test_ziplistStressWithRandomPayloadsOfDifferentEncoding(int argc, char **arg
for (i = 0; i < iteration; i++) {
zl = ziplistNew();
ref = listCreate();
listSetFreeMethod(ref, (void (*)(void *)) sdsfree);
listSetFreeMethod(ref, (void (*)(void *))sdsfree);
len = rand() % 256;
/* Create lists */
@ -679,22 +655,15 @@ int test_ziplistStressWithRandomPayloadsOfDifferentEncoding(int argc, char **arg
buflen = randstring(buf, 1, sizeof(buf) - 1);
} else {
switch (rand() % 3) {
case 0:
buflen = snprintf(buf, sizeof(buf), "%lld", (0LL + rand()) >> 20);
break;
case 1:
buflen = snprintf(buf, sizeof(buf), "%lld", (0LL + rand()));
break;
case 2:
buflen = snprintf(buf, sizeof(buf), "%lld", (0LL + rand()) << 20);
break;
default:
TEST_ASSERT(NULL);
case 0: buflen = snprintf(buf, sizeof(buf), "%lld", (0LL + rand()) >> 20); break;
case 1: buflen = snprintf(buf, sizeof(buf), "%lld", (0LL + rand())); break;
case 2: buflen = snprintf(buf, sizeof(buf), "%lld", (0LL + rand()) << 20); break;
default: TEST_ASSERT(NULL);
}
}
/* Add to ziplist */
zl = ziplistPush(zl, (unsigned char *) buf, buflen, where);
zl = ziplistPush(zl, (unsigned char *)buf, buflen, where);
/* Add to reference list */
if (where == ZIPLIST_HEAD) {
@ -725,22 +694,20 @@ int test_ziplistStressWithRandomPayloadsOfDifferentEncoding(int argc, char **arg
}
zfree(zl);
listRelease(ref);
}
return 0;
}
int test_ziplistCascadeUpdateEdgeCases(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
/* Inserting a entry with data length greater than ZIP_BIG_PREVLEN-4
* will leads to cascade update. */
size_t s1 = ZIP_BIG_PREVLEN - 4, s2 = ZIP_BIG_PREVLEN - 3;
zl = ziplistNew();
zlentry e[4] = {{.prevrawlensize = 0, .prevrawlen = 0, .lensize = 0,
.len = 0, .headersize = 0, .encoding = 0, .p = NULL}};
zlentry e[4] = {
{.prevrawlensize = 0, .prevrawlen = 0, .lensize = 0, .len = 0, .headersize = 0, .encoding = 0, .p = NULL}};
zl = insertHelper(zl, 'a', s1, ZIPLIST_ENTRY_HEAD(zl));
verify(zl, e);
@ -811,9 +778,8 @@ int test_ziplistCascadeUpdateEdgeCases(int argc, char **argv, int flags) {
int test_ziplistInsertEdgeCase(int argc, char **argv, int flags) {
UNUSED(flags);
if (argc >= 4)
srand(atoi(argv[3]));
//From issue #7170
if (argc >= 4) srand(atoi(argv[3]));
// From issue #7170
zl = ziplistNew();
/* We set some values to almost reach the critical point - 254 */
@ -822,15 +788,15 @@ int test_ziplistInsertEdgeCase(int argc, char **argv, int flags) {
memset(A_250, 'A', 250);
/* After the rpush, the list look like: [one two A_252 A_250 three 10] */
zl = ziplistPush(zl, (unsigned char *) "one", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "two", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) A_252, strlen(A_252), ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) A_250, strlen(A_250), ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "three", 5, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "10", 2, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"one", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"two", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)A_252, strlen(A_252), ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)A_250, strlen(A_250), ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"three", 5, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"10", 2, ZIPLIST_TAIL);
p = ziplistIndex(zl, 2);
TEST_ASSERT(ziplistCompare(p, (unsigned char *) A_252, strlen(A_252)));
TEST_ASSERT(ziplistCompare(p, (unsigned char *)A_252, strlen(A_252)));
/* When we remove A_252, the list became: [one two A_250 three 10]
* A_250's prev node became node two, because node two quite small
@ -840,23 +806,22 @@ int test_ziplistInsertEdgeCase(int argc, char **argv, int flags) {
zl = ziplistDelete(zl, &p);
p = ziplistIndex(zl, 3);
TEST_ASSERT(ziplistCompare(p, (unsigned char *) "three", 5));
TEST_ASSERT(ziplistCompare(p, (unsigned char *)"three", 5));
/* We want to insert a node after A_250, the list became: [one two A_250 10 three 10]
* Because the new node is quite small, node three prevlenSize will shrink to 1 */
zl = ziplistInsert(zl, p, (unsigned char *) "10", 2);
zl = ziplistInsert(zl, p, (unsigned char *)"10", 2);
/* Last element should equal 10 */
p = ziplistIndex(zl, -1);
TEST_ASSERT(ziplistCompare(p, (unsigned char *) "10", 2));
TEST_ASSERT(ziplistCompare(p, (unsigned char *)"10", 2));
zfree(zl);
return 0;
}
int test_ziplistStressWithVariableSize(int argc, char **argv, int flags) {
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
int accurate = (flags & UNIT_TEST_ACCURATE);
unsigned long long start = usec();
@ -872,30 +837,29 @@ int test_ziplistStressWithVariableSize(int argc, char **argv, int flags) {
}
int test_BenchmarkziplistFind(int argc, char **argv, int flags) {
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
int accurate = (flags & UNIT_TEST_ACCURATE);
zl = ziplistNew();
iteration = accurate ? 100000 : 100;
for (int i = 0; i < iteration; i++) {
char buf[4096] = "asdf";
zl = ziplistPush(zl, (unsigned char *) buf, 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 40, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 400, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 4000, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "1", 1, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "10", 2, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "100", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "1000", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "10000", 5, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "100000", 6, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 40, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 400, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 4000, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"1", 1, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"10", 2, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"100", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"1000", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"10000", 5, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"100000", 6, ZIPLIST_TAIL);
}
unsigned long long start = usec();
for (int i = 0; i < 2000; i++) {
unsigned char *fptr = ziplistIndex(zl, ZIPLIST_HEAD);
fptr = ziplistFind(zl, fptr, (unsigned char *) "nothing", 7, 1);
fptr = ziplistFind(zl, fptr, (unsigned char *)"nothing", 7, 1);
}
TEST_PRINT_INFO("Benchmark ziplistFind: usec=%lld", usec() - start);
@ -904,24 +868,23 @@ int test_BenchmarkziplistFind(int argc, char **argv, int flags) {
}
int test_BenchmarkziplistIndex(int argc, char **argv, int flags) {
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
int accurate = (flags & UNIT_TEST_ACCURATE);
zl = ziplistNew();
iteration = accurate ? 100000 : 100;
for (int i = 0; i < iteration; i++) {
char buf[4096] = "asdf";
zl = ziplistPush(zl, (unsigned char *) buf, 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 40, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 400, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 4000, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "1", 1, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "10", 2, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "100", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "1000", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "10000", 5, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "100000", 6, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 40, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 400, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 4000, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"1", 1, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"10", 2, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"100", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"1000", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"10000", 5, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"100000", 6, ZIPLIST_TAIL);
}
unsigned long long start = usec();
@ -935,23 +898,22 @@ int test_BenchmarkziplistIndex(int argc, char **argv, int flags) {
}
int test_BenchmarkziplistValidateIntegrity(int argc, char **argv, int flags) {
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
int accurate = (flags & UNIT_TEST_ACCURATE);
zl = ziplistNew();
iteration = accurate ? 100000 : 100;
for (int i = 0; i < iteration; i++) {
char buf[4096] = "asdf";
zl = ziplistPush(zl, (unsigned char *) buf, 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 40, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 400, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 4000, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "1", 1, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "10", 2, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "100", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "1000", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "10000", 5, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "100000", 6, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 40, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 400, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 4000, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"1", 1, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"10", 2, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"100", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"1000", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"10000", 5, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"100000", 6, ZIPLIST_TAIL);
}
unsigned long long start = usec();
for (int i = 0; i < 2000; i++) {
@ -964,29 +926,28 @@ int test_BenchmarkziplistValidateIntegrity(int argc, char **argv, int flags) {
}
int test_BenchmarkziplistCompareWithString(int argc, char **argv, int flags) {
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
int accurate = (flags & UNIT_TEST_ACCURATE);
zl = ziplistNew();
iteration = accurate ? 100000 : 100;
for (int i = 0; i < iteration; i++) {
char buf[4096] = "asdf";
zl = ziplistPush(zl, (unsigned char *) buf, 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 40, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 400, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 4000, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "1", 1, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "10", 2, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "100", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "1000", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "10000", 5, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "100000", 6, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 40, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 400, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 4000, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"1", 1, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"10", 2, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"100", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"1000", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"10000", 5, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"100000", 6, ZIPLIST_TAIL);
}
unsigned long long start = usec();
for (int i = 0; i < 2000; i++) {
unsigned char *eptr = ziplistIndex(zl, 0);
while (eptr != NULL) {
ziplistCompare(eptr, (unsigned char *) "nothing", 7);
ziplistCompare(eptr, (unsigned char *)"nothing", 7);
eptr = ziplistNext(zl, eptr);
}
}
@ -997,29 +958,28 @@ int test_BenchmarkziplistCompareWithString(int argc, char **argv, int flags) {
}
int test_BenchmarkziplistCompareWithNumber(int argc, char **argv, int flags) {
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
int accurate = (flags & UNIT_TEST_ACCURATE);
zl = ziplistNew();
iteration = accurate ? 100000 : 100;
for (int i = 0; i < iteration; i++) {
char buf[4096] = "asdf";
zl = ziplistPush(zl, (unsigned char *) buf, 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 40, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 400, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) buf, 4000, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "1", 1, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "10", 2, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "100", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "1000", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "10000", 5, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *) "100000", 6, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 40, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 400, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)buf, 4000, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"1", 1, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"10", 2, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"100", 3, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"1000", 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"10000", 5, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)"100000", 6, ZIPLIST_TAIL);
}
unsigned long long start = usec();
for (int i = 0; i < 2000; i++) {
unsigned char *eptr = ziplistIndex(zl, 0);
while (eptr != NULL) {
ziplistCompare(eptr, (unsigned char *) "99999", 5);
ziplistCompare(eptr, (unsigned char *)"99999", 5);
eptr = ziplistNext(zl, eptr);
}
}
@ -1030,17 +990,16 @@ int test_BenchmarkziplistCompareWithNumber(int argc, char **argv, int flags) {
}
int test_ziplistStress__ziplistCascadeUpdate(int argc, char **argv, int flags) {
if (argc >= 4)
srand(atoi(argv[3]));
if (argc >= 4) srand(atoi(argv[3]));
int accurate = (flags & UNIT_TEST_ACCURATE);
char data[ZIP_BIG_PREVLEN];
zl = ziplistNew();
iteration = accurate ? 100000 : 100;
for (int i = 0; i < iteration; i++) {
zl = ziplistPush(zl, (unsigned char *) data, ZIP_BIG_PREVLEN - 4, ZIPLIST_TAIL);
zl = ziplistPush(zl, (unsigned char *)data, ZIP_BIG_PREVLEN - 4, ZIPLIST_TAIL);
}
unsigned long long start = usec();
zl = ziplistPush(zl, (unsigned char *) data, ZIP_BIG_PREVLEN - 3, ZIPLIST_HEAD);
zl = ziplistPush(zl, (unsigned char *)data, ZIP_BIG_PREVLEN - 3, ZIPLIST_HEAD);
TEST_PRINT_INFO("Stress __ziplistCascadeUpdate: usec=%lld", usec() - start);

View File

@ -93,12 +93,12 @@ static int stringmatchlen_impl(const char *pattern,
stringLen--;
break;
case '[': {
int not, match;
int not_op, match;
pattern++;
patternLen--;
not= pattern[0] == '^';
if (not) {
not_op = pattern[0] == '^';
if (not_op) {
pattern++;
patternLen--;
}
@ -141,7 +141,7 @@ static int stringmatchlen_impl(const char *pattern,
pattern++;
patternLen--;
}
if (not) match = !match;
if (not_op) match = !match;
if (!match) return 0; /* no match */
string++;
stringLen--;

View File

@ -115,8 +115,8 @@ static struct config {
int cluster_node_count;
struct clusterNode **cluster_nodes;
struct serverConfig *redis_config;
struct hdr_histogram* latency_histogram;
struct hdr_histogram* current_sec_latency_histogram;
struct hdr_histogram *latency_histogram;
struct hdr_histogram *current_sec_latency_histogram;
_Atomic int is_fetching_slots;
_Atomic int is_updating_slots;
_Atomic int slots_last_update;
@ -344,7 +344,7 @@ static void freeClient(client c) {
aeDeleteFileEvent(el, c->context->fd, AE_WRITABLE);
aeDeleteFileEvent(el, c->context->fd, AE_READABLE);
if (c->thread_id >= 0) {
int requests_finished = atomic_load_explicit(&config.requests_finished,memory_order_relaxed);
int requests_finished = atomic_load_explicit(&config.requests_finished, memory_order_relaxed);
if (requests_finished >= config.requests) {
aeStop(el);
}
@ -402,7 +402,7 @@ static void setClusterKeyHashTag(client c) {
assert(c->thread_id >= 0);
clusterNode *node = c->cluster_node;
assert(node);
int is_updating_slots = atomic_load_explicit(&config.is_updating_slots,memory_order_relaxed);
int is_updating_slots = atomic_load_explicit(&config.is_updating_slots, memory_order_relaxed);
/* If updateClusterSlotsConfiguration is updating the slots array,
* call updateClusterSlotsConfiguration is order to block the thread
* since the mutex is locked. When the slots will be updated by the
@ -423,7 +423,7 @@ static void setClusterKeyHashTag(client c) {
}
static void clientDone(client c) {
int requests_finished = atomic_load_explicit(&config.requests_finished,memory_order_relaxed);
int requests_finished = atomic_load_explicit(&config.requests_finished, memory_order_relaxed);
if (requests_finished >= config.requests) {
freeClient(c);
if (!config.num_threads && config.el) aeStop(config.el);
@ -517,23 +517,27 @@ static void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
}
continue;
}
int requests_finished = atomic_fetch_add_explicit(&config.requests_finished,1,memory_order_relaxed);
if (requests_finished < config.requests){
if (config.num_threads == 0) {
hdr_record_value(
config.latency_histogram, // Histogram to record to
(long)c->latency<=CONFIG_LATENCY_HISTOGRAM_MAX_VALUE ? (long)c->latency : CONFIG_LATENCY_HISTOGRAM_MAX_VALUE); // Value to record
hdr_record_value(
config.current_sec_latency_histogram, // Histogram to record to
(long)c->latency<=CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE ? (long)c->latency : CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE); // Value to record
} else {
hdr_record_value_atomic(
config.latency_histogram, // Histogram to record to
(long)c->latency<=CONFIG_LATENCY_HISTOGRAM_MAX_VALUE ? (long)c->latency : CONFIG_LATENCY_HISTOGRAM_MAX_VALUE); // Value to record
hdr_record_value_atomic(
config.current_sec_latency_histogram, // Histogram to record to
(long)c->latency<=CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE ? (long)c->latency : CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE); // Value to record
}
int requests_finished = atomic_fetch_add_explicit(&config.requests_finished, 1, memory_order_relaxed);
if (requests_finished < config.requests) {
if (config.num_threads == 0) {
hdr_record_value(config.latency_histogram, // Histogram to record to
(long)c->latency <= CONFIG_LATENCY_HISTOGRAM_MAX_VALUE
? (long)c->latency
: CONFIG_LATENCY_HISTOGRAM_MAX_VALUE); // Value to record
hdr_record_value(config.current_sec_latency_histogram, // Histogram to record to
(long)c->latency <= CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE
? (long)c->latency
: CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE); // Value to record
} else {
hdr_record_value_atomic(config.latency_histogram, // Histogram to record to
(long)c->latency <= CONFIG_LATENCY_HISTOGRAM_MAX_VALUE
? (long)c->latency
: CONFIG_LATENCY_HISTOGRAM_MAX_VALUE); // Value to record
hdr_record_value_atomic(config.current_sec_latency_histogram, // Histogram to record to
(long)c->latency <= CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE
? (long)c->latency
: CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE); // Value to record
}
}
c->pending--;
if (c->pending == 0) {
@ -556,7 +560,7 @@ static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
/* Initialize request when nothing was written. */
if (c->written == 0) {
/* Enforce upper bound to number of requests. */
int requests_issued = atomic_fetch_add_explicit(&config.requests_issued,config.pipeline,memory_order_relaxed);
int requests_issued = atomic_fetch_add_explicit(&config.requests_issued, config.pipeline, memory_order_relaxed);
if (requests_issued >= config.requests) {
return;
}
@ -794,7 +798,7 @@ static client createClient(char *cmd, size_t len, client from, int thread_id) {
/* In idle mode, clients still need to register readHandler for catching errors */
aeCreateFileEvent(el, c->context->fd, AE_READABLE, readHandler, c);
listAddNodeTail(config.clients,c);
listAddNodeTail(config.clients, c);
atomic_fetch_add_explicit(&config.liveclients, 1, memory_order_relaxed);
c->slots_last_update = atomic_load_explicit(&config.slots_last_update, memory_order_relaxed);
@ -1232,10 +1236,9 @@ static int fetchClusterSlotsConfiguration(client c) {
redisReply *reply = NULL;
is_fetching_slots = atomic_fetch_add_explicit(&config.is_fetching_slots, 1, memory_order_relaxed);
if (is_fetching_slots) return -1; //TODO: use other codes || errno ?
if (is_fetching_slots) return -1; // TODO: use other codes || errno ?
atomic_store_explicit(&config.is_fetching_slots, 1, memory_order_relaxed);
fprintf(stderr,
"WARNING: Cluster slots configuration changed, fetching new one...\n");
fprintf(stderr, "WARNING: Cluster slots configuration changed, fetching new one...\n");
const char *errmsg = "Failed to update cluster slots configuration";
static dictType dtype = {
dictSdsHash, /* hash function */

View File

@ -388,7 +388,7 @@ char *zstrdup(const char *s) {
}
size_t zmalloc_used_memory(void) {
size_t um = atomic_load_explicit(&used_memory,memory_order_relaxed);
size_t um = atomic_load_explicit(&used_memory, memory_order_relaxed);
return um;
}

View File

@ -26,6 +26,7 @@ if __name__ == '__main__':
test_suites.append({'file': file, 'tests': tests})
test_suites.sort(key=lambda test_suite: test_suite['file'])
output.write("""/* Do not modify this file, it's automatically generated from utils/generate-unit-test-header.py */
/* clang-format off */
typedef int unitTestProc(int argc, char **argv, int flags);
typedef struct unitTest {
@ -56,4 +57,4 @@ struct unitTestSuite {
""")
for test_suite in test_suites:
output.write(' {{"{0}", __{1}}},\n'.format(test_suite['file'], test_suite['file'].replace('.c', '_c')))
output.write('};\n')
output.write('};\n')