-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuffer.cpp
More file actions
549 lines (479 loc) · 13.3 KB
/
buffer.cpp
File metadata and controls
549 lines (479 loc) · 13.3 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
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
#ifndef DEBUG
#define DEBUG
#include "buffer.h"
#endif // !DEBUG
string rootdir = ".\\minisql\\";
FileInfo filelist = nullptr; //文件链表
int fileNum = 0; //文件数
int global_block_num = 0; //现有block数量
//寻找空块
BlockInfo findBlock()
{
FileInfo fileptr = filelist;
BlockInfo resblock = nullptr;
while (fileptr)
{
BlockInfo blockptr = fileptr->firstBlock;
if (fileptr->freeNum == 0) //这个文件下面没有空块了
{
fileptr = fileptr->next;
continue;
}
while (blockptr) //这个文件下面有空块
{
if (blockptr->isfree) //找到一个空块
{
resblock = blockptr;
fileptr->freeNum--; //空块数量-1
blockptr->isfree = false;
remove_block_from_file(resblock); //把这个块从原文件中移出
break;
}
blockptr = blockptr->next;
}
if (!resblock)
cout << "findBlock::freeNum record may be wrong." << endl;
else
return resblock;
fileptr = fileptr->next;
}
//已经没有空块了
//还能申请空块
if (global_block_num < MAX_BLOCK)
{
resblock = new struct blockInfo;
if (resblock)
{
resblock->call_times = 0;
resblock->charNum = 0;
resblock->dirtyBit = 0;
resblock->file = nullptr;
resblock->isfree = 0;
resblock->lock = 0;
resblock->next = nullptr;
resblock->cBlock = new char[BLOCK_SIZE];
memset(resblock->cBlock, 0, BLOCK_SIZE * sizeof(char)); //初始化
if (resblock->cBlock)
return resblock;
}
}
//不能申请空块了,用LRU遍历所有块,替换掉一个
int time = 0x7fffffff;
fileptr = filelist;
while (fileptr)
{
BlockInfo blockptr = fileptr->firstBlock;
while (blockptr)
{
if (blockptr->call_times < time && !blockptr->lock) //用LRU,没被锁的可以替换
{
time = blockptr->call_times;
resblock = blockptr;
if (blockptr->dirtyBit == 1)
write_block_to_disk("db_name", blockptr);
}
blockptr = blockptr->next;
}
fileptr = fileptr->next;
}
resblock->call_times = 1;
remove_block_from_file(resblock);
return resblock;
}
//在filelist中找一个文件,没有的话就打开
FileInfo get_file_info(string fileName, int fileType)
{
FileInfo fileptr = filelist;
//找文件
while (fileptr)
{
if (fileptr->fileName == fileName && fileptr->type == fileType)
return fileptr;
fileptr = fileptr->next;
}
return add_file_to_list(fileName,fileType,20,20);
//文件不存在
//cout << "get_file_info::No such file" << endl;
}
//在buffer中查找一个块
BlockInfo get_block_in_file(FileInfo file, int blockNum)
{
if (!file)
return nullptr;
BlockInfo blockptr = file->firstBlock;
while (blockptr)
{
if (blockptr->blockNum == blockNum)
return blockptr;
blockptr = blockptr->next;
}
return nullptr;
}
//在buffer中把一个块移出对应文件
void remove_block_from_file(BlockInfo block_to_remove)
{
FileInfo file = block_to_remove->file;
BlockInfo last_block = nullptr;
BlockInfo blockptr = file->firstBlock;
while (blockptr) //在该文件中寻找对应块
{
if (blockptr->next == block_to_remove)
break;
last_block = blockptr;
blockptr = blockptr->next;
}
if (blockptr) //找到了
{
if (last_block)
file->firstBlock = blockptr->next;
else
last_block->next = blockptr->next;
}
else
cout << "remove_block_from_file::Cannot find this block" << endl;
return;
}
//缓冲区操作:把一个块挂到文件上
void add_block_to_file(BlockInfo block, FileInfo file)
{
block->file = file;
block->next = file->firstBlock;
file->firstBlock = block;
return;
}
//从磁盘读一个块
BlockInfo read_block_from_disk(FileInfo file, string db_name, int blocknum, BlockInfo tmpblock)
{
if (!tmpblock) //没提供块,先找一个
{
tmpblock = findBlock();
tmpblock->isfree = 0;
}
tmpblock->blockNum = blocknum;
//打开文件
string filepath = rootdir + db_name;
if (file->type == 0) //表
filepath += "\\table\\";
else //索引
filepath += "\\index\\";
filepath += file->fileName + ".txt";
fstream infile(filepath, ios::in | ios::binary);
//infile.open(filepath);
if (!infile.is_open())
{
cout << "readblock::no such file!" + filepath << endl;
return nullptr;
}
//读取数据
int offset = BLOCK_SIZE * (blocknum);
infile.seekg(offset, ios::beg);
if (!tmpblock->cBlock)
tmpblock->cBlock = new char[BLOCK_SIZE];
//需要先清空,否则会被自动填充屯屯屯
memset(tmpblock->cBlock, 0, BLOCK_SIZE * sizeof(char));
infile.read(tmpblock->cBlock, BLOCK_SIZE);
infile.close();
tmpblock->charNum = strlen(tmpblock->cBlock);
tmpblock->blockNum=blocknum;
return tmpblock;
}
//把一个块写到磁盘
void write_block_to_disk(string db_name, BlockInfo block)
{
if (!block->dirtyBit || block->isfree )
return;
FileInfo file = block->file;
if (!file)
{
cout << "write_block_to_disk::No such file opened!" << endl;
return;
}
//打开文件
string filepath = rootdir + db_name;
if (file->type == 0) //表
filepath += "\\table\\";
else //索引
filepath += "\\index\\";
filepath += file->fileName + ".txt";
//读取数据
int offset = BLOCK_SIZE * (block->blockNum);
fstream ofile(filepath, ios::out | ios::in | ios::binary);
ofile.seekp(offset, ios::beg);
string str(block->cBlock);
ofile.write(block->cBlock, BLOCK_SIZE);
ofile.close();
}
//从文件中读取某个块
BlockInfo get_block_info(string db_name, string table_name, int file_type, int BlockNum)
{
//cout << db_name << endl;
FileInfo fileptr = get_file_info(table_name, file_type);
if (!fileptr)
{
//cout << table_name << endl;
cout << "get_block_info::No such file!" << endl;
return nullptr; //没找到对应文件
}
BlockInfo blockptr = get_block_in_file(fileptr, BlockNum);
if (blockptr && blockptr->lock) //块是否被锁住
{
cout << "get_block_info::The block has been locked" << endl;
return blockptr;
}
if (blockptr) //找到了blocknum的块
{
if (!blockptr->isfree || strlen(blockptr->cBlock) == 0) //不是空块,直接返回
{
blockptr->call_times++;
return blockptr;
}
else //是空块,先从磁盘中读取出来再返回
{
blockptr = read_block_from_disk(fileptr, db_name, BlockNum, blockptr);
blockptr->call_times = 1;
blockptr->isfree = 0;
return blockptr;
}
}
//文件在buffer中没有编号为blocknum的块
blockptr = read_block_from_disk(fileptr, db_name, BlockNum, nullptr);
add_block_to_file(blockptr, fileptr);
blockptr->isfree = 0;
blockptr->file = fileptr;
blockptr->call_times = 1;
return blockptr;
}
//关闭文件
void closefile(string db_name, FileInfo file)
{
if (file == nullptr)
{
cout << "closefile::No such file!" << endl;
return;
}
BlockInfo blockptr = file->firstBlock;
BlockInfo tmpblock = blockptr;
fileNum--;
while (blockptr)
{
write_block_to_disk(db_name, blockptr);
tmpblock = blockptr;
blockptr = blockptr->next;
delete[] tmpblock->cBlock;
delete tmpblock;
global_block_num--;
}
//从文件表中删除
FileInfo fileptr = filelist;
if (fileptr == file) //filelist头结点
{
filelist = file->next;
delete file;
return;
}
while (fileptr)
{
if (fileptr->next == file)
{
fileptr->next = file->next;
delete file;
return;
}
fileptr = fileptr->next;
}
}
//把一个文件加入filelist
FileInfo add_file_to_list(string table_name, int type, int recordAmount, int recordlen)
{
if (fileNum >= MAX_ACTIVE_FILE)
{
cout << "Cannot add more file to list" << endl;
return nullptr;
}
FileInfo newfile = new struct fileInfo;
newfile->next = filelist;
filelist = newfile;
newfile->fileName = table_name;
newfile->firstBlock = nullptr;
newfile->freeNum = 0;
newfile->recordAmount = recordAmount;
newfile->recordLength = recordlen;
newfile->type = type;
fileNum++;
return newfile;
}
//锁住一个块
void lock_block(BlockInfo block)
{
if (block == nullptr)
{
cout << "lock_block::Block doesn't exist" << endl;
return;
}
block->lock = 1;
}
//解锁一个块
void unlock_block(BlockInfo block)
{
if (block == nullptr)
{
cout << "unlock_block::Block doesn't exist" << endl;
return;
}
block->lock = 0;
}
void write_to_block(BlockInfo block, char to_write[]) //两个覆写
{
block->charNum = strlen(to_write);
strcpy_s(block->cBlock, BLOCK_SIZE, to_write);
}
void write_to_block(BlockInfo block, char to_write[], int length)
{
block->charNum = length;
strncpy_s(block->cBlock, BLOCK_SIZE, to_write, length);
}
void add_to_block(BlockInfo block, char to_write[]) //两个添加
{
if (strlen(block->cBlock) == 0)
{
write_to_block(block, to_write);
return;
}
strcat_s(block->cBlock, BLOCK_SIZE, to_write);
block->charNum = strlen(block->cBlock);
}
void add_to_block(BlockInfo block, char to_write[], int length)
{
if (block->charNum == 0)
{
write_to_block(block, to_write, length);
return;
}
memcpy_s(block->cBlock, BLOCK_SIZE, to_write, length);
block->charNum += length;
}
bool readData(string fileName, string db_name, int index, char to_read[], int &length, int filetype)
{
int recnum=index/length;//记录数
int recs_inblock=BLOCK_SIZE/(length);//每块能容纳的记录数
int blocknum = recnum/recs_inblock;//第多少个块
int offset_b = index % (length*recs_inblock);//在那个块的偏移量
FileInfo tmpfile = new struct fileInfo;
tmpfile->fileName=fileName;
tmpfile->type=0;
BlockInfo block=findBlock();
block->blockNum=blocknum;
block->file=tmpfile;
read_block_from_disk(tmpfile,"db_name",blocknum,block);
//没有这个块
if (!block)
{
//返回空字符串和false
cout << "error::readData" << endl; //test usage
strcpy_s(to_read, 5, "");
return false;
}
bool flag = true;
//长度不足,改短
if (offset_b + length>BLOCK_SIZE)
{
length = BLOCK_SIZE - offset_b;
flag = false;
}
memcpy_s(to_read, length + 1, block->cBlock + offset_b, length);
delete tmpfile;
delete block->cBlock;
delete block;
return flag;
}
//把to_write[]数据的length长度的数据,写入从block开始的第index个字节上,注意把后面的数据后移,防止覆盖,如果index<0那么表示将数据插入最后
/*bool writeToIndex(string fileName, int index, char* to_write, int length, string db_name, int filetype)
{
int blocknum = index/BLOCK_SIZE;
//BlockInfo block = get_block_info(db_name, fileName, filetype, blocknum);
BlockInfo block=findBlock();
block->dirtyBit = 1;
if (!block)//写入失败
{
cout<<"error::writetoIndex"<<endl;
return false;
}
FileInfo tmpfile = new struct fileInfo;//开个临时文件
block->file=tmpfile;
int rec_len = recordLength;//单条记录长度
int rec_tot = length / rec_len;//要插入的记录数
int rec_avai = (BLOCK_SIZE - block->charNum) / rec_len;//要求块剩余能插入记录的空间
if (length > BLOCK_SIZE - strlen(block->cBlock))//对应块写不下,找后面的某个块继续写进去
{
int offset_w = 0;//字符串中的偏移量
while (1)
{
char tmp[BLOCK_SIZE];
strncpy_s(tmp,BLOCK_SIZE, to_write + offset_w, rec_avai*rec_len);
add_to_block(block, tmp);
rec_tot -= rec_avai;//待写入的记录数减少
offset_w += rec_avai * rec_len;//已写入的长度增加,字符串偏移量增加
if (offset_w >= length)
break;
blocknum++;//看下一块
block = get_block_info(db_name, fileName, filetype, blocknum);
if(!block)
{
cout<<"error::writrtoINDEX"<<endl;
return false;
}
block->dirtyBit = 1;
rec_avai = (BLOCK_SIZE - block->charNum)/rec_len;//计算在当前块需要写入的记录条数
if (rec_avai > rec_tot)
rec_avai = rec_tot;
}
}
else
add_to_block(block, to_write);
return true;
}*/
bool writeToIndex(string fileName, int index, char *to_write, int length, string db_name, int filetype)
{
int recnum = index / length; //记录数
int recs_inblock = BLOCK_SIZE / (length); //每块能容纳的记录数
int blocknum = recnum / recs_inblock; //第多少个块
int offset = index % (length * recs_inblock); //在那个块的偏移量
BlockInfo block = findBlock();
if (!block) //写入失败
{
cout << "error::writetoIndex" << endl;
return false;
}
FileInfo tmpfile = new struct fileInfo; //开个临时文件
tmpfile->fileName = fileName;
tmpfile->type = 0;
block->file = tmpfile;
read_block_from_disk(tmpfile, db_name, blocknum, block);
if (length > BLOCK_SIZE - block->charNum)
{
read_block_from_disk(tmpfile, db_name, blocknum+1, block);
offset=0;
}
// if (length > BLOCK_SIZE - block->charNum) //对应块写不下,找后面的某个块继续写进去
// {
// int offset_w = 0; //字符串中的偏移量
// while (1)
// {
// blocknum++; //看下一块
// read_block_from_disk(tmpfile, db_name, blocknum, block);
// if (length <= BLOCK_SIZE - block->charNum)
// {
// memcpy_s((block->cBlock)+offset,BLOCK_SIZE,to_write,length);
// break;
// }
// }
// }
//else
memcpy_s((block->cBlock) + offset, BLOCK_SIZE, to_write, length);
block->dirtyBit=1;
write_block_to_disk(db_name, block);
delete block->cBlock;
delete block;
delete tmpfile;
return true;
}