This repository was archived by the owner on Jun 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferUnit.h
More file actions
66 lines (53 loc) · 1.55 KB
/
Copy pathBufferUnit.h
File metadata and controls
66 lines (53 loc) · 1.55 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
//
// Created by Kira on 2017/6/10.
//
#ifndef MINISQL_BUFFERUNIT_H
#define MINISQL_BUFFERUNIT_H
#include <vector>
#include <string>
#include <cstring>
using namespace std;
class BufferDataBlock {
public:
char *data;
bool visited;
bool dirty;
int indexInFile; // 文件中第几个block
size_t size; // data的大小
BufferDataBlock(size_t size) : visited(false), dirty(false), indexInFile(-1) {
// 需要delete
data = new char[size];
this->size = size;
}
BufferDataBlock(const BufferDataBlock &block) : visited(block.visited), dirty(block.dirty),
indexInFile(block.indexInFile), size(block.size) {
data = new char[block.size];
memcpy(data, block.data, block.size);
};
~BufferDataBlock() {
delete[] data;
}
};
// 管理一张表的所有buffer
class BufferUnit {
private:
// 以下两个vector为映射关系
vector<int> blockIndexInBuffer; // 他的size就是文件有几个block
vector<BufferDataBlock> blocks;
size_t blockSize;
size_t blockNum;
size_t fileSize;
public:
string fileName;
private:
int getAvailableBlock();
public:
bool readBlock(int blockIndexInFile, char *outBuffer);
bool writeBlock(int blockIndexInFile, char const *inBuffer);
bool deleteLastBlock();
bool enlargeFile();
// 这里fileSize是0有问题吗?
BufferUnit(const string &tableName, size_t blockSize, size_t blockNum, size_t fileSize);
~BufferUnit();
};
#endif //MINISQL_BUFFERUNIT_H