* STREAM.TRIM key (MAXLEN (=|~) length | MINID (=|~) id) */
| 174 | * STREAM.TRIM key (MAXLEN (=|~) length | MINID (=|~) id) |
| 175 | */ |
| 176 | int stream_trim(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { |
| 177 | if (argc != 5) { |
| 178 | RedisModule_WrongArity(ctx); |
| 179 | return REDISMODULE_OK; |
| 180 | } |
| 181 | |
| 182 | /* Parse args */ |
| 183 | int trim_by_id = 0; /* 0 = maxlen, 1 = minid */ |
| 184 | long long maxlen; |
| 185 | RedisModuleStreamID minid; |
| 186 | size_t arg_len; |
| 187 | const char *arg = RedisModule_StringPtrLen(argv[2], &arg_len); |
| 188 | if (!strcasecmp(arg, "minid")) { |
| 189 | trim_by_id = 1; |
| 190 | if (RedisModule_StringToStreamID(argv[4], &minid) != REDISMODULE_OK) { |
| 191 | RedisModule_ReplyWithError(ctx, "ERR Invalid stream ID"); |
| 192 | return REDISMODULE_OK; |
| 193 | } |
| 194 | } else if (!strcasecmp(arg, "maxlen")) { |
| 195 | if (RedisModule_StringToLongLong(argv[4], &maxlen) == REDISMODULE_ERR) { |
| 196 | RedisModule_ReplyWithError(ctx, "ERR Maxlen must be a number"); |
| 197 | return REDISMODULE_OK; |
| 198 | } |
| 199 | } else { |
| 200 | RedisModule_ReplyWithError(ctx, "ERR Invalid arguments"); |
| 201 | return REDISMODULE_OK; |
| 202 | } |
| 203 | |
| 204 | /* Approx or exact */ |
| 205 | int flags; |
| 206 | arg = RedisModule_StringPtrLen(argv[3], &arg_len); |
| 207 | if (arg_len == 1 && arg[0] == '~') { |
| 208 | flags = REDISMODULE_STREAM_TRIM_APPROX; |
| 209 | } else if (arg_len == 1 && arg[0] == '=') { |
| 210 | flags = 0; |
| 211 | } else { |
| 212 | RedisModule_ReplyWithError(ctx, "ERR Invalid approx-or-exact mark"); |
| 213 | return REDISMODULE_OK; |
| 214 | } |
| 215 | |
| 216 | /* Trim */ |
| 217 | RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_WRITE); |
| 218 | long long trimmed; |
| 219 | if (trim_by_id) { |
| 220 | trimmed = RedisModule_StreamTrimByID(key, flags, &minid); |
| 221 | } else { |
| 222 | trimmed = RedisModule_StreamTrimByLength(key, flags, maxlen); |
| 223 | } |
| 224 | |
| 225 | /* Return result */ |
| 226 | if (trimmed < 0) { |
| 227 | RedisModule_ReplyWithError(ctx, "ERR Trimming failed"); |
| 228 | } else { |
| 229 | RedisModule_ReplyWithLongLong(ctx, trimmed); |
| 230 | } |
| 231 | RedisModule_CloseKey(key); |
| 232 | return REDISMODULE_OK; |
| 233 | } |
nothing calls this directly
no test coverage detected