Skip to content

Commit ec887e7

Browse files
ranshidxbasel
authored andcommitted
remove lazy expiration logic and tests
Signed-off-by: Ran Shidlansik <ranshid@amazon.com>
1 parent 399533c commit ec887e7

2 files changed

Lines changed: 8 additions & 88 deletions

File tree

src/hashtable.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -886,23 +886,9 @@ static void compactBucketChain(hashtable *ht, size_t bucket_index, int table_ind
886886
}
887887
}
888888

889-
static inline hashtableElementAccessState accessElementIfNeeded(hashtable *ht, void *elem, bucket *b, int pos_in_bucket, int table_index) {
889+
static inline hashtableElementAccessState accessElementIfNeeded(hashtable *ht, void *elem) {
890890
if (ht->type->accessElement == NULL) return ELEMENT_VALID;
891-
892-
hashtableElementAccessState element_status = ht->type->accessElement(ht, elem);
893-
if (element_status == ELEMENT_DELETE) {
894-
b->presence &= ~(1 << pos_in_bucket);
895-
ht->used[table_index]--;
896-
if (b->chained && !hashtableIsRehashingPaused(ht)) {
897-
/* Rehashing is paused while iterating and when a scan callback is
898-
* running. In those cases, we do the compaction in the scan and
899-
* iterator code instead. */
900-
fillBucketHole(ht, b, pos_in_bucket, table_index);
901-
}
902-
hashtableShrinkIfNeeded(ht);
903-
freeEntry(ht, elem);
904-
}
905-
return element_status;
891+
return ht->type->accessElement(ht, elem);
906892
}
907893

908894
/* Find an empty position in the table for inserting an entry with the given hash. */
@@ -2059,7 +2045,7 @@ int hashtableNext(hashtableIterator *iterator, void **elemptr) {
20592045
/* No entry here. */
20602046
continue;
20612047
}
2062-
if (accessElementIfNeeded(iter->hashtable, b->entries[iter->pos_in_bucket], b, iter->pos_in_bucket, iter->table) != ELEMENT_VALID) {
2048+
if (accessElementIfNeeded(iter->hashtable, b->entries[iter->pos_in_bucket]) != ELEMENT_VALID) {
20632049
continue;
20642050
}
20652051
if (!(iter->flags & HASHTABLE_ITER_SKIP_VALIDATION) && validateElementIfNeeded(iter->hashtable, b->entries[iter->pos_in_bucket]) != ENTRY_VALID) {

src/t_hash.c

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,6 @@ int hashTypeExpireEntry(void *entry) {
284284
}
285285
hashTypePropagateDeletion(server.access_context.db, key, entry);
286286
decrRefCount(keyobj);
287-
return 1;
288-
}
289-
290-
int hashTypeExpireRemoveEntry(void *entry) {
291-
serverAssert(server.access_context.key && server.access_context.db);
292-
hashTypeExpireEntry(entry);
293287
return hashTypeDelete(server.access_context.key, (sds)entry);
294288
}
295289

@@ -301,55 +295,7 @@ hashtableElementAccessState hashHashtableTypeAccess(hashtable *ht, void *entry)
301295

302296
if ((server.access_context.flags & OBJ_ACCESS_IGNORE_TTL) || !hashTypeEntryIsExpired(entry)) return ELEMENT_VALID;
303297

304-
if (!delete_expired) return ELEMENT_INVALID;
305-
306-
if (server.access_context.flags == OBJ_ACCESS_NONE) return ELEMENT_INVALID;
307-
308-
/* From this point we will be deleting the entry */
309-
server.access_context.expired++;
310-
robj *o = server.access_context.val;
311-
serverDb *db = server.access_context.db;
312-
313-
serverAssert(o && db);
314-
315-
hashTypeUntrackEntry(o, entry);
316-
hashTypeExpireEntry(entry);
317-
return ELEMENT_DELETE;
318-
}
319-
320-
void hashTypeSetAccessContext(robj *key, robj *val, serverDb *db) {
321-
setAccessContext(key, val, db);
322-
}
323-
324-
void hashTypeResetAccessContext(void) {
325-
robj *keyobj = server.access_context.key;
326-
robj *o = server.access_context.val;
327-
serverDb *db = server.access_context.db;
328-
serverAssert(!o || o->type == OBJ_HASH);
329-
uint64_t num_expired = server.access_context.expired;
330-
resetAccessContext();
331-
if (o) {
332-
int is_empty = hashTypeLength(o) == 0;
333-
if (is_empty || num_expired) {
334-
/* We need to report key changes and notifications. for that we need to make sure we have the key object */
335-
if (!keyobj) {
336-
sds key = objectGetKey(o);
337-
keyobj = createStringObject(key, sdslen(key));
338-
} else {
339-
incrRefCount(keyobj);
340-
}
341-
/* In case we have some entries which are expired we need to report it */
342-
if (num_expired)
343-
notifyKeyspaceEvent(NOTIFY_EXPIRED, "hexpired", keyobj, db->id);
344-
/* In casethe object was left empty, we need to make sure to delete it (we do not support zero size hashes) */
345-
if (is_empty) {
346-
notifyKeyspaceEvent(NOTIFY_GENERIC, "del", keyobj, db->id);
347-
dbDelete(db, keyobj);
348-
}
349-
signalModifiedKey(server.current_client, db, keyobj);
350-
decrRefCount(keyobj);
351-
}
352-
}
298+
return ELEMENT_INVALID;
353299
}
354300

355301
/*-----------------------------------------------------------------------------
@@ -1081,12 +1027,10 @@ void hincrbyCommand(client *c) {
10811027

10821028
if (getLongLongFromObjectOrReply(c, c->argv[3], &incr, NULL) != C_OK) return;
10831029
if ((o = hashTypeLookupWriteOrCreate(c, c->argv[1])) == NULL) return;
1084-
hashTypeSetAccessContext(c->argv[1], o, c->db);
10851030
if (hashTypeGetValue(o, c->argv[2]->ptr, &vstr, &vlen, &value) == C_OK) {
10861031
if (vstr) {
10871032
if (string2ll((char *)vstr, vlen, &value) == 0) {
10881033
addReplyError(c, "hash value is not an integer");
1089-
hashTypeResetAccessContext();
10901034
return;
10911035
}
10921036
} /* Else hashTypeGetValue() already stored it into &value */
@@ -1098,7 +1042,6 @@ void hincrbyCommand(client *c) {
10981042
if ((incr < 0 && oldvalue < 0 && incr < (LLONG_MIN - oldvalue)) ||
10991043
(incr > 0 && oldvalue > 0 && incr > (LLONG_MAX - oldvalue))) {
11001044
addReplyError(c, "increment or decrement would overflow");
1101-
hashTypeResetAccessContext();
11021045
return;
11031046
}
11041047
value += incr;
@@ -1108,7 +1051,6 @@ void hincrbyCommand(client *c) {
11081051
notifyKeyspaceEvent(NOTIFY_HASH, "hincrby", c->argv[1], c->db->id);
11091052
server.dirty++;
11101053
addReplyLongLong(c, value);
1111-
hashTypeResetAccessContext();
11121054
}
11131055

11141056
void hincrbyfloatCommand(client *c) {
@@ -1130,7 +1072,6 @@ void hincrbyfloatCommand(client *c) {
11301072
if (vstr) {
11311073
if (string2ld((char *)vstr, vlen, &value) == 0) {
11321074
addReplyError(c, "hash value is not a float");
1133-
hashTypeResetAccessContext();
11341075
return;
11351076
}
11361077
} else {
@@ -1143,7 +1084,6 @@ void hincrbyfloatCommand(client *c) {
11431084
value += incr;
11441085
if (isnan(value) || isinf(value)) {
11451086
addReplyError(c, "increment would produce NaN or Infinity");
1146-
hashTypeResetAccessContext();
11471087
return;
11481088
}
11491089

@@ -1164,7 +1104,6 @@ void hincrbyfloatCommand(client *c) {
11641104
rewriteClientCommandArgument(c, 0, shared.hset);
11651105
rewriteClientCommandArgument(c, 3, newobj);
11661106
decrRefCount(newobj);
1167-
hashTypeResetAccessContext();
11681107
}
11691108

11701109
static void addHashFieldToReply(client *c, robj *o, sds field) {
@@ -1193,8 +1132,6 @@ void hgetCommand(client *c) {
11931132

11941133
if ((o = lookupKeyReadOrReply(c, c->argv[1], shared.null[c->resp])) == NULL || checkType(c, o, OBJ_HASH)) return;
11951134
addHashFieldToReply(c, o, c->argv[2]->ptr);
1196-
1197-
hashTypeResetAccessContext();
11981135
}
11991136

12001137
void hmgetCommand(client *c) {
@@ -1207,8 +1144,6 @@ void hmgetCommand(client *c) {
12071144

12081145
if (checkType(c, o, OBJ_HASH)) return;
12091146

1210-
hashTypeSetAccessContext(c->argv[1], o, c->db);
1211-
12121147
addReplyArrayLen(c, c->argc - 2);
12131148
for (i = 2; i < c->argc; i++) {
12141149
addHashFieldToReply(c, o, c->argv[i]->ptr);
@@ -1220,23 +1155,25 @@ void hmgetCommand(client *c) {
12201155

12211156
void hdelCommand(client *c) {
12221157
robj *o;
1223-
int j, deleted = 0;
1158+
int j, deleted = 0, keyremoved = 0;
12241159

12251160
if ((o = lookupKeyWriteOrReply(c, c->argv[1], shared.czero)) == NULL || checkType(c, o, OBJ_HASH)) return;
12261161
for (j = 2; j < c->argc; j++) {
12271162
if (hashTypeDelete(o, c->argv[j]->ptr)) {
12281163
deleted++;
12291164
if (hashTypeLength(o) == 0) {
1165+
dbDelete(c->db, c->argv[1]);
1166+
keyremoved = 1;
12301167
break;
12311168
}
12321169
}
12331170
}
12341171
if (deleted) {
12351172
signalModifiedKey(c, c->db, c->argv[1]);
12361173
notifyKeyspaceEvent(NOTIFY_HASH, "hdel", c->argv[1], c->db->id);
1174+
if (keyremoved) notifyKeyspaceEvent(NOTIFY_GENERIC, "del", c->argv[1], c->db->id);
12371175
server.dirty += deleted;
12381176
}
1239-
hashTypeResetAccessContext();
12401177
addReplyLongLong(c, deleted);
12411178
}
12421179

@@ -1252,9 +1189,7 @@ void hstrlenCommand(client *c) {
12521189
robj *o;
12531190

12541191
if ((o = lookupKeyReadOrReply(c, c->argv[1], shared.czero)) == NULL || checkType(c, o, OBJ_HASH)) return;
1255-
hashTypeSetAccessContext(c->argv[1], o, c->db);
12561192
addReplyLongLong(c, hashTypeGetValueLength(o, c->argv[2]->ptr));
1257-
hashTypeResetAccessContext();
12581193
}
12591194

12601195
static void addHashIteratorCursorToReply(writePreparedClient *wpc, hashTypeIterator *hi, int what) {
@@ -1594,7 +1529,6 @@ void hexistsCommand(client *c) {
15941529
robj *o;
15951530
if ((o = lookupKeyReadOrReply(c, c->argv[1], shared.czero)) == NULL || checkType(c, o, OBJ_HASH)) return;
15961531
addReply(c, hashTypeExists(o, c->argv[2]->ptr) ? shared.cone : shared.czero);
1597-
hashTypeResetAccessContext();
15981532
}
15991533

16001534
void hscanCommand(client *c) {

0 commit comments

Comments
 (0)