MCPcopy Create free account
hub / github.com/F-Stack/f-stack / xaddCommand

Function xaddCommand

app/redis-6.2.6/src/t_stream.c:1785–1865  ·  view source on GitHub ↗

XADD key [(MAXLEN [~|=] | MINID [~|=] ) [LIMIT ]] [NOMKSTREAM] [field value] [field value] ... */

Source from the content-addressed store, hash-verified

1783
1784/* XADD key [(MAXLEN [~|=] <count> | MINID [~|=] <id>) [LIMIT <entries>]] [NOMKSTREAM] <ID or *> [field value] [field value] ... */
1785void xaddCommand(client *c) {
1786 /* Parse options. */
1787 streamAddTrimArgs parsed_args;
1788 int idpos = streamParseAddOrTrimArgsOrReply(c, &parsed_args, 1);
1789 if (idpos < 0)
1790 return; /* streamParseAddOrTrimArgsOrReply already replied. */
1791 int field_pos = idpos+1; /* The ID is always one argument before the first field */
1792
1793 /* Check arity. */
1794 if ((c->argc - field_pos) < 2 || ((c->argc-field_pos) % 2) == 1) {
1795 addReplyError(c,"wrong number of arguments for XADD");
1796 return;
1797 }
1798
1799 /* Return ASAP if minimal ID (0-0) was given so we avoid possibly creating
1800 * a new stream and have streamAppendItem fail, leaving an empty key in the
1801 * database. */
1802 if (parsed_args.id_given &&
1803 parsed_args.id.ms == 0 && parsed_args.id.seq == 0)
1804 {
1805 addReplyError(c,"The ID specified in XADD must be greater than 0-0");
1806 return;
1807 }
1808
1809 /* Lookup the stream at key. */
1810 robj *o;
1811 stream *s;
1812 if ((o = streamTypeLookupWriteOrCreate(c,c->argv[1],parsed_args.no_mkstream)) == NULL) return;
1813 s = o->ptr;
1814
1815 /* Return ASAP if the stream has reached the last possible ID */
1816 if (s->last_id.ms == UINT64_MAX && s->last_id.seq == UINT64_MAX) {
1817 addReplyError(c,"The stream has exhausted the last possible ID, "
1818 "unable to add more items");
1819 return;
1820 }
1821
1822 /* Append using the low level function and return the ID. */
1823 streamID id;
1824 if (streamAppendItem(s,c->argv+field_pos,(c->argc-field_pos)/2,
1825 &id, parsed_args.id_given ? &parsed_args.id : NULL) == C_ERR)
1826 {
1827 if (errno == EDOM)
1828 addReplyError(c,"The ID specified in XADD is equal or smaller than "
1829 "the target stream top item");
1830 else
1831 addReplyError(c,"Elements are too large to be stored");
1832 return;
1833 }
1834 addReplyStreamID(c,&id);
1835
1836 signalModifiedKey(c,c->db,c->argv[1]);
1837 notifyKeyspaceEvent(NOTIFY_STREAM,"xadd",c->argv[1],c->db->id);
1838 server.dirty++;
1839
1840 /* Trim if needed. */
1841 if (parsed_args.trim_strategy != TRIM_STRATEGY_NONE) {
1842 if (streamTrim(s, &parsed_args)) {

Callers

nothing calls this directly

Calls 14

addReplyErrorFunction · 0.85
streamAppendItemFunction · 0.85
addReplyStreamIDFunction · 0.85
signalModifiedKeyFunction · 0.85
notifyKeyspaceEventFunction · 0.85
streamTrimFunction · 0.85
createObjectFromStreamIDFunction · 0.85

Tested by

no test coverage detected