MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / xtrimCommand

Function xtrimCommand

src/t_stream.cpp:3266–3299  ·  view source on GitHub ↗

General form: XTRIM [... options ...] * * List of options: * * Trim strategies: * * MAXLEN [~|=] -- Trim so that the stream will be capped at * the specified length. Use ~ before the * count in order to demand approximated trimming * (like XADD MAXLEN option). * MINID [~|=]

Source from the content-addressed store, hash-verified

3264 * Has meaning only if `~` was provided.
3265 */
3266void xtrimCommand(client *c) {
3267 robj *o;
3268
3269 /* Argument parsing. */
3270 streamAddTrimArgs parsed_args;
3271 if (streamParseAddOrTrimArgsOrReply(c, &parsed_args, 0) < 0)
3272 return; /* streamParseAddOrTrimArgsOrReply already replied. */
3273
3274 /* If the key does not exist, we are ok returning zero, that is, the
3275 * number of elements removed from the stream. */
3276 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL
3277 || checkType(c,o,OBJ_STREAM)) return;
3278 stream *s = (stream*)ptrFromObj(o);
3279
3280 /* Perform the trimming. */
3281 int64_t deleted = streamTrim(s, &parsed_args);
3282 if (deleted) {
3283 notifyKeyspaceEvent(NOTIFY_STREAM,"xtrim",c->argv[1],c->db->id);
3284 if (parsed_args.approx_trim) {
3285 /* In case our trimming was limited (by LIMIT or by ~) we must
3286 * re-write the relevant trim argument to make sure there will be
3287 * no inconsistencies in AOF loading or in the replica.
3288 * It's enough to check only args->approx because there is no
3289 * way LIMIT is given without the ~ option. */
3290 streamRewriteApproxSpecifier(c,parsed_args.trim_strategy_arg_idx-1);
3291 streamRewriteTrimArgument(c,s,parsed_args.trim_strategy,parsed_args.trim_strategy_arg_idx);
3292 }
3293
3294 /* Propagate the write. */
3295 signalModifiedKey(c, c->db,c->argv[1]);
3296 g_pserver->dirty += deleted;
3297 }
3298 addReplyLongLong(c,deleted);
3299}
3300
3301/* Helper function for xinfoCommand.
3302 * Handles the variants of XINFO STREAM */

Callers

nothing calls this directly

Calls 10

lookupKeyWriteOrReplyFunction · 0.85
checkTypeFunction · 0.85
ptrFromObjFunction · 0.85
streamTrimFunction · 0.85
notifyKeyspaceEventFunction · 0.85
signalModifiedKeyFunction · 0.85
addReplyLongLongFunction · 0.85

Tested by

no test coverage detected