Skip to content

Commit

Permalink
Merge pull request #24 from AnargyrosArg/anarg
Browse files Browse the repository at this point in the history
Anarg
  • Loading branch information
AnargyrosArg authored Dec 11, 2022
2 parents 733ad8e + e6db100 commit 89ef947
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 82 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ $(BUILD_DIR)/%.o : $(SRC_DIR)/%.c
test: $(OBJ_FILES) $(TEST_DIR)
cd $(TEST_DIR) && make

test_valgrind: $(OBJ_FILES) $(TEST_DIR)
cd $(TEST_DIR) && make run_tests_valgrind

#Rule to run individual test , eg: make test_hash1
test_%: $(BUILD_DIR)/%.o $(TEST_DIR)
cd $(TEST_DIR) && make $@
Expand Down
4 changes: 2 additions & 2 deletions include/intermediates.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ typedef struct Intermediate Intermediate;
struct Intermediates {
size_t count;
size_t capacity;
Intermediate* intermediates;
Intermediate** intermediates;
};

// array of rowid arrays
Expand All @@ -43,7 +43,7 @@ void delete_intermediates(Intermediates* inter_array);
void relation_to_intermediate(table* tabl,int rel,int actualid,Intermediate** result);
void set_intermediate(Intermediate* inter,int rowid_count,bool rels[MAX_RELS_PER_QUERY]);
void get_intermediates(Intermediates* intermediates, uint relation_index,int actualid, Intermediate** ret,table* tabl);
int in_same_intermediate_relation(Intermediates* inter ,int rel1 , int rel2);
int in_same_intermediate_relation(Intermediates* inter ,int rel1 , int rel2,Intermediate** ret);
void insert_intermediate(Intermediate* joinres,Intermediates* intermediates);
void remove_intermediate(Intermediate* res,Intermediates* intermediates);
#endif
22 changes: 10 additions & 12 deletions src/execqueries.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ void printsum(int rel, int column, Intermediates* inter, table *tabl,int actuali
else{
printf("%lu",sum);
}

return;
}


void selfjoin(int rel1,int rel2,uint col1,uint col2,Intermediate* inter,table* tabl,QueryInfo* query,Intermediates* intermediates,Intermediate** res){
Intermediate* selfjoin(int rel1,int rel2,uint col1,uint col2,Intermediate* inter,table* tabl,QueryInfo* query,Intermediates* intermediates){
Intermediate* res = malloc(sizeof(Intermediate));
//we have to get the actual realtion from the table, remember that the int rel is the id of the relation in the query, not the actual id of the relation in the table
int actualid1 = query->rel_ids[rel1];
int actualid2 = query->rel_ids[rel2];
Expand All @@ -46,19 +46,20 @@ void selfjoin(int rel1,int rel2,uint col1,uint col2,Intermediate* inter,table* t
}
}

init_intermediate(*res);
set_intermediate(*res,joinres.result_size,inter->valid_rels);
init_intermediate(res);
set_intermediate(res,joinres.result_size,inter->valid_rels);
int counter=0;
for(int i=0;i<inter->rowids_count;i++){
if(inter->rowids[i][rel1] == joinres.pairs[counter].key1 && inter->rowids[i][rel2] == joinres.pairs[counter].key2){
for(int k=0;k<MAX_RELS_PER_QUERY;k++){
(*res)->rowids[counter][k] = inter->rowids[i][k];
(res)->rowids[counter][k] = inter->rowids[i][k];
}
counter++;
}
}
//we free the memory of relations each time
delete_result(&joinres);
return res;
}

void exec_query(QueryInfo *query, table* tabl){
Expand Down Expand Up @@ -88,7 +89,7 @@ void exec_query(QueryInfo *query, table* tabl){
filter_intermediate(intermediate,&filter_result,op,value,rel,col,tabl,actualid);
remove_intermediate(intermediate,intermediates);
insert_intermediate(filter_result,intermediates);
free(filter_result);
// free(filter_result);
current_filter = current_filter->next;
}

Expand All @@ -109,14 +110,11 @@ void exec_query(QueryInfo *query, table* tabl){
Intermediate* inter2;

int index;
if((index = in_same_intermediate_relation(intermediates,rel1,rel2)) != -1){
if((index = in_same_intermediate_relation(intermediates,rel1,rel2,&inter1)) != -1){
//get common intermediate , and perform join as a filter on it
inter1 = &(intermediates->intermediates[index]);
Intermediate* selfjoin_result;
selfjoin(rel1,rel2,col1,col2,inter1,tabl,query,intermediates,&selfjoin_result);
Intermediate* selfjoin_result= selfjoin(rel1,rel2,col1,col2,inter1,tabl,query,intermediates);
remove_intermediate(inter1,intermediates);
insert_intermediate(selfjoin_result,intermediates);
free(selfjoin_result);
}else{
get_intermediates(intermediates,rel1,actualid1,&inter1,tabl);
get_intermediates(intermediates,rel2,actualid2,&inter2,tabl);
Expand All @@ -125,7 +123,7 @@ void exec_query(QueryInfo *query, table* tabl){
remove_intermediate(inter1,intermediates);
remove_intermediate(inter2,intermediates);
insert_intermediate(joinres,intermediates);
free(joinres);
//free(joinres);
}


Expand Down
41 changes: 20 additions & 21 deletions src/intermediates.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ Intermediates* init_intermediates() {
Intermediates* inter_array = malloc(sizeof(Intermediates));
inter_array->count = 0;
inter_array->capacity = START_N_INTERMEDIATES;
inter_array->intermediates = malloc(START_N_INTERMEDIATES * sizeof(Intermediate));
inter_array->intermediates = malloc(START_N_INTERMEDIATES * sizeof(Intermediate*));
for(int i=0;i<START_N_INTERMEDIATES;i++){
inter_array->intermediates[i].rowids_count= -1 ;
inter_array->intermediates[i].rowids = NULL;
inter_array->intermediates[i]= NULL;
}
return inter_array;
}

void delete_intermediates(Intermediates* inter_array) {
for(int i=0;i<inter_array->capacity;i++){
delete_intermediate(&(inter_array->intermediates[i]));
if(inter_array->intermediates[i]!=NULL){
delete_intermediate(inter_array->intermediates[i]);
free(inter_array->intermediates[i]);
}
}
free(inter_array->intermediates);
inter_array->capacity=0;
inter_array->count=0;
free(inter_array);
return;
}
Expand All @@ -44,17 +44,14 @@ void set_intermediate(Intermediate* inter,int rowid_count,bool rels[MAX_RELS_PER
}

void delete_intermediate(Intermediate* inter){
if(inter->rowids!=NULL){
for(int i=0;i<inter->rowids_count;i++){
free(inter->rowids[i]);
}
for(int i=0;i<inter->rowids_count;i++){
free(inter->rowids[i]);
}
free(inter->rowids);
}

//creates a relation from given intermediate, but this time the keys in the relation correspond to intermediate indexes , not actual relation keys
relation intermediate_to_relation(Intermediate* inter,int rel , int col,table* tabl,QueryInfo* query){

if(!inter->valid_rels[rel]){
fprintf(stderr,"Invalid intermediate to instantiate relation\n");
exit(-1);
Expand All @@ -76,8 +73,8 @@ relation intermediate_to_relation(Intermediate* inter,int rel , int col,table* t

void get_intermediates(Intermediates* intermediates, uint relation_index,int actualid, Intermediate** ret,table* tabl) {
for (int i=0; i<intermediates->count; i++){
if (intermediates->intermediates[i].valid_rels[relation_index]) {
(*ret) = &(intermediates->intermediates[i]);
if (intermediates->intermediates[i]!=NULL && intermediates->intermediates[i]->valid_rels[relation_index]) {
(*ret) = intermediates->intermediates[i];
return;
}
}
Expand All @@ -104,20 +101,22 @@ void relation_to_intermediate(table* tabl,int rel,int actualid,Intermediate** re
}


int in_same_intermediate_relation(Intermediates* inter ,int rel1 , int rel2){
int in_same_intermediate_relation(Intermediates* inter ,int rel1 , int rel2,Intermediate** ret){
for(int i=0;i<inter->count;i++){
if(inter->intermediates[i].valid_rels[rel1] && inter->intermediates[i].valid_rels[rel2]){
if(inter->intermediates[i]!=NULL && inter->intermediates[i]->valid_rels[rel1] && inter->intermediates[i]->valid_rels[rel2]){
*ret = inter->intermediates[i];
return i;
}
}
*ret =NULL;
return -1;
}


void insert_intermediate(Intermediate* res,Intermediates* intermediates){
for(int i=0;i<intermediates->capacity;i++){
if(intermediates->intermediates[i].rowids==NULL){
intermediates->intermediates[i] = *res;
if(intermediates->intermediates[i]==NULL){
intermediates->intermediates[i] = res;
intermediates->count++;
return;
}
Expand All @@ -128,10 +127,10 @@ void insert_intermediate(Intermediate* res,Intermediates* intermediates){

void remove_intermediate(Intermediate* res,Intermediates* intermediates){
for(int i=0;i<intermediates->capacity;i++){
if(intermediates->intermediates[i].rowids==res->rowids){
delete_intermediate(res);
intermediates->intermediates[i].rowids=NULL;
intermediates->intermediates[i].rowids_count =-1;
if(intermediates->intermediates[i]!=NULL && intermediates->intermediates[i]->rowids == res->rowids){
delete_intermediate(intermediates->intermediates[i]);
free(intermediates->intermediates[i]);
intermediates->intermediates[i] = NULL;
intermediates->count--;
return;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ run_tests: $(TESTS)
$(foreach test,$(TESTS),@echo ==========$(test)==========$(\n)@./$(test)$(\n))
@echo ====================================

run_tests_valgrind: $(TESTS)
$(foreach test,$(TESTS),@echo ==========$(test)==========$(\n)@valgrind ./$(test)$(\n))
@echo ====================================


%: %.c
gcc $(GCC_FLAGS) $@.c $(BUILD_DIR)/$(@:_tests=.o) -o $@

Expand Down
38 changes: 5 additions & 33 deletions tests/filter_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,8 @@
#include "intermediates.h"


void list_init_test(void) {
list* l = init_list();
TEST_ASSERT(l->row_id == -1);
TEST_ASSERT(l->payload == -1);
TEST_ASSERT(l->next == NULL);
TEST_ASSERT(l->tail == l);
delete_list(l);
}

void list_append_test(void) {
list* l = init_list();
list_append(l, 1, 2);
TEST_ASSERT(l->row_id == 1);
TEST_ASSERT(l->payload == 2);
TEST_ASSERT(l->next == NULL);
TEST_ASSERT(l->tail == l);
list_append(l, 3, 4);
TEST_ASSERT(l->row_id == 1);
TEST_ASSERT(l->payload == 2);
TEST_ASSERT(l->next->row_id == 3);
TEST_ASSERT(l->next->payload == 4);
TEST_ASSERT(l->next->next == NULL);
TEST_ASSERT(l->tail->row_id == 3);
TEST_ASSERT(l->tail->payload == 4);
TEST_ASSERT(l->tail->next == NULL);
delete_list(l);
}



void filter_test(void){

//int* test = malloc(sizeof(int)*100);
relation r;
read_file(&r,"./relations/filterte.txt");
relation ret;
Expand All @@ -49,12 +19,14 @@ void filter_test(void){
better_filter_function(&r,&ret2,_LESS,1);
int result2 = ret2.num_tuples;
TEST_ASSERT(result2 == 5);

delete_relation(r);
delete_relation(ret);
delete_relation(ret2);
}

TEST_LIST = {
{ "Basic Filter Functionality",filter_test},
{ "Test List Init", list_init_test },
{ "Test List Append", list_append_test },
{ NULL, NULL }
};

31 changes: 17 additions & 14 deletions tests/intermediates_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@ void basic_intermediate_functionality(void){
Intermediates* inters = init_intermediates();
bool val_rels[4] = {true , false , false ,true};
bool val_rels2[4] = {true , true , false ,false};

table* tabl=NULL;


table* tabl;
Intermediate int1;
init_intermediate(&int1);
set_intermediate(&int1,100,val_rels);
insert_intermediate(&int1,inters);
Intermediate* int1= malloc(sizeof(Intermediate));
init_intermediate(int1);
set_intermediate(int1,100,val_rels);
insert_intermediate(int1,inters);

Intermediate int2;
init_intermediate(&int2);
set_intermediate(&int2,100,val_rels2);
insert_intermediate(&int2,inters);
Intermediate* int2 = malloc(sizeof(Intermediate));
init_intermediate(int2);
set_intermediate(int2,100,val_rels2);
insert_intermediate(int2,inters);

Intermediate* test;
get_intermediates(inters,0,0, &test,tabl);

TEST_ASSERT(test->rowids == int1.rowids);
TEST_ASSERT(test->rowids == int1->rowids);
Intermediate* temp;

TEST_ASSERT(in_same_intermediate_relation(inters ,0,3,&temp) == 0);
TEST_ASSERT(in_same_intermediate_relation(inters ,0,1,&temp) == 1);
TEST_ASSERT(in_same_intermediate_relation(inters ,0,2,&temp) == -1);

TEST_ASSERT(in_same_intermediate_relation(inters ,0,3) == 0);
TEST_ASSERT(in_same_intermediate_relation(inters ,0,1) == 1);
TEST_ASSERT(in_same_intermediate_relation(inters ,0,2) == -1);

delete_intermediates(inters);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/join_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ void join_test(void){
read_file(&relB,"./relations/2pass_required.txt");
result res = joinfunction(relA,relB);
TEST_ASSERT(res.result_size == 1593);
delete_relation(relA);
delete_relation(relB);
delete_result(&res);
return;
}

TEST_LIST = {
Expand Down
2 changes: 2 additions & 0 deletions tests/partition_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ void partition_test(void){
delete_relation(relA);
delete_relation(relB);
delete_part_info(info);
delete_relation(info.relA_info.ordered_rel);
delete_relation(info.relB_info.ordered_rel);

}

Expand Down

0 comments on commit 89ef947

Please sign in to comment.