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='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); } // gettimeofday(&t2,NULL); t = clock(); numrec= 0; char val = 'b'; sd = AM_OpenIndexScan(fd,CHAR_TYPE,sizeof(char),GT_OP,&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); }
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; }
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"); }