MEMORY USAGE: support for modules data types.

As a side effect of supporting it, we no longer crash when MEMORY USAGE
is called against a module data type.

Close #3637.
This commit is contained in:
antirez 2017-01-12 09:08:43 +01:00
parent 634b096610
commit baa9898821
3 changed files with 16 additions and 5 deletions

View File

@ -2743,8 +2743,8 @@ moduleType *RM_CreateDataType(RedisModuleCtx *ctx, const char *name, int encver,
moduleTypeLoadFunc rdb_load; moduleTypeLoadFunc rdb_load;
moduleTypeSaveFunc rdb_save; moduleTypeSaveFunc rdb_save;
moduleTypeRewriteFunc aof_rewrite; moduleTypeRewriteFunc aof_rewrite;
moduleTypeDigestFunc digest;
moduleTypeMemUsageFunc mem_usage; moduleTypeMemUsageFunc mem_usage;
moduleTypeDigestFunc digest;
moduleTypeFreeFunc free; moduleTypeFreeFunc free;
} *tms = (struct typemethods*) typemethods_ptr; } *tms = (struct typemethods*) typemethods_ptr;

View File

@ -226,10 +226,12 @@ void HelloTypeAofRewrite(RedisModuleIO *aof, RedisModuleString *key, void *value
} }
} }
void HelloTypeDigest(RedisModuleDigest *digest, void *value) { /* The goal of this function is to return the amount of memory used by
REDISMODULE_NOT_USED(digest); * the HelloType value. */
REDISMODULE_NOT_USED(value); size_t HelloTypeMemUsage(void *value) {
/* TODO: The DIGEST module interface is yet not implemented. */ struct HelloTypeObject *hto = value;
struct HelloTypeNode *node = hto->head;
return sizeof(*hto) + sizeof(*node)*hto->len;
} }
void HelloTypeFree(void *value) { void HelloTypeFree(void *value) {
@ -250,6 +252,7 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
.rdb_load = HelloTypeRdbLoad, .rdb_load = HelloTypeRdbLoad,
.rdb_save = HelloTypeRdbSave, .rdb_save = HelloTypeRdbSave,
.aof_rewrite = HelloTypeAofRewrite, .aof_rewrite = HelloTypeAofRewrite,
.mem_usage = HelloTypeMemUsage,
.free = HelloTypeFree .free = HelloTypeFree
}; };

View File

@ -786,6 +786,14 @@ size_t objectComputeSize(robj *o, size_t sample_size) {
} else { } else {
serverPanic("Unknown hash encoding"); serverPanic("Unknown hash encoding");
} }
} else if (o->type == OBJ_MODULE) {
moduleValue *mv = o->ptr;
moduleType *mt = mv->type;
if (mt->mem_usage != NULL) {
asize = mt->mem_usage(mv->value);
} else {
asize = 0;
}
} else { } else {
serverPanic("Unknown object type"); serverPanic("Unknown object type");
} }