void UT_quit( void ) { int exit_val = EXIT_SUCCESS; if ( HF_CloseFile( this_db.attrCatDesc ) != HFE_OK ) { HF_PrintError( "Couldn't close attrCat" ); exit_val = EXIT_FAILURE; } if ( HF_CloseFile( this_db.relCatDesc ) != HFE_OK ) { HF_PrintError( "Couldn't close relCat" ); exit_val = EXIT_FAILURE; } if ( HF_CloseFile( this_db.viewAttrCatDesc ) != HFE_OK ) { HF_PrintError( "Couldn't close viewAttrCat" ); exit_val = EXIT_FAILURE; } if ( HF_CloseFile( this_db.viewCatDesc ) != HFE_OK ) { HF_PrintError( "Couldn't close viewCat" ); exit_val = EXIT_FAILURE; } printf( "Bye!\n\n" ); exit( exit_val ); }
int updateRelCat( relDesc * relInfo ) { char * record, * pch; if ( relInfo == NULL ) { return AMINIREL_GE; } /* Dhmiourgoume to record san ena megalo string, pou tha periexei me oles tis aparaithtes plhrofories, to opoio 8a perastei sto HF epipedo */ if (( record = malloc( sizeof( relDesc ) ) ) == NULL ) { printf( MEM_ERROR ); return AMINIREL_GE; } /* Fortwma twn plhroforiwn sthn eggrafh */ memcpy( record, relInfo->relname, sizeof( relInfo->relname ) ); pch = record + sizeof( relInfo->relname ); memcpy( pch, &( relInfo->relwidth ), sizeof( relInfo->relwidth ) ); pch += sizeof( relInfo->relwidth ); memcpy( pch, &( relInfo->attrcnt ), sizeof( relInfo->attrcnt ) ); pch += sizeof( relInfo->attrcnt ); memcpy( pch, &( relInfo->indexcnt ), sizeof( relInfo->indexcnt ) ); /* eisagoume thn eggrafh pou periexei tis plhrofories gia ton eayto toy */ if ( HF_InsertRec( this_db.relCatDesc, record, sizeof( relDesc ) ) < HFE_OK ) { HF_PrintError( "Couldn't insert in relCat" ); free( record ); return AMINIREL_GE; } free( record ); return AMINIREL_OK; }
int UT_Init( char * dbname ) { /* Arxikopoioume ta katwtera epipeda */ HF_Init(); AM_Init(); /* kai metaferomaste sto fakelo ths database */ if ( chdir( dbname ) ) { printf( "Error: Database %s does not exist", dbname ); return AMINIREL_GE; } /* initialize twn fileDescriptors se non valid timh */ this_db.attrCatDesc = this_db.relCatDesc = this_db.viewAttrCatDesc = this_db.viewCatDesc = AMINIREL_INVALID; /* Anoigma twn 4 vasikwn arxeiwn mias database pou tha mas xreiastoun argotera */ if (( this_db.attrCatDesc = HF_OpenFile( "attrCat" ) ) < HFE_OK ) { HF_PrintError( "Couldn't open attrCat" ); return AMINIREL_GE; } if (( this_db.relCatDesc = HF_OpenFile( "relCat" ) ) < HFE_OK ) { HF_PrintError( "Couldn't open relCat" ); return AMINIREL_GE; } if (( this_db.viewAttrCatDesc = HF_OpenFile( "viewAttrCat" ) ) < HFE_OK ) { HF_PrintError( "Couldn't open viewAttrCat" ); return AMINIREL_GE; } if (( this_db.viewCatDesc = HF_OpenFile( "viewCat" ) ) < HFE_OK ) { HF_PrintError( "Couldn't open viewCat" ); return AMINIREL_GE; } return AMINIREL_OK; }
int main() { int athletesFd; int eventsFd; int partsFd; int athletesIndexFd0; int athletesIndexFd1; int athletesIndexFd2; int eventsIndexFd0; int eventsIndexFd1; int partsIndexFd0; int partsIndexFd1; int partsIndexFd2; int recId; char *athleteRec; char *eventRec; char *partRec; int athId; char surname[AM_MAIN_NAME_SIZE]; char name[AM_MAIN_NAME_SIZE]; int eventId; char eventName[AM_MAIN_EVENT_NAME_SIZE]; char partDate[AM_MAIN_DATE_SIZE]; /* Αρχικοποίηση Επιπέδου HF */ HF_Init(); /* Αρχικοποίηση Επιπέδου AM */ AM_Init(); /* ’νοιγμα Αρχείου ATHLETES */ athletesFd = HF_OpenFile("ATHLETES"); if (athletesFd < 0) { HF_PrintError("Error in HF_OpenFile called on ATHLETES."); return -1; } /* ’νοιγμα Αρχείου EVENTS */ eventsFd = HF_OpenFile("EVENTS"); if (eventsFd < 0) { HF_PrintError("Error in HF_OpenFile called on EVENTS."); return -1; } /* ’νοιγμα Αρχείου PARTICIPATIONS */ partsFd = HF_OpenFile("PARTICIPATIONS"); if (partsFd < 0) { HF_PrintError("Error in HF_OpenFile called on PARTICIPATIONS."); return -1; } /*Δημιουργία ευρετηρίου στο 1ο πεδίο του αρχείου ATHLETES*/ if (AM_CreateIndex("ATHLETES", 0, 'i', sizeof(int)) != AME_OK) { AM_PrintError("Error in AM_CreateIndex called on ATHLETES.0."); } /*’νοιγμα του 1ου ευρετηρίου στο αρχείο ATHLETES*/ athletesIndexFd0 = AM_OpenIndex("ATHLETES", 0); if (athletesIndexFd0 < 0) { AM_PrintError("Error in AM_OpenIndex called on ATHLETES.0."); return -1; } /*Δημιουργία ευρετηρίου στο 2ο πεδίο του αρχείου ATHLETES*/ if (AM_CreateIndex("ATHLETES", 1, 'c', AM_MAIN_NAME_SIZE) != AME_OK) { AM_PrintError("Error in AM_CreateIndex called on ATHLETES.1."); } /* //’νοιγμα του 2ου ευρετηρίου στο αρχείο ATHLETES*/ athletesIndexFd1 = AM_OpenIndex("ATHLETES", 1); if (athletesIndexFd1 < 0) { AM_PrintError("Error in AM_OpenIndex called on ATHLETES.1."); return -1; } /* //Δημιουργία ευρετηρίου στο 3ο πεδίο του αρχείου ATHLETES */ if (AM_CreateIndex("ATHLETES", 2, 'c', AM_MAIN_NAME_SIZE) != AME_OK) { AM_PrintError("Error in AM_CreateIndex called on ATHLETES.2."); } /* //’νοιγμα του 3ου ευρετηρίου στο αρχείο ATHLETES*/ athletesIndexFd2 = AM_OpenIndex("ATHLETES", 2); if (athletesIndexFd2 < 0) { AM_PrintError("Error in AM_OpenIndex called on ATHLETES.2."); return -1; } athleteRec = (char *) malloc(AM_MAIN_ATHLETE_REC_SIZE); if (athleteRec == NULL) { printf("Athlete record malloc failed\n"); return -1; } /* // Εισαγωγή δεδομένων στα ευρετήρια του αρχείου ATHLETES*/ recId = HF_GetFirstRec(athletesFd, athleteRec, AM_MAIN_ATHLETE_REC_SIZE); while (recId >= 0) { memcpy((void *) &athId, (void *) athleteRec, sizeof(int)); athleteRec += sizeof(int); strcpy(surname, athleteRec); athleteRec += AM_MAIN_NAME_SIZE; strcpy(name, athleteRec); athleteRec += AM_MAIN_NAME_SIZE; athleteRec -= AM_MAIN_ATHLETE_REC_SIZE; if (AM_InsertEntry(athletesIndexFd0, 'i', sizeof(int), (char *) &athId, recId) != AME_OK) AM_PrintError("Failed to insert entry."); if (AM_InsertEntry(athletesIndexFd1, 'c', AM_MAIN_NAME_SIZE, surname, recId) != AME_OK) AM_PrintError("Failed to insert entry."); if (AM_InsertEntry(athletesIndexFd2, 'c', AM_MAIN_NAME_SIZE, name, recId) != AME_OK) AM_PrintError("Failed to insert entry."); recId = HF_GetNextRec(athletesFd, recId, athleteRec, AM_MAIN_ATHLETE_REC_SIZE); } /* //Δημιουργία ευρετηρίου στο 1ο πεδίο του αρχείου EVENTS*/ if (AM_CreateIndex("EVENTS", 0, 'i', sizeof(int)) != AME_OK) { AM_PrintError("Error in AM_CreateIndex called on EVENTS.0."); } /* //’νοιγμα του 1ου ευρετηρίου στο αρχείο EVENTS*/ eventsIndexFd0 = AM_OpenIndex("EVENTS", 0); if (eventsIndexFd0 < 0) { AM_PrintError("Error in AM_OpenIndex called on EVENTS.0."); return -1; } /* //Δημιουργία ευρετηρίου στο 2ο πεδίο του αρχείου EVENTS*/ if (AM_CreateIndex("EVENTS", 1, 'c', AM_MAIN_EVENT_NAME_SIZE) != AME_OK) { AM_PrintError("Error in AM_CreateIndex called on EVENTS.1."); } /* //’νοιγμα του 2ου ευρετηρίου στο αρχείο EVENTS*/ eventsIndexFd1 = AM_OpenIndex("EVENTS", 1); if (eventsIndexFd1 < 0) { AM_PrintError("Error in AM_OpenIndex called on EVENTS.1."); return -1; } eventRec = (char *) malloc(AM_MAIN_EVENT_REC_SIZE); if (eventRec == NULL) { printf("Event record malloc failed\n"); return -1; } /* // Εισαγωγή δεδομένων στα ευρετήρια του αρχείου EVENTS*/ recId = HF_GetFirstRec(eventsFd, eventRec, AM_MAIN_EVENT_REC_SIZE); while (recId >= 0) { memcpy((void *) &eventId, (void *) eventRec, sizeof(int)); eventRec += sizeof(int); strcpy(eventName, eventRec); eventRec += AM_MAIN_EVENT_NAME_SIZE; eventRec -= AM_MAIN_EVENT_REC_SIZE; if (AM_InsertEntry(eventsIndexFd0, 'i', sizeof(int), (char *) &eventId, recId) != AME_OK) AM_PrintError("Failed to insert entry."); if (AM_InsertEntry(eventsIndexFd1, 'c', AM_MAIN_EVENT_NAME_SIZE, eventName, recId) != AME_OK) AM_PrintError("Failed to insert entry."); recId = HF_GetNextRec(eventsFd, recId, eventRec, AM_MAIN_ATHLETE_REC_SIZE); } /* //Δημιουργία ευρετηρίου στο 1ο πεδίο του αρχείου PARTICIPATIONS*/ if (AM_CreateIndex("PARTICIPATIONS", 0, 'i', sizeof(int)) != AME_OK) { AM_PrintError("Error in AM_CreateIndex called on PARTICIPATIONS.0."); } /* //’νοιγμα του 1ου ευρετηρίου στο αρχείο PARTICIPATIONS*/ partsIndexFd0 = AM_OpenIndex("PARTICIPATIONS", 0); if (partsIndexFd0 < 0) { AM_PrintError("Error in AM_OpenIndex called on PARTICIPATIONS.0."); return -1; } /* //Δημιουργία ευρετηρίου στο 2ο πεδίο του αρχείου PARTICIPATIONS*/ if (AM_CreateIndex("PARTICIPATIONS", 1, 'i', sizeof(int)) != AME_OK) { AM_PrintError("Error in AM_CreateIndex called on PARTICIPATIONS.1."); } /* //’νοιγμα του 2ου ευρετηρίου στο αρχείο PARTICIPATIONS*/ partsIndexFd1 = AM_OpenIndex("PARTICIPATIONS", 1); if (partsIndexFd1 < 0) { AM_PrintError("Error in AM_OpenIndex called on PARTICIPATIONS.1."); return -1; } /* //Δημιουργία ευρετηρίου στο 3ο πεδίο του αρχείου PARTICIPATIONS*/ if (AM_CreateIndex("PARTICIPATIONS", 2, 'c', AM_MAIN_DATE_SIZE) != AME_OK) { AM_PrintError("Error in AM_CreateIndex called on PARTICIPATIONS.2."); } /* //’νοιγμα του 3ου ευρετηρίου στο αρχείο PARTICIPATIONS*/ partsIndexFd2 = AM_OpenIndex("PARTICIPATIONS", 2); if (partsIndexFd2 < 0) { AM_PrintError("Error in AM_OpenIndex called on PARTICIPATIONS.2."); return -1; } partRec = (char *) malloc(AM_MAIN_PARTICIPATION_REC_SIZE); if (partRec == NULL) { printf("Participation record malloc failed\n"); return -1; } /* // Εισαγωγή δεδομένων στα ευρετήρια του αρχείου PARTICIPATIONS*/ recId = HF_GetFirstRec(partsFd, partRec, AM_MAIN_PARTICIPATION_REC_SIZE); while (recId >= 0) { memcpy((void *) &athId, (void *) partRec, sizeof(int)); partRec += sizeof(int); memcpy((void *) &eventId, (void *) partRec, sizeof(int)); partRec += sizeof(int); strcpy(partDate, partRec); partRec += AM_MAIN_DATE_SIZE; partRec -= AM_MAIN_PARTICIPATION_REC_SIZE; if (AM_InsertEntry(partsIndexFd0, 'i', sizeof(int), (char *) &athId, recId) != AME_OK) AM_PrintError("Failed to insert entry."); if (AM_InsertEntry(partsIndexFd1, 'i', sizeof(int), (char *) &eventId, recId) != AME_OK) AM_PrintError("Failed to insert entry."); if (AM_InsertEntry(partsIndexFd2, 'c', AM_MAIN_DATE_SIZE, partDate, recId) != AME_OK) AM_PrintError("Failed to insert entry."); recId = HF_GetNextRec(partsFd, recId, partRec, AM_MAIN_PARTICIPATION_REC_SIZE); } free(athleteRec); free(eventRec); free(partRec); /* //Κλείσιμο Αρχείων*/ if (HF_CloseFile(athletesFd) != HFE_OK) HF_PrintError("Error in HF_CloseFile called on ATHLETES."); if (HF_CloseFile(eventsFd) != HFE_OK) HF_PrintError("Error in HF_CloseFile called on EVENTS."); if (HF_CloseFile(partsFd) != HFE_OK) HF_PrintError("Error in HF_CloseFile called on PARTICIPATIONS."); if (AM_CloseIndex(athletesIndexFd0) != AME_OK) AM_PrintError("Error in AM_CloseIndex called on ATHLETES.0."); if (AM_CloseIndex(athletesIndexFd1) != AME_OK) AM_PrintError("Error in AM_CloseIndex called on ATHLETES.1."); if (AM_CloseIndex(athletesIndexFd2) != AME_OK) AM_PrintError("Error in AM_CloseIndex called on ATHLETES.2."); if (AM_CloseIndex(eventsIndexFd0) != AME_OK) AM_PrintError("Error in AM_CloseIndex called on EVENTS.0."); if (AM_CloseIndex(eventsIndexFd1) != AME_OK) AM_PrintError("Error in AM_CloseIndex called on EVENTS.1."); if (AM_CloseIndex(partsIndexFd0) != AME_OK) AM_PrintError("Error in AM_CloseIndex called on PARTICIPATIONS.0."); if (AM_CloseIndex(partsIndexFd1) != AME_OK) AM_PrintError("Error in AM_CloseIndex called on PARTICIPATIONS.1."); if (AM_CloseIndex(partsIndexFd2) != AME_OK) AM_PrintError("Error in AM_CloseIndex called on PARTICIPATIONS.2."); return 0; }
int VW_CreateSelect(int argc, char* argv[]){ int scanDesc,check,found=0; relDesc recrel; attrDesc recattr; int scanDescattr; char* rec; int utc,j; char** utcargs; //pinakas mesa ston opoion tha ftiaksoume ta orismata ths UT_Create //elegxos gia to an dinetai akribos mia sxesi i parapano if((scanDesc=HF_OpenFileScan(viewfd,sizeof(viewDesc),'c',MAXNAME,0,EQUAL,argv[1]))<0) HF_PrintError("Problem while opening file scan"); check=HF_FindNextRec(scanDesc,rec); while(check!=HFE_EOF){ if(check>0){ found=1; break; } check=HF_FindNextRec(scanDesc,rec); } if(HF_CloseFileScan(scanDesc)<0) HF_PrintError("Error while closing scan"); if(found==0){//an den iparxei i opsi sto viewCat //desmeush xorou gia ton pinaka utcargs gia na enimerosoume ta relcat,attrcat me UT_create utcargs=malloc((2*atoi(argv[2])+2)*sizeof(char**)); for(i=0;i<(2*atoi(argv[2])+2);i++) utcargs[i]=malloc(256*sizeof(char)); sprintf(utcargs[0],"%s","create"); sprintf(utcargs[1],"%s",argv[1]); for(i=2;i<(2*atoi(argv[2])+2);i++)//pername ston utcargs ta onomata ton pedion if(i%2==0) sprintf(utcargs[i],"%s",argv[i+2]); //skanaroume to attrCat gia na broume tous tipous ton pedion kai na tous perasoume ston utcargs j=3; for(i=4;i<=(2*atoi(argv[2])+2);i=i+2){ found=0; if((scanDescattr=HF_OpenFileScan(attrfd,sizeof(attrDesc),'c',MAXNAME,0,EQUAL,argv[3]))<0) HF_PrintError("Problem while opening file scan"); check=HF_FindNextRec(scanDescattr,(char*)recattr); while(check!=HFE_EOF){ //brikame to probalomeno pedio san pedio tou relname if(strcmp(recattr.attrname,argv[i])==0){ recsize+=recattr.attrlength; found=1; if(recattr.attrtype=='c') sprintf(utcargs[j],"%c%d",recattr.attrtype,recattr.attrlength); else sprintf(utcargs[j],"%c",recattr.attrtype); j+=2; //telos kataskeuhs break; } check=HF_FindNextRec(scanDescattr,(char*)&recattr); } if(HF_CloseFileScan(scanDescattr)<0) HF_PrintError("CreateSelect : Error while closing scan"); if(found==0){ printf("REQUESTED FIELD FOR PROJECTION NOT FOUND IN RELCAT... RETYPE SELECTION USING DIFFERENT ARGUMENTS\n"); return 1; } if((utc=UT_create(2*atoi(argv[2])+2,utcargs))!=0){//dimiourgia tis sxesis gia na enimerosoume ta Relcat kai Attrcat printf("View could not be created\n"); return -1; } } else{
attrDesc * getAttribute( char * relName, char * attrName, int * recId ) { attrDesc * attrInfo; char * record, found_it, * pch; int temp_recId; inline cleanup() { if ( record != NULL ) { free( record ); } if ( attrInfo != NULL ) { free( attrInfo ); } } /* initialization */ record = NULL; attrInfo = NULL; ( *recId ) = AMINIREL_INVALID; /* desmeuoume mnhmh gia to struct pou tha krataei tis plhrofories toy attribute */ if (( attrInfo = malloc( sizeof( attrDesc ) ) ) == NULL ) { printf( MEM_ERROR ); cleanup(); return NULL; } /* desmeuoume mnhmh gia to xwro ston opoio tha eisaxthei h eggrafh tou arxeiou attrCat */ if (( record = malloc( sizeof( attrDesc ) ) ) == NULL ) { printf( MEM_ERROR ); cleanup(); return NULL; } /* ksekiname thn anazhthsh ths sxeshs apo thn prwth eggrafh sto arxeio relCat ! */ if (( temp_recId = HF_GetFirstRec( this_db.attrCatDesc, record, sizeof( attrDesc ) ) ) < 0 ) { HF_PrintError( "Could not retrieve first record from file relCat" ); cleanup(); return NULL; } found_it = FALSE; do { /* To onoma ths sxeshs einai sthn arxh tou record */ if ( !strcmp( record, relName ) ) { pch = record; pch += sizeof( attrInfo->relname ); /* To onoma tou attribute akolouthei amesws meta to onoma ths sxeshs */ if ( !strcmp( pch, attrName ) ) { /* An bre8hke, symplirwnoume tis plhrofories sto attrInfo kai to epistrefoume */ /* symplhrwnoume to onoma ths sxeshs */ memset( attrInfo->relname, 0, sizeof( attrInfo->relname ) ); strcpy( attrInfo->relname, relName ); /* kai to onoma tou attribute */ memset( attrInfo->attrname, 0, sizeof( attrInfo->attrname ) ); strcpy( attrInfo->attrname, attrName ); /* to offset */ pch += sizeof( attrInfo->attrname ); memcpy( &( attrInfo->offset ), pch, sizeof( int ) ); /* attrlength */ pch += sizeof( int ); memcpy( &( attrInfo->attrlength ), pch, sizeof( int ) ); /* attrtype */ pch += sizeof( int ); memcpy( &( attrInfo->attrtype ), pch, sizeof( char ) ); /* indexed */ pch += sizeof( char ); memcpy( &( attrInfo->indexed ), pch, sizeof( int ) ); /* indexno */ pch += sizeof( int ); memcpy( &( attrInfo->indexno ), pch, sizeof( int ) ); ( *recId ) = temp_recId; free( record ); return attrInfo; } } } while (( temp_recId = HF_GetNextRec( this_db.attrCatDesc, temp_recId, record , sizeof( attrDesc ) ) ) >= 0 ); if ( temp_recId != HFE_EOF ) { HF_PrintError( "Error in getRelation()\n" ); cleanup(); return NULL; } cleanup(); return NULL; }
int UT_destroy( int argc, char* argv[] ) { int recId, attrcnt, indexcnt, return_val; char * relName, * record; relDesc * relPtr; if ( argc != 2 ) { printf( "Error: Two arguments expected for function UT_destroy\n" ); return AMINIREL_GE; } return_val = AMINIREL_OK; inline cleanup() { if ( record != NULL ) { free( record ); } if ( attrcnt > 0 ) { printf( "Error: De diagraftikan ola ta records twn attributes ths sxeshs '%s'\n", relName ); return_val = AMINIREL_GE; } if ( indexcnt > 0 ) { printf( "Error: De diagraftikan ola ta indeces twn attributes ths sxeshs '%s'\n", relName ); return_val = AMINIREL_GE; } if ( relName != NULL ) { if ( HF_DestroyFile( relName ) != HFE_OK ) { HF_PrintError( "Could not destroy relation in UT_destroy() " ); return_val = AMINIREL_GE; } } } /* Arxikopoihseis */ relName = argv[1]; record = NULL; /* Kanoume retrieve tis plhrofories gia thn relation */ if (( relPtr = getRelation( relName, &recId ) ) == NULL ) { printf( "Error: Cannot destroy relation '%s'. Could not retrieve information about relation.\n", argv[1] ); return_val = AMINIREL_GE; cleanup(); return return_val; } /* an th vrhkame, swzoume posa attributes kai indeces exei, kai th diagrafoume */ attrcnt = relPtr->attrcnt; indexcnt = relPtr->indexcnt; free( relPtr ); /* Diagrafoume thn eggrafh sto relCat */ if ( HF_DeleteRec( this_db.relCatDesc, recId, sizeof( relDesc ) ) != HFE_OK ) { HF_PrintError( "Error in UT Destroy: Could not delete record from relCat\n" ); return_val = AMINIREL_GE; cleanup(); return return_val; } /* An th vrhkame sto relCat arxeio, anoigoume to attrCat arxeio gia na diagrapsoume ola ta records gia ta attrcnt synolika attributes ths */ /* sto record tha apothikeyetai h eggrafh tou arxeiou attrCat */ if (( record = malloc( sizeof( attrDesc ) ) ) == NULL ) { printf( MEM_ERROR ); return_val = AMINIREL_GE; cleanup(); return return_val; } /* Ksekiname to psaksimo apo thn arxh tou arxeiou */ if (( recId = HF_GetFirstRec( this_db.attrCatDesc, record, sizeof( attrDesc ) ) ) < 0 ) { HF_PrintError( "Could not retrieve first Record from file relCat" ); return_val = AMINIREL_GE; cleanup(); return return_val; } do { /* To onoma ths sxeshs einai sthn arxh tou record */ if ( !strcmp( record, relName ) ) { char * pch; int indexed; /* to pch twra deixnei sto 'indexed' melos tou struct attrDesc */ pch = record + 2 * MAXNAME * sizeof( char ) + 2 * sizeof( int ) + sizeof( char ); memcpy( &indexed, pch, sizeof( int ) ); /* An exei eurethrio, prepei na diagrafei kai auto */ if ( indexed == TRUE ) { int indexno; /* to pch twra deixnei sto indexno melos tou struct attrDesc */ pch += sizeof( int ); memcpy( &indexno, pch, sizeof( int ) ); /* diagrafoume to eurethrio */ if ( AM_DestroyIndex( relName, indexno ) != AME_OK ) { AM_PrintError( "Could not delete index: " ); return_val = AMINIREL_GE; cleanup(); return return_val; } indexcnt--; } /* diagrafoume thn eggrafh tou attribute sto attrCat */ if ( HF_DeleteRec( this_db.attrCatDesc, recId, sizeof( attrDesc ) ) != HFE_OK ) { return_val = AMINIREL_GE; cleanup(); return return_val; } attrcnt--; } } while (( recId = HF_GetNextRec( this_db.attrCatDesc, recId, record , sizeof( attrDesc ) ) ) >= 0 ); if ( recId != HFE_EOF ) { HF_PrintError( "Error in UT Destroy while deleting records from file attrCat\n" ); return_val = AMINIREL_GE; cleanup(); return return_val; } cleanup(); return return_val; }
int UT_buildindex( int argc, char* argv[] ) { relDesc * relInfo; attrDesc * attrInfo; char * relName, * attrName, * record, * value; int relRecId, attrRecId, return_val, recId, indexFileDesc, fileDesc; if ( argc != 3 ) { printf( "Error: Three arguments expected for function UT_buildindex\n" ); return AMINIREL_GE; } inline cleanup() { /* apodesmeush dynamikhs mnhmhs */ if ( relInfo != NULL ) { free( relInfo ); } if ( attrInfo != NULL ) { free( attrInfo ); } if ( record != NULL ) { free( record ); } /* Kleinoyme arxeia pou isws anoiksame */ /* kleinoyme to arxeio ths sxeshs */ if ( fileDesc >= 0 ) { if ( HF_CloseFile( fileDesc ) != HFE_OK ) { HF_PrintError( "Could not close relation's file" ); return_val = AMINIREL_GE; } } /* kleinoyme to eyrethrio */ if ( indexFileDesc >= 0 ) { if ( AM_CloseIndex( indexFileDesc ) != AME_OK ) { AM_PrintError( "Could not close index" ); return_val = AMINIREL_GE; } } } /* initialization */ relName = argv[1]; attrName = argv[2]; relInfo = NULL; attrInfo = NULL; record = NULL; return_val = AMINIREL_OK; fileDesc = indexFileDesc = AMINIREL_INVALID; /* kanoume retrieve tis plhrofories gia th sxesh me onoma relName */ if (( relInfo = getRelation( relName, &relRecId ) ) == NULL ) { printf( "Error: Could not retrieve information about relation '%s'\n", relName ); return AMINIREL_GE; } /* Mias kai mporoume na exoume to poly 1 eurethrio gia kathe attribute, an o arithmos twn eurethriwn isoutai me ton arithmo twn attributes tote de ginetai na ftiaksoume allo index */ if ( relInfo->attrcnt == relInfo->indexcnt ) { printf( "Error: Cannot create yet another index for any of %s's attributes, delete one first\n", relName ); return_val = AMINIREL_GE; cleanup(); return return_val; } /* afou mporesame na vroume th sxesh relName, twra kanoume retrieve tis plhrofories gia to attribute ths */ if (( attrInfo = getAttribute( relName, attrName, &attrRecId ) ) == NULL ) { printf( "Error: Could not retrieve information about attribute '%s' of relation %s\n", attrName, relName ); return_val = AMINIREL_GE; cleanup(); return return_val; } /* Vrhkame to attribute, twra elegxoume an yparxei hdh ena eurethrio gia to attribute! */ if ( attrInfo->indexed == TRUE ) { printf( "Error: There is already an index for attribute '%s' of relation '%s'\n", attrName, relName ); return_val = AMINIREL_GE; cleanup(); return return_val; } else { /* Mporoume na ftiaksoume neo eurethrio! :v */ /* Enhmerwnoume to indexed kai indexno tou attrInfo */ attrInfo->indexed = TRUE; attrInfo->indexno = relInfo->indexcnt; /* Enhmerwnoume ton indexcnt tou relInfo */ ( relInfo->indexcnt )++; /* Mias kai to HF epipedo mas epistrefei antigrafa twn records, emeis twra pou exoume tis nees versions twn records prepei na diagrapsoume ta original kai na eisagoume pali ta kainourgia! */ /* Diagrafh ths eggrafhs sto relCat */ if ( HF_DeleteRec( this_db.relCatDesc, relRecId, sizeof( relDesc ) ) != HFE_OK ) { HF_PrintError( "Could not Destroy record in relCat " ); return_val = AMINIREL_GE; cleanup(); return return_val; } /* Diagrafh ths eggrafhs sto attrCat */ if ( HF_DeleteRec( this_db.attrCatDesc, attrRecId, sizeof( attrDesc ) ) != HFE_OK ) { HF_PrintError( "Could not Destroy record in attrCat " ); return_val = AMINIREL_GE; cleanup(); return return_val; } /* Eisagwgh twn updated records sto relCat */ if ( updateRelCat( relInfo ) != AMINIREL_OK ) { printf( "ton hpie h update relcat\n" ) ; return_val = AMINIREL_GE; cleanup(); return return_val; } /* kai sto attrCat */ if ( updateAttrCat( attrInfo ) != AMINIREL_OK ) { printf( "ton hpie h update attrcat\n" ) ; return_val = AMINIREL_GE; cleanup(); return return_val; } /* Telos, dhmioyrgoyme to eurethrio */ if ( AM_CreateIndex( relName, attrInfo->indexno, attrInfo->attrtype, attrInfo->attrlength ) != AME_OK ) { AM_PrintError( "Could not Create Index in UT_buildindex() " ); return_val = AMINIREL_GE; cleanup(); return return_val; } /* Twra pou dhmioyrghsame to eyrethrio, prepei oses eggrafes yparxoun sto arxeio ths sxeshs na perastoun sto eurethrio! */ /* Desmeuoume xwro gia to xwro pou tha apothikeuetai to kathe record tou arxeiou */ if (( record = malloc( relInfo->relwidth ) ) == NULL ) { printf( MEM_ERROR ); return_val = AMINIREL_GE; cleanup(); return return_val; } memset( record, 0, relInfo->relwidth ); /* Anoigoume to eurethrio */ if (( indexFileDesc = AM_OpenIndex( relName, attrInfo->indexno ) ) < 0 ) { AM_PrintError( "Could not Open Index in UT_buildindex() " ); return_val = AMINIREL_GE; cleanup(); return return_val; } /* Anoigoume to arxeio ths sxeshs */ if (( fileDesc = HF_OpenFile( relName ) ) < 0 ) { HF_PrintError( "Could not Open relation's file in UT_buildindex() " ); return_val = AMINIREL_GE; cleanup(); return return_val; } /* ksekiname me thn prwth eggrafh */ if (( recId = HF_GetFirstRec( fileDesc, record, relInfo->relwidth ) ) < 0 ) { /* Yparxei h periptwsh to arxeio na mhn exei eggrafes */ if ( recId != HFE_EOF ) { HF_PrintError( "Could not retrieve first Record from relation's file" ); return_val = AMINIREL_GE; } cleanup(); return return_val; } /* Eisagoume oles tis eggrafes tou arxeiou ths sxeshs, sto eurethrio */ do { /* Thetoume ton pointer value sto swsto shmeio ths eggrafhs ap opou tha paroume tin timh mas */ value = record + attrInfo->offset; /* Eisagoume thn eggrafh sto eurethrio */ if ( AM_InsertEntry( indexFileDesc, attrInfo->attrtype, attrInfo->attrlength, value, recId ) != AME_OK ) { AM_PrintError( "Could not insert into index" ); return_val = AMINIREL_GE; } } while (( recId = HF_GetNextRec( fileDesc, recId, record , relInfo->relwidth ) ) >= 0 ); if ( recId != HFE_EOF ) { HF_PrintError( "Error: Cannot build index\n" ); cleanup(); return return_val; } return_val = AMINIREL_OK; } cleanup(); return return_val; }
int UT_create( int argc, char* argv[] ) { relDesc relInfo, * relPtr; attrDesc attrInfo; char * pch, * relName; int i, j, offset, num; /* Apaitoume, ektos apo to onoma ths sxeshs: - toulaxiston ena gnwrisma kai ton typo tou - oloklhrwmena zeugh <onomatos gnwrismatos, typou gnwrismatos> */ if (( argc < 4 ) || ( argc % 2 != 0 ) ) { printf( "Error: Cannot create relation: Not enough information\n" ); return AMINIREL_GE; } relName = argv[1]; /* max epitrepto mhkos onomatos = MAXNAME */ if ( strlen( relName ) >= MAXNAME ) { printf( "Error: Cannot create relation: Relation name too long\n" ); return AMINIREL_GE; } /* mhpws yparxei hdh h relation ? */ if (( relPtr = getRelation( relName, &i ) ) != NULL ) { printf( "Error: Cannot create relation '%s'. Relation already exists.\n", relName ); free( relPtr ); return AMINIREL_GE; } /* * H relation "temp_relation" einai reserved apo to systhma * Epitrepetai na dimioyrgithei mono apo tis dm_select & dm_join */ if (strcmp(relName, "temp_relation") == 0 && strcmp(argv[0], "\"DMcreate\"") != 0) { printf("Error: Cannot create 'temp_relation'. It is a relation reserved by the system\n"); return AMINIREL_GE; } /* Elegxoume an dwthikan idia onomata se 2 attributes ths relation to opoio einai lathos */ for ( i = 2; i < argc; i += 2 ) { for ( j = i + 2; j < argc; j += 2 ) { if ( strcmp( argv[i], argv[j] ) == 0 ) { printf( "Error: More than one attributes named '%s'.\n", argv[i] ); return AMINIREL_GE; } } } /* Arxikopoihsh ths eggrafhs perigrafhs pleiadas */ memset( relInfo.relname, 0, MAXNAME ); strcpy( relInfo.relname, relName ); /* To relwidth 8a symplhrw8ei meta, otan tha swsoume tis plhrofories twn attributes */ relInfo.relwidth = 0; relInfo.attrcnt = ( argc - 2 ) / 2; relInfo.indexcnt = 0; /* Arxikopoihsh ths eggrafhs perigrafhs pediwn */ memset( attrInfo.relname, 0, MAXNAME ); strcpy( attrInfo.relname, relName ); attrInfo.indexed = FALSE; attrInfo.indexno = 0; /* Fortwsh twn plhroforiwn twn pediwn */ for ( i = 2, offset = 0 ; argv[i] != NULL ; i += 2 ) { if ( strlen( argv[i] ) >= MAXNAME ) { printf( "Error: Cannot create relation: Attribute name too long\n" ); return AMINIREL_GE; } memset( attrInfo.attrname, 0, MAXNAME ); strcpy( attrInfo.attrname, argv[i] ); attrInfo.offset = offset; pch = argv[i+1]; /* Prospername to arxiko eisagwgiko ' */ pch++; switch ( *pch ) { case 'i': offset += sizeof( int ); attrInfo.attrtype = 'i'; attrInfo.attrlength = sizeof( int ); break; case 'f': offset += sizeof( float ); attrInfo.attrtype = 'f'; attrInfo.attrlength = sizeof( float ); break; case 'c': num = atoi( pch + 1 ); if ( num <= 0 || num > 255 ) { printf( "Error: Wrong attribute size\n" ); return AMINIREL_GE; } offset += num; attrInfo.attrtype = 'c'; attrInfo.attrlength = num; break; default: printf( "Error: Unrecognised type\n" ); return AMINIREL_GE; } /* Pros8etoume thn eggrafh gia to sygkekrimeno gnwrisma */ if ( updateAttrCat( &attrInfo ) != AMINIREL_OK ) { printf( "Error: Couldn't update attrCat\n" ); return AMINIREL_GE; } } relInfo.relwidth = offset; /* Pros8etoume thn eggrafh ths sxeshs sto relCat */ if ( updateRelCat( &relInfo ) != AMINIREL_OK ) { printf( "Error: Couldn't update relCat\n" ); return AMINIREL_GE; } /* Dhmioyrgoyme to neo arxeio */ if ( HF_CreateFile( relName ) != HFE_OK ) { HF_PrintError( "Could not create new relation file in UT_create() " ); return AMINIREL_GE; } return AMINIREL_OK; }
void main() { int athletesFd; int eventsFd; int partsFd; char *athleteRec; char *eventRec; char *partsRec; int athletesScan; int eventsScan; int partsScan; int eventId; int eventRecId; int partRecId; int partsCounter = 0; //Αρχικοποίηση Επιπέδου HF HF_Init(); //’νοιγμα Αρχείου EVENTS eventsFd = HF_OpenFile("EVENTS"); if (eventsFd < 0) { HF_PrintError("Error in HF_OpenFile called on EVENTS."); return; } //’νοιγμα Αρχείου PARTICIPATIONS partsFd = HF_OpenFile("PARTICIPATIONS"); if (partsFd < 0) { HF_PrintError("Error in HF_OpenFile called on PARTICIPATIONS."); return; } //’νοιγμα Αρχείου ATHLETES athletesFd = HF_OpenFile("ATHLETES"); if (athletesFd < 0) { HF_PrintError("Error in HF_OpenFile called on ATHLETES."); return; } eventId= 30; //Scan στο Αρχείο EVENTS με συνθήκη eventId >= "30" eventsScan = HF_OpenFileScan(eventsFd, HF_MAIN_EVENT_REC_SIZE, 'i', sizeof(int), 0, 5, (char *) &eventId); if (eventsScan < 0) { HF_PrintError("HF_OpenFileScan on EVENTS failed"); return; } eventRec = (char *) malloc(HF_MAIN_EVENT_REC_SIZE); if (eventRec == NULL) { printf("Event Record malloc failed\n"); return; } printf("Scan of file EVENTS with condition: eventId >= %d\n\n", eventId); while ( (eventRecId = HF_FindNextRec(eventsScan, eventRec)) >= 0) { int recEventId; char *eventName = (char *) malloc(HF_MAIN_EVENT_NAME_SIZE); if (eventName == NULL) { printf("Event Name malloc failed\n"); return; } memcpy((void *) &recEventId, (void *) eventRec, sizeof(int)); eventRec += sizeof(int); memcpy((void *) eventName, (void *) eventRec, HF_MAIN_EVENT_NAME_SIZE); eventRec += HF_MAIN_EVENT_NAME_SIZE; printf("Found Event Record: recId=%d, data=(%d, %s)\n", eventRecId, recEventId, eventName); eventRec -= HF_MAIN_EVENT_REC_SIZE; //Επαλήθευση της τιμής που ανακτήθηκε if (recEventId < eventId) { printf("Invalid Event record\n"); return; } //Scan στο Αρχείο PARTICIPATIONS με συνθήκη eventId = recEventId partsScan = HF_OpenFileScan(partsFd, HF_MAIN_PARTICIPATION_REC_SIZE, 'i', sizeof(int), sizeof(int), 1, (char *) &recEventId); if (partsScan < 0) { HF_PrintError("HF_OpenFileScan on PARTICIPATIONS failed"); return; } partsRec = (char *) malloc(HF_MAIN_PARTICIPATION_REC_SIZE); if (partsRec == NULL) { printf("Participation Record malloc failed\n"); return; } printf("Scan of file PARTICIPATIONS with condition: eventId = %d\n", recEventId); while ( (partRecId = HF_FindNextRec(partsScan, partsRec)) >= 0) { int partAthId, partEvId; char *partDate; partDate = (char *) malloc(HF_MAIN_DATE_SIZE); if (partDate == NULL) { printf("Participation Date malloc failed\n"); return; } memcpy((void *) &partAthId, (void *)partsRec, sizeof(int)); partsRec += sizeof(int); memcpy((void *) &partEvId, (void *)partsRec, sizeof(int)); partsRec += sizeof(int); memcpy((void *) partDate, (void *)partsRec, HF_MAIN_DATE_SIZE); partsRec += HF_MAIN_DATE_SIZE; printf("Found Participation Record: recId=%d, data=(%d, %d, %s)\n", partRecId, partAthId, partEvId, partDate); partsRec -= HF_MAIN_PARTICIPATION_REC_SIZE; //Επαλήθευση της τιμής που ανακτήθηκε if (partEvId != recEventId) { HF_PrintError("Invalid Participation Record\n"); return; } //Scan στο Αρχείο ATHLETES με συνθήκη athleteId = partAthId athletesScan = HF_OpenFileScan(athletesFd, HF_MAIN_ATHLETE_REC_SIZE, 'i', sizeof(int), 0, 1, (char *) &partAthId); if (athletesScan < 0) { HF_PrintError("HF_OpenFileScan on ATHLETES failed"); return; } athleteRec = (char *) malloc(HF_MAIN_ATHLETE_REC_SIZE); if (athleteRec == NULL) { printf("Athlete Record malloc failed\n"); return; } printf("Scan of file ATHLETES with condition: athleteId = %d\n", partAthId); if (HF_FindNextRec(athletesScan, athleteRec) >= 0) { int athleteId; char *athleteName; char *athleteSurname; athleteName = (char *) malloc(HF_MAIN_NAME_SIZE); if (athleteName == NULL) { printf("Athlete Name malloc failed\n"); return; } athleteSurname = (char *) malloc(HF_MAIN_NAME_SIZE); if (athleteSurname == NULL) { printf("Athlete Surname malloc failed\n"); return; } memcpy((void *) &athleteId, (void *) athleteRec, sizeof(int)); athleteRec += sizeof(int); memcpy((void *) athleteSurname, (void *) athleteRec, HF_MAIN_NAME_SIZE); athleteRec += HF_MAIN_NAME_SIZE; memcpy((void *) athleteName, (void *) athleteRec, HF_MAIN_NAME_SIZE); athleteRec += HF_MAIN_NAME_SIZE; athleteRec -= HF_MAIN_ATHLETE_REC_SIZE; printf("Found Athlete Record: (%d, %s, %s)\n", athleteId, athleteSurname, athleteName); //Επαλήθευση της τιμής που ανακτήθηκε if (athleteId != partAthId) { HF_PrintError("Invalid Athlete Record\n"); return; } printf("Athlete %s, %s participated in %s on %s.\n", athleteSurname, athleteName, eventName, partDate); free(athleteSurname); free(athleteName); } if (HF_FindNextRec(athletesScan, athleteRec) >= 0) { HF_PrintError("File Scan on Athletes returned a second Athlete record"); return; } free(athleteRec); //Κλείσιμο του Scan στο Αρχείο ATHLETES if (HF_CloseFileScan(athletesScan) != HFE_OK) { HF_PrintError("Close of Scan on ATHLETES Failed."); return; } printf("Deleting Participation Record %d\n", partRecId); if (HF_DeleteRec(partsFd, partRecId, HF_MAIN_PARTICIPATION_REC_SIZE) != HFE_OK) { HF_PrintError("Deletion of Participation Record failed."); return; } partsCounter++; free(partDate); } free(partsRec); //Κλείσιμο του Scan στο Αρχείο PARTICIPATIONS if (HF_CloseFileScan(partsScan) != HFE_OK) { HF_PrintError("Close of Scan on PARTICIPATIONS Failed."); return; } printf("Deleting Event Record: recId=%d, data=(%d, %s)\n\n", eventRecId, recEventId, eventName); if (HF_DeleteRec(eventsFd, eventRecId, HF_MAIN_EVENT_REC_SIZE) != HFE_OK) { HF_PrintError("Deletion of Event Record failed."); return; } free(eventName); } free(eventRec); printf("Deleted %d participations\n", partsCounter); //Κλείσιμο Αρχείου PARTICIPATIONS if (HF_CloseFile(partsFd) != HFE_OK) HF_PrintError("Error in HF_CloseFile called on PARTICIPATIONS."); //Κλείσιμο Αρχείου EVENTS if (HF_CloseFile(eventsFd) != HFE_OK) HF_PrintError("Error in HF_CloseFile called on EVENTS."); //Κλείσιμο Αρχείου ATHLETES if (HF_CloseFile(athletesFd) != HFE_OK) HF_PrintError("Error in HF_CloseFile called on ATHLETES."); }