mirror of
https://github.com/fluencelabs/redis
synced 2025-06-16 10:41:22 +00:00
Order output of commands returning random arrays using table.sort when called from Lua, partially fixing issue #165. The issue is yet not completely fixed since we can't add the REDIS_CMD_SORT_FOR_SCRIPT flag in SORT currently, both because it may contain NULLs and because it is not cool to re-sort everything at every call when instead this should be sorted only if BY <constant> is used.
This commit is contained in:
@ -27,7 +27,7 @@ int redis_math_randomseed (lua_State *L);
|
||||
* is like a normal client that bypasses all the slow I/O paths.
|
||||
*
|
||||
* Note: in this function we do not do any sanity check as the reply is
|
||||
* generated by Redis directly. This allows use to go faster.
|
||||
* generated by Redis directly. This allows us to go faster.
|
||||
* The reply string can be altered during the parsing as it is discared
|
||||
* after the conversion is completed.
|
||||
*
|
||||
@ -128,6 +128,22 @@ void luaPushError(lua_State *lua, char *error) {
|
||||
lua_settable(lua,-3);
|
||||
}
|
||||
|
||||
/* Sort the array currently in the stack. We do this to make the output
|
||||
* of commands like KEYS or SMEMBERS something deterministic when called
|
||||
* from Lua (to play well with AOf/replication).
|
||||
*
|
||||
* The array is sorted using table.sort itself, and assuming all the
|
||||
* list elements are strings. */
|
||||
void luaSortArray(lua_State *lua) {
|
||||
/* Initial Stack: array */
|
||||
lua_getglobal(lua,"table");
|
||||
lua_pushstring(lua,"sort");
|
||||
lua_gettable(lua,-2); /* Stack: array, table, table.sort */
|
||||
lua_pushvalue(lua,-3); /* Stack: array, table, table.sort, array */
|
||||
lua_call(lua,1,0); /* Stack: array (sorted), table */
|
||||
lua_pop(lua,1); /* Stack: array (sorted) */
|
||||
}
|
||||
|
||||
int luaRedisGenericCommand(lua_State *lua, int raise_error) {
|
||||
int j, argc = lua_gettop(lua);
|
||||
struct redisCommand *cmd;
|
||||
@ -208,6 +224,12 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
|
||||
}
|
||||
if (raise_error && reply[0] != '-') raise_error = 0;
|
||||
redisProtocolToLuaType(lua,reply);
|
||||
/* Sort the output array if needed, assuming it is a non-null multi bulk
|
||||
* reply as expected. */
|
||||
if ((cmd->flags & REDIS_CMD_SORT_FOR_SCRIPT) &&
|
||||
(reply[0] == '*' && reply[1] != '-')) {
|
||||
luaSortArray(lua);
|
||||
}
|
||||
sdsfree(reply);
|
||||
|
||||
cleanup:
|
||||
|
Reference in New Issue
Block a user