Skip to content
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

feat: refactor db tasks for simplicity #1431

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/creatures/players/account/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace account {
query << "UPDATE `accounts` SET `coins_transferable` = " << (current_coins + amount)
<< " WHERE `id` = " << id_;

db_tasks_->addTask(query.str());
db_tasks_->execute(query.str());
return ERROR_NO;
}

Expand All @@ -135,7 +135,7 @@ namespace account {
query << "UPDATE `accounts` SET `coins_transferable` = " << (current_coins - amount)
<< " WHERE `id` = " << id_;

db_tasks_->addTask(query.str());
db_tasks_->execute(query.str());

return ERROR_NO;
}
Expand Down Expand Up @@ -177,7 +177,7 @@ namespace account {
query << "UPDATE `accounts` SET `coins` = " << (current_coins + amount)
<< " WHERE `id` = " << id_;

db_tasks_->addTask(query.str());
db_tasks_->execute(query.str());
return ERROR_NO;
}

Expand All @@ -202,7 +202,7 @@ namespace account {
query << "UPDATE `accounts` SET `coins` = " << (current_coins - amount)
<< " WHERE `id` = " << id_;

db_tasks_->addTask(query.str());
db_tasks_->execute(query.str());

return ERROR_NO;
}
Expand Down
6 changes: 3 additions & 3 deletions src/creatures/players/management/ban.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ bool IOBan::isAccountBanned(uint32_t accountId, BanInfo &banInfo) {
// Move the ban to history if it has expired
query.str(std::string());
query << "INSERT INTO `account_ban_history` (`account_id`, `reason`, `banned_at`, `expired_at`, `banned_by`) VALUES (" << accountId << ',' << db.escapeString(result->getString("reason")) << ',' << result->getNumber<time_t>("banned_at") << ',' << expiresAt << ',' << result->getNumber<uint32_t>("banned_by") << ')';
g_databaseTasks().addTask(query.str());
g_databaseTasks().execute(query.str());

query.str(std::string());
query << "DELETE FROM `account_bans` WHERE `account_id` = " << accountId;
g_databaseTasks().addTask(query.str());
g_databaseTasks().execute(query.str());
return false;
}

Expand Down Expand Up @@ -96,7 +96,7 @@ bool IOBan::isIpBanned(uint32_t clientIP, BanInfo &banInfo) {
if (expiresAt != 0 && time(nullptr) > expiresAt) {
query.str(std::string());
query << "DELETE FROM `ip_bans` WHERE `ip` = " << clientIP;
g_databaseTasks().addTask(query.str());
g_databaseTasks().execute(query.str());
return false;
}

Expand Down
30 changes: 12 additions & 18 deletions src/database/databasetasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,18 @@ DatabaseTasks &DatabaseTasks::getInstance() {
return inject<DatabaseTasks>();
}

void DatabaseTasks::addTask(std::string query, std::function<void(DBResult_ptr, bool)> callback /* = nullptr*/, bool store /* = false*/) {
threadPool.addLoad([this, query, callback, store]() {
std::lock_guard lockClass(threadSafetyMutex);

bool success;
DBResult_ptr result;
if (store) {
result = db.storeQuery(query);
success = true;
} else {
result = nullptr;
success = db.executeQuery(query);
}
void DatabaseTasks::execute(const std::string &query, std::function<void(DBResult_ptr, bool)> callback /* nullptr */) {
threadPool.addLoad([this, query, callback]() {
bool success = db.executeQuery(query);
if (callback != nullptr)
g_dispatcher().addTask([callback, success]() { callback(nullptr, success); });
});
}

if (callback) {
g_dispatcher().addTask(
[callback, result, success]() { callback(result, success); }
);
}
void DatabaseTasks::store(const std::string &query, std::function<void(DBResult_ptr, bool)> callback /* nullptr */) {
threadPool.addLoad([this, query, callback]() {
DBResult_ptr result = db.storeQuery(query);
if (callback != nullptr)
g_dispatcher().addTask([callback, result]() { callback(result, true); });
});
}
3 changes: 2 additions & 1 deletion src/database/databasetasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class DatabaseTasks {

static DatabaseTasks &getInstance();

void addTask(std::string query, std::function<void(DBResult_ptr, bool)> callback = nullptr, bool store = false);
void execute(const std::string &query, std::function<void(DBResult_ptr, bool)> callback = nullptr);
void store(const std::string &query, std::function<void(DBResult_ptr, bool)> callback = nullptr);

private:
Database &db;
Expand Down
6 changes: 3 additions & 3 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7829,7 +7829,7 @@ void Game::playerCyclopediaCharacterInfo(Player* player, uint32_t characterID, C
} while (result->next());
player->sendCyclopediaCharacterRecentDeaths(page, static_cast<uint16_t>(pages), entries);
};
g_databaseTasks().addTask(query.str(), callback, true);
g_databaseTasks().store(query.str(), callback);
player->addAsyncOngoingTask(PlayerAsyncTask_RecentDeaths);
break;
}
Expand Down Expand Up @@ -7882,7 +7882,7 @@ void Game::playerCyclopediaCharacterInfo(Player* player, uint32_t characterID, C
} while (result->next());
player->sendCyclopediaCharacterRecentPvPKills(page, static_cast<uint16_t>(pages), entries);
};
g_databaseTasks().addTask(query.str(), callback, true);
g_databaseTasks().store(query.str(), callback);
player->addAsyncOngoingTask(PlayerAsyncTask_RecentPvPKills);
break;
}
Expand Down Expand Up @@ -8027,7 +8027,7 @@ void Game::playerHighscores(Player* player, HighscoreType_t type, uint8_t catego
} while (result->next());
player->sendHighscores(characters, category, vocation, page, static_cast<uint16_t>(pages));
};
g_databaseTasks().addTask(query.str(), callback, true);
g_databaseTasks().store(query.str(), callback);
player->addAsyncOngoingTask(PlayerAsyncTask_Highscore);
}

Expand Down
4 changes: 2 additions & 2 deletions src/io/iomarket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void IOMarket::checkExpiredOffers() {

std::ostringstream query;
query << "SELECT `id`, `amount`, `price`, `itemtype`, `player_id`, `sale`, `tier` FROM `market_offers` WHERE `created` <= " << lastExpireDate;
g_databaseTasks().addTask(query.str(), IOMarket::processExpiredOffers, true);
g_databaseTasks().store(query.str(), IOMarket::processExpiredOffers);

int32_t checkExpiredMarketOffersEachMinutes = g_configManager().getNumber(CHECK_EXPIRED_MARKET_OFFERS_EACH_MINUTES);
if (checkExpiredMarketOffersEachMinutes <= 0) {
Expand Down Expand Up @@ -275,7 +275,7 @@ void IOMarket::appendHistory(uint32_t playerId, MarketAction_t type, uint16_t it
query << "INSERT INTO `market_history` (`player_id`, `sale`, `itemtype`, `amount`, `price`, `expires_at`, `inserted`, `state`, `tier`) VALUES ("
<< playerId << ',' << type << ',' << itemId << ',' << amount << ',' << price << ','
<< timestamp << ',' << getTimeNow() << ',' << state << ',' << std::to_string(tier) << ')';
g_databaseTasks().addTask(query.str());
g_databaseTasks().execute(query.str());
}

bool IOMarket::moveOfferToHistory(uint32_t offerId, MarketOfferState_t state) {
Expand Down
4 changes: 2 additions & 2 deletions src/lua/functions/core/libs/db_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int DBFunctions::luaDatabaseAsyncExecute(lua_State* L) {
luaL_unref(luaState, LUA_REGISTRYINDEX, ref);
};
}
g_databaseTasks().addTask(getString(L, -1), callback);
g_databaseTasks().execute(getString(L, -1), callback);
return 0;
}

Expand Down Expand Up @@ -86,7 +86,7 @@ int DBFunctions::luaDatabaseAsyncStoreQuery(lua_State* L) {
luaL_unref(luaState, LUA_REGISTRYINDEX, ref);
};
}
g_databaseTasks().addTask(getString(L, -1), callback, true);
g_databaseTasks().store(getString(L, -1), callback);
return 0;
}

Expand Down