Added caching for behavior parameter table (#621)

* Added caching for table

Added caching for table

Add more caching

Update MasterServer.cpp

grds

Update CDBehaviorParameterTable.cpp

Update CDBehaviorParameterTable.h

Update CDBehaviorTemplateTable.cpp

Update Behavior.cpp

Update Behavior.cpp

change to map

Remove redundant query

* Remove include

* change to enum

* Update Behavior.cpp

* Use already cached table

* Update Behavior.cpp
This commit is contained in:
David Markowitz 2022-07-08 23:07:52 -07:00 committed by GitHub
parent dddc33607b
commit 485de6173a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 93 additions and 126 deletions

View File

@ -8,7 +8,7 @@ void CDClientManager::Initialize(void) {
tables.insert(std::make_pair("ActivityRewards", new CDActivityRewardsTable())); tables.insert(std::make_pair("ActivityRewards", new CDActivityRewardsTable()));
UNUSED(tables.insert(std::make_pair("Animations", new CDAnimationsTable()))); UNUSED(tables.insert(std::make_pair("Animations", new CDAnimationsTable())));
tables.insert(std::make_pair("BehaviorParameter", new CDBehaviorParameterTable())); tables.insert(std::make_pair("BehaviorParameter", new CDBehaviorParameterTable()));
UNUSED(tables.insert(std::make_pair("BehaviorTemplate", new CDBehaviorTemplateTable()))); tables.insert(std::make_pair("BehaviorTemplate", new CDBehaviorTemplateTable()));
tables.insert(std::make_pair("ComponentsRegistry", new CDComponentsRegistryTable())); tables.insert(std::make_pair("ComponentsRegistry", new CDComponentsRegistryTable()));
tables.insert(std::make_pair("CurrencyTable", new CDCurrencyTableTable())); tables.insert(std::make_pair("CurrencyTable", new CDCurrencyTableTable()));
tables.insert(std::make_pair("DestructibleComponent", new CDDestructibleComponentTable())); tables.insert(std::make_pair("DestructibleComponent", new CDDestructibleComponentTable()));

View File

@ -3,33 +3,25 @@
//! Constructor //! Constructor
CDBehaviorParameterTable::CDBehaviorParameterTable(void) { CDBehaviorParameterTable::CDBehaviorParameterTable(void) {
#ifdef CDCLIENT_CACHE_ALL
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM BehaviorParameter"); auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM BehaviorParameter");
size_t hash = 0;
while (!tableData.eof()) { while (!tableData.eof()) {
hash = 0;
CDBehaviorParameter entry; CDBehaviorParameter entry;
entry.behaviorID = tableData.getIntField(0, -1); entry.behaviorID = tableData.getIntField(0, -1);
entry.parameterID = tableData.getStringField(1, ""); entry.parameterID = tableData.getStringField(1, "");
entry.value = tableData.getFloatField(2, -1.0f); entry.value = tableData.getFloatField(2, -1.0f);
//Check if we have an entry with this ID: GeneralUtils::hash_combine(hash, entry.behaviorID);
auto it = m_entries.find(entry.behaviorID); GeneralUtils::hash_combine(hash, entry.parameterID);
if (it != m_entries.end()) {
it->second.insert(std::make_pair(entry.parameterID, entry.value));
}
else {
//Otherwise, insert it:
m_entries.insert(std::make_pair(entry.behaviorID, std::map<std::string, float>()));
auto jit = m_entries.find(entry.behaviorID);
//Add our value as well: auto it = m_Entries.find(entry.behaviorID);
jit->second.insert(std::make_pair(entry.parameterID, entry.value)); m_ParametersList.insert(entry.parameterID);
} m_Entries.insert(std::make_pair(hash, entry));
tableData.nextRow(); tableData.nextRow();
} }
tableData.finalize(); tableData.finalize();
#endif
} }
//! Destructor //! Destructor
@ -40,51 +32,33 @@ std::string CDBehaviorParameterTable::GetName(void) const {
return "BehaviorParameter"; return "BehaviorParameter";
} }
float CDBehaviorParameterTable::GetEntry(const uint32_t behaviorID, const std::string& name, const float defaultValue) CDBehaviorParameter CDBehaviorParameterTable::GetEntry(const uint32_t behaviorID, const std::string& name, const float defaultValue)
{ {
CDBehaviorParameter returnValue;
returnValue.behaviorID = 0;
returnValue.parameterID = "";
returnValue.value = defaultValue;
size_t hash = 0; size_t hash = 0;
GeneralUtils::hash_combine(hash, behaviorID); GeneralUtils::hash_combine(hash, behaviorID);
GeneralUtils::hash_combine(hash, name); GeneralUtils::hash_combine(hash, name);
// Search for specific parameter // Search for specific parameter
const auto& it = m_Entries.find(hash); const auto& it = m_Entries.find(hash);
if (it != m_Entries.end()) { return it != m_Entries.end() ? it->second : returnValue;
return it->second; }
}
std::map<std::string, float> CDBehaviorParameterTable::GetParametersByBehaviorID(uint32_t behaviorID) {
// Check if this behavior has already been checked size_t hash;
const auto& itChecked = m_Entries.find(behaviorID); std::map<std::string, float> returnInfo;
if (itChecked != m_Entries.end()) { for (auto parameterCandidate : m_ParametersList) {
return defaultValue; hash = 0;
} GeneralUtils::hash_combine(hash, behaviorID);
GeneralUtils::hash_combine(hash, parameterCandidate);
#ifndef CDCLIENT_CACHE_ALL auto infoCandidate = m_Entries.find(hash);
auto query = CDClientDatabase::CreatePreppedStmt( if (infoCandidate != m_Entries.end()) {
"SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = ?;"); returnInfo.insert(std::make_pair(infoCandidate->second.parameterID, infoCandidate->second.value));
query.bind(1, (int) behaviorID); }
}
auto tableData = query.execQuery(); return returnInfo;
m_Entries.insert_or_assign(behaviorID, 0);
while (!tableData.eof()) {
const std::string parameterID = tableData.getStringField(0, "");
const float value = tableData.getFloatField(1, 0);
size_t parameterHash = 0;
GeneralUtils::hash_combine(parameterHash, behaviorID);
GeneralUtils::hash_combine(parameterHash, parameterID);
m_Entries.insert_or_assign(parameterHash, value);
tableData.nextRow();
}
const auto& it2 = m_Entries.find(hash);
if (it2 != m_Entries.end()) {
return it2->second;
}
#endif
return defaultValue;
} }

View File

@ -2,7 +2,8 @@
// Custom Classes // Custom Classes
#include "CDTable.h" #include "CDTable.h"
#include <map> #include <unordered_map>
#include <unordered_set>
/*! /*!
\file CDBehaviorParameterTable.hpp \file CDBehaviorParameterTable.hpp
@ -19,8 +20,8 @@ struct CDBehaviorParameter {
//! BehaviorParameter table //! BehaviorParameter table
class CDBehaviorParameterTable : public CDTable { class CDBehaviorParameterTable : public CDTable {
private: private:
std::map<size_t, float> m_Entries; std::unordered_map<size_t, CDBehaviorParameter> m_Entries;
std::unordered_set<std::string> m_ParametersList;
public: public:
//! Constructor //! Constructor
@ -35,5 +36,7 @@ public:
*/ */
std::string GetName(void) const override; std::string GetName(void) const override;
float GetEntry(const uint32_t behaviorID, const std::string& name, const float defaultValue = 0); CDBehaviorParameter GetEntry(const uint32_t behaviorID, const std::string& name, const float defaultValue = 0);
std::map<std::string, float> GetParametersByBehaviorID(uint32_t behaviorID);
}; };

View File

@ -16,7 +16,7 @@ CDBehaviorTemplateTable::CDBehaviorTemplateTable(void) {
// Reserve the size // Reserve the size
this->entries.reserve(size); this->entries.reserve(size);
// Now get the data // Now get the data
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM BehaviorTemplate"); auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM BehaviorTemplate");
while (!tableData.eof()) { while (!tableData.eof()) {
@ -27,6 +27,7 @@ CDBehaviorTemplateTable::CDBehaviorTemplateTable(void) {
entry.effectHandle = tableData.getStringField(3, ""); entry.effectHandle = tableData.getStringField(3, "");
this->entries.push_back(entry); this->entries.push_back(entry);
this->entriesMappedByBehaviorID.insert(std::make_pair(entry.behaviorID, entry));
tableData.nextRow(); tableData.nextRow();
} }
@ -55,3 +56,15 @@ std::vector<CDBehaviorTemplate> CDBehaviorTemplateTable::Query(std::function<boo
std::vector<CDBehaviorTemplate> CDBehaviorTemplateTable::GetEntries(void) const { std::vector<CDBehaviorTemplate> CDBehaviorTemplateTable::GetEntries(void) const {
return this->entries; return this->entries;
} }
const CDBehaviorTemplate CDBehaviorTemplateTable::GetByBehaviorID(uint32_t behaviorID) {
auto entry = this->entriesMappedByBehaviorID.find(behaviorID);
if (entry == this->entriesMappedByBehaviorID.end()) {
CDBehaviorTemplate entryToReturn;
entryToReturn.behaviorID = 0;
entryToReturn.effectHandle = "";
return entryToReturn;
} else {
return entry->second;
}
}

View File

@ -2,6 +2,7 @@
// Custom Classes // Custom Classes
#include "CDTable.h" #include "CDTable.h"
#include <unordered_map>
/*! /*!
\file CDBehaviorTemplateTable.hpp \file CDBehaviorTemplateTable.hpp
@ -21,7 +22,7 @@ struct CDBehaviorTemplate {
class CDBehaviorTemplateTable : public CDTable { class CDBehaviorTemplateTable : public CDTable {
private: private:
std::vector<CDBehaviorTemplate> entries; std::vector<CDBehaviorTemplate> entries;
std::unordered_map<uint32_t, CDBehaviorTemplate> entriesMappedByBehaviorID;
public: public:
//! Constructor //! Constructor
@ -47,5 +48,6 @@ public:
\return The entries \return The entries
*/ */
std::vector<CDBehaviorTemplate> GetEntries(void) const; std::vector<CDBehaviorTemplate> GetEntries(void) const;
const CDBehaviorTemplate GetByBehaviorID(uint32_t behaviorID);
}; };

View File

@ -7,6 +7,7 @@
#include "dLogger.h" #include "dLogger.h"
#include "BehaviorTemplates.h" #include "BehaviorTemplates.h"
#include "BehaviorBranchContext.h" #include "BehaviorBranchContext.h"
#include <unordered_map>
/* /*
* Behavior includes * Behavior includes
@ -69,7 +70,7 @@
#include "RenderComponent.h" #include "RenderComponent.h"
#include "DestroyableComponent.h" #include "DestroyableComponent.h"
std::map<uint32_t, Behavior*> Behavior::Cache = {}; std::unordered_map<uint32_t, Behavior*> Behavior::Cache = {};
CDBehaviorParameterTable* Behavior::BehaviorParameterTable = nullptr; CDBehaviorParameterTable* Behavior::BehaviorParameterTable = nullptr;
Behavior* Behavior::GetBehavior(const uint32_t behaviorId) Behavior* Behavior::GetBehavior(const uint32_t behaviorId)
@ -285,28 +286,22 @@ Behavior* Behavior::CreateBehavior(const uint32_t behaviorId)
} }
BehaviorTemplates Behavior::GetBehaviorTemplate(const uint32_t behaviorId) { BehaviorTemplates Behavior::GetBehaviorTemplate(const uint32_t behaviorId) {
auto query = CDClientDatabase::CreatePreppedStmt( auto behaviorTemplateTable = CDClientManager::Instance()->GetTable<CDBehaviorTemplateTable>("BehaviorTemplate");
"SELECT templateID FROM BehaviorTemplate WHERE behaviorID = ?;");
query.bind(1, (int) behaviorId);
auto result = query.execQuery(); BehaviorTemplates templateID = BehaviorTemplates::BEHAVIOR_EMPTY;
// Find behavior template by its behavior id. Default to 0.
// Make sure we do not proceed if we are trying to load an invalid behavior if (behaviorTemplateTable) {
if (result.eof()) auto templateEntry = behaviorTemplateTable->GetByBehaviorID(behaviorId);
{ if (templateEntry.behaviorID == behaviorId) {
if (behaviorId != 0) templateID = static_cast<BehaviorTemplates>(templateEntry.templateID);
{
Game::logger->Log("Behavior::GetBehaviorTemplate", "Failed to load behavior template with id (%i)!\n", behaviorId);
} }
return BehaviorTemplates::BEHAVIOR_EMPTY;
} }
const auto id = static_cast<BehaviorTemplates>(result.getIntField(0)); if (templateID == BehaviorTemplates::BEHAVIOR_EMPTY && behaviorId != 0) {
Game::logger->Log("Behavior", "Failed to load behavior template with id (%i)!\n", behaviorId);
}
result.finalize(); return templateID;
return id;
} }
// For use with enemies, to display the correct damage animations on the players // For use with enemies, to display the correct damage animations on the players
@ -412,6 +407,17 @@ void Behavior::PlayFx(std::u16string type, const LWOOBJID target, const LWOOBJID
Behavior::Behavior(const uint32_t behaviorId) Behavior::Behavior(const uint32_t behaviorId)
{ {
auto behaviorTemplateTable = CDClientManager::Instance()->GetTable<CDBehaviorTemplateTable>("BehaviorTemplate");
CDBehaviorTemplate templateInDatabase;
if (behaviorTemplateTable) {
auto templateEntry = behaviorTemplateTable->GetByBehaviorID(behaviorId);
if (templateEntry.behaviorID == behaviorId) {
templateInDatabase = templateEntry;
}
}
this->m_behaviorId = behaviorId; this->m_behaviorId = behaviorId;
// Add to cache // Add to cache
@ -423,14 +429,8 @@ Behavior::Behavior(const uint32_t behaviorId)
this->m_templateId = BehaviorTemplates::BEHAVIOR_EMPTY; this->m_templateId = BehaviorTemplates::BEHAVIOR_EMPTY;
} }
auto query = CDClientDatabase::CreatePreppedStmt(
"SELECT templateID, effectID, effectHandle FROM BehaviorTemplate WHERE behaviorID = ?;");
query.bind(1, (int) behaviorId);
auto result = query.execQuery();
// Make sure we do not proceed if we are trying to load an invalid behavior // Make sure we do not proceed if we are trying to load an invalid behavior
if (result.eof()) if (templateInDatabase.behaviorID == 0)
{ {
Game::logger->Log("Behavior", "Failed to load behavior with id (%i)!\n", behaviorId); Game::logger->Log("Behavior", "Failed to load behavior with id (%i)!\n", behaviorId);
@ -441,34 +441,19 @@ Behavior::Behavior(const uint32_t behaviorId)
return; return;
} }
this->m_templateId = static_cast<BehaviorTemplates>(result.getIntField(0)); this->m_templateId = static_cast<BehaviorTemplates>(templateInDatabase.templateID);
this->m_effectId = result.getIntField(1); this->m_effectId = templateInDatabase.effectID;
if (!result.fieldIsNull(2)) this->m_effectHandle = templateInDatabase.effectHandle != "" ? new std::string(templateInDatabase.effectHandle) : nullptr;
{
const std::string effectHandle = result.getStringField(2);
if (effectHandle == "")
{
this->m_effectHandle = nullptr;
}
else
{
this->m_effectHandle = new std::string(effectHandle);
}
}
else
{
this->m_effectHandle = nullptr;
}
result.finalize();
} }
float Behavior::GetFloat(const std::string& name, const float defaultValue) const float Behavior::GetFloat(const std::string& name, const float defaultValue) const
{ {
return BehaviorParameterTable->GetEntry(this->m_behaviorId, name, defaultValue); // Get the behavior parameter entry and return its value.
if (!BehaviorParameterTable) BehaviorParameterTable = CDClientManager::Instance()->GetTable<CDBehaviorParameterTable>("BehaviorParameter");
return BehaviorParameterTable->GetEntry(this->m_behaviorId, name, defaultValue).value;
} }
@ -498,24 +483,14 @@ Behavior* Behavior::GetAction(float value) const
std::map<std::string, float> Behavior::GetParameterNames() const std::map<std::string, float> Behavior::GetParameterNames() const
{ {
std::map<std::string, float> parameters; std::map<std::string, float> templatesInDatabase;
// Find behavior template by its behavior id.
auto query = CDClientDatabase::CreatePreppedStmt( if (!BehaviorParameterTable) BehaviorParameterTable = CDClientManager::Instance()->GetTable<CDBehaviorParameterTable>("BehaviorParameter");
"SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = ?;"); if (BehaviorParameterTable) {
query.bind(1, (int) this->m_behaviorId); templatesInDatabase = BehaviorParameterTable->GetParametersByBehaviorID(this->m_behaviorId);
auto tableData = query.execQuery();
while (!tableData.eof())
{
parameters.insert_or_assign(tableData.getStringField(0, ""), tableData.getFloatField(1, 0));
tableData.nextRow();
} }
tableData.finalize(); return templatesInDatabase;
return parameters;
} }
void Behavior::Load() void Behavior::Load()

View File

@ -19,7 +19,7 @@ public:
/* /*
* Static * Static
*/ */
static std::map<uint32_t, Behavior*> Cache; static std::unordered_map<uint32_t, Behavior*> Cache;
static CDBehaviorParameterTable* BehaviorParameterTable; static CDBehaviorParameterTable* BehaviorParameterTable;
static Behavior* GetBehavior(uint32_t behaviorId); static Behavior* GetBehavior(uint32_t behaviorId);