Skip to content

Commit

Permalink
Optimize expire check in scan
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Söderqvist <[email protected]>
  • Loading branch information
zuiderkwast committed Nov 25, 2024
1 parent 02bf165 commit 6f4aed5
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ void keysCommand(client *c) {
typedef struct {
list *keys; /* elements that collect from dict */
robj *o; /* o must be a hash/set/zset object, NULL means current db */
serverDb *db; /* database currently being scanned */
long long type; /* the particular type when scan the db */
sds pattern; /* pattern string, NULL means no pattern */
long sampled; /* cumulative number of keys sampled */
Expand Down Expand Up @@ -954,6 +955,15 @@ void keysScanCallback(void *privdata, void *entry) {
}
}

/* Handle and skip expired key. */
if (objectIsExpired(obj)) {
robj kobj;
initStaticStringObject(kobj, key);
if (expireIfNeeded(data->db, &kobj, obj, 0) != KEY_VALID) {
return;
}
}

/* Keep this key. */
list *keys = data->keys;
listAddNodeTail(keys, key);
Expand Down Expand Up @@ -1184,6 +1194,7 @@ void scanGenericCommand(client *c, robj *o, unsigned long long cursor) {
* only keys are returned. */
scanData data = {
.keys = keys,
.db = c->db,
.o = o,
.type = type,
.pattern = use_pattern ? pat : NULL,
Expand Down Expand Up @@ -1252,25 +1263,7 @@ void scanGenericCommand(client *c, robj *o, unsigned long long cursor) {
serverPanic("Not handled encoding in SCAN.");
}

/* Step 3: Filter the expired keys */
/* TODO: Do this in the keysScanCallback where we have the valkey objects
* that contain the TTL (or add valkey object to the list instead of just
* the keys). Then we don't need to look them up again here. */
if (o == NULL && listLength(keys)) {
robj kobj;
listIter li;
listNode *ln;
listRewind(keys, &li);
while ((ln = listNext(&li))) {
sds key = listNodeValue(ln);
initStaticStringObject(kobj, key);
if (expireIfNeeded(c->db, &kobj, NULL, 0) != KEY_VALID) {
listDelNode(keys, ln);
}
}
}

/* Step 4: Reply to the client. */
/* Step 3: Reply to the client. */
addReplyArrayLen(c, 2);
addReplyBulkLongLong(c, cursor);

Expand Down

0 comments on commit 6f4aed5

Please sign in to comment.