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

Function sortCommand

app/redis-6.2.6/src/sort.c:192–597  ·  view source on GitHub ↗

The SORT command is the most complex command in Redis. Warning: this code * is optimized for speed and a bit less for readability */

Source from the content-addressed store, hash-verified

190/* The SORT command is the most complex command in Redis. Warning: this code
191 * is optimized for speed and a bit less for readability */
192void sortCommand(client *c) {
193 list *operations;
194 unsigned int outputlen = 0;
195 int desc = 0, alpha = 0;
196 long limit_start = 0, limit_count = -1, start, end;
197 int j, dontsort = 0, vectorlen;
198 int getop = 0; /* GET operation counter */
199 int int_conversion_error = 0;
200 int syntax_error = 0;
201 robj *sortval, *sortby = NULL, *storekey = NULL;
202 redisSortObject *vector; /* Resulting vector to sort */
203
204 /* Create a list of operations to perform for every sorted element.
205 * Operations can be GET */
206 operations = listCreate();
207 listSetFreeMethod(operations,zfree);
208 j = 2; /* options start at argv[2] */
209
210 /* The SORT command has an SQL-alike syntax, parse it */
211 while(j < c->argc) {
212 int leftargs = c->argc-j-1;
213 if (!strcasecmp(c->argv[j]->ptr,"asc")) {
214 desc = 0;
215 } else if (!strcasecmp(c->argv[j]->ptr,"desc")) {
216 desc = 1;
217 } else if (!strcasecmp(c->argv[j]->ptr,"alpha")) {
218 alpha = 1;
219 } else if (!strcasecmp(c->argv[j]->ptr,"limit") && leftargs >= 2) {
220 if ((getLongFromObjectOrReply(c, c->argv[j+1], &limit_start, NULL)
221 != C_OK) ||
222 (getLongFromObjectOrReply(c, c->argv[j+2], &limit_count, NULL)
223 != C_OK))
224 {
225 syntax_error++;
226 break;
227 }
228 j+=2;
229 } else if (!strcasecmp(c->argv[j]->ptr,"store") && leftargs >= 1) {
230 storekey = c->argv[j+1];
231 j++;
232 } else if (!strcasecmp(c->argv[j]->ptr,"by") && leftargs >= 1) {
233 sortby = c->argv[j+1];
234 /* If the BY pattern does not contain '*', i.e. it is constant,
235 * we don't need to sort nor to lookup the weight keys. */
236 if (strchr(c->argv[j+1]->ptr,'*') == NULL) {
237 dontsort = 1;
238 } else {
239 /* If BY is specified with a real patter, we can't accept
240 * it in cluster mode. */
241 if (server.cluster_enabled) {
242 addReplyError(c,"BY option of SORT denied in Cluster mode.");
243 syntax_error++;
244 break;
245 }
246 }
247 j++;
248 } else if (!strcasecmp(c->argv[j]->ptr,"get") && leftargs >= 1) {
249 if (server.cluster_enabled) {

Callers

nothing calls this directly

Calls 15

listCreateFunction · 0.85
strcasecmpFunction · 0.85
getLongFromObjectOrReplyFunction · 0.85
strchrFunction · 0.85
addReplyErrorFunction · 0.85
listAddNodeTailFunction · 0.85
createSortOperationFunction · 0.85
addReplyErrorObjectFunction · 0.85
listReleaseFunction · 0.85
lookupKeyReadFunction · 0.85
lookupKeyWriteFunction · 0.85
incrRefCountFunction · 0.85

Tested by

no test coverage detected