Core/DataStores: Optimized DB2Storage::begin to remove searching for lowest id on every call

This commit is contained in:
Shauren
2023-04-27 00:17:25 +02:00
parent 7eb4a67ae7
commit f540477fed
6 changed files with 62 additions and 10 deletions

View File

@@ -177,6 +177,7 @@ public:
virtual DB2RecordCopy GetRecordCopy(uint32 copyNumber) const = 0;
virtual uint32 GetRecordCount() const = 0;
virtual uint32 GetRecordCopyCount() const = 0;
virtual uint32 GetMinId() const = 0;
virtual uint32 GetMaxId() const = 0;
virtual DB2FileLoadInfo const* GetLoadInfo() const = 0;
virtual DB2SectionHeader& GetSection(uint32 section) const = 0;
@@ -223,6 +224,7 @@ public:
DB2RecordCopy GetRecordCopy(uint32 copyNumber) const override;
uint32 GetRecordCount() const override;
uint32 GetRecordCopyCount() const override;
uint32 GetMinId() const override;
uint32 GetMaxId() const override;
DB2FileLoadInfo const* GetLoadInfo() const override;
DB2SectionHeader& GetSection(uint32 section) const override;
@@ -288,6 +290,7 @@ public:
DB2RecordCopy GetRecordCopy(uint32 copyNumber) const override;
uint32 GetRecordCount() const override;
uint32 GetRecordCopyCount() const override;
uint32 GetMinId() const override;
uint32 GetMaxId() const override;
DB2FileLoadInfo const* GetLoadInfo() const override;
DB2SectionHeader& GetSection(uint32 section) const override;
@@ -597,11 +600,15 @@ char* DB2FileLoaderRegularImpl::AutoProduceStrings(char** indexTable, uint32 ind
offset += 8;
break;
case FT_STRING:
reinterpret_cast<LocalizedString*>(&recordData[offset])->Str[locale] = stringPool + (RecordGetString(rawRecord, x, z) - reinterpret_cast<char const*>(_stringTable));
if (char const* string = RecordGetString(rawRecord, x, z))
reinterpret_cast<LocalizedString*>(&recordData[offset])->Str[locale] = stringPool + (string - reinterpret_cast<char const*>(_stringTable));
offset += sizeof(LocalizedString);
break;
case FT_STRING_NOT_LOCALIZED:
*reinterpret_cast<char**>(&recordData[offset]) = stringPool + (RecordGetString(rawRecord, x, z) - reinterpret_cast<char const*>(_stringTable));
if (char const* string = RecordGetString(rawRecord, x, z))
*reinterpret_cast<char**>(&recordData[offset]) = stringPool + (string - reinterpret_cast<char const*>(_stringTable));
offset += sizeof(char*);
break;
default:
@@ -800,7 +807,7 @@ char const* DB2FileLoaderRegularImpl::RecordGetString(uint8 const* record, uint3
uint32 fieldOffset = GetFieldOffset(field) + sizeof(uint32) * arrayIndex;
uint32 stringOffset = RecordGetVarInt<uint32>(record, field, arrayIndex);
ASSERT(stringOffset < _header->RecordSize * _header->RecordCount + _header->StringTableSize);
return stringOffset ? reinterpret_cast<char const*>(record + fieldOffset + stringOffset) : EmptyDb2String;
return stringOffset ? reinterpret_cast<char const*>(record + fieldOffset + stringOffset) : nullptr;
}
template<typename T>
@@ -927,6 +934,34 @@ void DB2FileLoaderRegularImpl::RecordDestroyFieldOffsets(std::size_t*& /*fieldOf
{
}
uint32 DB2FileLoaderRegularImpl::GetMinId() const
{
uint32 minId = std::numeric_limits<uint32>::max();
for (uint32 row = 0; row < _header->RecordCount; ++row)
{
unsigned char const* rawRecord = GetRawRecordData(row, nullptr);
if (!rawRecord)
continue;
uint32 id = RecordGetId(rawRecord, row);
if (id < minId)
minId = id;
}
for (uint32 copy = 0; copy < GetRecordCopyCount(); ++copy)
{
uint32 id = GetRecordCopy(copy).NewRowId;
if (id < minId)
minId = id;
}
if (minId == std::numeric_limits<uint32>::max())
minId = 0;
ASSERT(minId >= _header->MinId);
return minId;
}
uint32 DB2FileLoaderRegularImpl::GetMaxId() const
{
uint32 maxId = 0;
@@ -1617,6 +1652,11 @@ void DB2FileLoaderSparseImpl::CalculateAndStoreFieldOffsets(uint8 const* rawReco
}
}
uint32 DB2FileLoaderSparseImpl::GetMinId() const
{
return _header->MinId;
}
uint32 DB2FileLoaderSparseImpl::GetMaxId() const
{
return _header->MaxId;
@@ -2076,6 +2116,11 @@ uint32 DB2FileLoader::GetRecordCopyCount() const
return _impl->GetRecordCopyCount();
}
uint32 DB2FileLoader::GetMinId() const
{
return _impl->GetMinId();
}
uint32 DB2FileLoader::GetMaxId() const
{
return _impl->GetMaxId();

View File

@@ -204,6 +204,7 @@ public:
uint32 GetRecordCopyCount() const;
uint32 GetTableHash() const { return _header.TableHash; }
uint32 GetLayoutHash() const { return _header.LayoutHash; }
uint32 GetMinId() const;
uint32 GetMaxId() const;
DB2Header const& GetHeader() const { return _header; }

View File

@@ -25,7 +25,7 @@
static char const* nullStr = "";
char* DB2DatabaseLoader::Load(bool custom, uint32& records, char**& indexTable, std::vector<char*>& stringPool)
char* DB2DatabaseLoader::Load(bool custom, uint32& records, char**& indexTable, std::vector<char*>& stringPool, uint32& minId)
{
// Even though this query is executed only once, prepared statement is used to send data from mysql server in binary format
HotfixDatabasePreparedStatement* stmt = HotfixDatabase.GetPreparedStatement(_loadInfo->Statement);
@@ -170,7 +170,11 @@ char* DB2DatabaseLoader::Load(bool custom, uint32& records, char**& indexTable,
// insert new records to index table
for (uint32 i = 0; i < newRecords; ++i)
indexTable[newIndexes[i]] = &dataTable[i * recordSize];
{
uint32 newId = newIndexes[i];
indexTable[newId] = &dataTable[i * recordSize];
minId = std::min(minId, newId);
}
delete[] tempDataTable;
delete[] newIndexes;

View File

@@ -37,7 +37,7 @@ class TC_SHARED_API DB2DatabaseLoader
public:
DB2DatabaseLoader(std::string const& storageName, DB2LoadInfo const* loadInfo) : _storageName(storageName), _loadInfo(loadInfo) { }
char* Load(bool custom, uint32& records, char**& indexTable, std::vector<char*>& stringPool);
char* Load(bool custom, uint32& records, char**& indexTable, std::vector<char*>& stringPool, uint32& minId);
void LoadStrings(bool custom, LocaleConstant locale, uint32 records, char** indexTable, std::vector<char*>& stringPool);
static char* AddString(char const** holder, std::string const& value);

View File

@@ -24,7 +24,7 @@
DB2StorageBase::DB2StorageBase(char const* fileName, DB2LoadInfo const* loadInfo)
: _tableHash(0), _layoutHash(0), _fileName(fileName), _fieldCount(0), _loadInfo(loadInfo), _dataTable(nullptr), _dataTableEx(),
_indexTable(nullptr), _indexTableSize(0)
_indexTable(nullptr), _indexTableSize(0), _minId(0)
{
}
@@ -95,6 +95,7 @@ void DB2StorageBase::Load(std::string const& path, LocaleConstant locale)
_fieldCount = db2.GetCols();
_tableHash = db2.GetTableHash();
_layoutHash = db2.GetLayoutHash();
_minId = db2.GetMinId();
// load raw non-string data
_dataTable = db2.AutoProduceData(_indexTableSize, _indexTable);
@@ -127,8 +128,8 @@ void DB2StorageBase::LoadFromDB()
{
DB2DatabaseLoader loader(_fileName, _loadInfo);
_dataTableEx[0] = loader.Load(false, _indexTableSize, _indexTable, _stringPool);
_dataTableEx[1] = loader.Load(true, _indexTableSize, _indexTable, _stringPool);
_dataTableEx[0] = loader.Load(false, _indexTableSize, _indexTable, _stringPool, _minId);
_dataTableEx[1] = loader.Load(true, _indexTableSize, _indexTable, _stringPool, _minId);
_stringPool.shrink_to_fit();
}

View File

@@ -65,6 +65,7 @@ protected:
std::vector<char*> _stringPool;
char** _indexTable;
uint32 _indexTableSize;
uint32 _minId;
friend class UnitTestDataLoader;
};
@@ -82,7 +83,7 @@ public:
T const* LookupEntry(uint32 id) const { return (id >= _indexTableSize) ? nullptr : reinterpret_cast<T const*>(_indexTable[id]); }
T const* AssertEntry(uint32 id) const { return ASSERT_NOTNULL(LookupEntry(id)); }
iterator begin() const { return iterator(reinterpret_cast<T const* const*>(_indexTable), _indexTableSize); }
iterator begin() const { return iterator(reinterpret_cast<T const* const*>(_indexTable), _indexTableSize, _minId); }
iterator end() const { return iterator(reinterpret_cast<T const* const*>(_indexTable), _indexTableSize, _indexTableSize); }
};