-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.cpp
566 lines (460 loc) · 13.4 KB
/
file.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#include "file.h"
#include <string>
#include <sstream>
// contain key(int: 4 bytes) and a " "(1 byte)
#define INDEX_BYTE sizeof(int)
// contain value(4 bytes)
#define VALUE_BYTE sizeof(int)
// contain key(int: 4 bytes), " "(1 byte), '\n' (1 byte) and value(int: 4 bytes)
#define NODE_BYTE VALUE_BYTE+INDEX_BYTE
#define TO_LEFT false
#define TO_RIGHT true
//================================================================================================
//------------------------------------ function about node ----------------------------------------
// ----------- view a node's value ----------------------
void viewNode(RBTree & tree, Cache & cache) {
if (!haveOpenedFile()) {
cout << "Error: no data file opened yet!" << endl;
return;
}
string strKey = "";
stringstream ss;
int key = 0;
int value = 0;
cout << "Viewing..." << endl;
cout << "Please enter the key." << endl;
// delete
cin.clear();
getline(cin, strKey);
// make sure user's input is int
while (!isNum(strKey)) {
// delete
cin.clear();
getline(cin, strKey);
}
ss << strKey;
ss >> key;
// first check cache
map<Node*, int>::iterator it;
for (it = cache.viewNode.begin(); it != cache.viewNode.end(); ++it) {
if (it->first->key == key) {
value = it->second;
cout << "Key: " << key << " Value: " << value << endl;
return;
}
}
for (it = cache.addNode.begin(); it != cache.addNode.end(); ++it) {
if (it->first->key == key) {
value = it->second;
cout << "Key: " << key << " Value: " << value << endl;
cache.viewNode[it->first] = it->second;
return;
}
}
for (it = cache.modifyNode.begin(); it != cache.modifyNode.end(); ++it) {
if (it->first->key == key) {
value = it->second;
cout << "Key: " << key << " Value: " << value << endl;
cache.viewNode[it->first] = it->second;
return;
}
}
// if the node is not in cache, get value from data file
Node* node = tree.fetch(key);
if (node != NULL && node->pos != -1) {
int pos = node->pos;
value = viewValue(pos);
cout << "Key: " << key << " Value: " << value << endl;
cache.viewNode[node] = value;
}
else {
cout << "Error: key does not exits!" << endl;
}
}
int viewValue(int pos) {
// get to the pos in the file
int startByte = pos * 2 * sizeof(int) + sizeof(int);
int value = 0;
fstream dataFile;
dataFile.open(dataFileName, ios::in | ios::binary);
dataFile.seekg(startByte, ios::beg);
dataFile.read((char*)&value, sizeof(int));
dataFile.close();
return value;
}
//--------------- add a node into the file -------------------
void addNode(RBTree & tree, Cache & cache) {
if (!haveOpenedFile()) {
cout << "Error: no data file opened yet!" << endl;
return;
}
stringstream ss1, ss2;
string strKey = "";
string strValue = "";
int key = 0;
int value = 0;
cout << "Adding..." << endl;
cout << "Please enter the key:" << endl;
// delete
cin.clear();
getline(cin, strKey);
while (!isNum(strKey)) {
// delete
cin.clear();
getline(cin, strKey);
}
cout << "Please enter the value:" << endl;
// delete
cin.clear();
getline(cin, strValue);
while (!isNum(strValue)) {
// delete
cin.clear();
getline(cin, strValue);
}
ss1 << strKey;
ss2 << strValue;
ss1 >> key;
ss2 >> value;
if (tree.add(key, value, cache)) {
cout << "Add succeed!" << endl;
}
// only modify data file if the cache size reaches SIZE_TO_UPDATE
if (cache.addNode.size() == SIZE_TO_UPDATE) {
updateFile(tree, cache);
}
}
//------------ modify a node's value -----------------
void modifyNode(RBTree & tree, Cache & cache) {
if (!haveOpenedFile()){
cout << "Error: no data file opened yet!" << endl;
return;
}
stringstream ss1, ss2;
string strKey = "";
string strValue = "";
int key = 0;
int value = 0;
cout << "Modifying..." << endl;
cout << "Please enter the key:" << endl;
// delete
cin.clear();
getline(cin, strKey);
while (!isNum(strKey)) {
// delete
cin.clear();
getline(cin, strKey);
}
cout << "Please enter the new value:" << endl;
// delete
cin.clear();
getline(cin, strValue);
while (!isNum(strKey)) {
// delete
cin.clear();
getline(cin, strValue);
}
ss1 << strKey;
ss2 << strValue;
ss1 >> key;
ss2 >> value;
tree.modify(key, value, cache);
if (cache.modifyNode.size() == SIZE_TO_UPDATE) {
updateFile(tree, cache);
}
}
//--------------- delete a node ---------------------------
void deleteNode(RBTree & tree, Cache & cache) {
if (!haveOpenedFile()) {
cout << "Error: no data file opened yet!" << endl;
return;
}
stringstream ss;
string strKey = "";
int key = 0;
cout << "Deleting..." << endl;
cout << "Please enter the key:" << endl;
// delete
cin.clear();
getline(cin, strKey);
while (!isNum(strKey)) {
// delete
cin.clear();
getline(cin, strKey);
}
ss << strKey;
ss >> key;
// cache is updated in tree.remove(int key, Cache& cache)
tree.remove(key, cache);
if (cache.deletePos.size() == SIZE_TO_UPDATE) {
updateFile(tree, cache);
}
}
//------------------------------------ end --------------------------------------
//================================================================================
//===============================================================================
//----------------- function about file -----------------------------------------
//===========================================
//------------- getFile -------------------
void getFile(RBTree & tree, Cache & cache) {
getFileName();
fstream dataFile;
dataFile.open(dataFileName, ios::in | ios::binary);
while (!dataFile) {
cout << "open " << dataFileName << " failed." << endl;
getFileName();
if (dataFileName == "quit")
return;
dataFile.open(dataFileName, ios::in | ios::binary);
}
dataFile.close();
indexFileName = "indexFor" + dataFileName;
treeFromFile(tree, cache);
}
//===========================================
//------------- getFileName -------------------
void getFileName() {
cout << "Please enter the and name of data file" << endl;
cout << "(quit) to quit" << endl;
// delete
cin.clear();
getline(cin, dataFileName);
}
//===========================================
//------------- updateFile -------------------
void updateFile(RBTree & tree, Cache & cache) {
int modifySize = cache.modifyNode.size();
int addSize = cache.addNode.size();
int deleteSize = cache.deletePos.size();
if (modifySize || addSize || deleteSize) {
cout << "Updating the file..." << endl;
map<Node*, int>::iterator it;
for (it = cache.addNode.begin(); it != cache.addNode.end(); ++it) {
int key = it->first->key;
int pos = it->first->pos;
int value = it->second;
cout << "Add data in data file, key: " << key << " pos: " << pos << " value: " << value << endl;
fileAddNode(pos, key, value);
}
for (it = cache.modifyNode.begin(); it != cache.modifyNode.end(); ++it) {
int key = it->first->key;
int pos = it->first->pos;
int value = it->second;
cout << "Modify data in data file, key: " << key << " pos: " << pos << " value: " << value << endl;
fileModifyNode(pos, value);
}
for (int i = 0; i < 0; ++i) {
cout << "Delete value at pos:" << cache.deletePos.top() << endl;
cache.deletePos.pop();
}
cache.modifyNode.clear();
cache.addNode.clear();
}
setIndexFile(tree);
}
void fileModifyNode(int pos, int value) {
fstream dataFile(dataFileName, ios::out | ios::binary);
if (!dataFile) {
cout << "open " << dataFileName << " fail." << endl;
}
int nodePos = (pos * 2 + 1) * sizeof(int);
dataFile.seekg(nodePos, ios::beg);
dataFile.write((char*)& value, sizeof(int));
dataFile.close();
}
void fileAddNode(int pos, int key, int value) {
fstream dataFile(dataFileName, ios::out | ios::binary);
int nodePos = (pos * 2) * sizeof(int) ;
dataFile.seekg(nodePos, ios::beg);
dataFile.write((char*)& key, sizeof(int));
dataFile.write((char*)& value, sizeof(int));
dataFile.close();
}
//-------------------------------------------- end --------------------------------------------
//=============================================================================================
//=============================================================================================
//------------------ function for tree --------------------------------------------------------
void treeFromFile(RBTree & tree, Cache & cache) {
fstream indexFile;
indexFile.open(indexFileName, ios::in | ios::binary);
if (!indexFile) {
// there is no index file, set tree from raw data
treeFromData(tree, cache);
return;
}
// set tree from index file
indexFile.seekg(0, ios::end);
int indexLen = indexFile.tellg();
indexFile.seekg(0, ios::beg);
// the current position of file pointer
int indexPos = 0;
while (indexPos != indexLen) {
int nodeInforLen = 0;
vector<int> nodePos;
int key = -2;
int pos = -2;
int color = -1;
// get the length of the information of the node
indexFile.read((char*)& nodeInforLen, sizeof(int));
int nodePosLen = nodeInforLen - 3 * sizeof(int);
for (int i = 0; i < nodePosLen/4; ++i) {
int temp = 0;
indexFile.read((char*)& temp, sizeof(int));
nodePos.push_back(temp);
}
indexFile.read((char*)& key, sizeof(int));
indexFile.read((char*)& pos, sizeof(int));
indexFile.read((char*)& color, sizeof(int));
indexAddNode(tree, nodePos, key, pos, color);
indexPos = indexFile.tellg();
}
indexFile.close();
}
// add nodes into tree from index, don't need to balance the tree
void indexAddNode(RBTree & tree, vector<int>& nodePos, int key, int pos, int color) {
Node* cur = tree.getRoot();
// if the tree is empty
if (cur == NULL || cur->pos == -1) {
Node* root = new Node(key, pos);
tree.setRoot(root);
tree.setLastPos(1);
return;
}
int posSize = nodePos.size();
for (int i = 0; i < posSize; ++i) {
if (nodePos[i] == LEFT) {
cur = cur->left;
}
else {
cur = cur->right;
}
}
cur->key = key;
cur->pos = pos;
cur->color = color;
cur->left = new Node;
cur->right = new Node;
cur->left->father = cur->right->father = cur;
// update tree's lastPos
// so that when add node it won't make mistake
int lastPos = tree.getLastPos();
tree.setLastPos(lastPos + 1);
}
// set index file
// nodes are managed in level-order
void setIndexFile(RBTree & tree) {
ofstream indexFile(indexFileName, ios::out | ios::binary);
queue<Node*> nodeWindow;
queue<vector<int>> posWindow;
Node* root = tree.getRoot();
// if the tree is empty
if (root == NULL) {
indexFile.close();
return;
}
// initialize two windows
nodeWindow.push(root);
vector<int> rootPos;
posWindow.push(rootPos);
while (nodeWindow.size() != 0) {
Node* topNode = nodeWindow.front();
nodeWindow.pop();
vector<int> topPos = posWindow.front();
posWindow.pop();
// the length of the vector(is also the level of this node)
int posLen = topPos.size();
// the length of the node's information( curPos + key + pos + color )
int nodeInforLen = (3 + posLen) * sizeof(int);
// first write how long the node's information is
// then write every necessary information
indexFile.write((char*)& nodeInforLen, sizeof(int));
for (int i = 0; i < posLen; ++i) {
indexFile.write((char*)& topPos[i], sizeof(int));
}
indexFile.write((char*)&topNode->key, sizeof(int));
indexFile.write((char*)&topNode->pos, sizeof(int));
indexFile.write((char*)&topNode->color, sizeof(int));
Node* lc = topNode->left;
Node* rc = topNode->right;
// don't write leaves into index file
if (lc->pos != -1) {
nodeWindow.push(lc);
vector<int> leftPos = topPos;
leftPos.push_back(LEFT);
posWindow.push(leftPos);
}
if (rc->pos != -1) {
nodeWindow.push(rc);
vector<int> rightPos = topPos;
rightPos.push_back(RIGHT);
posWindow.push(rightPos);
}
}
indexFile.close();
}
// there is no index file, set the tree from raw data
void treeFromData(RBTree & tree, Cache & cache) {
ifstream dataFile(dataFileName, ios::in | ios::binary);
dataFile.seekg(0, ios::end);
int length = dataFile.tellg();
dataFile.seekg(0, ios::beg);
// current position in dataFile
int curPos = 0;
while (curPos != length) {
//dataFile.clear(ios::goodbit);
int nodeKey = -1;
int nodeValue = -1;
dataFile.read((char*)& nodeKey, sizeof(int));
dataFile.read((char*)& nodeValue, sizeof(int));
tree.add(nodeKey, nodeValue, cache);
curPos += NODE_BYTE;
}
//=======================
// delete or not
cache.addNode.clear();
//=======================
dataFile.close();
// set the index file
setIndexFile(tree);
}
//===============================================================
//----------------------------------- check input ---------------
bool isNum(string & str) {
int len = str.size();
string sub = "";
for (int i = 0; i < len; ++i) {
sub = str.substr(i, 1);
if ((sub < "0" || sub > "9") && (sub != "-")) {
cout << "Error: Invalid input!( int only )" << endl;
cout << "Please enter again." << endl;
return false;
}
}
stringstream ss;
ss << str;
long int tempInt = 0;
ss >> tempInt;
if (tempInt > numeric_limits<int>::max()) {
cout << "Error: number out of int range( too big )!" << endl;
cout << "Please enter again." << endl;
return false;
}
if (tempInt < numeric_limits<int>::min()) {
cout << "Error: number out of int range( too small )!" << endl;
cout << "Please enter again." << endl;
return false;
}
return true;
}
//===============================================================
//----------------- check if a data file is opened ---------------
bool haveOpenedFile() {
fstream dataFile(dataFileName, ios::in | ios::binary);
if (!dataFile) {
return false;
}
else {
dataFile.close();
return true;
}
}