#include #include #include #include #include "logstore.h" #include "datapage.h" #include "logiterators.h" #include "merger.h" #include #include #include #include #include #include #undef begin #undef end #include "check_util.h" void insertProbeIter(int NUM_ENTRIES) { srand(1000); //unlink("storefile.txt"); //unlink("logfile.txt"); sync(); double delete_freq = .05; double update_freq = .15; //data generation typedef std::vector key_v_t; const static int max_partition_size = 100000; int KEY_LEN = 100; std::vector *key_v_list = new std::vector; int list_size = NUM_ENTRIES / max_partition_size + 1; for(int i =0; ibegin(), key_arr->end(), &mycmp); key_v_list->push_back(key_arr); printf("size partition %d is %d\n", i+1, key_arr->size()); } key_v_t * key_arr = new key_v_t; std::vector iters; for(int i=0; ibegin())); } int lc = 0; while(true) { int list_index = -1; for(int i=0; iend()) continue; if(list_index == -1 || mycmp(**iters[i], **iters[list_index])) list_index = i; } if(list_index == -1) break; if(key_arr->size() == 0 || mycmp(key_arr->back(), **iters[list_index])) key_arr->push_back(**iters[list_index]); (*iters[list_index])++; lc++; if(lc % max_partition_size == 0) printf("%d/%d completed.\n", lc, NUM_ENTRIES); } for(int i=0; iclear(); delete (*key_v_list)[i]; delete iters[i]; } key_v_list->clear(); delete key_v_list; // preprandstr(NUM_ENTRIES, data_arr, 10*8192); printf("key arr size: %d\n", key_arr->size()); //removeduplicates(key_arr); if(key_arr->size() > NUM_ENTRIES) key_arr->erase(key_arr->begin()+NUM_ENTRIES, key_arr->end()); NUM_ENTRIES=key_arr->size(); bufferManagerNonBlockingSlowHandleType = IO_HANDLE_PFILE; Tinit(); int xid = Tbegin(); merge_scheduler mscheduler; logtable ltable; int pcount = 40; ltable.set_fixed_page_count(pcount); recordid table_root = ltable.allocTable(xid); Tcommit(xid); xid = Tbegin(); int lindex = mscheduler.addlogtable(<able); ltable.setMergeData(mscheduler.getMergeData(lindex)); mscheduler.startlogtable(lindex); printf("Stage 1: Writing %d keys\n", NUM_ENTRIES); struct timeval start_tv, stop_tv, ti_st, ti_end; double insert_time = 0; int dpages = 0; int npages = 0; int delcount = 0, upcount = 0; DataPage *dp=0; int64_t datasize = 0; std::vector dsp; std::vector del_list; gettimeofday(&start_tv,0); for(int i = 0; i < NUM_ENTRIES; i++) { //prepare the key datatuple newtuple; uint32_t keylen = (*key_arr)[i].length()+1; newtuple.keylen = &keylen; newtuple.key = (datatuple::key_t) malloc(keylen); memcpy((byte*)newtuple.key, (*key_arr)[i].c_str(), keylen); //for(int j=0; j= 0 && std::find(del_list.begin(), del_list.end(), del_index) == del_list.end()) { delcount++; datatuple deltuple; keylen = (*key_arr)[del_index].length()+1; deltuple.keylen = &keylen; deltuple.key = (datatuple::key_t) malloc(keylen); memcpy((byte*)deltuple.key, (*key_arr)[del_index].c_str(), keylen); deltuple.datalen = &datalen; deltuple.setDelete(); gettimeofday(&ti_st,0); ltable.insertTuple(deltuple); gettimeofday(&ti_end,0); insert_time += tv_to_double(ti_end) - tv_to_double(ti_st); free(deltuple.key); del_list.push_back(del_index); } } else if(rval < delete_freq + update_freq) //update a record { int up_index = i - (rand()%50); //update one of the last inserted 50 elements if(up_index >= 0 && std::find(del_list.begin(), del_list.end(), up_index) == del_list.end()) {//only update non-deleted elements upcount++; datatuple uptuple; keylen = (*key_arr)[up_index].length()+1; uptuple.keylen = &keylen; uptuple.key = (datatuple::key_t) malloc(keylen); memcpy((byte*)uptuple.key, (*key_arr)[up_index].c_str(), keylen); getnextdata(ditem, 512); datalen = ditem.length()+1; uptuple.datalen = &datalen; uptuple.data = (datatuple::data_t) malloc(datalen); memcpy((byte*)uptuple.data, ditem.c_str(), datalen); gettimeofday(&ti_st,0); ltable.insertTuple(uptuple); gettimeofday(&ti_end,0); insert_time += tv_to_double(ti_end) - tv_to_double(ti_st); free(uptuple.key); free(uptuple.data); } } } gettimeofday(&stop_tv,0); printf("insert time: %6.1f\n", insert_time); printf("insert time: %6.1f\n", (tv_to_double(stop_tv) - tv_to_double(start_tv))); printf("#deletions: %d\n#updates: %d\n", delcount, upcount); printf("\nTREE STRUCTURE\n"); //ltable.get_tree_c1()->print_tree(xid); printf("datasize: %lld\n", datasize); //sleep(20); Tcommit(xid); xid = Tbegin(); printf("Stage 2: Looking up %d keys:\n", NUM_ENTRIES); int found_tuples=0; for(int i=NUM_ENTRIES-1; i>=0; i--) { int ri = i; //printf("key index%d\n", i); fflush(stdout); //get the key uint32_t keylen = (*key_arr)[ri].length()+1; datatuple::key_t rkey = (datatuple::key_t) malloc(keylen); memcpy((byte*)rkey, (*key_arr)[ri].c_str(), keylen); //for(int j=0; jisDelete()); found_tuples++; assert(*(dt->keylen) == (*key_arr)[ri].length()+1); //assert(*(dt->datalen) == (*data_arr)[ri].length()+1); free(dt->keylen); free(dt); } else { if(dt!=0) { assert(*(dt->keylen) == (*key_arr)[ri].length()+1); assert(dt->isDelete()); free(dt->keylen); free(dt); } } dt = 0; free(rkey); } printf("found %d\n", found_tuples); key_arr->clear(); //data_arr->clear(); delete key_arr; //delete data_arr; mscheduler.shutdown(); printf("merge threads finished.\n"); gettimeofday(&stop_tv,0); printf("run time: %6.1f\n", (tv_to_double(stop_tv) - tv_to_double(start_tv))); Tcommit(xid); Tdeinit(); } /** @test */ int main() { //insertProbeIter(25000); insertProbeIter(400000); /* insertProbeIter(5000); insertProbeIter(2500); insertProbeIter(1000); insertProbeIter(500); insertProbeIter(1000); insertProbeIter(100); insertProbeIter(10); */ return 0; }