Commit 3abea237 authored by Gaurav Kukreja's avatar Gaurav Kukreja

Assign5: Query Compiler, Shared Lib not working

parent bca14067
CC=g++
CCFLAGS=-O0 -fno-stack-protector -std=c++11
CCFLAGS=-O0 -fno-stack-protector -std=c++11 -fpermissive
SOURCES=main.cpp data_migrate.cpp schema_methods.cpp transactions.cpp oltp.cpp schema_constructors.cpp storage.cpp query.cpp
SOURCES= data_migrate.cpp schema_methods.cpp transactions.cpp oltp.cpp schema_constructors.cpp storage.cpp query.cpp
INCLUDE=include/data_migrate.h include/main.h include/transactions.h include/oltp.h include/schema.h include/query.h
fakedb: $(SOURCES) $(INCLUDE)
fakedb_query: main_query.cpp $(SOURCES) $(INCLUDE) gen_query_code.cpp
$(CC) $(CCFLAGS) $^ -o $@
parser:
yacc -d par.y
lex lex.l
$(CC) lex.yy.c y.tab.c -o $@ -lfl
fakedb: main.cpp $(SOURCES) $(INCLUDE)
$(CC) $(CCFLAGS) $^ -o $@ -ldl
parser_clean:
rm -rf lex.yy.c y.tab.c parser
lib_query: fakedb gen_query_code.cpp storage.cpp
$(CC) $(CCFLAGS) -c -fPIC gen_query_code.cpp -o gen_query_code.o
$(CC) $(CCFLAGS) -c -fPIC storage.cpp -o storage.o
$(CC) $(CCFLAGS) -shared -Wl,-soname,lib_query.so.1 -o lib_query.so.1.0.1 gen_query_code.o storage.o
mkdir -p /tmp/lib_query
mv lib_query.so.1.0.1 /tmp/lib_query/lib_query.so.1.0.1
ln -sf /tmp/lib_query/lib_query.so.1.0.1 /tmp/lib_query/lib_query.so.1
ln -sf /tmp/lib_query/lib_query.so.1.0.1 /tmp/lib_query/lib_query.so
clean:
rm -rf fakedb
rm -rf fakedb fakedb_query *.o
run: clean fakedb
./fakedb
......
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include "include/schema.h"
#include "include/storage.h"
class mystring {
public:
char* str;
mystring operator=(char *_str);
mystring operator=(uint64_t val);
int operator==(uint64_t val);
int operator==(const char* _str);
int operator==(mystring _mystr);
};
mystring mystring::operator=(char *_str) {
str = (char*)malloc(sizeof(char)*strlen(_str));
strcpy(str, _str);
return *this;
}
mystring mystring::operator=(uint64_t val) {
str = (char*)malloc(sizeof(char)*20);
sprintf(str, "%llu", val);
return *this;
}
int mystring::operator==(uint64_t val) {
char temp_str[20];
sprintf(temp_str, "%llu", val);
return !strcmp(str, temp_str);
}
int mystring::operator==(const char* _str) {
return !strcmp(str, _str);
}
int mystring::operator==(mystring _mystr) {
return !strcmp(str, _mystr.str);
}
void query2() {
for(vector<warehouse>::iterator hJ_0_iter=warehouse_vect.begin(); hJ_0_iter<warehouse_vect.end(); hJ_0_iter++)
{
mystring w_name;
w_name = hJ_0_iter->w_name;
mystring w_id;
w_id = hJ_0_iter->w_id;
for(vector<district>::iterator hJ_1_iter=district_vect.begin(); hJ_1_iter<district_vect.end(); hJ_1_iter++)
{
mystring d_name;
d_name = hJ_1_iter->d_name;
mystring d_w_id;
d_w_id = hJ_1_iter->d_w_id;
mystring d_tax;
d_tax = hJ_1_iter->d_tax;
if(w_id == d_w_id && w_id == 1)
{
printf("%s\t", w_name.str);
printf("%s\t", d_name.str);
printf("%s\t", d_tax.str);
printf("\n");
}
}
}
}
#ifndef _GEN_QUERY_CODE_H_
#define _GEN_QUERY_CODE_H_
void query2();
#endif //_GEN_QUERY_CODE_H_
\ No newline at end of file
......@@ -16,6 +16,8 @@
#include "include/oltp.h"
#include "include/query.h"
#include <dlfcn.h>
using namespace std;
/*
......@@ -56,6 +58,44 @@ int main(int argc, char* argv[]) {
load_stock_from_file();
load_warehouse_from_file();
void *qcode_handle;
void (*query_fn)(void);
int choice;
while(1)
{
printf("\nChoice :\n1. Run Query\n2.Quit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter path to library\n");
char lib_path[50];
scanf("%s",lib_path);
qcode_handle = dlopen(lib_path, RTLD_LAZY);
if(!qcode_handle)
{
printf("Error in loading library\n%s",dlerror());
continue;
}
query_fn = dlsym(qcode_handle, "query2");
if(!query_fn)
{
printf("Error in loading symbol\n");
continue;
}
query_fn();
dlclose(qcode_handle);
break;
case 2: return 0;
break;
default:
printf("Error !!! \n");
}
}
/*
//while(1) {
int choice;
timeval start_time, end_time, time_taken;
......@@ -100,6 +140,8 @@ int main(int argc, char* argv[]) {
unsigned long int tps;
tps=no_iterations*1000/(time_taken.tv_sec*1000+time_taken.tv_usec/1000);
cout << "Transactions per second are " << tps << endl;
*/
return 0;
}
\ No newline at end of file
#include <iostream>
#include <cstdlib> //for rand
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <vector>
#include <ctime>
#include <sys/time.h>
#include <signal.h>
#include <sys/types.h>
#include "include/schema.h"
#include "include/storage.h"
#include "include/data_migrate.h"
#include "include/oltp.h"
#include "include/query.h"
#include "include/gen_query_code.h"
#include <dlfcn.h>
using namespace std;
/*
ostream &operator<<(ostream &output, const warehouse &o)
{
output << o.w_id << ' ' << o.w_name << ' ' << o.w_zip << endl;
return output;
}*/
volatile bool childRunning=false;
static void SIGCHLD_handler(int sig) {
childRunning=false;
}
void display_order(const order o)
{
printf("%llu\t%llu\t%llu\n",o.o_id, o.o_d_id, o.o_w_id);
return;
}
int warehouses=5;
int main(int argc, char* argv[]) {
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags=0;
sa.sa_handler=SIGCHLD_handler;
sigaction(SIGCHLD,&sa,NULL);
load_customer_from_file();
load_district_from_file();
load_history_from_file();
load_item_from_file();
load_neworder_from_file();
load_order_from_file();
load_orderline_from_file();
load_stock_from_file();
load_warehouse_from_file();
query2();
}
#include <iostream>
#include <stdint.h> // for uint64_t
#include <vector>
......
CC=g++
CCFLAGS=-O0
SOURCES=main.cpp query_code.cpp exp_tree.cpp
HEADERS=include/query_code.h include/exp_tree.h
run_load: query_compiler
./query_compiler
mv gen_query_code.cpp ../fakedb
query_compiler: $(SOURCES) $(HEADERS)
$(CC) $(CCFLAGS) $^ -o $@
clean:
rm -rf query_compiler
\ No newline at end of file
#include <stdio.h>
#include <iostream>
#include <string.h>
#include "include/exp_tree.h"
operation_t exp_root;
/*
* This function hard codes the creation of the tree for
* the given query
*
* select w_name, d_name, d_tax
* from warehouse, district
* where w_id = d_w_id
* and w_id = 1
*/
void build_exp_tree() {
//PRINT Operator
exp_root.oper_type = PRINT;
exp_root.l_op = new operand_t();
exp_root.l_op->operand_type = OPERATION;
exp_root.field_list.push_back((field_t)"w_name");
exp_root.field_list.push_back((field_t)"d_name");
exp_root.field_list.push_back((field_t)"d_tax");
exp_root.l_op->oper = new operation_t();
printf("%s:%d\n", __func__,__LINE__);
//HASH Join Operator
operation_t *hash1 = exp_root.l_op->oper;
hash1->oper_type = HASH_JOIN;
predicate_t pred;
pred.pred_oper_type = EQUALS;
strcpy(pred.pred_l_op, "w_id");
strcpy(pred.pred_r_op, "d_w_id");
hash1->predicate_list.push_back(pred);
//delete l_op;
predicate_t pred2;
pred2.pred_oper_type = EQUALS;
strcpy(pred2.pred_l_op, "w_id");
strcpy(pred2.pred_r_op, "1");
hash1->predicate_list.push_back(pred2);
//delete r_op;
//TABLE_SCAN Operator
exp_root.l_op->oper->l_op = new operand_t();
exp_root.l_op->oper->l_op->operand_type = OPERATION;
exp_root.l_op->oper->l_op->oper = new operation_t();
operation_t* tscan1 = exp_root.l_op->oper->l_op->oper;
tscan1->oper_type = TABLE_SCAN;
tscan1->field_list.push_back((field_t)"w_name");
tscan1->field_list.push_back((field_t)"w_id");
tscan1->l_op = new operand_t();
tscan1->l_op->operand_type = TABLE;
strcpy(tscan1->l_op->table_name,"warehouse");
exp_root.l_op->oper->r_op = new operand_t();
exp_root.l_op->oper->r_op->operand_type = OPERATION;
exp_root.l_op->oper->r_op->oper = new operation_t();
operation_t* tscan2 = exp_root.l_op->oper->r_op->oper;
tscan2->oper_type = TABLE_SCAN;
tscan2->field_list.push_back((field_t)"d_name");
tscan2->field_list.push_back((field_t)"d_w_id");
tscan2->field_list.push_back((field_t)"d_tax");
tscan2->l_op = new operand_t();
tscan2->l_op->operand_type = TABLE;
strcpy(tscan2->l_op->table_name,"district");
}
\ No newline at end of file
#ifndef _EXP_TREE_H_
#define _EXP_TREE_H_
#include <vector>
#include <string>
using namespace std;
class operand_t;
class operation_t;
class predicate_t;
extern void build_exp_tree();
enum oper_enum {
OPER_NONE=0,
PRINT,
SELECT,
TABLE_SCAN,
HASH_JOIN
};
enum operand_enum {
OPERAND_NONE=0,
OPERATION,
TABLE
};
enum pred_oper_enum {
PRED_OPER_NONE = 0,
EQUALS
};
//typedef string pred_operand_t;
class predicate_t {
public:
pred_oper_enum pred_oper_type;
//pred_operand_t pred_l_op;
//pred_operand_t pred_r_op;
char pred_l_op[20];
char pred_r_op[20];
};
typedef string field_t;
class operation_t {
public:
oper_enum oper_type;
operand_t *l_op;
operand_t *r_op;
vector<field_t> field_list; //for PRINT, TABLE_SCAN and SELECT
vector<predicate_t> predicate_list; //for HASH_JOIN
operation_t() {
oper_type = OPER_NONE;
}
};
class operand_t {
public:
operand_enum operand_type;
char table_name[50]; //for TABLE_SCAN
operation_t *oper; //for all other operators
operand_t() {
operand_type = OPERAND_NONE;
}
};
extern operation_t exp_root;
#endif //_EXP_TREE_H_
\ No newline at end of file
#ifndef _EXP_TREE_H_
#define _EXP_TREE_H_
enum node_type_enum {
OPN_PRINT,
OPN_SELECT,
OPN_HASHJOIN,
OPN_TABLESCAN,
OPD_TABLE
}
class node {
node_type_enum type;
node *l_opd;
node *r_opd;
};
#endif //_EXP_TREE_H_
\ No newline at end of file
#ifndef _QUERY_CODE_H_
#define _QUERY_CODE_H_
#include <vector>
#include <string>
#include "exp_tree.h"
using namespace std;
void generate_query_code();
class print_operation;
class select_operation;
class hashJoin_operation;
class tableScan_operation;
class print_operation {
public:
operation_t *oper;
hashJoin_operation *input;
void *consumer;
void consume();
void produce();
};
class select_operation {
public:
operation_t *oper;
void *input;
void *consumer;
void consume();
void produce();
};
class hashJoin_operation {
public:
operation_t *oper;
tableScan_operation *l_input;
tableScan_operation *r_input;
print_operation *consumer;
void consume();
void produce();
};
class tableScan_operation {
public:
operation_t *oper;
void *input;
hashJoin_operation *consumer;
void produce();
};
#endif //_QUERY_CODE_H_
\ No newline at end of file
#include <stdio.h>
#include <iostream>
#include "include/exp_tree.h"
#include "include/query_code.h"
int main()
{
build_exp_tree();
printf("%s:%d\n", __func__,__LINE__);
generate_query_code();
return 0;
}
\ No newline at end of file
#include <stdio.h>
#include "include/exp_tree.h"
#include "include/query_code.h"
#include <iostream>
FILE *qcode_fp;
static int hashJoin_consumption = 0;
static int open_braces = 0;
void indent() {
for(int i=0; i<open_braces; i++)
fprintf(qcode_fp, "\t");
}
void print_operation::produce() {
switch(oper->l_op->oper->oper_type)
{
case HASH_JOIN:
input = new hashJoin_operation();
input->oper = oper->l_op->oper;
input->consumer = this;
input->produce();
break;
default:
printf("ERR: %s: %s: %d\n",__FILE__,__func__,__LINE__);
}
}
void print_operation::consume() {
vector<field_t>::iterator field_iter;
for(field_iter=oper->field_list.begin(); field_iter<oper->field_list.end(); field_iter++)
{
indent();
fprintf(qcode_fp, "printf(\"%%s\\t\", %s.str);\n", (char *)(field_iter->c_str()));
}
indent();
fprintf(qcode_fp, "printf(\"\\n\");\n");
}
void hashJoin_operation::produce() {
switch(oper->l_op->oper->oper_type)
{
case TABLE_SCAN:
l_input = new tableScan_operation();
l_input->oper = oper->l_op->oper;
l_input->consumer = this;
l_input->produce();
break;
default:
printf("ERR: %s: %s: %d\n",__FILE__,__func__,__LINE__);
}
}
void hashJoin_operation::consume() {
if(hashJoin_consumption % 2 ==0) {
hashJoin_consumption++;
switch(oper->r_op->oper->oper_type)
{
case TABLE_SCAN:
r_input = new tableScan_operation();
r_input->oper = oper->r_op->oper;
r_input->consumer = this;
r_input->produce();
break;
default:
printf("ERR: %s: %s: %d\n",__FILE__,__func__,__LINE__);
}
}
else {
hashJoin_consumption++;
if(!oper->predicate_list.empty())
{
vector<predicate_t>::iterator pred_iter;
for(pred_iter=oper->predicate_list.begin();pred_iter < oper->predicate_list.end();pred_iter++)
{
if(pred_iter != oper->predicate_list.begin())
{
fprintf(qcode_fp, " && ");
}
else
{
indent();
fprintf(qcode_fp, "if(");
}
fprintf(qcode_fp, "%s", pred_iter->pred_l_op);
switch(pred_iter->pred_oper_type)
{
case EQUALS:
fprintf(qcode_fp, " == ");
break;
default:
printf("ERR: %s: %s: %d\n",__FILE__,__func__,__LINE__);
}
fprintf(qcode_fp, "%s", pred_iter->pred_r_op);
}
fprintf(qcode_fp, ")\n");
indent();
fprintf(qcode_fp, "{\n");
open_braces++;
}
consumer->consume();
open_braces--;
indent();
fprintf(qcode_fp, "}\n");
}
}
void tableScan_operation::produce() {
switch(oper->l_op->operand_type) {
case TABLE:
indent();
fprintf(qcode_fp, "for(vector<%s>::iterator hJ_%d_iter=%s_vect.begin(); hJ_%d_iter<%s_vect.end(); hJ_%d_iter++)\n", oper->l_op->table_name, hashJoin_consumption, oper->l_op->table_name, hashJoin_consumption, oper->l_op->table_name, hashJoin_consumption);
indent();
fprintf(qcode_fp, "{\n");
open_braces++;
for(vector<field_t>::iterator field_iter=oper->field_list.begin(); field_iter < oper->field_list.end(); field_iter++)
{
indent();
fprintf(qcode_fp, "mystring %s;\n", (char*)(field_iter->c_str()));
indent();
fprintf(qcode_fp, "%s = hJ_%d_iter->%s;\n", (char*)(field_iter->c_str()), hashJoin_consumption, (char*)(field_iter->c_str()));
}
consumer->consume();
open_braces--;
indent();
fprintf(qcode_fp, "}\n");
break;
default:
printf("ERR: %s: %s: %d\n",__FILE__,__func__,__LINE__);
}
}
void generate_query_code() {
operation_t *root = &exp_root;
qcode_fp = fopen("gen_query_code.cpp", "w");
fprintf(qcode_fp, "#include <stdio.h>\n");
fprintf(qcode_fp, "#include <string.h>\n");
fprintf(qcode_fp, "#include <stdint.h>\n");
fprintf(qcode_fp, "#include <stdlib.h>\n\n");
fprintf(qcode_fp, "#include \"include/schema.h\"\n");
fprintf(qcode_fp, "#include \"include/storage.h\"\n\n");
fprintf(qcode_fp, "");
fprintf(qcode_fp, "");
fprintf(qcode_fp, "class mystring {\n");
fprintf(qcode_fp, "public:\n");
fprintf(qcode_fp, "\tchar* str;\n\n");
fprintf(qcode_fp, "\tmystring operator=(char *_str);\n");
fprintf(qcode_fp, "\tmystring operator=(uint64_t val);\n");
fprintf(qcode_fp, "\tint operator==(uint64_t val);\n");
fprintf(qcode_fp, "\tint operator==(const char* _str);\n");
fprintf(qcode_fp, "\tint operator==(mystring _mystr);\n");
fprintf(qcode_fp, "};\n\n");
fprintf(qcode_fp, "mystring mystring::operator=(char *_str) {\n");
fprintf(qcode_fp, "\tstr = (char*)malloc(sizeof(char)*strlen(_str));\n");
fprintf(qcode_fp, "\tstrcpy(str, _str);\n");
fprintf(qcode_fp, "\treturn *this;\n");
fprintf(qcode_fp, "}\n\n");
fprintf(qcode_fp, "mystring mystring::operator=(uint64_t val) {\n");
fprintf(qcode_fp, "\tstr = (char*)malloc(sizeof(char)*20);\n");
fprintf(qcode_fp, "\tsprintf(str, \"%%llu\", val);\n");
fprintf(qcode_fp, "\treturn *this;\n");
fprintf(qcode_fp, "}\n\n");
fprintf(qcode_fp, "int mystring::operator==(uint64_t val) {\n");
fprintf(qcode_fp, "\tchar temp_str[20];\n");
fprintf(qcode_fp, "\tsprintf(temp_str, \"%%llu\", val);\n");
fprintf(qcode_fp, "\treturn !strcmp(str, temp_str);\n");
fprintf(qcode_fp, "}\n\n");
fprintf(qcode_fp, "int mystring::operator==(const char* _str) {\n");
fprintf(qcode_fp, "\treturn !strcmp(str, _str);\n");
fprintf(qcode_fp, "}\n\n");
fprintf(qcode_fp, "int mystring::operator==(mystring _mystr) {\n");
fprintf(qcode_fp, "\treturn !strcmp(str, _mystr.str);\n");
fprintf(qcode_fp, "}\n\n");
fprintf(qcode_fp, "");
fprintf(qcode_fp, "");
indent();
fprintf(qcode_fp, "void query2() {\n");
open_braces++;
print_operation *print = new print_operation();
print->oper = root;
print->produce();
open_braces--;
indent();
fprintf(qcode_fp, "}\n");
fclose(qcode_fp);
}
\ No newline at end of file
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
class mystring {
public:
char *str;
mystring operator=(char *name);
mystring operator=(uint64_t val);
int operator==(uint64_t val);
};
mystring mystring::operator=(char *name )
{
if(!name) {
str=NULL;
}
else {
str=(char*)malloc(sizeof(char)*strlen(name));
strcpy(str, name);
}
return *this;
}
mystring mystring::operator=(uint64_t val)
{
str=(char*)malloc(sizeof(char)*20);
sprintf(str, "%llu", val);
return *this;
}
int mystring::operator==(uint64_t val)
{
char temp_str[20];
sprintf(temp_str, "%llu", val);
return !strcmp(str, temp_str);
}
int main()
{
mystring t;
char p[50]="Gaurav";
t=p;
printf("%s\n",t.str);
t=(uint64_t)200;
printf("%s\n",t.str);
if(t==200)
printf("Equal\n");
else
printf("Not Equal\n");
return 0;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment