Commit 84105336 authored by Pieter Noordhuis's avatar Pieter Noordhuis
Browse files

moved code to delete a single node from a zset to a separate function

parent f84d3933
...@@ -4958,24 +4958,9 @@ static void zslInsert(zskiplist *zsl, double score, robj *obj) { ...@@ -4958,24 +4958,9 @@ static void zslInsert(zskiplist *zsl, double score, robj *obj) {
zsl->length++; zsl->length++;
} }
/* Delete an element with matching score/object from the skiplist. */ /* Internal function used by zslDelete, zslDeleteByScore and zslDeleteByRank */
static int zslDelete(zskiplist *zsl, double score, robj *obj) { void zslDeleteNode(zskiplist *zsl, zskiplistNode *x, zskiplistNode **update) {
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
int i; int i;
x = zsl->header;
for (i = zsl->level-1; i >= 0; i--) {
while (x->forward[i] &&
(x->forward[i]->score < score ||
(x->forward[i]->score == score &&
compareStringObjects(x->forward[i]->obj,obj) < 0)))
x = x->forward[i];
update[i] = x;
}
/* We may have multiple elements with the same score, what we need
* is to find the element with both the right score and object. */
x = x->forward[0];
if (x && score == x->score && compareStringObjects(x->obj,obj) == 0) {
for (i = 0; i < zsl->level; i++) { for (i = 0; i < zsl->level; i++) {
if (update[i]->forward[i] == x) { if (update[i]->forward[i] == x) {
if (i > 0) { if (i > 0) {
...@@ -4993,10 +4978,31 @@ static int zslDelete(zskiplist *zsl, double score, robj *obj) { ...@@ -4993,10 +4978,31 @@ static int zslDelete(zskiplist *zsl, double score, robj *obj) {
} else { } else {
zsl->tail = x->backward; zsl->tail = x->backward;
} }
zslFreeNode(x);
while(zsl->level > 1 && zsl->header->forward[zsl->level-1] == NULL) while(zsl->level > 1 && zsl->header->forward[zsl->level-1] == NULL)
zsl->level--; zsl->level--;
zsl->length--; zsl->length--;
}
/* Delete an element with matching score/object from the skiplist. */
static int zslDelete(zskiplist *zsl, double score, robj *obj) {
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
int i;
x = zsl->header;
for (i = zsl->level-1; i >= 0; i--) {
while (x->forward[i] &&
(x->forward[i]->score < score ||
(x->forward[i]->score == score &&
compareStringObjects(x->forward[i]->obj,obj) < 0)))
x = x->forward[i];
update[i] = x;
}
/* We may have multiple elements with the same score, what we need
* is to find the element with both the right score and object. */
x = x->forward[0];
if (x && score == x->score && compareStringObjects(x->obj,obj) == 0) {
zslDeleteNode(zsl, x, update);
zslFreeNode(x);
return 1; return 1;
} else { } else {
return 0; /* not found */ return 0; /* not found */
...@@ -5023,31 +5029,10 @@ static unsigned long zslDeleteRangeByScore(zskiplist *zsl, double min, double ma ...@@ -5023,31 +5029,10 @@ static unsigned long zslDeleteRangeByScore(zskiplist *zsl, double min, double ma
* is to find the element with both the right score and object. */ * is to find the element with both the right score and object. */
x = x->forward[0]; x = x->forward[0];
while (x && x->score <= max) { while (x && x->score <= max) {
zskiplistNode *next; zskiplistNode *next = x->forward[0];
zslDeleteNode(zsl, x, update);
for (i = 0; i < zsl->level; i++) {
if (update[i]->forward[i] == x) {
if (i > 0) {
update[i]->span[i-1] += x->span[i-1] - 1;
}
update[i]->forward[i] = x->forward[i];
} else {
/* invariant: i > 0, because update[0]->forward[0]
* is always equal to x */
update[i]->span[i-1] -= 1;
}
}
if (x->forward[0]) {
x->forward[0]->backward = x->backward;
} else {
zsl->tail = x->backward;
}
next = x->forward[0];
dictDelete(dict,x->obj); dictDelete(dict,x->obj);
zslFreeNode(x); zslFreeNode(x);
while(zsl->level > 1 && zsl->header->forward[zsl->level-1] == NULL)
zsl->level--;
zsl->length--;
removed++; removed++;
x = next; x = next;
} }
...@@ -5073,31 +5058,10 @@ static unsigned long zslDeleteRangeByRank(zskiplist *zsl, unsigned int start, un ...@@ -5073,31 +5058,10 @@ static unsigned long zslDeleteRangeByRank(zskiplist *zsl, unsigned int start, un
traversed++; traversed++;
x = x->forward[0]; x = x->forward[0];
while (x && traversed <= end) { while (x && traversed <= end) {
zskiplistNode *next; zskiplistNode *next = x->forward[0];
zslDeleteNode(zsl, x, update);
for (i = 0; i < zsl->level; i++) {
if (update[i]->forward[i] == x) {
if (i > 0) {
update[i]->span[i-1] += x->span[i-1] - 1;
}
update[i]->forward[i] = x->forward[i];
} else {
/* invariant: i > 0, because update[0]->forward[0]
* is always equal to x */
update[i]->span[i-1] -= 1;
}
}
if (x->forward[0]) {
x->forward[0]->backward = x->backward;
} else {
zsl->tail = x->backward;
}
next = x->forward[0];
dictDelete(dict,x->obj); dictDelete(dict,x->obj);
zslFreeNode(x); zslFreeNode(x);
while(zsl->level > 1 && zsl->header->forward[zsl->level-1] == NULL)
zsl->level--;
zsl->length--;
removed++; removed++;
traversed++; traversed++;
x = next; x = next;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment