-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.c
More file actions
124 lines (109 loc) · 2.85 KB
/
main.c
File metadata and controls
124 lines (109 loc) · 2.85 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
#define _GNU_SOURCE
#include "parser.h"
#include "query.h"
#include "catalog.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <fstream>
// global objects
Catalog *cat;
Buffer_Manager *buf_mng;
time_t now();
//consider using the GNU Readline library instead of myreadline()
//it has a similar interface, and you'll get nice features like working
//cursor keys and (if initialized properly) history.
//run "man 3 readline" for more information
char* myreadline(char* prompt);
int main(int argc, char** argv){
mkdir("data",0777);
if(argc!=2){
printf("Usage: %s <number of buffers>\n", *argv);
printf("Buffers defaults to 3.\n");
buf_mng=new Buffer_Manager();
}else{
int ret = strtol(*(argv+1), NULL,10);
if(ret==0 || ret < 0){
printf("The number of buffers should be a positive integer!\n");
return 0;
}else{
printf("Using %d Buffers\n", ret);
buf_mng =new Buffer_Manager(ret);
}
}
//load catalog from the "CATALOG" file
cat=new Catalog();
{
std::ifstream ifs("data/CATALOG");
if(ifs.good()){
boost::archive::text_iarchive ia(ifs);
ia >> *cat;
}else{
fopen("data/CATALOG", "w");
}
}
//Catalog catalog;
char* input=NULL;
statement_t* parsed=NULL;
int stillrunning=1;
int timing=0;
while (stillrunning && (input=myreadline(">> ")) != 0){
parsed=parse_statement(input);
if (parsed){
time_t begintime=now();
/*
* dispatch_print is an example of how to work with the AST to detect
* and run the various commands, as well as how to get at all of the
* information in the AST. Remove this call when you implement the
* logic to actually run the commands in the DBMS.
* (You can use the functions in print.c for debugging purposes)
*/
dispatch_query(parsed);
if(parsed->set && parsed->set->variable == CONFIG_TIMER)
timing = parsed->set->value;
if(parsed->parameterless == CMD_EXIT){
stillrunning=0;
}
time_t endtime=now();
if (timing)
printf("Elapsed time: %dus\n", endtime-begintime);
}else{
/* There was a syntax error, and the parser has already
* printed an error message, so nothing to do here.*/
}
free(input);
input=NULL;
free_statement(parsed);
parsed=NULL;
}
{
//persisting catalog data
std::ofstream ofs("data/CATALOG");
boost::archive::text_oarchive oa(ofs);
oa << *cat;
}
delete buf_mng;
delete cat;
return 0;
}
char* myreadline(char* prompt){
char* input=NULL;
size_t inputlength=0;
printf("%s",prompt);
if (getline(&input, &inputlength, stdin) != -1){
return input;
}
else{
return input;
}
}
time_t now(){
struct timeval t;
gettimeofday(&t,0);
return t.tv_sec*1000000+t.tv_usec;
}