aboutsummaryrefslogtreecommitdiff
path: root/src/server/shared/DataStores/DB2Store.cpp
blob: 79fbfe4a9db3e0a0bceaa58d21a2bb5892ff3aae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "DB2Store.h"
#include "ByteBuffer.h"
#include "DB2DatabaseLoader.h"
#include "DB2FileSystemSource.h"
#include "DB2Meta.h"
#include "StringFormat.h"

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), _minId(0)
{
}

DB2StorageBase::~DB2StorageBase()
{
    delete[] _dataTable;
    delete[] _dataTableEx[0];
    delete[] _dataTableEx[1];
    for (char* strings : _stringPool)
        delete[] strings;
    delete[] _indexTable;
}

void DB2StorageBase::WriteRecord(uint32 id, LocaleConstant locale, ByteBuffer& buffer) const
{
    ASSERT(id < _indexTableSize);
    char const* entry = ASSERT_NOTNULL(_indexTable[id]);

    if (!_loadInfo->Meta->HasIndexFieldInData())
        entry += 4;

    for (uint32 i = 0; i < _loadInfo->Meta->FieldCount; ++i)
    {
        for (uint8 arr = 0; arr < _loadInfo->Meta->Fields[i].ArraySize; ++arr)
        {
            switch (_loadInfo->Meta->Fields[i].Type)
            {
                case FT_INT:
                    buffer << *reinterpret_cast<uint32 const*>(entry);
                    entry += 4;
                    break;
                case FT_FLOAT:
                    buffer << *reinterpret_cast<float const*>(entry);
                    entry += 4;
                    break;
                case FT_BYTE:
                    buffer << *reinterpret_cast<uint8 const*>(entry);
                    entry += 1;
                    break;
                case FT_SHORT:
                    buffer << *reinterpret_cast<uint16 const*>(entry);
                    entry += 2;
                    break;
                case FT_LONG:
                    buffer << *reinterpret_cast<uint64 const*>(entry);
                    entry += 8;
                    break;
                case FT_STRING:
                    buffer << (*reinterpret_cast<LocalizedString const*>(entry))[locale];
                    entry += sizeof(LocalizedString);
                    break;
                case FT_STRING_NOT_LOCALIZED:
                    buffer << *reinterpret_cast<char const* const*>(entry);
                    entry += sizeof(char const*);
                    break;
            }
        }
    }
}

void DB2StorageBase::Load(std::string const& path, LocaleConstant locale)
{
    DB2FileLoader db2;
    DB2FileSystemSource source(path + _fileName);
    // Check if load was successful, only then continue
    db2.Load(&source, _loadInfo);

    _fieldCount = db2.GetCols();
    _tableHash = db2.GetTableHash();
    _layoutHash = db2.GetLayoutHash();
    _minId = db2.GetMinId();

    // load raw non-string data
    _dataTable = db2.AutoProduceData(_indexTableSize, _indexTable);

    // load strings from db2 data
    if (char* stringBlock = db2.AutoProduceStrings(_indexTable, _indexTableSize, locale))
        _stringPool.push_back(stringBlock);

    db2.AutoProduceRecordCopies(_indexTableSize, _indexTable, _dataTable);
}

void DB2StorageBase::LoadStringsFrom(std::string const& path, LocaleConstant locale)
{
    // DB2 must be already loaded using Load
    if (!_indexTable)
        throw DB2FileLoadException(Trinity::StringFormat("{} was not loaded properly, cannot load strings", path));

    DB2FileLoader db2;
    DB2FileSystemSource source(path + _fileName);
    // Check if load was successful, only then continue
    db2.Load(&source, _loadInfo);

    // load strings from another locale db2 data
    if (_loadInfo->GetStringFieldCount(true))
        if (char* stringBlock = db2.AutoProduceStrings(_indexTable, _indexTableSize, locale))
            _stringPool.push_back(stringBlock);
}

void DB2StorageBase::LoadFromDB()
{
    DB2DatabaseLoader loader(_fileName, _loadInfo);

    _dataTableEx[0] = loader.Load(false, _indexTableSize, _indexTable, _stringPool, _minId);
    _dataTableEx[1] = loader.Load(true, _indexTableSize, _indexTable, _stringPool, _minId);
    _stringPool.shrink_to_fit();
}

void DB2StorageBase::LoadStringsFromDB(LocaleConstant locale)
{
    if (!_loadInfo->GetStringFieldCount(true))
        return;

    DB2DatabaseLoader loader(_fileName, _loadInfo);
    loader.LoadStrings(false, locale, _indexTableSize, _indexTable, _stringPool);
    loader.LoadStrings(true, locale, _indexTableSize, _indexTable, _stringPool);
    _stringPool.shrink_to_fit();
}