From 3a379f09341d87d92b66c53a7cab636f77df2a35 Mon Sep 17 00:00:00 2001 From: malavan Date: Wed, 14 Jul 2021 22:13:29 +0000 Subject: [PATCH] use INVALID_EXPIRE instead of -1 Former-commit-id: 9e45984a97a293d87474f87612204a24c831a343 --- src/cluster.cpp | 4 ++-- src/db.cpp | 2 +- src/debug.cpp | 4 ++-- src/expire.cpp | 4 ++-- src/expire.h | 45 ++++++++++++++++++++++++++------------------- src/module.cpp | 4 ++-- src/rdb.cpp | 2 +- 7 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/cluster.cpp b/src/cluster.cpp index f51a62d42..e3fce7ec0 100644 --- a/src/cluster.cpp +++ b/src/cluster.cpp @@ -5425,11 +5425,11 @@ try_again: for (j = 0; j < num_keys; j++) { long long ttl = 0; expireEntry *pexpire = getExpire(c->db,kv[j]); - long long expireat = -1; + long long expireat = INVALID_EXPIRE; if (pexpire != nullptr) pexpire->FGetPrimaryExpire(&expireat); - if (expireat != -1) { + if (expireat != INVALID_EXPIRE) { ttl = expireat-mstime(); if (ttl < 0) { continue; diff --git a/src/db.cpp b/src/db.cpp index 7019aa29c..c7a27ea05 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -1626,7 +1626,7 @@ int keyIsExpired(redisDb *db, robj *key) { /* Don't expire anything while loading. It will be done later. */ if (g_pserver->loading) return 0; - long long when = pexpire->whenFull(); + long long when = pexpire->FGetPrimaryExpire(); if (when == INVALID_EXPIRE) return 0; diff --git a/src/debug.cpp b/src/debug.cpp index 53beaca4c..331f90d0e 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -128,7 +128,7 @@ void xorObjectDigest(redisDb *db, robj_roptr keyobj, unsigned char *digest, robj uint32_t aux = htonl(o->type); mixDigest(digest,&aux,sizeof(aux)); expireEntry *pexpire = getExpire(db,keyobj); - long long expiretime = -1; + long long expiretime = INVALID_EXPIRE; char buf[128]; if (pexpire != nullptr) @@ -260,7 +260,7 @@ void xorObjectDigest(redisDb *db, robj_roptr keyobj, unsigned char *digest, robj serverPanic("Unknown object type"); } /* If the key has an expire, add it to the mix */ - if (expiretime != -1) xorDigest(digest,"!!expire!!",10); + if (expiretime != INVALID_EXPIRE) xorDigest(digest,"!!expire!!",10); } /* Compute the dataset digest. Since keys, sets elements, hashes elements diff --git a/src/expire.cpp b/src/expire.cpp index b6022c712..662d3c71c 100644 --- a/src/expire.cpp +++ b/src/expire.cpp @@ -668,7 +668,7 @@ void pexpireatCommand(client *c) { /* Implements TTL and PTTL */ void ttlGenericCommand(client *c, int output_ms) { - long long expire = -1, ttl = -1; + long long expire = INVALID_EXPIRE, ttl = -1; /* If the key does not exist at all, return -2 */ if (lookupKeyReadWithFlags(c->db,c->argv[1],LOOKUP_NOTOUCH) == nullptr) { @@ -702,7 +702,7 @@ void ttlGenericCommand(client *c, int output_ms) { } - if (expire != -1) { + if (expire != INVALID_EXPIRE) { ttl = expire-mstime(); if (ttl < 0) ttl = 0; } diff --git a/src/expire.h b/src/expire.h index 2c9e94cf1..de55857f0 100644 --- a/src/expire.h +++ b/src/expire.h @@ -34,14 +34,7 @@ public: ~expireEntryFat(); long long when() const noexcept { return m_vecexpireEntries.front().when; } - long long whenFull() const noexcept { - for (size_t i = 0; i < size(); ++i) { - if (m_vecexpireEntries[i].spsubkey == nullptr) { - return m_vecexpireEntries[i].when; - } - } - return INVALID_EXPIRE; - } + const char *key() const noexcept { return m_keyPrimary; } bool operator<(long long when) const noexcept { return this->when() < when; } @@ -120,7 +113,10 @@ public: expireEntry(expireEntryFat *pfatentry) { u.m_pfatentry = pfatentry; - m_when = FFatMask() | pfatentry->whenFull(); + if (FGetPrimaryExpireSlow(&m_when)) + m_when = FFatMask() | m_when; + else + m_when = INVALID_EXPIRE; } expireEntry(expireEntry &&e) @@ -173,11 +169,7 @@ public: { if (FFat()) return u.m_pfatentry->when(); - return whenFull(); - } - long long whenFull() const noexcept - { - return m_when & (~FFatMask()); + return FGetPrimaryExpire(); } void update(const char *subkey, long long when) @@ -221,12 +213,27 @@ public: pfatentry()->m_vecexpireEntries.begin() + itr.m_idx); } - bool FGetPrimaryExpire(long long *pwhen) + long long FGetPrimaryExpire() const noexcept + { + return m_when & (~FFatMask()); + } + + bool FGetPrimaryExpire(long long *pwhen) const noexcept + { + *pwhen = FGetPrimaryExpire(); + return *pwhen != INVALID_EXPIRE; + } + + bool FGetPrimaryExpireSlow(long long *pwhen) { - *pwhen = -1; - if (this->whenFull() != INVALID_EXPIRE) { - *pwhen = this->whenFull(); - return true; + *pwhen = INVALID_EXPIRE; + for (auto itr : *this) + { + if (itr.subkey() == nullptr) + { + *pwhen = itr.when(); + return true; + } } return false; } diff --git a/src/module.cpp b/src/module.cpp index 183269c16..321ce1418 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -2264,11 +2264,11 @@ int RM_UnlinkKey(RedisModuleKey *key) { * REDISMODULE_NO_EXPIRE is returned. */ mstime_t RM_GetExpire(RedisModuleKey *key) { expireEntry *pexpire = getExpire(key->db,key->key); - mstime_t expire = -1; + mstime_t expire = INVALID_EXPIRE; if (pexpire != nullptr) pexpire->FGetPrimaryExpire(&expire); - if (expire == -1 || key->value == NULL) + if (expire == INVALID_EXPIRE || key->value == NULL) return REDISMODULE_NO_EXPIRE; expire -= mstime(); return expire >= 0 ? expire : 0; diff --git a/src/rdb.cpp b/src/rdb.cpp index 487992e57..c183bd34b 100644 --- a/src/rdb.cpp +++ b/src/rdb.cpp @@ -1101,7 +1101,7 @@ int rdbSaveKeyValuePair(rio *rdb, robj *key, robj *val, expireEntry *pexpire) { int savelfu = g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU; /* Save the expire time */ - long long expiretime = -1; + long long expiretime = INVALID_EXPIRE; if (pexpire != nullptr && pexpire->FGetPrimaryExpire(&expiretime)) { if (rdbSaveType(rdb,RDB_OPCODE_EXPIRETIME_MS) == -1) return -1; if (rdbSaveMillisecondTime(rdb,expiretime) == -1) return -1;