-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedPageReadWriter.cpp
More file actions
298 lines (255 loc) · 9.62 KB
/
CachedPageReadWriter.cpp
File metadata and controls
298 lines (255 loc) · 9.62 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "CachedPageReadWriter.h"
#include <string>
#include <cstring>
#include <algorithm>
#include <string>
#include <fcntl.h>
#include <unistd.h>
const char CachedPageReadWriter::LOG_ACTION_CHANGE[CachedPageReadWriter::LOG_ACTION_SIZE] = "CHANGE_";
const char CachedPageReadWriter::LOG_ACTION_DB_OPEN[CachedPageReadWriter::LOG_ACTION_SIZE] = "DB_OPEN";
const char CachedPageReadWriter::LOG_ACTION_DB_CLOSE[CachedPageReadWriter::LOG_ACTION_SIZE] = "DBCLOSE";
const char CachedPageReadWriter::LOG_ACTION_CHECKPOINT[CachedPageReadWriter::LOG_ACTION_SIZE] = "CHCKPNT";
const char CachedPageReadWriter::LOG_ACTION_INSERT[CachedPageReadWriter::LOG_ACTION_SIZE] = "INSERT_";
const char CachedPageReadWriter::LOG_ACTION_DELETE[CachedPageReadWriter::LOG_ACTION_SIZE] = "DELETE_";
const char CachedPageReadWriter::LOG_ACTION_COMMIT[CachedPageReadWriter::LOG_ACTION_SIZE] = "COMMIT_";
const char CachedPageReadWriter::LOG_SEEK_DELIM[CachedPageReadWriter::LOG_SEEK_DELIM_SIZE] = {'|'};
CachedPageReadWriter::CachedPageReadWriter(PageReadWriter *source, GlobalConfiguration *globConf)
: m_globConf(globConf)
, m_source(source)
, m_writesCounter(0)
, m_inOperation(false)
, m_pendingOperation(NONE)
, m_pendingKey(0, nullptr)
, m_pendingValue(0, nullptr)
{
if (m_globConf->cacheSize() % m_globConf->pageSize()) {
throw std::string("Page size should divide cache size.");
}
size_t cacheCells = m_globConf->cacheSize() / m_globConf->pageSize();
m_cache.assign(cacheCells, nullptr);
m_isDirty.assign(cacheCells, false);
for (size_t i = 0; i < cacheCells; i++) {
m_lruList.push_back(i);
}
// m_posInCache already empty
// m_pinnedCells already empty
bool noJournalBefore = access(globConf->journalPath(), F_OK) == -1;
m_logFd = open(globConf->journalPath(), O_RDWR | O_CREAT, 0666);
if (noJournalBefore) {
::write(m_logFd, LOG_ACTION_CHECKPOINT, LOG_ACTION_SIZE);
writeLogStumb(LOG_ACTION_SIZE);
} else {
size_t recordSize = LOG_ACTION_SIZE + sizeof(size_t) + m_globConf->pageSize();
lseek(m_logFd, 0, SEEK_END);
char recordType[LOG_ACTION_SIZE];
bool isLastOperationFinished = true;
bool hasSeenOperationEnd = false;
off_t lastOperationOffset;
do {
off_t curOffset = lseek(m_logFd, -static_cast<off_t>(recordSize), SEEK_CUR);
::read(m_logFd, recordType, LOG_ACTION_SIZE);
if (!strcmp(recordType, LOG_ACTION_COMMIT)) {
hasSeenOperationEnd = true;
}
if (!strcmp(recordType, LOG_ACTION_INSERT) || !strcmp(recordType, LOG_ACTION_DELETE)) {
if (!hasSeenOperationEnd) {
isLastOperationFinished = false;
lastOperationOffset = curOffset;
if (!strcmp(recordType, LOG_ACTION_INSERT)) {
m_pendingOperation = INSERT;
::read(m_logFd, &(m_pendingKey.size), sizeof(m_pendingKey.size));
m_pendingKey.data = new char[m_pendingKey.size];
::read(m_logFd, m_pendingKey.data, m_pendingKey.size);
::read(m_logFd, &(m_pendingValue.size), sizeof(m_pendingValue.size));
m_pendingValue.data = new char[m_pendingValue.size];
::read(m_logFd, m_pendingValue.data, m_pendingValue.size);
} else {
m_pendingOperation = DELETE;
::read(m_logFd, &(m_pendingKey.size), sizeof(m_pendingKey.size));
m_pendingKey.data = new char[m_pendingKey.size];
::read(m_logFd, m_pendingKey.data, m_pendingKey.size);
}
}
}
lseek(m_logFd, curOffset, SEEK_SET);
} while (strcmp(recordType, LOG_ACTION_CHECKPOINT));
// Now pointing begin of check point, lets skip it
lseek(m_logFd, recordSize, SEEK_CUR);
while (::read(m_logFd, recordType, LOG_ACTION_SIZE) == LOG_ACTION_SIZE) {
off_t curOffset = lseek(m_logFd, 0, SEEK_CUR);
if (!strcmp(recordType, LOG_ACTION_CHANGE)) {
size_t pageNumber;
::read(m_logFd, &pageNumber, sizeof(pageNumber));
Page p(pageNumber, m_globConf->pageSize());
::read(m_logFd, p.rawData(), m_globConf->pageSize());
m_source->write(p);
} else if (!isLastOperationFinished && !strcmp(recordType, LOG_ACTION_INSERT)
&& !strcmp(recordType, LOG_ACTION_DELETE) && curOffset == lastOperationOffset) {
// we don't need to restore anything below, lets delete it
ftruncate(m_logFd, curOffset);
break;
} else {
lseek(m_logFd, recordSize - LOG_ACTION_SIZE, SEEK_CUR); // skip this entry
}
}
}
lseek(m_logFd, 0, SEEK_END);
::write(m_logFd, LOG_ACTION_DB_OPEN, LOG_ACTION_SIZE);
writeLogStumb(LOG_ACTION_SIZE);
}
CachedPageReadWriter::~CachedPageReadWriter()
{
close();
}
void CachedPageReadWriter::startOperation(OpType type, const DatabaseNode::Record &key, const DatabaseNode::Record &value)
{
if (type == INSERT) {
// Assuming here that all of this is less than record size
::write(m_logFd, LOG_ACTION_INSERT, LOG_ACTION_SIZE);
::write(m_logFd, &(key.size), sizeof(key.size));
::write(m_logFd, key.data, key.size);
::write(m_logFd, &(value.size), sizeof(value.size));
::write(m_logFd, value.data, value.size);
writeLogStumb(LOG_ACTION_SIZE + sizeof(key.size) + key.size + sizeof(value.size) + value.size);
} else if (type == DELETE) {
::write(m_logFd, LOG_ACTION_DELETE, LOG_ACTION_SIZE);
::write(m_logFd, &(key.size), sizeof(key.size));
::write(m_logFd, key.data, key.size);
writeLogStumb(LOG_ACTION_SIZE + sizeof(key.size) + key.size);
} else {
throw std::string("Uknown operation type");
}
m_inOperation = true;
}
void CachedPageReadWriter::endOperation()
{
::write(m_logFd, LOG_ACTION_COMMIT, LOG_ACTION_SIZE);
writeLogStumb(LOG_ACTION_SIZE);
m_inOperation = false;
m_pinnedCells.clear();
}
size_t CachedPageReadWriter::allocatePageNumber()
{
return m_source->allocatePageNumber();
}
void CachedPageReadWriter::deallocatePageNumber(const size_t &number)
{
std::map<size_t, size_t>::iterator it = m_posInCache.find(number);
if (it != m_posInCache.end()) {
delete m_cache[it->second];
m_cache[it->second] = nullptr;
m_isDirty[it->second] = false;
m_lruList.erase(std::find(m_lruList.begin(), m_lruList.end(), it->second));
m_lruList.push_back(it->second); // pushing to oldest to use first
m_posInCache.erase(it);
}
m_source->deallocatePageNumber(number);
}
void CachedPageReadWriter::read(Page &page)
{
std::map<size_t, size_t>::iterator it = m_posInCache.find(page.number());
if (it == m_posInCache.end()) { // no page in cache
size_t freeCachePos = freeCachePosition(); // will do poping if needed
m_cache[freeCachePos] = new Page(page.number(), m_globConf->pageSize());
m_posInCache[page.number()] = freeCachePos;
m_isDirty[freeCachePos] = false;
m_source->read(*m_cache[freeCachePos]);
}
size_t cachePos = m_posInCache[page.number()];
memcpy(page.rawData(), m_cache[cachePos]->rawData(), m_globConf->pageSize());
m_lruList.erase(std::find(m_lruList.begin(), m_lruList.end(), cachePos));
m_lruList.push_front(cachePos);
}
void CachedPageReadWriter::write(const Page &page)
{
if (m_writesCounter == CHECKPOINT_THRESHOLD) {
m_writesCounter = 0;
flush();
} else {
m_writesCounter++;
}
::write(m_logFd, LOG_ACTION_CHANGE, LOG_ACTION_SIZE);
size_t pageNumber = page.number();
::write(m_logFd, &pageNumber, sizeof(pageNumber));
::write(m_logFd, page.rawData(), m_globConf->pageSize());
std::map<size_t, size_t>::iterator it = m_posInCache.find(page.number());
if (it == m_posInCache.end()) { // no page in cache
size_t freeCachePos = freeCachePosition(); // will do poping if needed
m_cache[freeCachePos] = new Page(page.number(), m_globConf->pageSize());
m_posInCache[page.number()] = freeCachePos;
}
size_t cachePos = m_posInCache[page.number()];
m_isDirty[cachePos] = true;
memcpy(m_cache[cachePos]->rawData(), page.rawData(), m_globConf->pageSize());
if (m_inOperation) {
m_pinnedCells.insert(cachePos);
}
m_lruList.erase(std::find(m_lruList.begin(), m_lruList.end(), cachePos));
m_lruList.push_front(cachePos);
}
void CachedPageReadWriter::close()
{
flush();
m_source->close();
::write(m_logFd, LOG_ACTION_DB_CLOSE, LOG_ACTION_SIZE);
writeLogStumb(LOG_ACTION_SIZE);
::close(m_logFd);
if (m_pendingKey.data) {
delete[] m_pendingKey.data;
}
if (m_pendingValue.data) {
delete[] m_pendingValue.data;
}
}
void CachedPageReadWriter::flushCacheCell(size_t cachePos)
{
if (m_isDirty[cachePos]) {
m_source->write(*m_cache[cachePos]);
m_isDirty[cachePos] = false;
}
}
void CachedPageReadWriter::flush()
{
for (const std::pair<size_t, size_t> &p : m_posInCache) {
flushCacheCell(p.second);
}
m_source->flush();
::write(m_logFd, LOG_ACTION_CHECKPOINT, LOG_ACTION_SIZE);
writeLogStumb(LOG_ACTION_SIZE);
}
size_t CachedPageReadWriter::freeCachePosition()
{
auto it = m_lruList.rbegin();
for (auto it = m_lruList.rbegin(); it != m_lruList.rend(); it++) {
size_t cachePos = *it;
if (m_pinnedCells.count(cachePos)) {
continue;
}
if (m_cache[cachePos] != nullptr) {
flushCacheCell(cachePos);
m_posInCache.erase(m_cache[cachePos]->number());
delete m_cache[cachePos];
m_cache[cachePos] = nullptr;
}
return cachePos;
}
throw std::string("Everything in cache is pinned. Nothing to throw out!");
}
void CachedPageReadWriter::writeLogStumb(size_t toSkip)
{
size_t recordSize = LOG_ACTION_SIZE + sizeof(size_t) + m_globConf->pageSize();
lseek(m_logFd, recordSize - toSkip - LOG_SEEK_DELIM_SIZE, SEEK_CUR); // Seek forward other fields
::write(m_logFd, LOG_SEEK_DELIM, LOG_SEEK_DELIM_SIZE);
}
CachedPageReadWriter::OpType CachedPageReadWriter::pendingOperation() const
{
return m_pendingOperation;
}
const DatabaseNode::Record &CachedPageReadWriter::pendingKey() const
{
return m_pendingKey;
}
const DatabaseNode::Record &CachedPageReadWriter::pendingValue() const
{
return m_pendingValue;
}