1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
// SPDX-FileCopyrightText: 2024 Redict Contributors
// SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo <antirez at gmail dot com>
//
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-License-Identifier: LGPL-3.0-only
#include "redictmodule.h"
#include <string.h>
#include <assert.h>
#include <unistd.h>
#define UNUSED(V) ((void) V)
/* Registered type */
RedictModuleType *mallocsize_type = NULL;
typedef enum {
UDT_RAW,
UDT_STRING,
UDT_DICT
} udt_type_t;
typedef struct {
void *ptr;
size_t len;
} raw_t;
typedef struct {
udt_type_t type;
union {
raw_t raw;
RedictModuleString *str;
RedictModuleDict *dict;
} data;
} udt_t;
void udt_free(void *value) {
udt_t *udt = value;
switch (udt->type) {
case (UDT_RAW): {
RedictModule_Free(udt->data.raw.ptr);
break;
}
case (UDT_STRING): {
RedictModule_FreeString(NULL, udt->data.str);
break;
}
case (UDT_DICT): {
RedictModuleString *dk, *dv;
RedictModuleDictIter *iter = RedictModule_DictIteratorStartC(udt->data.dict, "^", NULL, 0);
while((dk = RedictModule_DictNext(NULL, iter, (void **)&dv)) != NULL) {
RedictModule_FreeString(NULL, dk);
RedictModule_FreeString(NULL, dv);
}
RedictModule_DictIteratorStop(iter);
RedictModule_FreeDict(NULL, udt->data.dict);
break;
}
}
RedictModule_Free(udt);
}
void udt_rdb_save(RedictModuleIO *rdb, void *value) {
udt_t *udt = value;
RedictModule_SaveUnsigned(rdb, udt->type);
switch (udt->type) {
case (UDT_RAW): {
RedictModule_SaveStringBuffer(rdb, udt->data.raw.ptr, udt->data.raw.len);
break;
}
case (UDT_STRING): {
RedictModule_SaveString(rdb, udt->data.str);
break;
}
case (UDT_DICT): {
RedictModule_SaveUnsigned(rdb, RedictModule_DictSize(udt->data.dict));
RedictModuleString *dk, *dv;
RedictModuleDictIter *iter = RedictModule_DictIteratorStartC(udt->data.dict, "^", NULL, 0);
while((dk = RedictModule_DictNext(NULL, iter, (void **)&dv)) != NULL) {
RedictModule_SaveString(rdb, dk);
RedictModule_SaveString(rdb, dv);
RedictModule_FreeString(NULL, dk); /* Allocated by RedictModule_DictNext */
}
RedictModule_DictIteratorStop(iter);
break;
}
}
}
void *udt_rdb_load(RedictModuleIO *rdb, int encver) {
if (encver != 0)
return NULL;
udt_t *udt = RedictModule_Alloc(sizeof(*udt));
udt->type = RedictModule_LoadUnsigned(rdb);
switch (udt->type) {
case (UDT_RAW): {
udt->data.raw.ptr = RedictModule_LoadStringBuffer(rdb, &udt->data.raw.len);
break;
}
case (UDT_STRING): {
udt->data.str = RedictModule_LoadString(rdb);
break;
}
case (UDT_DICT): {
long long dict_len = RedictModule_LoadUnsigned(rdb);
udt->data.dict = RedictModule_CreateDict(NULL);
for (int i = 0; i < dict_len; i += 2) {
RedictModuleString *key = RedictModule_LoadString(rdb);
RedictModuleString *val = RedictModule_LoadString(rdb);
RedictModule_DictSet(udt->data.dict, key, val);
}
break;
}
}
return udt;
}
size_t udt_mem_usage(RedictModuleKeyOptCtx *ctx, const void *value, size_t sample_size) {
UNUSED(ctx);
UNUSED(sample_size);
const udt_t *udt = value;
size_t size = sizeof(*udt);
switch (udt->type) {
case (UDT_RAW): {
size += RedictModule_MallocSize(udt->data.raw.ptr);
break;
}
case (UDT_STRING): {
size += RedictModule_MallocSizeString(udt->data.str);
break;
}
case (UDT_DICT): {
void *dk;
size_t keylen;
RedictModuleString *dv;
RedictModuleDictIter *iter = RedictModule_DictIteratorStartC(udt->data.dict, "^", NULL, 0);
while((dk = RedictModule_DictNextC(iter, &keylen, (void **)&dv)) != NULL) {
size += keylen;
size += RedictModule_MallocSizeString(dv);
}
RedictModule_DictIteratorStop(iter);
break;
}
}
return size;
}
/* MALLOCSIZE.SETRAW key len */
int cmd_setraw(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
if (argc != 3)
return RedictModule_WrongArity(ctx);
RedictModuleKey *key = RedictModule_OpenKey(ctx, argv[1], REDICTMODULE_WRITE);
udt_t *udt = RedictModule_Alloc(sizeof(*udt));
udt->type = UDT_RAW;
long long raw_len;
RedictModule_StringToLongLong(argv[2], &raw_len);
udt->data.raw.ptr = RedictModule_Alloc(raw_len);
udt->data.raw.len = raw_len;
RedictModule_ModuleTypeSetValue(key, mallocsize_type, udt);
RedictModule_CloseKey(key);
return RedictModule_ReplyWithSimpleString(ctx, "OK");
}
/* MALLOCSIZE.SETSTR key string */
int cmd_setstr(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
if (argc != 3)
return RedictModule_WrongArity(ctx);
RedictModuleKey *key = RedictModule_OpenKey(ctx, argv[1], REDICTMODULE_WRITE);
udt_t *udt = RedictModule_Alloc(sizeof(*udt));
udt->type = UDT_STRING;
udt->data.str = argv[2];
RedictModule_RetainString(ctx, argv[2]);
RedictModule_ModuleTypeSetValue(key, mallocsize_type, udt);
RedictModule_CloseKey(key);
return RedictModule_ReplyWithSimpleString(ctx, "OK");
}
/* MALLOCSIZE.SETDICT key field value [field value ...] */
int cmd_setdict(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
if (argc < 4 || argc % 2)
return RedictModule_WrongArity(ctx);
RedictModuleKey *key = RedictModule_OpenKey(ctx, argv[1], REDICTMODULE_WRITE);
udt_t *udt = RedictModule_Alloc(sizeof(*udt));
udt->type = UDT_DICT;
udt->data.dict = RedictModule_CreateDict(ctx);
for (int i = 2; i < argc; i += 2) {
RedictModule_DictSet(udt->data.dict, argv[i], argv[i+1]);
/* No need to retain argv[i], it is copied as the rax key */
RedictModule_RetainString(ctx, argv[i+1]);
}
RedictModule_ModuleTypeSetValue(key, mallocsize_type, udt);
RedictModule_CloseKey(key);
return RedictModule_ReplyWithSimpleString(ctx, "OK");
}
int RedictModule_OnLoad(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
UNUSED(argv);
UNUSED(argc);
if (RedictModule_Init(ctx,"mallocsize",1,REDICTMODULE_APIVER_1)== REDICTMODULE_ERR)
return REDICTMODULE_ERR;
RedictModuleTypeMethods tm = {
.version = REDICTMODULE_TYPE_METHOD_VERSION,
.rdb_load = udt_rdb_load,
.rdb_save = udt_rdb_save,
.free = udt_free,
.mem_usage2 = udt_mem_usage,
};
mallocsize_type = RedictModule_CreateDataType(ctx, "allocsize", 0, &tm);
if (mallocsize_type == NULL)
return REDICTMODULE_ERR;
if (RedictModule_CreateCommand(ctx, "mallocsize.setraw", cmd_setraw, "", 1, 1, 1) == REDICTMODULE_ERR)
return REDICTMODULE_ERR;
if (RedictModule_CreateCommand(ctx, "mallocsize.setstr", cmd_setstr, "", 1, 1, 1) == REDICTMODULE_ERR)
return REDICTMODULE_ERR;
if (RedictModule_CreateCommand(ctx, "mallocsize.setdict", cmd_setdict, "", 1, 1, 1) == REDICTMODULE_ERR)
return REDICTMODULE_ERR;
return REDICTMODULE_OK;
}
|