MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / luaSortArray

Function luaSortArray

src/scripting.cpp:329–352  ·  view source on GitHub ↗

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. */

Source from the content-addressed store, hash-verified

327 * The array is sorted using table.sort itself, and assuming all the
328 * list elements are strings. */
329void luaSortArray(lua_State *lua) {
330 /* Initial Stack: array */
331 lua_getglobal(lua,"table");
332 lua_pushstring(lua,"sort");
333 lua_gettable(lua,-2); /* Stack: array, table, table.sort */
334 lua_pushvalue(lua,-3); /* Stack: array, table, table.sort, array */
335 if (lua_pcall(lua,1,0,0)) {
336 /* Stack: array, table, error */
337
338 /* We are not interested in the error, we assume that the problem is
339 * that there are 'false' elements inside the array, so we try
340 * again with a slower function but able to handle this case, that
341 * is: table.sort(table, __redis__compare_helper) */
342 lua_pop(lua,1); /* Stack: array, table */
343 lua_pushstring(lua,"sort"); /* Stack: array, table, sort */
344 lua_gettable(lua,-2); /* Stack: array, table, table.sort */
345 lua_pushvalue(lua,-3); /* Stack: array, table, table.sort, array */
346 lua_getglobal(lua,"__redis__compare_helper");
347 /* Stack: array, table, table.sort, array, __redis__compare_helper */
348 lua_call(lua,2,0);
349 }
350 /* Stack: array (sorted), table */
351 lua_pop(lua,1); /* Stack: array (sorted) */
352}
353
354/* ---------------------------------------------------------------------------
355 * Lua reply to Redis reply conversion functions.

Callers 1

luaRedisGenericCommandFunction · 0.85

Calls 5

lua_pushstringFunction · 0.85
lua_gettableFunction · 0.85
lua_pushvalueFunction · 0.85
lua_pcallFunction · 0.85
lua_callFunction · 0.85

Tested by

no test coverage detected