mirror of
https://github.com/fluencelabs/redis
synced 2025-06-16 10:41:22 +00:00
Merge pull request #3383 from yossigo/datatype_load_save
Redis Module API calls to allow re-use of data type RDB save/load.
This commit is contained in:
55
src/module.c
55
src/module.c
@ -3883,6 +3883,59 @@ void RM_DigestEndSequence(RedisModuleDigest *md) {
|
||||
memset(md->o,0,sizeof(md->o));
|
||||
}
|
||||
|
||||
/* Decode a serialized representation of a module data type 'mt' from string
|
||||
* 'str' and return a newly allocated value, or NULL if decoding failed.
|
||||
*
|
||||
* This call basically reuses the 'rdb_load' callback which module data types
|
||||
* implement in order to allow a module to arbitrarily serialize/de-serialize
|
||||
* keys, similar to how the Redis 'DUMP' and 'RESTORE' commands are implemented.
|
||||
*
|
||||
* Modules should generally use the REDISMODULE_OPTIONS_HANDLE_IO_ERRORS flag and
|
||||
* make sure the de-serialization code properly checks and handles IO errors
|
||||
* (freeing allocated buffers and returning a NULL).
|
||||
*
|
||||
* If this is NOT done, Redis will handle corrupted (or just truncated) serialized
|
||||
* data by producing an error message and terminating the process.
|
||||
*/
|
||||
|
||||
void *RM_LoadDataTypeFromString(const RedisModuleString *str, const moduleType *mt) {
|
||||
rio payload;
|
||||
RedisModuleIO io;
|
||||
|
||||
rioInitWithBuffer(&payload, str->ptr);
|
||||
moduleInitIOContext(io,(moduleType *)mt,&payload,NULL);
|
||||
|
||||
/* All RM_Save*() calls always write a version 2 compatible format, so we
|
||||
* need to make sure we read the same.
|
||||
*/
|
||||
io.ver = 2;
|
||||
return mt->rdb_load(&io,0);
|
||||
}
|
||||
|
||||
/* Encode a module data type 'mt' value 'data' into serialized form, and return it
|
||||
* as a newly allocated RedisModuleString.
|
||||
*
|
||||
* This call basically reuses the 'rdb_save' callback which module data types
|
||||
* implement in order to allow a module to arbitrarily serialize/de-serialize
|
||||
* keys, similar to how the Redis 'DUMP' and 'RESTORE' commands are implemented.
|
||||
*/
|
||||
|
||||
RedisModuleString *RM_SaveDataTypeToString(RedisModuleCtx *ctx, void *data, const moduleType *mt) {
|
||||
rio payload;
|
||||
RedisModuleIO io;
|
||||
|
||||
rioInitWithBuffer(&payload,sdsempty());
|
||||
moduleInitIOContext(io,(moduleType *)mt,&payload,NULL);
|
||||
mt->rdb_save(&io,data);
|
||||
if (io.error) {
|
||||
return NULL;
|
||||
} else {
|
||||
robj *str = createObject(OBJ_STRING,payload.io.buffer.ptr);
|
||||
autoMemoryAdd(ctx,REDISMODULE_AM_STRING,str);
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* AOF API for modules data types
|
||||
* -------------------------------------------------------------------------- */
|
||||
@ -6875,6 +6928,8 @@ void moduleRegisterCoreAPI(void) {
|
||||
REGISTER_API(LoadFloat);
|
||||
REGISTER_API(SaveLongDouble);
|
||||
REGISTER_API(LoadLongDouble);
|
||||
REGISTER_API(SaveDataTypeToString);
|
||||
REGISTER_API(LoadDataTypeFromString);
|
||||
REGISTER_API(EmitAOF);
|
||||
REGISTER_API(Log);
|
||||
REGISTER_API(LogIOError);
|
||||
|
Reference in New Issue
Block a user