main(){
	int fd;	/* file descriptor for the index */
	char fname[FNAME_LENGTH];	/* file name */
	int recnum;	/* record number */
	int sd;	/* scan descriptor */
	int numrec;	/* # of records retrieved */
	int testval;

	clock_t t;	
	// timeval t1, t2;
	// double elapsedtime;

	/* init */
	printf("initializing\n");
	PF_Init();

	/* create index */
	printf("creating index\n");
	AM_CreateIndex(RELNAME,0,CHAR_TYPE,sizeof(char));

	/* open the index */
	printf("opening index\n");
	sprintf(fname,"%s.0",RELNAME);
	fd = PF_OpenFile(fname);

	// gettime
	for (recnum=1; recnum < MAXRECS; recnum++){
		char value;
		if(recnum%4==0) value=0;
		else if(recnum%4==1) value=1;
		else if(recnum%4==2) value=2;
		else value=3;
		AM_InsertEntry(fd,CHAR_TYPE,sizeof(char),(char *)&value,
				recnum);
	}
	// gettimeofday(&t2,NULL);
	
	t = clock();
	numrec= 0;
	char val = 3;
	sd = AM_OpenIndexScan(fd,CHAR_TYPE,sizeof(char),LE_OP,(char*)&val);
	while((recnum=AM_FindNextEntry(sd))>= 0){
		numrec++;
	}
	AM_CloseIndexScan(sd);
	t = clock()-t;



	double time_taken = ((double)t)/CLOCKS_PER_SEC;

	printf("Scan takes %f seconds to retreive records with repeating attributes\n", time_taken);
	printf("Number of pages used %d\n",totalNumberOfPages);
	printf("closing down\n");
	PF_CloseFile(fd);
	AM_DestroyIndex(RELNAME,0);
}
main() {
    int fd;	/* file descriptor for the index */
    char fname[FNAME_LENGTH];	/* file name */
    int recnum;	/* record number */
    int sd;	/* scan descriptor */
    int numrec;	/* # of records retrieved */
    int testval;

    clock_t t;
    // timeval t1, t2;
    // double elapsedtime;

    /* init */
    printf("initializing\n");
    PF_Init();

    /* create index */
    printf("creating index\n");
    AM_CreateIndex(RELNAME,0,CHAR_TYPE,sizeof(char));

    /* open the index */
    printf("opening index\n");
    sprintf(fname,"%s.0",RELNAME);
    fd = PF_OpenFile(fname);

    t = clock();
    // gettimeofday(&t1,NULL);
    for (recnum=1; recnum < MAXRECS; recnum++) {
        char value;
        if(recnum%4==0) value='a';
        else if(recnum%4==1) value='b';
        else if(recnum%4==2) value='c';
        else value='d';
        AM_InsertEntry(fd,CHAR_TYPE,sizeof(char),(char *)&value,
                       recnum);
    }
    t = clock()-t;
    // gettimeofday(&t2,NULL);

    double time_taken = ((double)t)/CLOCKS_PER_SEC;
    // elapsedtime = (t2.tv_sec - t1.tv_sec) * 1000.0;
    // elapsedtime += (t2.tv_usec - t1.tv_usec) / 1000.0;

    printf("Insert takes %f seconds to insert records with repeating attributes\n", time_taken);
    printf("Number of pages used %d\n",totalNumberOfPages);
    printf("closing down\n");
    PF_CloseFile(fd);
    AM_DestroyIndex(RELNAME,0);
}
Ejemplo n.º 3
0
main(){
	int fd;	/* file descriptor for the index */
	char fname[FNAME_LENGTH];	/* file name */
	int recnum;	/* record number */
	int sd;	/* scan descriptor */
	int numrec;	/* # of records retrieved */
	int testval;

	clock_t t;	
	// timeval t1, t2;
	// double elapsedtime;

	/* init */
	printf("initializing\n");
	PF_Init();

	/* create index */
	printf("creating index\n");
	AM_CreateIndex(RELNAME,0,INT_TYPE,sizeof(int));

	/* open the index */
	printf("opening index\n");
	sprintf(fname,"%s.0",RELNAME);
	fd = PF_OpenFile(fname);
	int value = 1;
	for (recnum=0; recnum < MAXRECS; recnum++){
		AM_InsertEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum,
				recnum);
	}
	t = clock();
	for(recnum=0;recnum < MAXRECS; recnum++){
		AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char*)&recnum,recnum);
	}
	t = clock()-t;

	double time_taken = ((double)t)/CLOCKS_PER_SEC;

	printf("Delete takes %f seconds to delete records\n", time_taken);
	printf("Number of pages used %d\n",totalNumberOfPages);
	printf("closing down\n");
	PF_CloseFile(fd);
	AM_DestroyIndex(RELNAME,0);
}
Ejemplo n.º 4
0
int main()
{
    int scan1;

    int eNentry;
    int eAentry;
    int eSentry;
    int dAentry;

    int eage;
    float esal;
    char edname[40];

    empName[9] = '\0';
    char fltname[40];


    int recordid = 1;

    char errStr[200];

    int* ivalue = NULL;
    char* cvalue = NULL;


    AM_Init();

    strcpy(empAge, "TestDb3");




    if (AM_CreateIndex(empAge, STRING, sizeof(empName) - 1, INTEGER, sizeof(int)) != AME_OK) {
        sprintf(errStr, "Error in AM_CreateIndex called on %s \n", empAge);
        AM_PrintError(errStr);
        return 1;
    }

    if ((eAentry = AM_OpenIndex(empAge)) < 0) {
        sprintf(errStr, "Error in AM_OpenIndex called on %s \n", empAge);
        AM_PrintError(errStr);
        return 1;
    }

    srand(time(NULL));
    int i, j;
    recordid = 0;
    int count = 0;
    for (i = 1; i <= 100000; ++i) {
        for (j = 0; j < 9; ++j) {
            empName[j] = (char) abs(rand()%(122-97) + 97);
        }
        if(empName[0] == 'a')
            count++;
        insertEntries(eAentry, empName, ++recordid);
//        printf("record id : %d\n", recordid);
    }
    strcpy(empName, "bla");
    insertEntries(eAentry, empName, ++recordid);

    printf("names starting with a : %d\n", count);
    empName[0] = 'b';
    empName[1] = '\0';
    int scan = AM_OpenIndexScan(eAentry, LESS_THAN, empName);
    i = 0;
    while(AM_FindNextEntry(scan) != NULL)
        i++;
    printf("Names with less than b : %d\n", i);
    AM_CloseIndexScan(scan);

    empName[0] = 'b';
    empName[1] = '\0';
    scan = AM_OpenIndexScan(eAentry, GREATER_THAN, empName);
    i = 0;
    while(AM_FindNextEntry(scan) != NULL)
        i++;
    printf("Names with more than b : %d\n", i);
    AM_CloseIndexScan(scan);

    strcpy(empName, "bla");
    scan = AM_OpenIndexScan(eAentry, EQUAL, empName);
    i = 0;
    int * kappa = NULL;
    while((kappa = (int *) AM_FindNextEntry(scan) )!= NULL)
    {
        printf("%d\n", *kappa);
        i++;
    }
    printf("Names equal with bla : %d\n", i);
    AM_CloseIndexScan(scan);


    printf("record id : %d\n", recordid);

    AM_CloseIndex(eAentry);
    AM_DestroyIndex(empAge);
    return 0;
}
Ejemplo n.º 5
0
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;
}
Ejemplo n.º 6
0
main()
{
int fd;	/* file descriptor for the index */
char fname[FNAME_LENGTH];	/* file name */
int recnum;	/* record number */
int sd;	/* scan descriptor */
int numrec;	/* # of records retrieved */
int testval;	

	/* init */
	printf("initializing\n");
	PF_Init();

	/* create index */
	printf("creating index\n");
	AM_CreateIndex(RELNAME,0,INT_TYPE,sizeof(int));

	/* open the index */
	printf("opening index\n");
	sprintf(fname,"%s.0",RELNAME);
	fd = PF_OpenFile(fname);

	/* first, make sure that simple deletions work */
	printf("inserting into index\n");
	for (recnum=0; recnum < 20; recnum++){
		AM_InsertEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum,
				recnum);
	}
	printf("deleting odd number records\n");
	for (recnum=1; recnum < 20; recnum += 2)
		AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum,
					recnum);

	printf("retrieving even number records\n");
	numrec= 0;
	sd = AM_OpenIndexScan(fd,INT_TYPE,sizeof(int),EQ_OP,NULL);
	while((recnum=AM_FindNextEntry(sd))>= 0){
		printf("%d\n",recnum);
		numrec++;
	}
	printf("retrieved %d records\n",numrec);
	AM_CloseIndexScan(sd);

	printf("deleting even number records\n");
	for (recnum=0; recnum < 20; recnum += 2)
		AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum,
					recnum);

	printf("retrieving from empty index\n");
	numrec= 0;
	sd = AM_OpenIndexScan(fd,INT_TYPE,sizeof(int),EQ_OP,NULL);
	while((recnum=AM_FindNextEntry(sd))>= 0){
		printf("%d\n",recnum);
		numrec++;
	}
	printf("retrieved %d records\n",numrec);
	AM_CloseIndexScan(sd);


	/* insert into index */
	printf("begin test of complex delete\n");
	printf("inserting into index\n");
	for (recnum=0; recnum < MAXRECS; recnum+=2){
		AM_InsertEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum,
				recnum);
	}
	for (recnum=1; recnum < MAXRECS; recnum+=2)
		AM_InsertEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum,
			recnum);

	/* delete everything */
	printf("deleting everything\n");
	for (recnum=1; recnum < MAXRECS; recnum += 2)
		AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum,
					recnum);
	for (recnum=0; recnum < MAXRECS; recnum +=2)
		AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum,
				recnum);


	/* print out what remains */
	printf("printing empty index\n");
	numrec= 0;
	sd = AM_OpenIndexScan(fd,INT_TYPE,sizeof(int),EQ_OP,NULL);
	while((recnum=AM_FindNextEntry(sd))>= 0){
		printf("%d\n",recnum);
		numrec++;
	}
	printf("retrieved %d records\n",numrec);
	AM_CloseIndexScan(sd);

	/* insert everything back */
	printf("inserting everything back\n");
	for (recnum=0; recnum < MAXRECS; recnum++){
		AM_InsertEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum,
				recnum);
	}
	/* delete records less than 100, using scan!! */
	printf("delete records less than 100\n");
	testval = 100;
	sd = AM_OpenIndexScan(fd,INT_TYPE,sizeof(int),LT_OP,(char *)&testval);
	while((recnum=AM_FindNextEntry(sd))>= 0){
		if (recnum >= 100){
			printf("invalid recnum %d\n",recnum);
			exit(1);
		}
		AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum,
		recnum);
	}
	AM_CloseIndexScan(sd);

	/* delete records greater than 150, using scan */
	printf("delete records greater than 150\n");
	testval = 150;
	sd = AM_OpenIndexScan(fd,INT_TYPE,sizeof(int),GT_OP,(char *)&testval);
	while((recnum=AM_FindNextEntry(sd))>= 0){
		if (recnum <= 150){
			printf("invalid recnum %d\n",recnum);
			exit(1);
		}
		AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum,
					recnum);
	}
	AM_CloseIndexScan(sd);


	/* print out what remains */
   printf("printing between 100 and 150\n");
	numrec= 0;
	sd = AM_OpenIndexScan(fd,INT_TYPE,sizeof(int),EQ_OP,NULL);
	while((recnum=AM_FindNextEntry(sd))>= 0){
		printf("%d\n",recnum);
		numrec++;
	}
	printf("retrieved %d records\n",numrec);
	AM_CloseIndexScan(sd);

	/* destroy everything */
	printf("closing down\n");
	PF_CloseFile(fd);
	AM_DestroyIndex(RELNAME,0);

	printf("test3 done!\n");
}
Ejemplo n.º 7
0
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;
}