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

Function sortCompare

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

sortCompare() is used by qsort in sortCommand(). Given that qsort_r with * the additional parameter is not standard but a BSD-specific we have to * pass sorting parameters via the global 'server' structure */

Source from the content-addressed store, hash-verified

139 * the additional parameter is not standard but a BSD-specific we have to
140 * pass sorting parameters via the global 'server' structure */
141int sortCompare(const void *s1, const void *s2) {
142 const redisSortObject *so1 = s1, *so2 = s2;
143 int cmp;
144
145 if (!server.sort_alpha) {
146 /* Numeric sorting. Here it's trivial as we precomputed scores */
147 if (so1->u.score > so2->u.score) {
148 cmp = 1;
149 } else if (so1->u.score < so2->u.score) {
150 cmp = -1;
151 } else {
152 /* Objects have the same score, but we don't want the comparison
153 * to be undefined, so we compare objects lexicographically.
154 * This way the result of SORT is deterministic. */
155 cmp = compareStringObjects(so1->obj,so2->obj);
156 }
157 } else {
158 /* Alphanumeric sorting */
159 if (server.sort_bypattern) {
160 if (!so1->u.cmpobj || !so2->u.cmpobj) {
161 /* At least one compare object is NULL */
162 if (so1->u.cmpobj == so2->u.cmpobj)
163 cmp = 0;
164 else if (so1->u.cmpobj == NULL)
165 cmp = -1;
166 else
167 cmp = 1;
168 } else {
169 /* We have both the objects, compare them. */
170 if (server.sort_store) {
171 cmp = compareStringObjects(so1->u.cmpobj,so2->u.cmpobj);
172 } else {
173 /* Here we can use strcoll() directly as we are sure that
174 * the objects are decoded string objects. */
175 cmp = strcoll(so1->u.cmpobj->ptr,so2->u.cmpobj->ptr);
176 }
177 }
178 } else {
179 /* Compare elements directly. */
180 if (server.sort_store) {
181 cmp = compareStringObjects(so1->obj,so2->obj);
182 } else {
183 cmp = collateStringObjects(so1->obj,so2->obj);
184 }
185 }
186 }
187 return server.sort_desc ? -cmp : cmp;
188}
189
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 */

Callers

nothing calls this directly

Calls 2

compareStringObjectsFunction · 0.85
collateStringObjectsFunction · 0.85

Tested by

no test coverage detected