TrinityCore
DB2Store.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "DB2Store.h"
19#include "ByteBuffer.h"
20#include "DB2DatabaseLoader.h"
21#include "DB2FileSystemSource.h"
22#include "DB2Meta.h"
23#include "StringFormat.h"
24
25DB2StorageBase::DB2StorageBase(char const* fileName, DB2LoadInfo const* loadInfo)
26 : _tableHash(0), _layoutHash(0), _fileName(fileName), _fieldCount(0), _loadInfo(loadInfo), _dataTable(nullptr), _dataTableEx(),
27 _indexTable(nullptr), _indexTableSize(0), _minId(0)
28{
29}
30
32{
33 delete[] _dataTable;
34 delete[] _dataTableEx[0];
35 delete[] _dataTableEx[1];
36 for (char* strings : _stringPool)
37 delete[] strings;
38 delete[] _indexTable;
39}
40
42{
44 char const* entry = ASSERT_NOTNULL(_indexTable[id]);
45
47 entry += 4;
48
49 for (uint32 i = 0; i < _loadInfo->Meta->FieldCount; ++i)
50 {
51 for (uint8 arr = 0; arr < _loadInfo->Meta->Fields[i].ArraySize; ++arr)
52 {
53 switch (_loadInfo->Meta->Fields[i].Type)
54 {
55 case FT_INT:
56 buffer << *reinterpret_cast<uint32 const*>(entry);
57 entry += 4;
58 break;
59 case FT_FLOAT:
60 buffer << *reinterpret_cast<float const*>(entry);
61 entry += 4;
62 break;
63 case FT_BYTE:
64 buffer << *reinterpret_cast<uint8 const*>(entry);
65 entry += 1;
66 break;
67 case FT_SHORT:
68 buffer << *reinterpret_cast<uint16 const*>(entry);
69 entry += 2;
70 break;
71 case FT_LONG:
72 buffer << *reinterpret_cast<uint64 const*>(entry);
73 entry += 8;
74 break;
75 case FT_STRING:
76 buffer << (*reinterpret_cast<LocalizedString const*>(entry))[locale];
77 entry += sizeof(LocalizedString);
78 break;
80 buffer << *reinterpret_cast<char const* const*>(entry);
81 entry += sizeof(char const*);
82 break;
83 }
84 }
85 }
86}
87
88void DB2StorageBase::Load(std::string const& path, LocaleConstant locale)
89{
90 DB2FileLoader db2;
91 DB2FileSystemSource source(path + _fileName);
92 // Check if load was successful, only then continue
93 db2.Load(&source, _loadInfo);
94
95 _fieldCount = db2.GetCols();
98 _minId = db2.GetMinId();
99
100 // load raw non-string data
102
103 // load strings from db2 data
104 if (char* stringBlock = db2.AutoProduceStrings(_indexTable, _indexTableSize, locale))
105 _stringPool.push_back(stringBlock);
106
108}
109
110void DB2StorageBase::LoadStringsFrom(std::string const& path, LocaleConstant locale)
111{
112 // DB2 must be already loaded using Load
113 if (!_indexTable)
114 throw DB2FileLoadException(Trinity::StringFormat("{} was not loaded properly, cannot load strings", path));
115
116 DB2FileLoader db2;
117 DB2FileSystemSource source(path + _fileName);
118 // Check if load was successful, only then continue
119 db2.Load(&source, _loadInfo);
120
121 // load strings from another locale db2 data
123 if (char* stringBlock = db2.AutoProduceStrings(_indexTable, _indexTableSize, locale))
124 _stringPool.push_back(stringBlock);
125}
126
128{
130
133 _stringPool.shrink_to_fit();
134}
135
137{
138 if (!_loadInfo->GetStringFieldCount(true))
139 return;
140
142 loader.LoadStrings(false, locale, _indexTableSize, _indexTable, _stringPool);
143 loader.LoadStrings(true, locale, _indexTableSize, _indexTable, _stringPool);
144 _stringPool.shrink_to_fit();
145}
LocaleConstant
Definition: Common.h:48
uint8_t uint8
Definition: Define.h:144
@ FT_FLOAT
Definition: Define.h:150
@ FT_SHORT
Definition: Define.h:153
@ FT_STRING
Definition: Define.h:148
@ FT_INT
Definition: Define.h:151
@ FT_STRING_NOT_LOCALIZED
Definition: Define.h:149
@ FT_BYTE
Definition: Define.h:152
@ FT_LONG
Definition: Define.h:154
uint32_t uint32
Definition: Define.h:142
#define ASSERT_NOTNULL(pointer)
Definition: Errors.h:84
#define ASSERT
Definition: Errors.h:68
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)
uint32 GetMinId() const
void Load(DB2FileSource *source, DB2FileLoadInfo const *loadInfo)
char * AutoProduceData(uint32 &indexTableSize, char **&indexTable)
char * AutoProduceStrings(char **indexTable, uint32 indexTableSize, LocaleConstant locale)
void AutoProduceRecordCopies(uint32 records, char **indexTable, char *dataTable)
uint32 GetTableHash() const
uint32 GetCols() const
uint32 GetLayoutHash() const
uint32 _minId
Definition: DB2Store.h:68
void WriteRecord(uint32 id, LocaleConstant locale, ByteBuffer &buffer) const
Definition: DB2Store.cpp:41
DB2LoadInfo const * _loadInfo
Definition: DB2Store.h:62
uint32 _layoutHash
Definition: DB2Store.h:59
char * _dataTableEx[2]
Definition: DB2Store.h:64
std::vector< char * > _stringPool
Definition: DB2Store.h:65
void LoadStringsFromDB(LocaleConstant locale)
Definition: DB2Store.cpp:136
uint32 _fieldCount
Definition: DB2Store.h:61
DB2StorageBase(char const *fileName, DB2LoadInfo const *loadInfo)
Definition: DB2Store.cpp:25
uint32 _tableHash
Definition: DB2Store.h:58
char * _dataTable
Definition: DB2Store.h:63
void LoadFromDB()
Definition: DB2Store.cpp:127
char ** _indexTable
Definition: DB2Store.h:66
void Load(std::string const &path, LocaleConstant locale)
Definition: DB2Store.cpp:88
void LoadStringsFrom(std::string const &path, LocaleConstant locale)
Definition: DB2Store.cpp:110
uint32 _indexTableSize
Definition: DB2Store.h:67
std::string _fileName
Definition: DB2Store.h:60
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
Definition: StringFormat.h:38
DB2Meta const * Meta
Definition: DB2FileLoader.h:91
uint32 GetStringFieldCount(bool localizedOnly) const
uint8 ArraySize
Definition: DB2Meta.h:26
DBCFormer Type
Definition: DB2Meta.h:25
DB2MetaField const * Fields
Definition: DB2Meta.h:60
bool HasIndexFieldInData() const
Definition: DB2Meta.cpp:22
uint32 FieldCount
Definition: DB2Meta.h:57