-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuffer.cpp
More file actions
208 lines (198 loc) · 5.58 KB
/
Buffer.cpp
File metadata and controls
208 lines (198 loc) · 5.58 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
#include "Buffer.h"
fileInfo* getFile(string DB_Name, string fileName, string attrName, int fileType, bufferInfo* bufferInfo){
fileInfo *ret;
ret = existFile(DB_Name, fileName, attrName, fileType, bufferInfo);
if (ret != NULL) return ret;
if (bufferInfo->fileCount >= MAX_FILE_ACTIVE)
closeFile(bufferInfo->fileHandle, "", "", attrName, 0, bufferInfo);
ret = new fileInfo(DB_Name, fileName, fileType);
std::ifstream fin;
string path;
path = "Data//" + DB_Name + "//" + fileName;
if (fileType == 0)
path = path + ".0.dat";
else
{
ret->attrName = attrName;
path = path + "_" + attrName + ".1.dat";
}
fin.open(path.c_str());
fin.close();
bufferInfo->fileCount++;
ret->next = bufferInfo->fileHandle;
bufferInfo->fileHandle = ret;
bufferInfo->fileCount++;
return ret;
}
blockInfo* findBlock(bufferInfo* bufferInfo)
{
blockInfo* block;
if (bufferInfo->blockHandle != NULL)
{
block = bufferInfo->blockHandle;
bufferInfo->blockHandle = bufferInfo->blockHandle->next;
}
else
{
if (bufferInfo->blockCount >= MAX_BLOCK)
{
fileInfo *fite, *minFile;
blockInfo *bite, *minBlock = NULL;
int minTime = -1;
for (fite = bufferInfo->fileHandle; fite != NULL; fite = fite->next)
{
for (bite = fite->firstBlock; bite != NULL; bite = bite->next)
if ((bite->iTime < minTime || minTime == -1) && bite->lock == 0)
{
minBlock = bite;
minTime = bite->iTime;
}
}
if (minBlock == NULL) return NULL;
minFile = minBlock->file;
if (minBlock == minFile->firstBlock)
{
minFile->blockSet.erase(minBlock->blockNum);
minFile->firstBlock = minBlock->next;
if (minBlock->next == NULL)
minFile->lastBlock = NULL;
block = minBlock;
}
else
{
for (bite = minFile->firstBlock; bite->next != minBlock; bite = bite->next);
minFile->blockSet.erase(minBlock->blockNum);
if (minBlock->next == NULL)
minFile->lastBlock = bite;
bite->next = minBlock->next;
block = minBlock;
}
}
else
{
block = new blockInfo();
bufferInfo->blockCount++;
}
}
return block;
}
blockInfo* getBlock(fileInfo* F, int blockNum, bufferInfo* bufferInfo){
fileInfo *file = F;
blockInfo *block;
SYSTEMTIME time;
GetSystemTime(&time);
block = findBlock(bufferInfo);
if (block == NULL) return NULL;
block->blockNum = blockNum;
block->dirtyBit = 0;
block->file = file;
block->iTime = time.wMilliseconds;
block->lock = 0;
block->next = file->firstBlock;
block->lock = 0;
if (file->lastBlock == NULL)
file->lastBlock = block;
file->firstBlock = block;
file->blockSet.insert(blockNum);
std::ifstream fin;
string path = "Data//" + file->dataBase + "//" + file->fileName + "//" + file->fileName;
if (file->type == 0)
path += ".0.dat";
else
path += "_" + file->attrName + ".1.dat";
fin.open(path.c_str());
fin.seekg(BLOCK_LEN*blockNum, std::ios::beg);
fin.get(block->cBlock, BLOCK_LEN, '~');
fin.close();
return block;
}
blockInfo* existBlock(fileInfo* file, int blockNum){
blockInfo* ret;
if (file->blockSet.count(blockNum) == 0)
return NULL;
else
{
for (ret = file->firstBlock; ret->blockNum != blockNum; ret = ret->next);
return ret;
}
}
fileInfo* existFile(string DB_Name, string fileName, string attrName, int fileType, bufferInfo* bufferInfo){
fileInfo *ite = bufferInfo->fileHandle;
for (; ite != NULL; ite = ite->next)
if (ite->fileName == fileName && ite->dataBase == DB_Name && ite->type == fileType &&ite->attrName == attrName)
return ite;
return NULL;
}
blockInfo* readBlock(string DB_Name, string fileName, string attrName, int blockNum, int fileType, bufferInfo* bufferInfo)
{
fileInfo *file;
SYSTEMTIME time;
GetSystemTime(&time);
file = existFile(DB_Name, fileName, attrName, fileType, bufferInfo);
if (file == NULL)
file = getFile(DB_Name, fileName, attrName, fileType, bufferInfo);
blockInfo *block;
block = existBlock(file, blockNum);
if (block == NULL)
block = getBlock(file, blockNum, bufferInfo);
if (block != NULL)
block->iTime = time.wMilliseconds;
return block;
}
void writeBlock(string DB_Name, blockInfo *block){
string path = "Data//" + DB_Name + "//" + block->file->fileName + "//" + block->file->fileName;
if (block->file->type == 0)
path = path + ".0.dat";
else
path = path + "_"+block->file->attrName+".1.dat";
FILE *fout = fopen(path.c_str(), "r+");
if (fout == NULL) return;
fseek(fout,BLOCK_LEN*(block->blockNum), 0);
for (int i = 0; i < BLOCK_LEN;i++)
fprintf(fout, "%c",block->cBlock[i] );
fclose(fout);
}
void closeFile(fileInfo* F, string DB_Name, string fileName,string attrName,int fileType, bufferInfo* bufferInfo){
fileInfo *file,*ite;
if (F == NULL)
{
file = existFile(DB_Name, fileName, attrName,fileType, bufferInfo);
if (file == NULL) return;
}
else
file = F;
blockInfo *block,*tmp;
for (block = file->firstBlock; block != NULL; block = tmp)
{
if (block->dirtyBit == 1)
writeBlock(DB_Name, block);
tmp = block->next;
block->next = bufferInfo->blockHandle;
bufferInfo->blockHandle = block;
}
if (file == bufferInfo->fileHandle)
bufferInfo->fileHandle = file->next;
else
{
for (ite = bufferInfo->fileHandle; ite->next != file; ite = ite->next);
ite->next = file->next;
}
delete file;
bufferInfo->fileCount--;
}
void closeDatabase(string DB_Name, bufferInfo* bufferInfo){
fileInfo *file,*tmp=NULL;
for (file = bufferInfo->fileHandle; file != NULL;file=tmp){
tmp = file->next;
closeFile(file,"","","",0, bufferInfo);
}
}
void quitProg(string DB_Name, bufferInfo* bufferInfo){
closeDatabase(DB_Name, bufferInfo);
blockInfo* bite,*tmp=NULL;
for (bite = bufferInfo->blockHandle; bite != NULL;bite=tmp)
{
tmp = bite->next;
delete bite;
}
}