Skip to content

Commit

Permalink
Support RDB_TYPE_HASH_METADATA relative time optimization (#51)
Browse files Browse the repository at this point in the history
Adapt librdb to modified layout of RDB_TYPE_HASH_METADATA which 
now store field's relative TTLs instead of absolute expiration time.
  • Loading branch information
moticless authored Jul 24, 2024
1 parent b401df1 commit f953088
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/lib/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1742,12 +1742,8 @@ RdbStatus elementHash(RdbParser *p) {

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

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

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

while(ctx->hash.visitingField < ctx->hash.numFields) {
uint64_t expireAt = 0;
if (p->parsingElement == PE_HASH_META)
IF_NOT_OK_RETURN(rdbLoadLen(p, NULL, &expireAt, NULL, NULL));
uint64_t val, expireAt = -1;

/* If parsing a hash with metadata, read field's expiration time
* (Applicable for opcodes RDB_TYPE_HASH_METADATA and
* RDB_TYPE_HASH_METADATA_PRE_GA) */
if (p->parsingElement == PE_HASH_META) {
IF_NOT_OK_RETURN(rdbLoadLen(p, NULL, &val, NULL, NULL));
if (val != 0) {
if (p->currOpcode == RDB_TYPE_HASH_METADATA) {
/* Value is relative to minExpire (with +1 to avoid
* 0 which indicates no expiration) */
expireAt = val + ctx->hash.hexpireMinMsec - 1;
} else {
/* absolute expiration time */
expireAt = val;
}
}
}
IF_NOT_OK_RETURN(rdbLoadString(p, RQ_ALLOC_APP_BULK, NULL, &binfoField));
IF_NOT_OK_RETURN(rdbLoadString(p, RQ_ALLOC_APP_BULK, NULL, &binfoValue));

/*** ENTER SAFE STATE ***/

registerAppBulkForNextCb(p, binfoField);
registerAppBulkForNextCb(p, binfoValue);

if (p->elmCtx.key.handleByLevel == RDB_LEVEL_STRUCT) {
CALL_HANDLERS_CB(p, NOP, RDB_LEVEL_STRUCT, rdbStruct.handleHashPlain,
binfoField->ref,
binfoValue->ref,
(expireAt) ? (int64_t) expireAt : -1);
expireAt);
}
else {
CALL_HANDLERS_CB(p, NOP, RDB_LEVEL_DATA, rdbData.handleHashField,
binfoField->ref,
binfoValue->ref,
(expireAt) ? (int64_t) expireAt : -1);
expireAt);
}
++ctx->hash.visitingField;

Expand Down
1 change: 1 addition & 0 deletions src/lib/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ typedef struct {
typedef struct {
uint64_t numFields;
uint64_t visitingField;
int64_t hexpireMinMsec;
} ElementHashCtx;

typedef struct {
Expand Down

0 comments on commit f953088

Please sign in to comment.