void modify_test() { double start_time, end_time; int built = false; Read_Buffer(input_file); // B+tree initialize BPlusTree_Init(); // MaxChildNumber printf("input depth: "); int depth; scanf("%d", &depth); int maxCh = 2; while (1) { int leaves = 1, i; for (i = 0; i < depth; i++) { leaves *= maxCh; if (leaves > TotalRecords) break; } if (leaves > TotalRecords) break; maxCh++; } printf("Desired depth = %d, calculated maxChildNumber = %d\n", depth, maxCh); BPlusTree_SetMaxChildNumber(maxCh); // build built = true; start_time = clock(); Read_Data_And_Insert(); end_time = clock(); printf("Valid Records inserted on B+tree = %d\n", validRecords); printf("Total number of B+tree nodes = %d\n", BPlusTree_GetTotalNodes()); printf("Build B+tree costs %lf s\n", (end_time - start_time) / CLOCKS_PER_SEC); // modify start_time = clock(); int key, i, j; srand((unsigned)time(NULL)); int mod_num = 10; for (i = 0; i < mod_num; i++) { new_key = keys[rand() % key_num]; new_len = 10; for (j = 0; j < new_len; j++) new_st[j] = 'a' + rand() % 26; char* value = (char*)malloc(sizeof(char) * strlen(new_st)); strcpy(value, new_st); int pos = BPlusTree_Find(new_key); if (pos != -1) { // found if (File_Modify(pos, new_key, new_st)) { // file modify success BPlusTree_Modify(new_key, value); //printf("Modify success.\n"); } else { //printf("Modify failed, the new value is too long to store in file\n"); } } else { printf("Modify failed, do not have the given key on B+tree.\n"); } } end_time = clock(); printf("modify %d elements, average cost is %lf s\n", mod_num, (end_time - start_time) / CLOCKS_PER_SEC / mod_num); BPlusTree_Destroy(); }
void delete_test() { double start_time, end_time; int built = false; Read_Buffer(input_file); // B+tree initialize BPlusTree_Init(); // MaxChildNumber printf("input depth: "); int depth; scanf("%d", &depth); int maxCh = 2; while (1) { int leaves = 1, i; for (i = 0; i < depth; i++) { leaves *= maxCh; if (leaves > TotalRecords) break; } if (leaves > TotalRecords) break; maxCh++; } printf("Desired depth = %d, calculated maxChildNumber = %d\n", depth, maxCh); BPlusTree_SetMaxChildNumber(maxCh); // build built = true; start_time = clock(); Read_Data_And_Insert(); end_time = clock(); printf("Valid Records inserted on B+tree = %d\n", validRecords); printf("Total number of B+tree nodes = %d\n", BPlusTree_GetTotalNodes()); printf("Build B+tree costs %lf s\n", (end_time - start_time) / CLOCKS_PER_SEC); // delete start_time = clock(); int key, i; srand((unsigned)time(NULL)); int del_num = 10; for (i = 0; i < del_num; i++) { key = keys[rand() % key_num]; int pos = BPlusTree_Find(key); if (pos != -1) { // found File_Delete(pos); BPlusTree_Delete(key); //printf("Delete success.\n"); } else { //printf("Delete failed, do not have the given key on B+tree.\n"); } } end_time = clock(); printf("delete %d elements, average cost is %lf s\n", del_num, (end_time - start_time) / CLOCKS_PER_SEC / del_num); BPlusTree_Destroy(); }
inline long long unsigned int Read_Data_And_Search(char* buffer) { int rid = 0; int cur = 0; int ret =0; int count = 0; long long unsigned int diff_time,total; struct timespec start,end; while (1) { while (cur < fsize && !('0' <= buffer[cur] && buffer[cur] <= '9')) cur++; // end of file if (cur == fsize) break; rid++; new_key = 0; new_pos = cur; while (buffer[cur] != ' ' && buffer[cur] != '\t') { new_key = new_key * 10 + (buffer[cur] - '0'); cur++; } cur++; new_len = 0; while (buffer[cur] == ' ') cur++; // util meet a character while (cur < fsize && buffer[cur] != '\n') { new_st[new_len++] = buffer[cur++]; } clock_gettime(CLOCK_REALTIME,&start); ret = BPlusTree_Find(new_key); clock_gettime(CLOCK_REALTIME,&end); diff_time = BILLION * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec); total += diff_time; if(ret == -1) { // printf("new_key: %d\n",new_key); count++; continue; } } // printf("Count : %d\n",count); free(buffer); buffer = NULL; return total; }
void MainLoop() { double start_time, end_time; int built = false; // Read data to buffer Read_Buffer(input_file); // B+tree initialize BPlusTree_Init(); while (1) { ShowHelp(); int request; scanf("%d", &request); switch (request) { case 0: { // Read data to buffer if (buffer != NULL) free(buffer); Read_Buffer(input_file); // B+tree initialize BPlusTree_Init(); // args built = false; validRecords = 0; break; } case 1: { // Set Depth printf("input depth: "); int depth; scanf("%d", &depth); int maxCh = 2; while (1) { int leaves = 1, i; for (i = 0; i < depth; i++) { leaves *= maxCh; if (leaves > TotalRecords) break; } if (leaves > TotalRecords) break; maxCh++; } printf("Desired depth = %d, calculated maxChildNumber = %d\n", depth, maxCh); BPlusTree_SetMaxChildNumber(maxCh); break; } case 2: { // Set MaxChildNumber printf("input MaxChildNumber: "); int maxCh; scanf("%d", &maxCh); BPlusTree_SetMaxChildNumber(maxCh); break; } case 3: { // Build B+tree if (built == true) { printf("You have built the B+tree\n"); break; } built = true; start_time = clock(); Read_Data_And_Insert(); end_time = clock(); printf("Valid Records inserted on B+tree = %d\n", validRecords); printf("Total number of B+tree nodes = %d\n", BPlusTree_GetTotalNodes()); printf("Build B+tree costs %lf s\n", (end_time - start_time) / CLOCKS_PER_SEC); break; } case 4: { // Query on a key printf("input the key: "); int key; scanf("%d", &key); start_time = clock(); BPlusTree_Query_Key(key); end_time = clock(); printf("Query on a key, costs %lf s\n", (end_time - start_time) / CLOCKS_PER_SEC); break; } case 5: { // Query on a range [l, r] printf("input range [l, r]: "); int l, r; scanf("%d %d", &l, &r); if (l > r) { printf("input illegal\n"); break; } start_time = clock(); BPlusTree_Query_Range(l, r); end_time = clock(); printf("Query on a range, costs %lf s\n", (end_time - start_time) / CLOCKS_PER_SEC); break; } case 6: { // Modify value on a key printf("input (key, value): "); scanf("%d %s", &new_key, new_st); char* value = (char*)malloc(sizeof(char) * strlen(new_st)); strcpy(value, new_st); start_time = clock(); int pos = BPlusTree_Find(new_key); if (pos != -1) { // found if (File_Modify(pos, new_key, new_st)) { // file modify success BPlusTree_Modify(new_key, value); printf("Modify success.\n"); } else { printf("Modify failed, the new value is too long to store in file\n"); } } else { printf("Modify failed, do not have the given key on B+tree.\n"); } end_time = clock(); printf("Modify value on a key, costs %lf s\n", (end_time - start_time) / CLOCKS_PER_SEC); break; } case 7: { // Delete value on a key printf("input key: "); int key; scanf("%d", &key); start_time = clock(); int pos = BPlusTree_Find(key); if (pos != -1) { // found File_Delete(pos); BPlusTree_Delete(key); printf("Delete success.\n"); } else { printf("Delete failed, do not have the given key on B+tree.\n"); } end_time = clock(); printf("Delete value on a key, costs %lf s\n", (end_time - start_time) / CLOCKS_PER_SEC); break; } case 8: { printf("input (key, value): "); scanf("%d %s", &new_key, new_st); char* value = (char*)malloc(sizeof(char) * new_len); strcpy(value, new_st); int pos = BPlusTree_Find(new_key); if (pos == -1) { new_pos = File_Insert(new_key, new_st); keys[key_num++] = new_key; BPlusTree_Insert(new_key, new_pos, value); validRecords++; printf("Insert success.\n"); } else { printf("Insert failed, the key already exist.\n"); } break; } case 9: return; default: break; } } BPlusTree_Destroy(); }