/
BufferManager.cpp
555 lines (524 loc) · 14 KB
/
BufferManager.cpp
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//
// BufferManager.cpp
// Minisql
//
// Created by xuyuhao on 14/11/1.
// Copyright (c) 2014年 xuyuhao. All rights reserved.
//
#include "BufferManager.h"
#include "Minisql.h"
#include <stdlib.h>
#include <string>
#include <cstring>
#include <queue>
/**
* Constructor Function: allocate memories for the pools
* and init the variable values
*
*/
BufferManager::BufferManager():total_block(0),total_file(0),fileHead(NULL)
{
for (int i = 0; i < MAX_FILE_NUM; i ++)
{
file_pool[i].fileName = new char[MAX_FILE_NAME];
if(file_pool[i].fileName == NULL)
{
printf("Can not allocate memory in initing the file pool!\n");
exit (1);
}
init_file(file_pool[i]);
}
for (int i = 0; i < MAX_BLOCK_NUM; i ++) {
block_pool[i].address = new char[BLOCK_SIZE];
if(block_pool[i].address == NULL)
{
printf("Can not allocate memory in initing the block pool!\n");
exit (1);
}
block_pool[i].fileName = new char[MAX_FILE_NAME];
if(block_pool[i].fileName == NULL)
{
printf("Can not allocate memory in initing the block pool!\n");
exit (1);
}
init_block(block_pool[i]);
}
}
/**
* Destructor Function: free memories of the pools
* and write back the buffer content if required
*
*/
BufferManager::~BufferManager()
{
writtenBackToDiskAll();
for (int i = 0; i < MAX_FILE_NUM; i ++)
{
delete [] file_pool[i].fileName;
}
for (int i = 0; i < MAX_BLOCK_NUM; i ++)
{
delete [] block_pool[i].address;
}
}
/**
* init the fileNode values
*
* @param fileNode& the file your want to init
*
* @return void
*
*/
void BufferManager::init_file(fileNode &file)
{
file.nextFile = NULL;
file.preFile = NULL;
file.blockHead = NULL;
file.pin = false;
memset(file.fileName,0,MAX_FILE_NAME);
}
/**
* init the blockNode values
*
* @param blockNode& the block your want to init
*
* @return void
*
*/
void BufferManager::init_block(blockNode &block)
{
memset(block.address,0,BLOCK_SIZE);
size_t init_usage = 0;
memcpy(block.address, (char*)&init_usage, sizeof(size_t)); // set the block head
block.using_size = sizeof(size_t);
block.dirty = false;
block.nextBlock = NULL;
block.preBlock = NULL;
block.offsetNum = -1;
block.pin = false;
block.reference = false;
block.ifbottom = false;
memset(block.fileName,0,MAX_FILE_NAME);
}
/**
* Get a fileNode
* 1.If the file is already in the list, return this fileNode
* 2.If the file is not in the list, replace some fileNode, if required, to make more space
*
* @param const char* the file name
* @param bool if true, the file will be locked.
*
* @return fileNode*
*
*/
fileNode* BufferManager::getFile(const char * fileName, bool if_pin)
{
blockNode * btmp = NULL;
fileNode * ftmp = NULL;
if(fileHead != NULL)
{
for(ftmp = fileHead;ftmp != NULL;ftmp = ftmp->nextFile)
{
if(!strcmp(fileName, ftmp->fileName)) //the fileNode is already in the list
{
ftmp->pin = if_pin;
return ftmp;
}
}
}
// The fileNode is not in the list
if(total_file == 0) // No file in the list now
{
ftmp = &file_pool[total_file];
total_file ++;
fileHead = ftmp;
}
else if(total_file < MAX_FILE_NUM) // There are empty fileNode in the pool
{
ftmp = &file_pool[total_file];
// add this fileNode into the tail of the list
file_pool[total_file-1].nextFile = ftmp;
ftmp->preFile = &file_pool[total_file-1];
total_file ++;
}
else // if total_file >= MAX_FILE_NUM, find one fileNode to replace, write back the block node belonging to the fileNode
{
ftmp = fileHead;
while(ftmp->pin)
{
if(ftmp -> nextFile)ftmp = ftmp->nextFile;
else //no enough file node in the pool
{
printf("There are no enough file node in the pool!");
exit(2);
}
}
for(btmp = ftmp->blockHead;btmp != NULL;btmp = btmp->nextBlock)
{
if(btmp->preBlock)
{
init_block(*(btmp->preBlock));
total_block --;
}
writtenBackToDisk(btmp->fileName,btmp);
}
init_file(*ftmp);
}
if(strlen(fileName) + 1 > MAX_FILE_NAME)
{
printf("文件名长度过长,最高不能超过%d\n",MAX_FILE_NAME);
exit(3);
}
strncpy(ftmp->fileName, fileName,MAX_FILE_NAME);
set_pin(*ftmp, if_pin);
return ftmp;
}
/**
* Get a block node
* 1.If the block is already in the list, return this blockNode
* 2.If the block is not in the list, replace some fileNode, using LRU replacement, if required, to make more space.
* 3.Only if the block is dirty, he will be written back to disk when been reaplaced.
*
* @param fileNode * the file you want to add the block into.
* @param blockNode the position that the Node will added to.
* @param bool if true, the block will be locked.
*
* @return blockNode*
*
*/
blockNode* BufferManager::getBlock(fileNode * file,blockNode *position, bool if_pin)
{
const char * fileName = file->fileName;
blockNode * btmp = NULL;
if(total_block == 0)
{
btmp = &block_pool[0];
total_block ++;
}
else if(total_block < MAX_BLOCK_NUM) // there are empty blocks in the block pool
{
for(int i = 0 ;i < MAX_BLOCK_NUM;i ++)
{
if(block_pool[i].offsetNum == -1)
{
btmp = &block_pool[i];
total_block ++;
break;
}
else
continue;
}
}
else // total_block >= MAX_BLOCK_NUM,which means that there are no empty block so we must replace one.
{
int i = replaced_block;
while (true)
{
i ++;
if(i >= total_block) i = 0;
if(!block_pool[i].pin)
{
if(block_pool[i].reference == true)
block_pool[i].reference = false;
else //choose this block for replacement
{
btmp = &block_pool[i];
if(btmp->nextBlock) btmp -> nextBlock -> preBlock = btmp -> preBlock;
if(btmp->preBlock) btmp -> preBlock -> nextBlock = btmp -> nextBlock;
if(file->blockHead == btmp) file->blockHead = btmp->nextBlock;
replaced_block = i; //record the replaced block and begin from the next block the next time.
writtenBackToDisk(btmp->fileName, btmp);
init_block(*btmp);
break;
}
}
else // this block is been locked
continue;
}
}
//add the block into the block list
if(position != NULL && position->nextBlock == NULL)
{
btmp -> preBlock = position;
position -> nextBlock = btmp;
btmp -> offsetNum = position -> offsetNum + 1;
}
else if (position !=NULL && position->nextBlock != NULL)
{
btmp->preBlock=position;
btmp->nextBlock=position->nextBlock;
position->nextBlock->preBlock=btmp;
position->nextBlock=btmp;
btmp -> offsetNum = position -> offsetNum + 1;
}
else // the block will be the head of the list
{
btmp -> offsetNum = 0;
if(file->blockHead) // if the file has a wrong block head
{
file->blockHead -> preBlock = btmp;
btmp->nextBlock = file->blockHead;
}
file->blockHead = btmp;
}
set_pin(*btmp, if_pin);
if(strlen(fileName) + 1 > MAX_FILE_NAME)
{
printf("文件名长度过长,最高不能超过%d\n",MAX_FILE_NAME);
exit(3);
}
strncpy(btmp->fileName, fileName, MAX_FILE_NAME);
//read the file content to the block
FILE * fileHandle;
if((fileHandle = fopen(fileName, "ab+")) != NULL)
{
if(fseek(fileHandle, btmp->offsetNum*BLOCK_SIZE, 0) == 0)
{
if(fread(btmp->address, 1, BLOCK_SIZE, fileHandle)==0)
btmp->ifbottom = true;
btmp ->using_size = getUsingSize(btmp);
}
else
{
printf("Problem seeking the file %s in reading",fileName);
exit(1);
}
fclose(fileHandle);
}
else
{
printf("Problem opening the file %s in reading",fileName);
exit(1);
}
return btmp;
}
/**
* Flush the block node to the disk, if the block is not dirty, do nothing.
*
* @param const char* the file name
* @param blockNode& the block your want to flush
*
* @return void
*
*/
void BufferManager::writtenBackToDisk(const char* fileName,blockNode* block)
{
if(!block->dirty) // this block is not been modified, so it do not need to written back to files
{
return;
}
else // written back to the file
{
FILE * fileHandle = NULL;
if((fileHandle = fopen(fileName, "rb+")) != NULL)
{
if(fseek(fileHandle, block->offsetNum*BLOCK_SIZE, 0) == 0)
{
if(fwrite(block->address, block->using_size+sizeof(size_t), 1, fileHandle) != 1)
{
printf("Problem writing the file %s in writtenBackToDisking",fileName);
exit(1);
}
}
else
{
printf("Problem seeking the file %s in writtenBackToDisking",fileName);
exit(1);
}
fclose(fileHandle);
}
else
{
printf("Problem opening the file %s in writtenBackToDisking",fileName);
exit(1);
}
}
}
/**
* Flush all block node in the list to the disk
*
* @return void
*
*/
void BufferManager::writtenBackToDiskAll()
{
blockNode *btmp = NULL;
fileNode *ftmp = NULL;
if(fileHead)
{
for(ftmp = fileHead;ftmp != NULL;ftmp = ftmp ->nextFile)
{
if(ftmp->blockHead)
{
for(btmp = ftmp->blockHead;btmp != NULL;btmp = btmp->nextBlock)
{
if(btmp->preBlock)init_block(*(btmp -> preBlock));
writtenBackToDisk(btmp->fileName, btmp);
}
}
}
}
}
/**
* Get the next block node of the node inputed
*
* @param fileNode* the file you want to add a block
* @param blockNode& the block your want to be added to
*
* @return blockNode*
*
*/
blockNode* BufferManager::getNextBlock(fileNode* file,blockNode* block)
{
if(block->nextBlock == NULL)
{
if(block->ifbottom) block->ifbottom = false;
return getBlock(file, block);
}
else //block->nextBlock != NULL
{
if(block->offsetNum == block->nextBlock->offsetNum - 1)
{
return block->nextBlock;
}
else //the block list is not in the right order
{
return getBlock(file, block);
}
}
}
/**
* Get the head block of the file
*
* @param fileNode*
*
* @return blockNode*
*
*/
blockNode* BufferManager::getBlockHead(fileNode* file)
{
blockNode* btmp = NULL;
if(file->blockHead != NULL)
{
if(file->blockHead->offsetNum == 0) //The right offset of the first block
{
btmp = file->blockHead;
}
else
{
btmp = getBlock(file, NULL);
}
}
else// If the file have no block head, get a new block node for it
{
btmp = getBlock(file,NULL);
}
return btmp;
}
/**
* Get the block of the file by offset number
*
* @param fileNode*
* @param int offsetNumber
*
* @return blockNode*
*
*/
blockNode* BufferManager::getBlockByOffset(fileNode* file, int offsetNumber)
{
blockNode* btmp = NULL;
if(offsetNumber == 0) return getBlockHead(file);
else
{
btmp = getBlockHead(file);
while( offsetNumber > 0)
{
btmp = getNextBlock(file, btmp);
offsetNumber --;
}
return btmp;
}
}
/**
* delete the file node and its block node
*
* @param const char * fileName
*
* @return
*/
void BufferManager::delete_fileNode(const char * fileName)
{
fileNode* ftmp = getFile(fileName);
blockNode* btmp = getBlockHead(ftmp);
queue<blockNode*> blockQ;
while (true) {
if(btmp == NULL) return;
blockQ.push(btmp);
if(btmp->ifbottom) break;
btmp = getNextBlock(ftmp,btmp);
}
total_block -= blockQ.size();
while(!blockQ.empty())
{
init_block(*blockQ.back());
blockQ.pop();
}
if(ftmp->preFile) ftmp->preFile->nextFile = ftmp->nextFile;
if(ftmp->nextFile) ftmp->nextFile->preFile = ftmp->preFile;
if(fileHead == ftmp) fileHead = ftmp->nextFile;
init_file(*ftmp);
total_file --;
}
void BufferManager::set_pin(blockNode &block,bool pin)
{
block.pin = pin;
if(!pin)
block.reference = true;
}
void BufferManager::set_pin(fileNode &file,bool pin)
{
file.pin = pin;
}
/**
* Set the block a dirty node
* Must call this function if modify the block node
*
* @param blockNode& the block your want to be added modify
*
* @return void
*
*/
void BufferManager::set_dirty(blockNode &block)
{
block.dirty = true;
}
void BufferManager::clean_dirty(blockNode &block)
{
block.dirty = false;
}
size_t BufferManager::getUsingSize(blockNode* block)
{
return *(size_t*)block->address;
}
void BufferManager::set_usingSize(blockNode & block,size_t usage)
{
block.using_size = usage;
memcpy(block.address,(char*)&usage,sizeof(size_t));
}
size_t BufferManager::get_usingSize(blockNode & block)
{
return block.using_size;
}
/**
*
* Get the content of the block except the block head
*
* @param blockNode&
*
* @return
*
*/
char* BufferManager::get_content(blockNode& block)
{
return block.address + sizeof(size_t);
}