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
7 changed files with 93 additions and 126 deletions

View File

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

View File

@@ -2,7 +2,8 @@
// Custom Classes
#include "CDTable.h"
#include <map>
#include <unordered_map>
#include <unordered_set>
/*!
\file CDBehaviorParameterTable.hpp
@@ -19,8 +20,8 @@ struct CDBehaviorParameter {
//! BehaviorParameter table
class CDBehaviorParameterTable : public CDTable {
private:
std::map<size_t, float> m_Entries;
std::unordered_map<size_t, CDBehaviorParameter> m_Entries;
std::unordered_set<std::string> m_ParametersList;
public:
//! Constructor
@@ -35,5 +36,7 @@ public:
*/
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
this->entries.reserve(size);
// Now get the data
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM BehaviorTemplate");
while (!tableData.eof()) {
@@ -27,6 +27,7 @@ CDBehaviorTemplateTable::CDBehaviorTemplateTable(void) {
entry.effectHandle = tableData.getStringField(3, "");
this->entries.push_back(entry);
this->entriesMappedByBehaviorID.insert(std::make_pair(entry.behaviorID, entry));
tableData.nextRow();
}
@@ -55,3 +56,15 @@ std::vector<CDBehaviorTemplate> CDBehaviorTemplateTable::Query(std::function<boo
std::vector<CDBehaviorTemplate> CDBehaviorTemplateTable::GetEntries(void) const {
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
#include "CDTable.h"
#include <unordered_map>
/*!
\file CDBehaviorTemplateTable.hpp
@@ -21,7 +22,7 @@ struct CDBehaviorTemplate {
class CDBehaviorTemplateTable : public CDTable {
private:
std::vector<CDBehaviorTemplate> entries;
std::unordered_map<uint32_t, CDBehaviorTemplate> entriesMappedByBehaviorID;
public:
//! Constructor
@@ -47,5 +48,6 @@ public:
\return The entries
*/
std::vector<CDBehaviorTemplate> GetEntries(void) const;
const CDBehaviorTemplate GetByBehaviorID(uint32_t behaviorID);
};