ElunaLuaEngine / Eluna

Eluna Lua Engine © for WoW Emulators
https://elunaluaengine.github.io
GNU General Public License v3.0
377 stars 362 forks source link

[Suggestion] Implement asynchronous DB queries #403

Closed anzz1 closed 9 months ago

anzz1 commented 2 years ago

Currently Eluna has only DBQuery and DBExecute which of Execute is pushed to the core's database worker thread pool and does not block, but also does not return results. If you want to make a database query, it is always synchronous and hence always blocking.

If something like this would be implemented in Eluna Engine

callbackfunc_tbl ={}
function Eluna.ASyncQueryResultsReturned(results, id)
    callbackfunc_tbl[id](results)
    callbackfunc_tbl[id] = nil
end
function Eluna.CharDBASyncQuery(queryString, callbackFunc)
    ... push the query and id of the query to the core database worker thread ...
    callbackfunc_tbl[id] = callbackFunc
end

Something like this could be used then in scripts:

local function resultsCallback(results)
    if results then
        repeat
            local entry, name = results:GetUInt32(0), results:GetString(1)
            print(entry, name)
        until not results:NextRow()
    else
        print("db fetch failed")
    end
end

local function whatever()
    CharDBASyncQuery("SELECT entry, name FROM creature_template LIMIT 10", resultsCallback)
    -- continue execution as normal, resultsCallback will be called later when results arrive
end

Such thing is probably implemented in all the supported cores (?) because doing synchronous queries for everything and blocking execution is not viable. I know that at least AzerothCore and TrinityCore does have this ASyncQuery method DatabaseWorkerPool.cpp#L225 , it just needs to be implemented in Eluna.

r-o-b-o-t-o commented 1 year ago

FYI, I implemented this in AzerothCore's fork: https://github.com/azerothcore/mod-eluna/pull/113

Rochet2 commented 1 year ago

Looks good. AsyncQuery exists in all cores we support, but I think it behaves a bit differently in some. So we need to do a bit more lifting to make it work all of the cores.

Foereaper commented 9 months ago

Async queries are implemented in the multistate branch, it will either be ported to main or available once multistate is merged down.