-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
328 lines (243 loc) · 7.49 KB
/
main.cpp
File metadata and controls
328 lines (243 loc) · 7.49 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
#include "Api.hpp"
#include "TransactionLayer.hpp"
#include "LogLayer.hpp"
#include "IndexLayer.hpp"
#include "test/Test.hpp"
#define DATABASE_BUFFER_SIZE (100)
int main() {
// Command
char instruction;
// Data file
char pathname[21];
int table_id;
// Buffer
int buf_size;
// Record (key-value pair)
int64_t input_key;
char input_value[120];
// Return value
char ret_val[120];
// For command 'I', 'D'
char a[120];
int64_t in_start, in_end, del_start, del_end;
// For time checking
time_t start, end;
// Result of each operation
int result;
// Transaction
int mode;
// Recovery
int _flag = 0;
int _log_num = 0;
char _log_path[120];
char _logmsg_path[120];
int test_cnt = 0;
/************************** VARIABLES **************************/
// Initialize DB
int BUF_SIZE = 100;
int FLAG = 0;
int LOG_NUM = 0;
char LOG_PATH[20] = { "LOG_FILE.db" };
char LOGMSG_PATH[20] = { "LOG_MSG.txt" };
// Open data file
char DATA_PATH[20] = { "DATA1" };
int TABLE_ID;
// Insert values
char VALUE[120];
// Transaction operations
int TRX_ID[10];
int UPDATE_CNT = 5;
int UPDATE_KEY;
char UPDATE_VALUE[120] = { "UPDATE" };
char UPDATE_VALUE_1[120];
char UPDATE_VALUE_2[120];
/***************************************************************/
// Print usage
print_usage();
printf("> ");
while (scanf("%c", &instruction) != EOF) {
switch (instruction) {
case 'O':
scanf("%s", pathname);
table_id = open_table(pathname);
if (table_id == -1)
printf("Fail to open the file\nFile open fault\n");
else if (table_id == -2)
printf("File name format is wrong\nFile name should be in \"DATA1 - DATA10\"\nFile open fault\n");
else if (table_id == -3)
printf("Memory allocation error in DiskLayer.cpp\n");
else
printf("File open is completed\nTable id : %d\n", table_id);
break;
case 'B':
scanf("%d", &buf_size);
scanf("%d %d %s %s", &_flag, &_log_num, _log_path, _logmsg_path);
result = init_db(buf_size, _flag, _log_num, _log_path, _logmsg_path);
if (result == 0) printf("DB initializing is completed\n");
else if (result == 1) printf("Buffer creation fault\n");
else if (result == 2) printf("DB is already initialized\nDB initializing fault\n");
else if (result == 3) printf("Buffer size must be over 0\nDB initializing fault\n");
else if (result == 4) printf("Lock table initialize error\n");
else if (result == 5) printf("Transaction mutex error\n");
else if (result == 6) printf("Recovery error\n");
else printf("? Error ?\n");
break;
case 'R':
scanf("%d %d %s %s", &_flag, &_log_num, _log_path, _logmsg_path);
index_init_db(DATABASE_BUFFER_SIZE);
init_log(_logmsg_path);
DB_recovery(_flag, _log_num, _log_path);
break;
case 'i':
scanf("%d %lld %[^\n]", &table_id, &input_key, input_value);
start = clock();
result = db_insert(table_id, input_key, input_value);
end = clock();
if (result == 0) {
printf("Insertion is completed\n");
printf("Time : %f\n", (double)(end - start) / CLOCKS_PER_SEC);
}
else if (result == 1) printf("Table_id[%d] file does not exist\n", table_id);
else if (result == 2) printf("Duplicate key <%lld>\nInsertion fault\n", input_key);
break;
case 'f':
scanf("%d %lld", &table_id, &input_key);
start = clock();
result = _find(table_id, input_key, input_value);
end = clock();
if (result == 0) {
printf("Find is completed\n");
printf("key: %lld, value: %s\n", input_key, input_value);
printf("Time : %f\n", (double)(end - start) / CLOCKS_PER_SEC);
}
else if (result == 2) printf("%lld does not exist in table %d\n", input_key, table_id);
break;
case 'd':
scanf("%d %lld", &table_id, &input_key);
start = clock();
result = db_delete(table_id, input_key);
end = clock();
if (result == 0) {
printf("Deletion is completed\n");
printf("Time : %f\n", (double)(end - start) / CLOCKS_PER_SEC);
}
else if (result == 1) printf("Table_id[%d] file does not exist\n", table_id);
else if (result == 2) printf("No such key <%lld>\nDeletion fault\n", input_key);
break;
case 'I':
scanf("%d %lld %lld", &table_id, &in_start, &in_end);
strcpy(a, "a");
start = clock();
for (int64_t i = in_start; i <= in_end; i++) {
sprintf(a, "%lld", i);
result = db_insert(table_id, i, a);
if (result == 2) printf("Duplicate key <%ld>\nInsertion fault\n", i);
}
end = clock();
printf("Time : %f\n", (double)(end - start) / CLOCKS_PER_SEC);
break;
case 'D':
scanf("%d %lld %lld", &table_id, &del_start, &del_end);
start = clock();
for (int64_t i = del_start; i <= del_end; i++) {
result = db_delete(table_id, i);
if (result == 2) printf("No such key <%lld>\nDeletion fault\n", i);
}
end = clock();
printf("Time : %f\n", (double)(end - start) / CLOCKS_PER_SEC);
break;
// Test
case 'u':
// Initialize DB
init_db(BUF_SIZE, FLAG, LOG_NUM, LOG_PATH, LOGMSG_PATH);
printf("SUCCESS :: INITIALIZE DB\n");
// Open data file
TABLE_ID = open_table(DATA_PATH);
printf("SUCCESS :: OPEN DATA FILE\n");
// Insert values
for (int64_t i = 0; i <= 1000; i++) {
sprintf(VALUE, "%lld", i);
db_insert(TABLE_ID, i, VALUE);
}
printf("SUCCESS :: INSERT VALUES\n");
// Transaction operations
for (int i = 0; i < 10; i++) {
TRX_ID[i] = trx_begin();
for (int j = 0; j < UPDATE_CNT; j++) {
UPDATE_KEY = rand() % 1000 + 1;
strcpy(UPDATE_VALUE_1, UPDATE_VALUE);
sprintf(UPDATE_VALUE_2, "%d", UPDATE_KEY);
strcat(UPDATE_VALUE_1, UPDATE_VALUE_2);
db_update(TABLE_ID, UPDATE_KEY, UPDATE_VALUE_1, TRX_ID[i]);
}
// COMMIT Transaction 1, 3, 5, 7
if (TRX_ID[i] % 2 == 1 && TRX_ID[i] != 9) trx_commit(TRX_ID[i]);
// ABORT Transaction 2, 4, 6, 10
else if (TRX_ID[i] % 2 == 0 && TRX_ID[i] != 8) trx_abort(TRX_ID[i]);
// Do not COMMIT OR ABORT Transaction 8, 9
else continue;
// FLUSH DATA BUFFER TO DATA DISK
if (TRX_ID[i] == 4) db_flush(TABLE_ID);
}
printf("SUCCESS :: TEST\n");
break;
case 'L':
write_log(0, 0);
break;
case 'T':
srand(123);
scanf("%d", &mode);
if (mode == 1) single_thread_test();
else if (mode == 2) slock_test();
else if (mode == 3) xlock_test();
else if (mode == 4) mlock_test();
else if (mode == 5) deadlock_test();
break;
case 't':
db_print_table_list();
break;
case 'p':
scanf("%d", &table_id);
db_print(table_id);
break;
case 'l':
scanf("%d", &table_id);
db_print_leaf(table_id);
break;
case 'C':
scanf("%d", &table_id);
result = close_table(table_id);
if (result == 0) printf("Close is completed\n");
else if (result == 1) printf("File having table_id[%d] does not exist\nClose fault\n", table_id);
else printf("Close fault\n");
break;
case 'S':
result = shutdown_db();
if (result == 0) printf("Shutdown is completed\n");
else if (result == 1) printf("Buffer does not exist\nShutdown is completed\n");
else printf("Shutdown fault\n");
break;
case 'Q':
while (getchar() != (int)'\n');
result = shutdown_db();
if (result == 0) printf("Shutdown is completed\n");
else if (result == 1) printf("Buffer does not exist\nShutdown is completed\n");
else printf("Shutdown fault\n");
return EXIT_SUCCESS;
case 'F':
scanf("%d", &table_id);
db_flush(table_id);
break;
case 'U':
print_usage();
break;
default:
printf("Invalid Command\n");
break;
}
while (getchar() != (int)'\n');
printf("> ");
}
printf("\n");
return EXIT_SUCCESS;
}