-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalConfiguration.cpp
More file actions
233 lines (207 loc) · 6.28 KB
/
GlobalConfiguration.cpp
File metadata and controls
233 lines (207 loc) · 6.28 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "GlobalConfiguration.h"
#include "Page.h"
#include <cstring>
#include <string>
#include <unistd.h>
const char GlobalConfiguration::MAGIC[GlobalConfiguration::MAGIC_SIZE] = "MYDB";
GlobalConfiguration::GlobalConfiguration(
const size_t &desiredPageCount,
const size_t &desiredPageSize,
const size_t &desiredRootNodePageNumber,
const size_t &desiredCacheSize,
const char *journalPath)
: m_isInitialized(false)
, m_pageCount(desiredPageCount)
, m_pageSize(desiredPageSize)
, m_rootNodePageNumber(desiredRootNodePageNumber)
, m_cacheSize(desiredCacheSize)
, m_journalPath(strdup(journalPath))
, m_isReadedFromFile(false)
{
}
GlobalConfiguration::~GlobalConfiguration()
{
free(m_journalPath);
}
void GlobalConfiguration::initialize(
const size_t &pageCount,
const size_t &pageSize,
const size_t &rootNodePageNumber,
const size_t &cacheSize,
const char *journalPath)
{
if (m_isInitialized) {
throw std::string("GlobalConfiguration is already initialized");
}
m_isInitialized = true;
m_pageCount = pageCount;
m_pageSize = pageSize;
m_rootNodePageNumber = rootNodePageNumber;
m_cacheSize = cacheSize;
char *oldMem = m_journalPath;
m_journalPath = strdup(journalPath);
if (oldMem) {
free(oldMem);
}
}
size_t GlobalConfiguration::desiredPageCount() const
{
if (m_isInitialized) {
throw std::string("GlobalConfiguration is already initialized");
}
return m_pageCount;
}
size_t GlobalConfiguration::desiredPageSize() const
{
if (m_isInitialized) {
throw std::string("GlobalConfiguration is already initialized");
}
return m_pageSize;
}
size_t GlobalConfiguration::desiredDatabaseSize() const
{
if (m_isInitialized) {
throw std::string("GlobalConfiguration is already initialized");
}
return m_pageCount * m_pageSize;
}
size_t GlobalConfiguration::desiredRootNodePageNumber() const
{
if (m_isInitialized) {
throw std::string("GlobalConfiguration is already initialized");
}
return m_rootNodePageNumber;
}
size_t GlobalConfiguration::desiredCacheSize() const
{
if (m_isInitialized) {
throw std::string("GlobalConfiguration is already initialized");
}
return m_cacheSize;
}
char *GlobalConfiguration::desiredJournalPath() const
{
if (m_isInitialized) {
throw std::string("GlobalConfiguration is already initialized");
}
return m_journalPath;
}
size_t GlobalConfiguration::pageCount() const
{
if (!m_isInitialized) {
throw std::string("GlobalConfiguration isn't initialized");
}
return m_pageCount;
}
size_t GlobalConfiguration::pageSize() const
{
if (!m_isInitialized) {
throw std::string("GlobalConfiguration isn't initialized");
}
return m_pageSize;
}
size_t GlobalConfiguration::databaseSize() const
{
if (!m_isInitialized) {
throw std::string("GlobalConfiguration isn't initialized");
}
return m_pageCount * m_pageSize;
}
size_t GlobalConfiguration::rootNodePageNumber() const
{
if (!m_isInitialized) {
throw std::string("GlobalConfiguration isn't initialized");
}
return m_rootNodePageNumber;
}
size_t GlobalConfiguration::cacheSize() const
{
if (!m_isInitialized) {
throw std::string("GlobalConfiguration isn't initialized");
}
return m_cacheSize;
}
char *GlobalConfiguration::journalPath() const
{
if (!m_isInitialized) {
throw std::string("GlobalConfiguration isn't initialized");
}
return m_journalPath;
}
bool GlobalConfiguration::isReadedFromFile() const
{
if (!m_isInitialized) {
throw std::string("GlobalConfiguration isn't initialized");
}
return m_isReadedFromFile;
}
void GlobalConfiguration::readFromFile(int fd)
{
if (m_isInitialized) {
throw std::string("GlobalConfiguration is already initialized");
}
m_isInitialized = true;
m_isReadedFromFile = true;
char magic[MAGIC_SIZE];
if (read(fd, magic, MAGIC_SIZE) != MAGIC_SIZE) {
throw std::string("Error reading global configuration");
}
if (strcmp(magic, MAGIC)) {
throw std::string("Invalid magic in database file");
}
if (read(fd, &m_pageCount, sizeof(m_pageCount)) != sizeof(m_pageCount)) {
throw std::string("Error reading global configuration");
}
if (read(fd, &m_pageSize, sizeof(m_pageSize)) != sizeof(m_pageSize)) {
throw std::string("Error reading global configuration");
}
if (read(fd, &m_rootNodePageNumber, sizeof(m_rootNodePageNumber)) != sizeof(m_rootNodePageNumber)) {
throw std::string("Error reading global configuration");
}
if (read(fd, &m_cacheSize, sizeof(m_cacheSize)) != sizeof(m_cacheSize)) {
throw std::string("Error reading global configuration");
}
size_t journalPathSize;
if (read(fd, &journalPathSize, sizeof(journalPathSize)) != sizeof(journalPathSize)) {
throw std::string("Error reading global configuration");
}
free(m_journalPath);
m_journalPath = static_cast<char *>(malloc(journalPathSize));
if (read(fd, m_journalPath, journalPathSize) != journalPathSize) {
throw std::string("Error reading global configuration");
}
}
void GlobalConfiguration::setRootNodePageNumber(const size_t &newRootNodePageNumber)
{
m_rootNodePageNumber = newRootNodePageNumber;
}
void GlobalConfiguration::skipDataOnPage(Page &page) const
{
if (!m_isInitialized) {
throw std::string("GlobalConfiguration isn't initialized");
}
size_t totalSeek = 0;
totalSeek += MAGIC_SIZE;
totalSeek += sizeof(m_pageCount);
totalSeek += sizeof(m_pageSize);
totalSeek += sizeof(m_rootNodePageNumber);
totalSeek += sizeof(m_cacheSize);
totalSeek += sizeof(size_t); // journal path size
totalSeek += (strlen(m_journalPath) + 1) * sizeof(*m_journalPath);
page.seekForward(totalSeek);
}
void GlobalConfiguration::writeToPage(Page &page) const
{
if (!m_isInitialized) {
throw std::string("GlobalConfiguration is already initialized");
}
page.seek(0);
page.write(MAGIC, MAGIC_SIZE);
page.write(&m_pageCount, sizeof(m_pageCount));
page.write(&m_pageSize, sizeof(m_pageSize));
page.write(&m_rootNodePageNumber, sizeof(m_rootNodePageNumber));
page.write(&m_cacheSize, sizeof(m_cacheSize));
size_t journalPathSize = (strlen(m_journalPath) + 1) * sizeof(*m_journalPath);
page.write(&journalPathSize, sizeof(journalPathSize));
page.write(m_journalPath, journalPathSize);
}