-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRecordManager.cpp
More file actions
322 lines (264 loc) · 8.14 KB
/
Copy pathRecordManager.cpp
File metadata and controls
322 lines (264 loc) · 8.14 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
#include "RecordManager.h"
#include "buffermanager.h"
#include <map>
#include<assert.h>
#include "indexmanager.h"
using namespace std;
//文件状态 (大小)
struct FileStatus {
FILE *fp;
int fileSize;
};
//块状态
map <string, map <int, int> > blockStatus;
//类型检测
bool fitInTable(const vector<element> &entry, const table &datatable) {
//元素个数是否符合表的要求
if (entry.size() != datatable.items.size())
return false;
//依次检测属性类型是否匹配
for (int i = 0; i<entry.size(); i++) {
if (entry[i].type != datatable.items[i].type)
return false;
}
return true;
}
//属性转为2进制
void entryToBinary(const vector<element> &entry, unsigned char * c, const table &datatable) {
int entrySize = datatable.entrySize;//表属性大小(字)
memset(c, 0, entrySize); //c的entrySize位初始化都为0
for (int i = 0; i<entry.size(); i++) {
switch (entry[i].type) {
//0:int
case 0: memcpy(c, &entry[i].datai, sizeof(entry[i].datai)); c += sizeof(entry[i].datai); break;
//1:float
case 1: memcpy(c, &entry[i].dataf, sizeof(entry[i].dataf)); c += sizeof(entry[i].dataf); break;
//2:string
case 2: memcpy(c, entry[i].datas.c_str(), entry[i].datas.length()); c += datatable.items[i].length; break;
default:assert(false);
}
}
}
static int fetchint(const unsigned char *loc) {
int ret;
memcpy(&ret, loc, sizeof(int));//string头调用
return ret;
}
static float fetchfloat(const unsigned char *loc) {
float ret;
memcpy(&ret, loc, sizeof(float));
return ret;
}
static string fetchstring(const unsigned char *loc, int len) {
char ret[BlockSize]; //这里用了buffer
memcpy(ret, loc, len);
ret[len] = 0;
return ret;
}
//2进制转回属性
vector <element> binaryToEntry(unsigned char *c, const table &datatable) {
vector <element> res;
//遍历表的每个属性(主要查类型)
for (int i = 0; i<datatable.items.size(); i++) {
switch (datatable.items[i].type) {
//0:int
case 0:res.push_back(fetchint(c)); c += sizeof(int); break;
//1:float
case 1:res.push_back(fetchfloat(c)); c += sizeof(float); break;
//2:string
case 2:res.push_back(fetchstring(c, datatable.items[i].length)); c += datatable.items[i].length; break;
default:assert(false);
}
}
return res;
}
//读块状态
void loadBlockStatus(const string &fileName) {
//块还没满,返回
if (blockStatus.find(fileName) != blockStatus.end())
return;
//块满了,开新块
FILE *fplist = fopen((fileName + ".blockinfo").c_str(), "r");//////////////////
if (fplist) {
int offset, occ;
while (fscanf(fplist, "%d%d", &offset, &occ) != EOF) {
blockStatus[fileName][offset] = occ;
}
fclose(fplist);
}
}
//数据插入 return the offset of data inserted
int rmInsertRecord(const string &fileName, const vector<element> &entry, const table &datatable) {
if (!fitInTable(entry, datatable)) {
return -1;
}
//看看块满了没,没有新开
loadBlockStatus(fileName);
int capacity = BlockSize / (datatable.entrySize + 1);
int offset = -1;
//找offset
for (map<int, int>::iterator it = blockStatus[fileName].begin(); it != blockStatus[fileName].end(); it++) {
if (it->second<capacity) {
offset = it->first;
break;
}
}
Block block;
if (offset == -1) {
block = bmNewBlock(fileName);
offset = block.offset;
blockStatus[fileName][offset] = 0;
memset(block.data, 0, sizeof(block.data));
}
else {
block = bmReadBlock(fileName, offset);
}
bool findd = false;
for (int i = 0; i<capacity; i++) {
if (!block.data[i]) {
//数据写入块
entryToBinary(entry, block.data + capacity + i*datatable.entrySize, datatable);
findd = true;
block.data[i] = true;
break;
}
}
assert(findd);
bmWriteBlock(block);
blockStatus[fileName][offset]++;
return offset;
}
//删除
void rmDeleteWithoutIndex(const string fileName, const Fitter &fitter, const table &datatable) {
//看看块满了没,没有新开
loadBlockStatus(fileName);
//遍历指定文件名字的块
for (map <int, int> ::iterator it = blockStatus[fileName].begin(); it != blockStatus[fileName].end(); it++) {
int offset = it->first;
if (it->second == 0)
continue;
Block block = bmReadBlock(fileName, offset);
int capacity = BlockSize / (datatable.entrySize + 1);
unsigned char *c = block.data + capacity;
//遍历block中的data
for (int i = 0; i<capacity; i++) {
if (block.data[i]) {
vector <element> entry = binaryToEntry(c, datatable);
//符合删除条件
if (fitter.test(datatable,entry)) {
block.data[i] = false;
blockStatus[fileName][offset]--;
}
}
c += datatable.entrySize;
}
assert(blockStatus[fileName][offset] >= 0);
//更改后的块写回
bmWriteBlock(block);
}
}
//查询
vector <vector <element> > rmSelectWithoutIndex(const string fileName, const Fitter &fitter, const table &datatable) {
vector <vector <element> > ret;
loadBlockStatus(fileName);
//遍历指定文件名字的块
for (map <int, int> ::iterator it = blockStatus[fileName].begin(); it != blockStatus[fileName].end(); it++) {
int offset = it->first;
if (it->second == 0)
continue;
Block block = bmReadBlock(fileName, offset);
int capacity = BlockSize / (datatable.entrySize + 1);
unsigned char *c = block.data + capacity;
for (int i = 0; i<capacity; i++) {
if (block.data[i]) {
vector <element> entry = binaryToEntry(c, datatable);
if (fitter.test(datatable,entry)) {
ret.push_back(entry);
}
}
c += datatable.entrySize;
}
}
return ret;
}
void rmAddIndex(const string dbName, const string BTreeName, const table &datatable, int itemIndex) {
btCreate(BTreeName, datatable.items[itemIndex].type, datatable.items[itemIndex].length);
loadBlockStatus(dbName);
for (map <int, int> ::iterator it = blockStatus[dbName].begin(); it != blockStatus[dbName].end(); it++) {
int offset = it->first;
if (it->second == 0)
continue;
Block block = bmReadBlock(dbName, offset);
int capacity = BlockSize / (datatable.entrySize + 1);
unsigned char *c = block.data + capacity;
for (int i = 0; i<capacity; i++) {
if (block.data[i]) {
vector <element> entry = binaryToEntry(c, datatable);
assert(itemIndex<entry.size());
assert(btFind(BTreeName, entry[itemIndex]) !=-1);
btInsert(BTreeName, entry[itemIndex], offset);
}
c += datatable.entrySize;
}
}
}
void rmClear(const string fileName) {
if (blockStatus.find(fileName) != blockStatus.end())
blockStatus.erase(blockStatus.find(fileName));
bmClear(fileName);
remove((fileName + ".blockinfo").c_str());
}
vector <vector <element> > rmSelectWithIndex(const string fileName, int offset, const Fitter &fitter, const table &datatable) {
vector <vector <element> > ret;
loadBlockStatus(fileName);
Block block = bmReadBlock(fileName, offset);
int capacity = BlockSize / (datatable.entrySize + 1);
unsigned char *c = block.data + capacity;
for (int i = 0; i<capacity; i++) {
if (block.data[i]) {
vector <element> entry = binaryToEntry(c, datatable);
if (fitter.test(datatable,entry)) {
ret.push_back(entry);
}
}
c += datatable.entrySize;
}
return ret;
}
set<int> rmGetAllOffsets(const string &fileName) {
loadBlockStatus(fileName);
set <int> ret;
for (map<int, int>::iterator it = blockStatus[fileName].begin(); it != blockStatus[fileName].end(); it++) {
ret.insert(it->first);
}
return ret;
}
void rmDeleteWithIndex(const string fileName, int offset, const Fitter &fitter, const table &datatable) {
loadBlockStatus(fileName);
Block block = bmReadBlock(fileName, offset);
int capacity = BlockSize / (datatable.entrySize + 1);
unsigned char *c = block.data + capacity;
for (int i = 0; i<capacity; i++) {
if (block.data[i]) {
vector <element> entry = binaryToEntry(c, datatable);
if (fitter.test(datatable,entry)) {
for (int j = 0; j<datatable.items.size(); j++) {
if (datatable.items[j].indices.size()) {
string tablename = datatable.name;
while (*(tablename.end() - 1) != '.')
tablename.erase(tablename.end() - 1);
tablename.erase(tablename.end() - 1);
string btreename = tablename + "." + datatable.items[j].name + ".index";
assert(btFind(btreename, entry[j])!=-1);
btDelete(btreename, entry[j]);
}
}
block.data[i] = false;
blockStatus[fileName][offset]--;
}
}
c += datatable.entrySize;
}
assert(blockStatus[fileName][offset] >= 0);
bmWriteBlock(block);
}