Skip to content

Commit f953088

Browse files
authored
Support RDB_TYPE_HASH_METADATA relative time optimization (#51)
Adapt librdb to modified layout of RDB_TYPE_HASH_METADATA which now store field's relative TTLs instead of absolute expiration time.
1 parent b401df1 commit f953088

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/lib/parser.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,12 +1742,8 @@ RdbStatus elementHash(RdbParser *p) {
17421742

17431743
switch (ctx->state) {
17441744
case ST_HASH_HEADER:
1745-
if (p->currOpcode == RDB_TYPE_HASH_METADATA) {
1746-
/* digest min HFE expiration time. No need to pass it to handlers
1747-
as each field will report its own expiration time anyway */
1748-
BulkInfo *binfoExpire;
1749-
IF_NOT_OK_RETURN(rdbLoad(p, 8, RQ_ALLOC, NULL, &binfoExpire));
1750-
}
1745+
if (p->currOpcode == RDB_TYPE_HASH_METADATA)
1746+
IF_NOT_OK_RETURN(rdbLoadMillisecTime(p, &ctx->hash.hexpireMinMsec));
17511747

17521748
IF_NOT_OK_RETURN(rdbLoadLen(p, NULL, &(ctx->hash.numFields), NULL, NULL));
17531749

@@ -1762,27 +1758,43 @@ RdbStatus elementHash(RdbParser *p) {
17621758
BulkInfo *binfoField, *binfoValue;
17631759

17641760
while(ctx->hash.visitingField < ctx->hash.numFields) {
1765-
uint64_t expireAt = 0;
1766-
if (p->parsingElement == PE_HASH_META)
1767-
IF_NOT_OK_RETURN(rdbLoadLen(p, NULL, &expireAt, NULL, NULL));
1761+
uint64_t val, expireAt = -1;
1762+
1763+
/* If parsing a hash with metadata, read field's expiration time
1764+
* (Applicable for opcodes RDB_TYPE_HASH_METADATA and
1765+
* RDB_TYPE_HASH_METADATA_PRE_GA) */
1766+
if (p->parsingElement == PE_HASH_META) {
1767+
IF_NOT_OK_RETURN(rdbLoadLen(p, NULL, &val, NULL, NULL));
1768+
if (val != 0) {
1769+
if (p->currOpcode == RDB_TYPE_HASH_METADATA) {
1770+
/* Value is relative to minExpire (with +1 to avoid
1771+
* 0 which indicates no expiration) */
1772+
expireAt = val + ctx->hash.hexpireMinMsec - 1;
1773+
} else {
1774+
/* absolute expiration time */
1775+
expireAt = val;
1776+
}
1777+
}
1778+
}
17681779
IF_NOT_OK_RETURN(rdbLoadString(p, RQ_ALLOC_APP_BULK, NULL, &binfoField));
17691780
IF_NOT_OK_RETURN(rdbLoadString(p, RQ_ALLOC_APP_BULK, NULL, &binfoValue));
17701781

17711782
/*** ENTER SAFE STATE ***/
17721783

17731784
registerAppBulkForNextCb(p, binfoField);
17741785
registerAppBulkForNextCb(p, binfoValue);
1786+
17751787
if (p->elmCtx.key.handleByLevel == RDB_LEVEL_STRUCT) {
17761788
CALL_HANDLERS_CB(p, NOP, RDB_LEVEL_STRUCT, rdbStruct.handleHashPlain,
17771789
binfoField->ref,
17781790
binfoValue->ref,
1779-
(expireAt) ? (int64_t) expireAt : -1);
1791+
expireAt);
17801792
}
17811793
else {
17821794
CALL_HANDLERS_CB(p, NOP, RDB_LEVEL_DATA, rdbData.handleHashField,
17831795
binfoField->ref,
17841796
binfoValue->ref,
1785-
(expireAt) ? (int64_t) expireAt : -1);
1797+
expireAt);
17861798
}
17871799
++ctx->hash.visitingField;
17881800

src/lib/parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ typedef struct {
211211
typedef struct {
212212
uint64_t numFields;
213213
uint64_t visitingField;
214+
int64_t hexpireMinMsec;
214215
} ElementHashCtx;
215216

216217
typedef struct {

0 commit comments

Comments
 (0)