1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
// SPDX-FileCopyrightText: 2024 Redict Contributors
// SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo <antirez at gmail dot com>
//
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-License-Identifier: LGPL-3.0-only
#include "redictmodule.h"
#include <math.h>
#include <errno.h>
/* ZSET.REM key element
*
* Removes an occurrence of an element from a sorted set. Replies with the
* number of removed elements (0 or 1).
*/
int zset_rem(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
if (argc != 3) return RedictModule_WrongArity(ctx);
RedictModule_AutoMemory(ctx);
int keymode = REDICTMODULE_READ | REDICTMODULE_WRITE;
RedictModuleKey *key = RedictModule_OpenKey(ctx, argv[1], keymode);
int deleted;
if (RedictModule_ZsetRem(key, argv[2], &deleted) == REDICTMODULE_OK)
return RedictModule_ReplyWithLongLong(ctx, deleted);
else
return RedictModule_ReplyWithError(ctx, "ERR ZsetRem failed");
}
/* ZSET.ADD key score member
*
* Adds a specified member with the specified score to the sorted
* set stored at key.
*/
int zset_add(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
if (argc != 4) return RedictModule_WrongArity(ctx);
RedictModule_AutoMemory(ctx);
int keymode = REDICTMODULE_READ | REDICTMODULE_WRITE;
RedictModuleKey *key = RedictModule_OpenKey(ctx, argv[1], keymode);
size_t len;
double score;
char *endptr;
const char *str = RedictModule_StringPtrLen(argv[2], &len);
score = strtod(str, &endptr);
if (*endptr != '\0' || errno == ERANGE)
return RedictModule_ReplyWithError(ctx, "value is not a valid float");
if (RedictModule_ZsetAdd(key, score, argv[3], NULL) == REDICTMODULE_OK)
return RedictModule_ReplyWithSimpleString(ctx, "OK");
else
return RedictModule_ReplyWithError(ctx, "ERR ZsetAdd failed");
}
/* ZSET.INCRBY key member increment
*
* Increments the score stored at member in the sorted set stored at key by increment.
* Replies with the new score of this element.
*/
int zset_incrby(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
if (argc != 4) return RedictModule_WrongArity(ctx);
RedictModule_AutoMemory(ctx);
int keymode = REDICTMODULE_READ | REDICTMODULE_WRITE;
RedictModuleKey *key = RedictModule_OpenKey(ctx, argv[1], keymode);
size_t len;
double score, newscore;
char *endptr;
const char *str = RedictModule_StringPtrLen(argv[3], &len);
score = strtod(str, &endptr);
if (*endptr != '\0' || errno == ERANGE)
return RedictModule_ReplyWithError(ctx, "value is not a valid float");
if (RedictModule_ZsetIncrby(key, score, argv[2], NULL, &newscore) == REDICTMODULE_OK)
return RedictModule_ReplyWithDouble(ctx, newscore);
else
return RedictModule_ReplyWithError(ctx, "ERR ZsetIncrby failed");
}
int RedictModule_OnLoad(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
REDICTMODULE_NOT_USED(argv);
REDICTMODULE_NOT_USED(argc);
if (RedictModule_Init(ctx, "zset", 1, REDICTMODULE_APIVER_1) == REDICTMODULE_ERR)
return REDICTMODULE_ERR;
if (RedictModule_CreateCommand(ctx, "zset.rem", zset_rem, "write",
1, 1, 1) == REDICTMODULE_ERR)
return REDICTMODULE_ERR;
if (RedictModule_CreateCommand(ctx, "zset.add", zset_add, "write",
1, 1, 1) == REDICTMODULE_ERR)
return REDICTMODULE_ERR;
if (RedictModule_CreateCommand(ctx, "zset.incrby", zset_incrby, "write",
1, 1, 1) == REDICTMODULE_ERR)
return REDICTMODULE_ERR;
return REDICTMODULE_OK;
}
|