-
Notifications
You must be signed in to change notification settings - Fork 23.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modules: add RM_GetCommandKeys(). #7884
Conversation
src/module.c
Outdated
if (res->numkeys == res->size) { | ||
/* If we're using the preallocated array, reallocate on the heap | ||
* and move the data. Otherwise we can just realloc. */ | ||
int newsize = res->size * 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
above a certain size, *2
may be too much.
we can consider just adding 1000 instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Capping the doubling is reasonable but I wouldn't want to use intervals of 1000 on extreme cases.
src/module.c
Outdated
@@ -672,12 +671,12 @@ int moduleGetCommandKeysViaAPI(struct redisCommand *cmd, robj **argv, int argc, | |||
ctx.module = cp->module; | |||
ctx.client = NULL; | |||
ctx.flags |= REDISMODULE_CTX_KEYS_POS_REQUEST; | |||
if (!result->keys) result->keys = result->keysbuf; /* Initialize to use preallocated array */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't the init with the preallocated array already handled by the caller?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it can't without a macro that takes the name of the variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ohh right, the caller does GETKEYS_RESULT_INIT
which doesn't do that.
it's prepareGetKeysResult
that does it (static and only used in db.c).
and it is designed to be used once (not repeatedly).
i guess what i was thinking is to make it non-static, and change it in a way that it can be called repeatedly, then it can be used in both moduleGetCommandKeysViaAPI
(first init), and also in RM_KeyAtPos
(doing the reallocs).
just a thought, your way is good too.
Avoid using a static buffer for short key index responses, and make it caller's responsibility to stack-allocate a result type. Responses that don't fit are still allocated on the heap.
This is essentially the same as calling COMMAND GETKEYS but provides a more efficient interface that can be used in every context (i.e. not a Redis command).
This is essentially the same as calling COMMAND GETKEYS but provides a more efficient interface that can be used in every context (i.e. not a Redis command).
This pull request also includes refactoring of the
getKeysFromCommand()
interface to eliminate the previously used static buffer.