From ffc06b27d083c905788c12c21e26e03cfb15543b Mon Sep 17 00:00:00 2001 From: Shauren Date: Tue, 18 Apr 2023 11:37:12 +0200 Subject: Core/DataStores: Minor db2 loader cleanup --- src/common/DataStores/DB2FileLoader.cpp | 121 ++++++++++++++++++++------------ 1 file changed, 78 insertions(+), 43 deletions(-) (limited to 'src/common/DataStores/DB2FileLoader.cpp') diff --git a/src/common/DataStores/DB2FileLoader.cpp b/src/common/DataStores/DB2FileLoader.cpp index 1254b98992b..2c472e66dd6 100644 --- a/src/common/DataStores/DB2FileLoader.cpp +++ b/src/common/DataStores/DB2FileLoader.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include enum class DB2ColumnCompression : uint32 @@ -146,14 +147,18 @@ int32 DB2FileLoadInfo::GetFieldIndexByMetaIndex(uint32 metaIndex) const return ourIndex; } -DB2FileSource::~DB2FileSource() -{ -} +DB2FileSource::DB2FileSource() = default; +DB2FileSource::~DB2FileSource() = default; class DB2FileLoaderImpl { public: - virtual ~DB2FileLoaderImpl() { } + DB2FileLoaderImpl() = default; + DB2FileLoaderImpl(DB2FileLoaderImpl const& other) = delete; + DB2FileLoaderImpl(DB2FileLoaderImpl&& other) noexcept = delete; + DB2FileLoaderImpl& operator=(DB2FileLoaderImpl const& other) = delete; + DB2FileLoaderImpl& operator=(DB2FileLoaderImpl&& other) noexcept = delete; + virtual ~DB2FileLoaderImpl() = default; virtual void LoadColumnData(std::unique_ptr sections, std::unique_ptr fields, std::unique_ptr columnMeta, std::unique_ptr[]> palletValues, std::unique_ptr[]> palletArrayValues, std::unique_ptr[]> commonValues) = 0; @@ -186,6 +191,7 @@ private: virtual float RecordGetFloat(uint8 const* record, uint32 field, uint32 arrayIndex) const = 0; virtual char const* RecordGetString(uint8 const* record, uint32 field, uint32 arrayIndex) const = 0; virtual std::size_t* RecordCreateDetachedFieldOffsets(std::size_t* oldOffsets) const = 0; + virtual std::size_t* RecordCopyDetachedFieldOffsets(std::size_t* oldOffsets) const = 0; virtual void RecordDestroyFieldOffsets(std::size_t*& fieldOffsets) const = 0; }; @@ -193,6 +199,10 @@ class DB2FileLoaderRegularImpl final : public DB2FileLoaderImpl { public: DB2FileLoaderRegularImpl(char const* fileName, DB2FileLoadInfo const* loadInfo, DB2Header const* header); + DB2FileLoaderRegularImpl(DB2FileLoaderRegularImpl const& other) = delete; + DB2FileLoaderRegularImpl(DB2FileLoaderRegularImpl&& other) noexcept = delete; + DB2FileLoaderRegularImpl& operator=(DB2FileLoaderRegularImpl const& other) = delete; + DB2FileLoaderRegularImpl& operator=(DB2FileLoaderRegularImpl&& other) noexcept = delete; ~DB2FileLoaderRegularImpl(); void LoadColumnData(std::unique_ptr sections, std::unique_ptr fields, std::unique_ptr columnMeta, @@ -232,6 +242,7 @@ private: uint64 RecordGetPackedValue(uint8 const* packedRecordData, uint32 bitWidth, uint32 bitOffset) const; uint16 GetFieldOffset(uint32 field) const; std::size_t* RecordCreateDetachedFieldOffsets(std::size_t* oldOffsets) const override; + std::size_t* RecordCopyDetachedFieldOffsets(std::size_t* oldOffsets) const override; void RecordDestroyFieldOffsets(std::size_t*& fieldOffsets) const override; char const* _fileName; @@ -253,6 +264,10 @@ class DB2FileLoaderSparseImpl final : public DB2FileLoaderImpl { public: DB2FileLoaderSparseImpl(char const* fileName, DB2FileLoadInfo const* loadInfo, DB2Header const* header, DB2FileSource* source); + DB2FileLoaderSparseImpl(DB2FileLoaderSparseImpl const& other) = delete; + DB2FileLoaderSparseImpl(DB2FileLoaderSparseImpl&& other) noexcept = delete; + DB2FileLoaderSparseImpl& operator=(DB2FileLoaderSparseImpl const& other) = delete; + DB2FileLoaderSparseImpl& operator=(DB2FileLoaderSparseImpl&& other) noexcept = delete; ~DB2FileLoaderSparseImpl(); void LoadColumnData(std::unique_ptr sections, std::unique_ptr fields, std::unique_ptr columnMeta, @@ -291,12 +306,10 @@ private: uint16 GetFieldOffset(uint32 field, uint32 arrayIndex) const; uint16 GetFieldSize(uint32 field) const; std::size_t* RecordCreateDetachedFieldOffsets(std::size_t* oldOffsets) const override; + std::size_t* RecordCopyDetachedFieldOffsets(std::size_t* oldOffsets) const override; void RecordDestroyFieldOffsets(std::size_t*& fieldOffsets) const override; void CalculateAndStoreFieldOffsets(uint8 const* rawRecord) const; -#pragma pack(push, 1) -#pragma pack(pop) - char const* _fileName; DB2FileLoadInfo const* _loadInfo; DB2Header const* _header; @@ -364,11 +377,9 @@ void DB2FileLoaderRegularImpl::SetAdditionalData(std::vector idTable, st _parentIndexes = std::move(parentIndexes); } -DB2FileLoaderRegularImpl::~DB2FileLoaderRegularImpl() -{ -} +DB2FileLoaderRegularImpl::~DB2FileLoaderRegularImpl() = default; -static char const* const nullStr = ""; +static char const* const EmptyDb2String = ""; char* DB2FileLoaderRegularImpl::AutoProduceData(uint32& indexTableSize, char**& indexTable) { @@ -411,7 +422,7 @@ char* DB2FileLoaderRegularImpl::AutoProduceData(uint32& indexTableSize, char**& uint32 fieldIndex = 0; if (!_loadInfo->Meta->HasIndexFieldInData()) { - *((uint32*)(&dataTable[offset])) = indexVal; + *reinterpret_cast(&dataTable[offset]) = indexVal; offset += 4; ++fieldIndex; } @@ -423,33 +434,33 @@ char* DB2FileLoaderRegularImpl::AutoProduceData(uint32& indexTableSize, char**& switch (_loadInfo->Fields[fieldIndex].Type) { case FT_FLOAT: - *((float*)(&dataTable[offset])) = RecordGetFloat(rawRecord, x, z); + *reinterpret_cast(&dataTable[offset]) = RecordGetFloat(rawRecord, x, z); offset += 4; break; case FT_INT: - *((uint32*)(&dataTable[offset])) = RecordGetVarInt(rawRecord, x, z); + *reinterpret_cast(&dataTable[offset]) = RecordGetVarInt(rawRecord, x, z); offset += 4; break; case FT_BYTE: - *((uint8*)(&dataTable[offset])) = RecordGetUInt8(rawRecord, x, z); + *reinterpret_cast(&dataTable[offset]) = RecordGetUInt8(rawRecord, x, z); offset += 1; break; case FT_SHORT: - *((uint16*)(&dataTable[offset])) = RecordGetUInt16(rawRecord, x, z); + *reinterpret_cast(&dataTable[offset]) = RecordGetUInt16(rawRecord, x, z); offset += 2; break; case FT_LONG: - *((uint64*)(&dataTable[offset])) = RecordGetUInt64(rawRecord, x, z); + *reinterpret_cast(&dataTable[offset]) = RecordGetUInt64(rawRecord, x, z); offset += 8; break; case FT_STRING: - for (char const*& localeStr : ((LocalizedString*)(&dataTable[offset]))->Str) - localeStr = nullStr; + for (char const*& localeStr : reinterpret_cast(&dataTable[offset])->Str) + localeStr = EmptyDb2String; offset += sizeof(LocalizedString); break; case FT_STRING_NOT_LOCALIZED: - *(char const**)(&dataTable[offset]) = nullStr; + *reinterpret_cast(&dataTable[offset]) = EmptyDb2String; offset += sizeof(char*); break; default: @@ -468,15 +479,15 @@ char* DB2FileLoaderRegularImpl::AutoProduceData(uint32& indexTableSize, char**& switch (_loadInfo->Fields[fieldIndex].Type) { case FT_INT: - *((uint32*)(&dataTable[offset])) = 0; + *reinterpret_cast(&dataTable[offset]) = 0; offset += 4; break; case FT_BYTE: - *((uint8*)(&dataTable[offset])) = 0; + *reinterpret_cast(&dataTable[offset]) = 0; offset += 1; break; case FT_SHORT: - *((uint16*)(&dataTable[offset])) = 0; + *reinterpret_cast(&dataTable[offset]) = 0; offset += 2; break; default: @@ -621,7 +632,7 @@ void DB2FileLoaderRegularImpl::AutoProduceRecordCopies(uint32 records, char** in { indexTable[copy.NewRowId] = &dataTable[offset]; memcpy(indexTable[copy.NewRowId], indexTable[copy.SourceRowId], recordsize); - *((uint32*)(&dataTable[offset + idFieldOffset])) = copy.NewRowId; + *reinterpret_cast(&dataTable[offset + idFieldOffset]) = copy.NewRowId; offset += recordsize; } } @@ -896,6 +907,11 @@ std::size_t* DB2FileLoaderRegularImpl::RecordCreateDetachedFieldOffsets(std::siz return nullptr; } +std::size_t* DB2FileLoaderRegularImpl::RecordCopyDetachedFieldOffsets(std::size_t* /*oldOffsets*/) const +{ + return nullptr; +} + void DB2FileLoaderRegularImpl::RecordDestroyFieldOffsets(std::size_t*& /*fieldOffsets*/) const { } @@ -1008,9 +1024,7 @@ DB2FileLoaderSparseImpl::DB2FileLoaderSparseImpl(char const* fileName, DB2FileLo { } -DB2FileLoaderSparseImpl::~DB2FileLoaderSparseImpl() -{ -} +DB2FileLoaderSparseImpl::~DB2FileLoaderSparseImpl() = default; void DB2FileLoaderSparseImpl::LoadColumnData(std::unique_ptr sections, std::unique_ptr fields, std::unique_ptr /*columnMeta*/, std::unique_ptr[]> /*palletValues*/, std::unique_ptr[]> /*palletArrayValues*/, @@ -1105,7 +1119,7 @@ char* DB2FileLoaderSparseImpl::AutoProduceData(uint32& indexTableSize, char**& i uint32 fieldIndex = 0; if (!_loadInfo->Meta->HasIndexFieldInData()) { - *((uint32*)(&dataTable[offset])) = indexVal; + *reinterpret_cast(&dataTable[offset]) = indexVal; offset += 4; ++fieldIndex; } @@ -1117,33 +1131,33 @@ char* DB2FileLoaderSparseImpl::AutoProduceData(uint32& indexTableSize, char**& i switch (_loadInfo->Fields[fieldIndex].Type) { case FT_FLOAT: - *((float*)(&dataTable[offset])) = RecordGetFloat(rawRecord, x, z); + *reinterpret_cast(&dataTable[offset]) = RecordGetFloat(rawRecord, x, z); offset += 4; break; case FT_INT: - *((uint32*)(&dataTable[offset])) = RecordGetVarInt(rawRecord, x, z, _loadInfo->Fields[fieldIndex].IsSigned); + *reinterpret_cast(&dataTable[offset]) = RecordGetVarInt(rawRecord, x, z, _loadInfo->Fields[fieldIndex].IsSigned); offset += 4; break; case FT_BYTE: - *((uint8*)(&dataTable[offset])) = RecordGetUInt8(rawRecord, x, z); + *reinterpret_cast(&dataTable[offset]) = RecordGetUInt8(rawRecord, x, z); offset += 1; break; case FT_SHORT: - *((uint16*)(&dataTable[offset])) = RecordGetUInt16(rawRecord, x, z); + *reinterpret_cast(&dataTable[offset]) = RecordGetUInt16(rawRecord, x, z); offset += 2; break; case FT_LONG: - *((uint64*)(&dataTable[offset])) = RecordGetUInt64(rawRecord, x, z); + *reinterpret_cast(&dataTable[offset]) = RecordGetUInt64(rawRecord, x, z); offset += 8; break; case FT_STRING: - for (char const*& localeStr : ((LocalizedString*)(&dataTable[offset]))->Str) - localeStr = nullStr; + for (char const*& localeStr : reinterpret_cast(&dataTable[offset])->Str) + localeStr = EmptyDb2String; offset += sizeof(LocalizedString); break; case FT_STRING_NOT_LOCALIZED: - *(char const**)(&dataTable[offset]) = nullStr; + *reinterpret_cast(&dataTable[offset]) = EmptyDb2String; offset += sizeof(char*); break; default: @@ -1162,15 +1176,15 @@ char* DB2FileLoaderSparseImpl::AutoProduceData(uint32& indexTableSize, char**& i switch (_loadInfo->Fields[fieldIndex].Type) { case FT_INT: - *((uint32*)(&dataTable[offset])) = 0; + *reinterpret_cast(&dataTable[offset]) = 0; offset += 4; break; case FT_BYTE: - *((uint8*)(&dataTable[offset])) = 0; + *reinterpret_cast(&dataTable[offset]) = 0; offset += 1; break; case FT_SHORT: - *((uint16*)(&dataTable[offset])) = 0; + *reinterpret_cast(&dataTable[offset]) = 0; offset += 2; break; default: @@ -1281,7 +1295,7 @@ char* DB2FileLoaderSparseImpl::AutoProduceStrings(char** indexTable, uint32 inde break; case FT_STRING: { - LocalizedString* db2str = (LocalizedString*)(&recordData[offset]); + LocalizedString* db2str = reinterpret_cast(&recordData[offset]); db2str->Str[locale] = stringPtr; strcpy(stringPtr, RecordGetString(rawRecord, x, z)); stringPtr += strlen(stringPtr) + 1; @@ -1290,7 +1304,7 @@ char* DB2FileLoaderSparseImpl::AutoProduceStrings(char** indexTable, uint32 inde } case FT_STRING_NOT_LOCALIZED: { - char const** db2str = (char const**)(&recordData[offset]); + char const** db2str = reinterpret_cast(&recordData[offset]); *db2str = stringPtr; strcpy(stringPtr, RecordGetString(rawRecord, x, z)); stringPtr += strlen(stringPtr) + 1; @@ -1327,7 +1341,7 @@ void DB2FileLoaderSparseImpl::AutoProduceRecordCopies(uint32 records, char** ind { indexTable[copy.NewRowId] = &dataTable[offset]; memcpy(indexTable[copy.NewRowId], indexTable[copy.SourceRowId], recordsize); - *((uint32*)(&dataTable[offset + idFieldOffset])) = copy.NewRowId; + *reinterpret_cast(&dataTable[offset + idFieldOffset]) = copy.NewRowId; offset += recordsize; } } @@ -1538,6 +1552,17 @@ std::size_t* DB2FileLoaderSparseImpl::RecordCreateDetachedFieldOffsets(std::size return newOffsets; } +std::size_t* DB2FileLoaderSparseImpl::RecordCopyDetachedFieldOffsets(std::size_t* oldOffsets) const +{ + if (oldOffsets == _fieldAndArrayOffsets.get()) + return oldOffsets; + + std::size_t size = _loadInfo->Meta->FieldCount + _loadInfo->FieldCount - (!_loadInfo->Meta->HasIndexFieldInData() ? 1 : 0); + std::size_t* newOffsets = new std::size_t[size]; + memcpy(newOffsets, _fieldAndArrayOffsets.get(), size * sizeof(std::size_t)); + return newOffsets; +} + void DB2FileLoaderSparseImpl::RecordDestroyFieldOffsets(std::size_t*& fieldOffsets) const { if (fieldOffsets == _fieldAndArrayOffsets.get()) @@ -1617,12 +1642,22 @@ DB2Record::DB2Record(DB2FileLoaderImpl const& db2, uint32 recordIndex, std::size { } +DB2Record::DB2Record(DB2Record const& other) + : _db2(other._db2), _recordIndex(other._recordIndex), _recordData(other._recordData), _fieldOffsets(_db2.RecordCopyDetachedFieldOffsets(other._fieldOffsets)) +{ +} + +DB2Record::DB2Record(DB2Record&& other) noexcept + : _db2(other._db2), _recordIndex(other._recordIndex), _recordData(other._recordData), _fieldOffsets(std::exchange(other._fieldOffsets, nullptr)) +{ +} + DB2Record::~DB2Record() { _db2.RecordDestroyFieldOffsets(_fieldOffsets); } -DB2Record::operator bool() +DB2Record::operator bool() const { return _recordData != nullptr; } -- cgit v1.2.3