Skip to content

Commit cd33f13

Browse files
committed
Always add index when active expire is disable
1 parent 47eb28d commit cd33f13

File tree

1 file changed

+81
-91
lines changed

1 file changed

+81
-91
lines changed

src/tairhash.c

Lines changed: 81 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,21 @@ inline RedisModuleString *takeAndRef(RedisModuleString *str) {
9999
RedisModule_Assert((e)); \
100100
} while (0)
101101

102-
#define ACTIVE_EXPIRE_INSERT(dbid, o, field, expire) \
103-
do { \
104-
if (enable_active_expire) { \
105-
if (expire) { \
106-
long long before_min_score = -1; \
107-
if (o->expire_index->header->level[0].forward) { \
108-
before_min_score = o->expire_index->header->level[0].forward->score; \
109-
} \
110-
m_zslInsert(o->expire_index, expire, takeAndRef(field)); \
111-
long long after_min_score = o->expire_index->header->level[0].forward->score; \
112-
if (before_min_score > 0) { \
113-
m_zslUpdateScore(g_expire_index[dbid], before_min_score, o->key, after_min_score); \
114-
} else { \
115-
m_zslInsert(g_expire_index[dbid], after_min_score, takeAndRef(o->key)); \
116-
} \
117-
} \
118-
} \
102+
#define ACTIVE_EXPIRE_INSERT(dbid, o, field, expire) \
103+
do { \
104+
if (expire) { \
105+
long long before_min_score = -1; \
106+
if (o->expire_index->header->level[0].forward) { \
107+
before_min_score = o->expire_index->header->level[0].forward->score; \
108+
} \
109+
m_zslInsert(o->expire_index, expire, takeAndRef(field)); \
110+
long long after_min_score = o->expire_index->header->level[0].forward->score; \
111+
if (before_min_score > 0) { \
112+
m_zslUpdateScore(g_expire_index[dbid], before_min_score, o->key, after_min_score); \
113+
} else { \
114+
m_zslInsert(g_expire_index[dbid], after_min_score, takeAndRef(o->key)); \
115+
} \
116+
} \
119117
} while (0)
120118
#else
121119
#define MY_Assert(_e) ((_e) ? (void)0 : (_myAssert(#_e, __FILE__, __LINE__), abort()))
@@ -125,79 +123,69 @@ void _myAssert(const char *estr, const char *file, int line) {
125123
*((char *)-1) = 'x';
126124
}
127125

128-
#define ACTIVE_EXPIRE_INSERT(dbid, o, field, expire) \
129-
do { \
130-
REDISMODULE_NOT_USED(dbid); \
131-
if (enable_active_expire) { \
132-
if (expire) { \
133-
long long before_min_score = -1; \
134-
if (o->expire_index->header->level[0].forward) { \
135-
before_min_score = o->expire_index->header->level[0].forward->score; \
136-
} \
137-
m_zslInsert(o->expire_index, expire, takeAndRef(field)); \
138-
} \
139-
} \
126+
#define ACTIVE_EXPIRE_INSERT(dbid, o, field, expire) \
127+
do { \
128+
REDISMODULE_NOT_USED(dbid); \
129+
if (expire) { \
130+
long long before_min_score = -1; \
131+
if (o->expire_index->header->level[0].forward) { \
132+
before_min_score = o->expire_index->header->level[0].forward->score; \
133+
} \
134+
m_zslInsert(o->expire_index, expire, takeAndRef(field)); \
135+
} \
140136
} while (0)
141137
#endif
142138

143139
#ifdef SORT_MODE
144-
#define ACTIVE_EXPIRE_UPDATE(dbid, o, field, cur_expire, new_expire) \
145-
do { \
146-
if (enable_active_expire) { \
147-
if (cur_expire != new_expire) { \
148-
long long before_min_score = -1; \
149-
m_zskiplistNode *ln = o->expire_index->header->level[0].forward; \
150-
MY_Assert(ln != NULL); \
151-
before_min_score = ln->score; \
152-
m_zslUpdateScore(o->expire_index, cur_expire, field, new_expire); \
153-
long long after_min_score = o->expire_index->header->level[0].forward->score; \
154-
m_zslUpdateScore(g_expire_index[dbid], before_min_score, o->key, after_min_score); \
155-
} \
156-
} \
140+
#define ACTIVE_EXPIRE_UPDATE(dbid, o, field, cur_expire, new_expire) \
141+
do { \
142+
if (cur_expire != new_expire) { \
143+
long long before_min_score = -1; \
144+
m_zskiplistNode *ln = o->expire_index->header->level[0].forward; \
145+
MY_Assert(ln != NULL); \
146+
before_min_score = ln->score; \
147+
m_zslUpdateScore(o->expire_index, cur_expire, field, new_expire); \
148+
long long after_min_score = o->expire_index->header->level[0].forward->score; \
149+
m_zslUpdateScore(g_expire_index[dbid], before_min_score, o->key, after_min_score); \
150+
} \
157151
} while (0)
158152

159153
#else
160-
#define ACTIVE_EXPIRE_UPDATE(dbid, o, field, cur_expire, new_expire) \
161-
do { \
162-
if (enable_active_expire) { \
163-
if (cur_expire != new_expire) { \
164-
long long before_min_score = -1; \
165-
m_zskiplistNode *ln = o->expire_index->header->level[0].forward; \
166-
MY_Assert(ln != NULL); \
167-
before_min_score = ln->score; \
168-
m_zslUpdateScore(o->expire_index, cur_expire, field, new_expire); \
169-
} \
170-
} \
154+
#define ACTIVE_EXPIRE_UPDATE(dbid, o, field, cur_expire, new_expire) \
155+
do { \
156+
if (cur_expire != new_expire) { \
157+
long long before_min_score = -1; \
158+
m_zskiplistNode *ln = o->expire_index->header->level[0].forward; \
159+
MY_Assert(ln != NULL); \
160+
before_min_score = ln->score; \
161+
m_zslUpdateScore(o->expire_index, cur_expire, field, new_expire); \
162+
} \
171163
} while (0)
172164
#endif
173165

174166
#ifdef SORT_MODE
175-
#define ACTIVE_EXPIRE_DELETE(dbid, o, field, cur_expire) \
176-
do { \
177-
if (enable_active_expire) { \
178-
if (cur_expire != 0) { \
179-
long long before_min_score = -1; \
180-
m_zskiplistNode *ln = o->expire_index->header->level[0].forward; \
181-
MY_Assert(ln != NULL); \
182-
before_min_score = ln->score; \
183-
m_zslDelete(o->expire_index, cur_expire, field, NULL); \
184-
if (o->expire_index->header->level[0].forward) { \
185-
long long after_min_score = o->expire_index->header->level[0].forward->score; \
186-
m_zslUpdateScore(g_expire_index[dbid], before_min_score, field, after_min_score); \
187-
} else { \
188-
m_zslDelete(g_expire_index[dbid], before_min_score, field, NULL); \
189-
} \
190-
} \
191-
} \
167+
#define ACTIVE_EXPIRE_DELETE(dbid, o, field, cur_expire) \
168+
do { \
169+
if (cur_expire != 0) { \
170+
long long before_min_score = -1; \
171+
m_zskiplistNode *ln = o->expire_index->header->level[0].forward; \
172+
MY_Assert(ln != NULL); \
173+
before_min_score = ln->score; \
174+
m_zslDelete(o->expire_index, cur_expire, field, NULL); \
175+
if (o->expire_index->header->level[0].forward) { \
176+
long long after_min_score = o->expire_index->header->level[0].forward->score; \
177+
m_zslUpdateScore(g_expire_index[dbid], before_min_score, field, after_min_score); \
178+
} else { \
179+
m_zslDelete(g_expire_index[dbid], before_min_score, field, NULL); \
180+
} \
181+
} \
192182
} while (0)
193183
#else
194-
#define ACTIVE_EXPIRE_DELETE(dbid, o, field, cur_expire) \
195-
do { \
196-
if (enable_active_expire) { \
197-
if (cur_expire != 0) { \
198-
m_zslDelete(o->expire_index, cur_expire, field, NULL); \
199-
} \
200-
} \
184+
#define ACTIVE_EXPIRE_DELETE(dbid, o, field, cur_expire) \
185+
do { \
186+
if (cur_expire != 0) { \
187+
m_zslDelete(o->expire_index, cur_expire, field, NULL); \
188+
} \
201189
} while (0)
202190
#endif
203191

@@ -596,7 +584,8 @@ void activeExpireTimerHandler(RedisModuleCtx *ctx, void *data) {
596584
}
597585

598586
stat_last_active_expire_time_msec = RedisModule_Milliseconds() - start;
599-
stat_max_active_expire_time_msec = stat_max_active_expire_time_msec < stat_last_active_expire_time_msec ? stat_last_active_expire_time_msec : stat_max_active_expire_time_msec;
587+
stat_max_active_expire_time_msec = stat_max_active_expire_time_msec < stat_last_active_expire_time_msec ?
588+
stat_last_active_expire_time_msec : stat_max_active_expire_time_msec;
600589
total_expire_time += stat_last_active_expire_time_msec;
601590
++loop_cnt;
602591
if (loop_cnt % 1000 == 0) {
@@ -1793,7 +1782,9 @@ int TairHashTypeHincrBy_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **ar
17931782
}
17941783
}
17951784

1796-
if ((incr < 0 && cur_val < 0 && incr < (LLONG_MIN - cur_val)) || (incr > 0 && cur_val > 0 && incr > (LLONG_MAX - cur_val)) || (max_p != NULL && cur_val + incr > max) || (min_p != NULL && cur_val + incr < min)) {
1785+
if ((incr < 0 && cur_val < 0 && incr < (LLONG_MIN - cur_val)) ||
1786+
(incr > 0 && cur_val > 0 && incr > (LLONG_MAX - cur_val)) ||
1787+
(max_p != NULL && cur_val + incr > max) || (min_p != NULL && cur_val + incr < min)) {
17971788
if (nokey) {
17981789
tairHashValRelease(tair_hash_val);
17991790
}
@@ -1847,7 +1838,8 @@ int TairHashTypeHincrBy_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **ar
18471838
}
18481839

18491840
if (milliseconds > 0) {
1850-
RedisModule_Replicate(ctx, "EXHSET", "sssclcl", argv[1], argv[2], tair_hash_val->value, "abs", tair_hash_val->version, "pxat", (milliseconds + RedisModule_Milliseconds()));
1841+
RedisModule_Replicate(ctx, "EXHSET", "sssclcl", argv[1], argv[2], tair_hash_val->value, "abs",
1842+
tair_hash_val->version, "pxat", (milliseconds + RedisModule_Milliseconds()));
18511843
} else {
18521844
RedisModule_Replicate(ctx, "EXHSET", "ssscl", argv[1], argv[2], tair_hash_val->value, "abs", tair_hash_val->version);
18531845
}
@@ -2081,7 +2073,8 @@ int TairHashTypeHincrByFloat_RedisCommand(RedisModuleCtx *ctx, RedisModuleString
20812073
}
20822074

20832075
if (milliseconds > 0) {
2084-
RedisModule_Replicate(ctx, "EXHSET", "sssclcl", argv[1], argv[2], tair_hash_val->value, "abs", tair_hash_val->version, "pxat", (milliseconds + RedisModule_Milliseconds()));
2076+
RedisModule_Replicate(ctx, "EXHSET", "sssclcl", argv[1], argv[2], tair_hash_val->value, "abs", tair_hash_val->version, "pxat",
2077+
(milliseconds + RedisModule_Milliseconds()));
20852078
} else {
20862079
RedisModule_Replicate(ctx, "EXHSET", "ssscl", argv[1], argv[2], tair_hash_val->value, "abs", tair_hash_val->version);
20872080
}
@@ -3048,9 +3041,7 @@ void *TairHashTypeRdbLoad(RedisModuleIO *rdb, int encver) {
30483041
hashv->value = takeAndRef(value);
30493042
m_dictAdd(o->hash, takeAndRef(skey), hashv);
30503043
if (hashv->expire) {
3051-
if (enable_active_expire) {
3052-
ACTIVE_EXPIRE_INSERT(dbid, o, skey, hashv->expire);
3053-
}
3044+
ACTIVE_EXPIRE_INSERT(dbid, o, skey, hashv->expire);
30543045
}
30553046
RedisModule_FreeString(NULL, value);
30563047
RedisModule_FreeString(NULL, skey);
@@ -3191,9 +3182,7 @@ void *TairHashTypeCopy2(RedisModuleKeyOptCtx *ctx, const void *value) {
31913182
newval->value = RedisModule_CreateStringFromString(NULL, oldval->value);
31923183
m_dictAdd(new->hash, field, newval);
31933184
if (newval->expire) {
3194-
if (enable_active_expire) {
3195-
ACTIVE_EXPIRE_INSERT(to_dbid, new, field, newval->expire);
3196-
}
3185+
ACTIVE_EXPIRE_INSERT(to_dbid, new, field, newval->expire);
31973186
}
31983187
}
31993188
m_dictReleaseIterator(di);
@@ -3352,12 +3341,13 @@ int __attribute__((visibility("default"))) RedisModule_OnLoad(RedisModuleCtx *ct
33523341
return REDISMODULE_ERR;
33533342
}
33543343

3355-
if (enable_active_expire) {
33563344
#ifdef SORT_MODE
3357-
for (int i = 0; i < DB_NUM; i++) {
3358-
g_expire_index[i] = m_zslCreate();
3359-
}
3345+
for (int i = 0; i < DB_NUM; i++) {
3346+
g_expire_index[i] = m_zslCreate();
3347+
}
33603348
#endif
3349+
3350+
if (enable_active_expire) {
33613351
RedisModuleCtx *ctx2 = RedisModule_GetThreadSafeContext(NULL);
33623352
startExpireTimer(ctx2, NULL);
33633353
RedisModule_FreeThreadSafeContext(ctx2);

0 commit comments

Comments
 (0)