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

Function zslDefrag

src/defrag.cpp:227–260  ·  view source on GitHub ↗

Defrag helper for sorted set. * Update the robj pointer, defrag the skiplist struct and return the new score * reference. We may not access oldele pointer (not even the pointer stored in * the skiplist), as it was already freed. Newele may be null, in which case we * only need to defrag the skiplist, but not update the obj pointer. * When return value is non-NULL, it is the score reference th

Source from the content-addressed store, hash-verified

225 * When return value is non-NULL, it is the score reference that must be updated
226 * in the dict record. */
227double *zslDefrag(zskiplist *zsl, double score, sds oldele, sds newele) {
228 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x, *newx;
229 int i;
230 sds ele = newele? newele: oldele;
231
232 /* find the skiplist node referring to the object that was moved,
233 * and all pointers that need to be updated if we'll end up moving the skiplist node. */
234 x = zsl->header;
235 for (i = zsl->level-1; i >= 0; i--) {
236 while (x->level(i)->forward &&
237 x->level(i)->forward->ele != oldele && /* make sure not to access the
238 ->obj pointer if it matches
239 oldele */
240 (x->level(i)->forward->score < score ||
241 (x->level(i)->forward->score == score &&
242 sdscmp(x->level(i)->forward->ele,ele) < 0)))
243 x = x->level(i)->forward;
244 update[i] = x;
245 }
246
247 /* update the robj pointer inside the skip list record. */
248 x = x->level(0)->forward;
249 serverAssert(x && score == x->score && x->ele==oldele);
250 if (newele)
251 x->ele = newele;
252
253 /* try to defrag the skiplist record itself */
254 newx = (zskiplistNode*)activeDefragAlloc(x);
255 if (newx) {
256 zslUpdateNode(zsl, x, newx, update);
257 return &newx->score;
258 }
259 return NULL;
260}
261
262/* Defrag helper for sorted set.
263 * Defrag a single dict entry key name, and corresponding skiplist struct */

Callers 1

activeDefragZsetEntryFunction · 0.85

Calls 4

sdscmpFunction · 0.85
activeDefragAllocFunction · 0.85
zslUpdateNodeFunction · 0.85
levelMethod · 0.80

Tested by

no test coverage detected