Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ else
PKGCONFIGDIR = $(LIBDIR)/pkgconfig
endif

LIBRDB_VERSION = $(shell sed -n 's|^\#define LIBRDB_VERSION_STRING "\([0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\)"|\1|p' ./src/lib/version.h)
LIBRDB_VERSION = $(shell sed -n 's|^\#define LIBRDB_VERSION_STRING "\([0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\)"|\1|p' ./src/version.h)
export LIBRDB_VERSION

# ------------------------- ALL --------------------------------------
Expand Down
25 changes: 2 additions & 23 deletions src/ext/handlersToResp.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,16 @@
#include "../../deps/redis/util.h"
#include "../../deps/redis/endianconv.h"
#include "../../deps/redis/rax.h"
#include "../redisver.h"

/* RDB opcode defines */
#define _RDB_TYPE_MODULE_2 7
#define _RDB_TYPE_STRING 0
#define _RDB_TYPE_STREAM_LISTPACKS_2 19
#define _REDISMODULE_AUX_BEFORE_RDB (1<<0)

#define VER_VAL(major,minor) (((unsigned int)(major)<<8) | (unsigned int)(minor))

#define KEY_CMD_ID_DBG "_RDB_CLI_CMD_ID_"

typedef struct RedisToRdbVersion {
const char *redisStr;
unsigned int redis;
unsigned char rdb;
} RedisToRdbVersion;

const RedisToRdbVersion redisToRdbVersion[] = {
{"7.4", VER_VAL(7,4), 12},
{"7.2", VER_VAL(7,2), 11},
{"7.0", VER_VAL(7,0), 10},
{"5.0", VER_VAL(5,0), 9}, //6 and 6.2 had v9 too
{"4.0", VER_VAL(4,0), 8},
{"3.2", VER_VAL(3,2), 7},
{"2.6", VER_VAL(2,6), 6}, //2.8 had v6 too
{"2.4", VER_VAL(2,4), 5},
};

typedef enum DelKeyBeforeWrite {
DEL_KEY_BEFORE_NONE,
DEL_KEY_BEFORE_BY_DEL_CMD,
Expand Down Expand Up @@ -160,7 +142,7 @@ static int setRdbVerFromDestRedisVer(RdbxToResp *ctx) {

ctx->targetRedisVerVal = VER_VAL(mjr, mnr);
unsigned int i;
for (i = 0; i < sizeof(redisToRdbVersion) / sizeof(redisToRdbVersion[0]); i++)
for (i = 0; i < REDIS_TO_RDB_VERSION_COUNT ; i++)
if (ctx->targetRedisVerVal >= redisToRdbVersion[i].redis) {
return redisToRdbVersion[i].rdb;
}
Expand Down Expand Up @@ -996,9 +978,6 @@ _LIBRDB_API RdbxToResp *RDBX_createHandlersToResp(RdbParser *p, RdbxToRespConf *

crc64_init_thread_safe();

/* Verify table is aligned with LIBRDB_SUPPORT_MAX_RDB_VER */
assert(redisToRdbVersion[0].rdb == RDB_getMaxSuppportRdbVersion());

if ((ctx = RDB_alloc(p, sizeof(RdbxToResp))) == NULL)
return NULL;

Expand Down
3 changes: 2 additions & 1 deletion src/lib/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
#include "../../deps/redis/crc64.h"
#include "bulkAlloc.h"
#include "parser.h"
#include "version.h"
#include "defines.h"
#include "../version.h"
#include "../redisver.h"
#include "../../deps/redis/util.h"
#include "../../deps/redis/endianconv.h"
#include "../../deps/redis/listpack.h"
Expand Down
4 changes: 3 additions & 1 deletion src/lib/version.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* Verify that the version string matches the individual version components */

#include <stddef.h>
#include <assert.h>

#include "version.h"
#include "../version.h"

#define STATIC_ASSERT(COND,MSG) typedef char static_assertion[(COND)?1:-1]

Expand Down
40 changes: 40 additions & 0 deletions src/redisver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Redis to RDB Version Mapping
*
* This table maps Redis versions to their corresponding RDB format versions.
* Entries are ordered from newest to oldest Redis version.
* Multiple Redis versions may share the same RDB format version.
*
* This Helps determine which commands can be applied. Particularly crucial if
* support-restore being used as RESTORE is closely tied to specific RDB versions.
*/

#ifndef REDIS_VER_H
#define REDIS_VER_H

#define VER_VAL(maj,min) ((maj) * 100 + (min))
#define VAL_MAJOR(val) ((val) / 100)
#define VAL_MINOR(val) ((val) % 100)

/* Redis Version Mapping */
typedef struct {
const char *redisStr;
unsigned int redis;
int rdb;
} RedisToRdbVersion;

static const RedisToRdbVersion redisToRdbVersion[] = {
{"7.4", VER_VAL(7,4), 12}, // + 8.0, 8.2, 8.4
{"7.2", VER_VAL(7,2), 11},
{"7.0", VER_VAL(7,0), 10},
{"5.0", VER_VAL(5,0), 9}, // + 6.0, 6.2
{"4.0", VER_VAL(4,0), 8},
{"3.2", VER_VAL(3,2), 7},
{"2.6", VER_VAL(2,6), 6}, // + 2.8
{"2.4", VER_VAL(2,4), 5},
};

#define REDIS_TO_RDB_VERSION_COUNT (sizeof(redisToRdbVersion) / sizeof(redisToRdbVersion[0]))
#define LIBRDB_SUPPORT_MAX_RDB_VER (redisToRdbVersion[0].rdb)

#endif /* REDIS_VER_H */
7 changes: 5 additions & 2 deletions src/lib/version.h → src/version.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#ifndef LIBRDB_VERSION_H
#define LIBRDB_VERSION_H

/* Library Version Information */
#define LIBRDB_MAJOR_VERSION 1
#define LIBRDB_MINOR_VERSION 0
#define LIBRDB_PATCH_VERSION 0

/* Keep direct value for external readers */
#define LIBRDB_VERSION_STRING "1.0.0"

/* Update Maximum supported RDB version */
#define LIBRDB_SUPPORT_MAX_RDB_VER 12
#endif /* LIBRDB_VERSION_H */
9 changes: 4 additions & 5 deletions test/test_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <sys/wait.h>
#include <dirent.h>
#include "../deps/hiredis/hiredis.h"
#include "../src/redisver.h"
#include "test_common.h"

/* Live Redis server for some of the tests (Optional) */
Expand Down Expand Up @@ -438,14 +439,12 @@ void setupRedisServer(const char *extraArgs) {
get_redis_version(redisConnContext, &redisVerMajor, &redisVerMinor);
snprintf(redisVer, sizeof(redisVer), "%d.%d", redisVerMajor, redisVerMinor);
if ((redisVerMajor == 255) && (redisVerMinor == 255)) {/* unstable version? */
snprintf(redisVer, sizeof(redisVer),
MAX_SUPPORTED_REDIS_VERSION);
strncpy(redisVer, redisToRdbVersion[0].redisStr, sizeof(redisVer));
prefixVer = "Unresolved Version. Assumed ";
assert_int_equal(sscanf(MAX_SUPPORTED_REDIS_VERSION, "%d.%d",
&redisVerMajor, &redisVerMinor), 2);
redisVerMajor = VAL_MAJOR(redisToRdbVersion[0].redis);
redisVerMinor = VAL_MINOR(redisToRdbVersion[0].redis);
}
redisVersionInit = 1;

}
printf(">> Redis Server(%d) started on port %d with PID %d (%sVersion=%s)\n",
currRedisInst, port, pid, prefixVer, redisVer);
Expand Down
2 changes: 0 additions & 2 deletions test/test_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include "../api/librdb-api.h" /* RDB library header */
#include "../api/librdb-ext-api.h" /* RDB library extension header */

#define MAX_SUPPORTED_REDIS_VERSION "7.4"

#define UNUSED(...) unused( (void *) NULL, __VA_ARGS__);
static inline void unused(void *dummy, ...) { (void)(dummy);}

Expand Down