MCPcopy Create free account
hub / github.com/Tencent/Tendis / luaSortArray

Function luaSortArray

src/tendisplus/script/lua_state.cpp:190–213  ·  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

188 * The array is sorted using table.sort itself, and assuming all the
189 * list elements are strings. */
190void luaSortArray(lua_State* lua) {
191 /* Initial Stack: array */
192 lua_getglobal(lua, "table");
193 lua_pushstring(lua, "sort");
194 lua_gettable(lua, -2); /* Stack: array, table, table.sort */
195 lua_pushvalue(lua, -3); /* Stack: array, table, table.sort, array */
196 if (lua_pcall(lua, 1, 0, 0)) {
197 /* Stack: array, table, error */
198
199 /* We are not interested in the error, we assume that the problem is
200 * that there are 'false' elements inside the array, so we try
201 * again with a slower function but able to handle this case, that
202 * is: table.sort(table, __redis__compare_helper) */
203 lua_pop(lua, 1); /* Stack: array, table */
204 lua_pushstring(lua, "sort"); /* Stack: array, table, sort */
205 lua_gettable(lua, -2); /* Stack: array, table, table.sort */
206 lua_pushvalue(lua, -3); /* Stack: array, table, table.sort, array */
207 lua_getglobal(lua, "__redis__compare_helper");
208 /* Stack: array, table, table.sort, array, __redis__compare_helper */
209 lua_call(lua, 2, 0);
210 }
211 /* Stack: array (sorted), table */
212 lua_pop(lua, 1); /* Stack: array (sorted) */
213}
214
215Expected<std::string> LuaState::luaCreateFunction(lua_State* lua,
216 const std::string& body) {

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected