Core/DataStores: Simplified string memory allocation in db2 files, dropped unneccessary level of indirection

This commit is contained in:
Shauren
2020-06-27 13:33:17 +02:00
parent eccc015ce7
commit deca201f77
38 changed files with 333 additions and 380 deletions

View File

@@ -22,10 +22,6 @@
#include "Errors.h"
#include "Log.h"
DB2LoadInfo::DB2LoadInfo() : DB2FileLoadInfo(), Statement(MAX_HOTFIXDATABASE_STATEMENTS)
{
}
DB2LoadInfo::DB2LoadInfo(DB2FieldMeta const* fields, std::size_t fieldCount, DB2Meta const* meta, HotfixDatabaseStatements statement)
: DB2FileLoadInfo(fields, fieldCount, meta), Statement(statement)
{
@@ -84,6 +80,7 @@ char* DB2DatabaseLoader::Load(bool custom, uint32& records, char**& indexTable,
}
char* tempDataTable = new char[result->GetRowCount() * recordSize];
memset(tempDataTable, 0, result->GetRowCount() * recordSize);
uint32* newIndexes = new uint32[result->GetRowCount()];
if (stringFields)
stringPool.reserve(std::max(stringPool.capacity(), stringPool.size() + stringFields * result->GetRowCount() + 1));
@@ -95,9 +92,9 @@ char* DB2DatabaseLoader::Load(bool custom, uint32& records, char**& indexTable,
{
Field* fields = result->Fetch();
uint32 offset = 0;
uint32 stringFieldOffset = 0;
uint32 indexValue = fields[indexField].GetUInt32();
bool isNew = false;
// Attempt to overwrite existing data
char* dataValue = indexTable[indexValue];
@@ -105,6 +102,7 @@ char* DB2DatabaseLoader::Load(bool custom, uint32& records, char**& indexTable,
{
newIndexes[newRecords] = indexValue;
dataValue = &tempDataTable[newRecords++ * recordSize];
isNew = true;
}
uint32 f = 0;
@@ -143,29 +141,28 @@ char* DB2DatabaseLoader::Load(bool custom, uint32& records, char**& indexTable,
break;
case FT_STRING:
{
LocalizedString** slot = (LocalizedString**)(&dataValue[offset]);
*slot = (LocalizedString*)(&stringHolders[stringHoldersRecordPoolSize * rec + stringFieldOffset]);
ASSERT(*slot);
LocalizedString* slot = (LocalizedString*)(&dataValue[offset]);
if (isNew)
for (char const*& localeStr : slot->Str)
localeStr = nullStr;
// Value in database in main table field must be for enUS locale
if (char* str = AddString(&(*slot)->Str[LOCALE_enUS], fields[f].GetString()))
if (char* str = AddString(&slot->Str[LOCALE_enUS], fields[f].GetString()))
stringPool.push_back(str);
stringFieldOffset += sizeof(LocalizedString);
offset += sizeof(char*);
offset += sizeof(LocalizedString);
break;
}
case FT_STRING_NOT_LOCALIZED:
{
char const** slot = (char const**)(&dataValue[offset]);
*slot = (char*)(&stringHolders[stringHoldersRecordPoolSize * rec + stringFieldOffset]);
ASSERT(*slot);
// Value in database in main table field must be for enUS locale
if (char* str = AddString(slot, fields[f].GetString()))
stringPool.push_back(str);
else
*slot = nullStr;
stringFieldOffset += sizeof(char*);
offset += sizeof(char*);
break;
}
@@ -205,7 +202,7 @@ char* DB2DatabaseLoader::Load(bool custom, uint32& records, char**& indexTable,
return dataTable;
}
void DB2DatabaseLoader::LoadStrings(bool custom, uint32 locale, uint32 records, char** indexTable, std::vector<char*>& stringPool)
void DB2DatabaseLoader::LoadStrings(bool custom, LocaleConstant locale, uint32 records, char** indexTable, std::vector<char*>& stringPool)
{
HotfixDatabasePreparedStatement* stmt = HotfixDatabase.GetPreparedStatement(HotfixDatabaseStatements(_loadInfo->Statement + 2));
stmt->setBool(0, !custom);
@@ -265,13 +262,12 @@ void DB2DatabaseLoader::LoadStrings(bool custom, uint32 locale, uint32 records,
case FT_STRING:
{
// fill only not filled entries
LocalizedString* db2str = *(LocalizedString**)(&dataValue[offset]);
if (db2str->Str[locale] == nullStr)
if (char* str = AddString(&db2str->Str[locale], fields[1 + stringFieldNumInRecord].GetString()))
stringPool.push_back(str);
LocalizedString* db2str = (LocalizedString*)(&dataValue[offset]);
if (char* str = AddString(&db2str->Str[locale], fields[1 + stringFieldNumInRecord].GetString()))
stringPool.push_back(str);
++stringFieldNumInRecord;
offset += sizeof(LocalizedString*);
offset += sizeof(LocalizedString);
break;
}
case FT_STRING_NOT_LOCALIZED:
@@ -298,16 +294,6 @@ char* DB2DatabaseLoader::AddString(char const** holder, std::string const& value
{
if (!value.empty())
{
std::size_t existingLength = strlen(*holder);
if (existingLength >= value.length())
{
// Reuse existing storage if there is enough space
char* str = const_cast<char*>(*holder);
memcpy(str, value.c_str(), value.length());
str[value.length()] = '\0';
return nullptr;
}
char* str = new char[value.length() + 1];
memcpy(str, value.c_str(), value.length());
str[value.length()] = '\0';