TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DB2Store.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
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 #ifndef DB2STORE_H
19 #define DB2STORE_H
20 
21 #include "Common.h"
22 #include "DB2StorageLoader.h"
23 #include "DBStorageIterator.h"
24 #include "ByteBuffer.h"
25 
28 {
29 public:
30  virtual ~DB2StorageBase() { }
31 
32  uint32 GetHash() const { return _tableHash; }
33 
34  virtual bool HasRecord(uint32 id) const = 0;
35 
36  virtual void WriteRecord(uint32 id, uint32 locale, ByteBuffer& buffer) const = 0;
37 
38  virtual void EraseRecord(uint32 id) = 0;
39 
40 protected:
42 };
43 
44 template<class T>
45 class DB2Storage : public DB2StorageBase
46 {
47  typedef std::list<char*> StringPoolList;
48 public:
50 
51  DB2Storage(char const* fileName, char const* format, HotfixDatabaseStatements preparedStmtIndex)
52  : _fileName(fileName), _indexTableSize(0), _fieldCount(0), _format(format), _dataTable(nullptr), _dataTableEx(nullptr), _hotfixStatement(preparedStmtIndex)
53  {
54  _indexTable.AsT = NULL;
55  }
56 
58  {
59  delete[] reinterpret_cast<char*>(_indexTable.AsT);
60  delete[] reinterpret_cast<char*>(_dataTable);
61  delete[] reinterpret_cast<char*>(_dataTableEx);
62  for (char* stringPool : _stringPoolList)
63  delete[] stringPool;
64  }
65 
66  bool HasRecord(uint32 id) const override { return id < _indexTableSize && _indexTable.AsT[id] != nullptr; }
67  void WriteRecord(uint32 id, uint32 locale, ByteBuffer& buffer) const override
68  {
69  ASSERT(id < _indexTableSize);
70  char const* entry = _indexTable.AsChar[id];
71  ASSERT(entry);
72 
73  std::size_t fields = strlen(_format);
74  for (uint32 i = 0; i < fields; ++i)
75  {
76  switch (_format[i])
77  {
78  case FT_IND:
79  case FT_INT:
80  buffer << *(uint32*)entry;
81  entry += 4;
82  break;
83  case FT_FLOAT:
84  buffer << *(float*)entry;
85  entry += 4;
86  break;
87  case FT_BYTE:
88  buffer << *(uint8*)entry;
89  entry += 1;
90  break;
91  case FT_STRING:
92  {
93  LocalizedString* locStr = *(LocalizedString**)entry;
94  if (locStr->Str[locale][0] == '\0')
95  locale = 0;
96 
97  char const* str = locStr->Str[locale];
98  std::size_t len = strlen(str);
99  buffer << uint16(len ? len + 1 : 0);
100  if (len)
101  {
102  buffer.append(str, len);
103  buffer << uint8(0);
104  }
105  entry += sizeof(LocalizedString*);
106  break;
107  }
109  {
110  char const* str = *(char const**)entry;
111  std::size_t len = strlen(str);
112  buffer << uint16(len ? len + 1 : 0);
113  if (len)
114  {
115  buffer.append(str, len);
116  buffer << uint8(0);
117  }
118  entry += sizeof(char const*);
119  break;
120  }
121  }
122  }
123  }
124 
125  void EraseRecord(uint32 id) override { if (id < _indexTableSize) _indexTable.AsT[id] = nullptr; }
126 
127  T const* LookupEntry(uint32 id) const { return (id >= _indexTableSize) ? nullptr : _indexTable.AsT[id]; }
128  T const* AssertEntry(uint32 id) const { return ASSERT_NOTNULL(LookupEntry(id)); }
129 
130  std::string const& GetFileName() const { return _fileName; }
131  uint32 GetNumRows() const { return _indexTableSize; }
132  char const* GetFormat() const { return _format; }
133  uint32 GetFieldCount() const { return _fieldCount; }
134  bool Load(std::string const& path, uint32 locale)
135  {
136  DB2FileLoader db2;
137  // Check if load was successful, only then continue
138  if (!db2.Load((path + _fileName).c_str(), _format))
139  return false;
140 
141  _fieldCount = db2.GetCols();
142  _tableHash = db2.GetHash();
143 
144  // load raw non-string data
145  _dataTable = reinterpret_cast<T*>(db2.AutoProduceData(_format, _indexTableSize, _indexTable.AsChar));
146 
147  // create string holders for loaded string fields
148  if (char* stringHolders = db2.AutoProduceStringsArrayHolders(_format, (char*)_dataTable))
149  {
150  _stringPoolList.push_back(stringHolders);
151 
152  // load strings from db2 data
153  if (char* stringBlock = db2.AutoProduceStrings(_format, (char*)_dataTable, locale))
154  _stringPoolList.push_back(stringBlock);
155  }
156 
157  // error in db2 file at loading if NULL
158  return _indexTable.AsT != NULL;
159  }
160 
161  bool LoadStringsFrom(std::string const& path, uint32 locale)
162  {
163  // DB2 must be already loaded using Load
164  if (!_indexTable.AsT)
165  return false;
166 
167  DB2FileLoader db2;
168  // Check if load was successful, only then continue
169  if (!db2.Load((path + _fileName).c_str(), _format))
170  return false;
171 
172  // load strings from another locale db2 data
174  if (char* stringBlock = db2.AutoProduceStrings(_format, (char*)_dataTable, locale))
175  _stringPoolList.push_back(stringBlock);
176  return true;
177  }
178 
179  void LoadFromDB()
180  {
181  char* extraStringHolders = nullptr;
182  if (char* dataTable = DB2DatabaseLoader(_fileName).Load(_format, _hotfixStatement, _indexTableSize, _indexTable.AsChar, extraStringHolders, _stringPoolList))
183  _dataTableEx = reinterpret_cast<T*>(dataTable);
184 
185  if (extraStringHolders)
186  _stringPoolList.push_back(extraStringHolders);
187  }
188 
190  {
192  return;
193 
195  }
196 
197  typedef bool(*SortFunc)(T const* left, T const* right);
198 
199  void Sort(SortFunc pred)
200  {
201  ASSERT(strpbrk(_format, "nd") == nullptr, "Only non-indexed storages can be sorted");
202  std::sort(_indexTable.AsT, _indexTable.AsT + _indexTableSize, pred);
203  }
204 
205  iterator begin() { return iterator(_indexTable.AsT, _indexTableSize); }
207 
208 private:
209  std::string _fileName;
212  char const* _format;
213  union
214  {
215  T** AsT;
216  char** AsChar;
217  } _indexTable;
220  StringPoolList _stringPoolList;
222 };
223 
224 #endif
void format(BasicFormatter< Char > &f, const Char *&format_str, const T &value)
Definition: format.h:2963
bool Load(const char *filename, const char *fmt)
Definition: DB2StorageLoader.cpp:44
HotfixDatabaseStatements
Definition: HotfixDatabase.h:24
T * _dataTable
Definition: DB2Store.h:218
T * ASSERT_NOTNULL(T *pointer)
Definition: Errors.h:58
Definition: ByteBuffer.h:70
std::list< char * > StringPoolList
Definition: DB2Store.h:47
Definition: Define.h:161
void EraseRecord(uint32 id) override
Definition: DB2Store.h:125
void WriteRecord(uint32 id, uint32 locale, ByteBuffer &buffer) const override
Definition: DB2Store.h:67
void Sort(SortFunc pred)
Definition: DB2Store.h:199
Definition: Common.h:146
Definition: Define.h:160
T ** AsT
Definition: DB2Store.h:215
uint32 GetHash() const
Definition: DB2StorageLoader.h:87
virtual ~DB2StorageBase()
Definition: DB2Store.h:30
uint32 _tableHash
Definition: DB2Store.h:41
arena_t NULL
Definition: jemalloc_internal.h:624
bool HasRecord(uint32 id) const override
Definition: DB2Store.h:66
char * AutoProduceStringsArrayHolders(const char *fmt, char *dataTable)
Definition: DB2StorageLoader.cpp:343
bool(* SortFunc)(T const *left, T const *right)
Definition: DB2Store.h:197
uint32 GetFieldCount() const
Definition: DB2Store.h:133
uint16_t uint16
Definition: g3dmath.h:166
char const * _format
Definition: DB2Store.h:212
#define bool
Definition: CascPort.h:16
std::string const & GetFileName() const
Definition: DB2Store.h:130
static uint32 GetFormatLocalizedStringFieldCount(const char *format)
Definition: DB2StorageLoader.cpp:255
Definition: DBStorageIterator.h:25
virtual bool HasRecord(uint32 id) const =0
char const * Str[TOTAL_LOCALES]
Definition: Common.h:148
bool Load(std::string const &path, uint32 locale)
Definition: DB2Store.h:134
virtual void EraseRecord(uint32 id)=0
Interface class for common access.
Definition: DB2Store.h:27
char * AutoProduceData(const char *fmt, uint32 &count, char **&indexTable)
Definition: DB2StorageLoader.cpp:265
T * _dataTableEx
Definition: DB2Store.h:219
HotfixDatabaseStatements _hotfixStatement
Definition: DB2Store.h:221
Definition: DB2StorageLoader.h:27
uint32 GetCols() const
Definition: DB2StorageLoader.h:85
Definition: Define.h:165
uint32_t uint32
Definition: Define.h:150
T const * AssertEntry(uint32 id) const
Definition: DB2Store.h:128
bool left(const int *a, const int *b, const int *c)
Definition: RecastContour.cpp:487
uint32 GetHash() const
Definition: DB2Store.h:32
char const * GetFormat() const
Definition: DB2Store.h:132
void append(T value)
Definition: ByteBuffer.h:143
Definition: Define.h:162
uint8_t uint8
Definition: g3dmath.h:164
DBStorageIterator< T > iterator
Definition: DB2Store.h:49
iterator begin()
Definition: DB2Store.h:205
Definition: DB2Store.h:45
bool LoadStringsFrom(std::string const &path, uint32 locale)
Definition: DB2Store.h:161
Definition: DB2StorageLoader.h:117
void LoadStringsFromDB(uint32 locale)
Definition: DB2Store.h:189
virtual void WriteRecord(uint32 id, uint32 locale, ByteBuffer &buffer) const =0
T const * LookupEntry(uint32 id) const
Definition: DB2Store.h:127
void LoadFromDB()
Definition: DB2Store.h:179
~DB2Storage()
Definition: DB2Store.h:57
DB2Storage(char const *fileName, char const *format, HotfixDatabaseStatements preparedStmtIndex)
Definition: DB2Store.h:51
char * AutoProduceStrings(const char *fmt, char *dataTable, uint32 locale)
Definition: DB2StorageLoader.cpp:412
Definition: Define.h:158
uint8_t uint8
Definition: Define.h:152
std::string _fileName
Definition: DB2Store.h:209
#define ASSERT
Definition: Errors.h:55
iterator end()
Definition: DB2Store.h:206
Definition: Define.h:159
StringPoolList _stringPoolList
Definition: DB2Store.h:220
uint32 GetNumRows() const
Definition: DB2Store.h:131
void LoadStrings(const char *format, HotfixDatabaseStatements preparedStatement, uint32 locale, char **&indexTable, std::list< char * > &stringPool)
Definition: DB2StorageLoader.cpp:643
uint32 _indexTableSize
Definition: DB2Store.h:210
union DB2Storage::@338 _indexTable
char ** AsChar
Definition: DB2Store.h:216
uint32 _fieldCount
Definition: DB2Store.h:211