Skip to content

Commit

Permalink
improve: slightly refactor on database tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrossi committed Aug 19, 2023
1 parent c73b5e8 commit 0d93c80
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 34 deletions.
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
32 changes: 13 additions & 19 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);
}

if (callback) {
g_dispatcher().addTask(
[callback, result, success]() { callback(result, success); }
);
}
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); });
});
}

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

0 comments on commit 0d93c80

Please sign in to comment.