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(); }
void query_range_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); // query start_time = clock(); int key, i; srand((unsigned)time(NULL)); int query_num = 10; for (i = 0; i < query_num; i++) { int r = ((long long)(rand() % MAX_KEY) * (rand() % MAX_KEY)) % MAX_KEY + 1; int l = ((long long)(rand() % MAX_KEY) * (rand() % MAX_KEY)) % r + 1; printf("range = (%d, %d)\n", l, r); BPlusTree_Query_Range(l, r); } end_time = clock(); printf("query %d ranges, average cost is %lf s\n", query_num, (end_time - start_time) / CLOCKS_PER_SEC / query_num); BPlusTree_Destroy(); }
void build_test() { double start_time, end_time, tot_time = 0; int built = false; // 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 int build_num = 10, i; for (i = 0; i < build_num; i++) { printf("=============== %d ==========\n", i); // read buffer Read_Buffer(input_file); start_time = clock(); // B+tree initialize BPlusTree_Init(); // args built = false; validRecords = 0; key_num = 0; // build built = true; Read_Data_And_Insert(); end_time = clock(); tot_time += (end_time - start_time); printf("Valid Records inserted on B+tree = %d\n", validRecords); printf("Total number of B+tree nodes = %d\n", BPlusTree_GetTotalNodes()); printf("this time costs %lf s\n\n", (end_time - start_time) / CLOCKS_PER_SEC); BPlusTree_Destroy(); } printf("build %d times, average cost is %lf s\n", build_num, tot_time / CLOCKS_PER_SEC / build_num); }
void MainLoop() { double start_time, end_time; struct timespec start,end; long long unsigned int diff_time; int built1 = false; int built2 = false; // args built2 = false; validRecords = 0; // B+tree initialize BPlusTree_Init(); while (1) { ShowHelp(); int request; scanf("%d", &request); switch (request) { case 0: { long long unsigned int total = 0; if (built2 == true) { printf("You have built the B+tree\n"); break; } buffer2 = Read_Buffer(buffer2,input_file2); built2 = true; clock_gettime(CLOCK_REALTIME,&start); Read_Data_And_Insert(buffer2); clock_gettime(CLOCK_REALTIME,&end); diff_time = BILLION * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec); total += diff_time; // BPlusTree_Print(); 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 %llu ms\n",total /1000000); printf("Total number of split_count = %d\n", BPlusTree_GetSplitCount()); printf("Node size %lu byte\n",sizeof(struct BPlusTreeNode)); 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: { long long unsigned int total = 0 ; // Build B+tree if (built1 == true) { printf("You have built the B+tree\n"); break; } buffer = Read_Buffer(buffer,input_file); built1 = true; clock_gettime(CLOCK_REALTIME,&start); Read_Data_And_Insert(buffer); clock_gettime(CLOCK_REALTIME,&end); diff_time = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; total += diff_time; 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 %llu ms\n",total /1000000); printf("Total number of split_count = %d\n", BPlusTree_GetSplitCount()); // BPlusTree_Print(); break; } case 4: { long long unsigned int total = 0; buffer2 = Read_Buffer(buffer2,input_file2); total = Read_Data_And_Search(buffer2); printf(" Total time %llu ms\n", total / 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 7:{ long long unsigned int total = 0; buffer2 = Read_Buffer(buffer2,input_file2); total = Read_Data_And_Delete(buffer2); // BPlusTree_Print(); printf("Total time costs %llu ms\n",total /1000000); break; }/* { // Modify value on a key case 9:{ int num,j,value; double total,temp; char s[] = "dkdka"; value = 4; total = 0; temp = 0; for(j=1; j<=4; j++) { start_time = clock(); BPlusTree_Insert(j,0,s); end_time = clock(); total += end_time - start_time; validRecords++; } BPlusTree_Print(); 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); printf("Total time %lf s\n",total/ CLOCKS_PER_SEC); printf("Total number of split_count : %d\n",BPlusTree_GetSplitCount()); break; } */ case 10: return; default: break; } } }
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(); }