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

Function streamParseAddOrTrimArgsOrReply

src/t_stream.cpp:894–1022  ·  view source on GitHub ↗

Parse the arguments of XADD/XTRIM. * * See streamAddTrimArgs for more details about the arguments handled. * * This function returns the position of the ID argument (relevant only to XADD). * On error -1 is returned and a reply is sent. */

Source from the content-addressed store, hash-verified

892 * This function returns the position of the ID argument (relevant only to XADD).
893 * On error -1 is returned and a reply is sent. */
894static int streamParseAddOrTrimArgsOrReply(client *c, streamAddTrimArgs *args, int xadd) {
895 /* Initialize arguments to defaults */
896 memset(args, 0, sizeof(*args));
897
898 /* Parse options. */
899 int i = 2; /* This is the first argument position where we could
900 find an option, or the ID. */
901 int limit_given = 0;
902 for (; i < c->argc; i++) {
903 int moreargs = (c->argc-1) - i; /* Number of additional arguments. */
904 char *opt = szFromObj(c->argv[i]);
905 if (xadd && opt[0] == '*' && opt[1] == '\0') {
906 /* This is just a fast path for the common case of auto-ID
907 * creation. */
908 break;
909 } else if (!strcasecmp(opt,"maxlen") && moreargs) {
910 if (args->trim_strategy != TRIM_STRATEGY_NONE) {
911 addReplyError(c,"syntax error, MAXLEN and MINID options at the same time are not compatible");
912 return -1;
913 }
914 args->approx_trim = 0;
915 char *next = szFromObj(c->argv[i+1]);
916 /* Check for the form MAXLEN ~ <count>. */
917 if (moreargs >= 2 && next[0] == '~' && next[1] == '\0') {
918 args->approx_trim = 1;
919 i++;
920 } else if (moreargs >= 2 && next[0] == '=' && next[1] == '\0') {
921 i++;
922 }
923 if (getLongLongFromObjectOrReply(c,c->argv[i+1],&args->maxlen,NULL)
924 != C_OK) return -1;
925
926 if (args->maxlen < 0) {
927 addReplyError(c,"The MAXLEN argument must be >= 0.");
928 return -1;
929 }
930 i++;
931 args->trim_strategy = TRIM_STRATEGY_MAXLEN;
932 args->trim_strategy_arg_idx = i;
933 } else if (!strcasecmp(opt,"minid") && moreargs) {
934 if (args->trim_strategy != TRIM_STRATEGY_NONE) {
935 addReplyError(c,"syntax error, MAXLEN and MINID options at the same time are not compatible");
936 return -1;
937 }
938 args->approx_trim = 0;
939 char *next = szFromObj(c->argv[i+1]);
940 /* Check for the form MINID ~ <id>|<age>. */
941 if (moreargs >= 2 && next[0] == '~' && next[1] == '\0') {
942 args->approx_trim = 1;
943 i++;
944 } else if (moreargs >= 2 && next[0] == '=' && next[1] == '\0') {
945 i++;
946 }
947
948 if (streamParseStrictIDOrReply(c,c->argv[i+1],&args->minid,0) != C_OK)
949 return -1;
950
951 i++;

Callers 2

xaddCommandFunction · 0.85
xtrimCommandFunction · 0.85

Calls 5

szFromObjFunction · 0.85
addReplyErrorFunction · 0.85
addReplyErrorObjectFunction · 0.85

Tested by

no test coverage detected