//Filename: load.c //Developer: Jay Suttiruttana #include "btree.h" void main(int argc, char *argv[]) { page_type d_parent; /* Dummy parent for the root */ Record_type record; Status_type retval; Item_type item, promo_item; int root, promo_rrn, rec_rrn; int rec_count=0, total_rec=0; char c; Record_type *block_record; block_record=(Record_type*) calloc (4,sizeof(Record_type)); if(argc != 2) // check arguments { printf("ERROR!"); exit(1); } inp_open(argv[1]); if ((fd_datafile = _open("records.txt", O_CREAT|O_TRUNC|O_RDWR/*|O_BINARY*/, 0777)) < 0) Error("\nError! problem creating the data base file"); pageinit(&d_parent); /* Initialize dummy parent */ rec_rrn = 0; /* Initialize record RRN */ /* Create B-Tree file and get RRN of the root*/ root = create_tree(&record); block_record[rec_count]= record; rec_count++; total_rec++; /* Get all the records in the input file*/ while(get_record(&record)) { /* Store key Number along with the RRN of the record*/ item.key = atoi(record.RICORDI); item.rec_rrn = ++rec_rrn; printf("%d inserted\n",item.key); /* Store RRN of the root in dummy parent*/ d_parent.child[0] = root; retval = insert(&d_parent, NIL, 0, item, &promo_rrn, &promo_item); if(retval == PROMOTION) root = create_root(promo_item, root, promo_rrn); else if(retval == ERROR) { printf("a duplicate key was located.\n", record.RICORDI); --rec_rrn; } if(retval != ERROR) block_record[rec_count]= record; if(total_rec < 4) { if (rec_count>=3) // AFTER 4 record at a time to datafile { write(fd_datafile,block_record,BUFFSIZE); rec_count =-1; // reset record count } } else if(rec_count>=3) // AFTER 4 record at a time to datafile { write(fd_datafile,block_record,BUFFSIZE); rec_count =-1; memset (block_record, '\0',BUFFSIZE); } rec_count++; total_rec++; } if(rec_count >=0) write(fd_datafile,block_record,BUFFSIZE); printf("\n\n%d records loaded!\n", total_rec); put_total_rec (total_rec); btclose(); //close b-tree file inp_close(); //close the data input file close(fd_datafile); //close the database file }