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

Function sortCompare

src/sort.cpp:142–189  ·  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

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

Callers

nothing calls this directly

Calls 3

compareStringObjectsFunction · 0.85
szFromObjFunction · 0.85
collateStringObjectsFunction · 0.85

Tested by

no test coverage detected