2010-01-23 02:13:59 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include "logstore.h"
|
2010-01-27 23:15:47 +00:00
|
|
|
#include "datapage.h"
|
2010-01-23 02:13:59 +00:00
|
|
|
#include "merger.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
#include <stasis/transactional.h>
|
2010-01-23 02:13:59 +00:00
|
|
|
#undef begin
|
|
|
|
#undef end
|
|
|
|
|
2010-01-26 03:37:07 +00:00
|
|
|
#include "check_util.h"
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2010-01-27 23:34:33 +00:00
|
|
|
void insertProbeIter(size_t NUM_ENTRIES)
|
2010-01-23 02:13:59 +00:00
|
|
|
{
|
|
|
|
srand(1000);
|
2010-02-10 21:49:50 +00:00
|
|
|
unlink("storefile.txt");
|
|
|
|
unlink("logfile.txt");
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
sync();
|
2010-02-15 23:02:01 +00:00
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
diskTreeComponent::internalNodes::init_stasis();
|
2010-02-15 23:02:01 +00:00
|
|
|
|
2010-01-23 02:13:59 +00:00
|
|
|
double delete_freq = .05;
|
|
|
|
double update_freq = .15;
|
2010-02-11 20:04:42 +00:00
|
|
|
|
2010-01-23 02:13:59 +00:00
|
|
|
//data generation
|
|
|
|
typedef std::vector<std::string> key_v_t;
|
2010-01-27 23:34:33 +00:00
|
|
|
const static size_t max_partition_size = 100000;
|
2010-01-23 02:13:59 +00:00
|
|
|
int KEY_LEN = 100;
|
|
|
|
std::vector<key_v_t*> *key_v_list = new std::vector<key_v_t*>;
|
|
|
|
int list_size = NUM_ENTRIES / max_partition_size + 1;
|
|
|
|
for(int i =0; i<list_size; i++)
|
|
|
|
{
|
|
|
|
key_v_t * key_arr = new key_v_t;
|
|
|
|
if(NUM_ENTRIES < max_partition_size*(i+1))
|
2010-01-26 03:37:07 +00:00
|
|
|
preprandstr(NUM_ENTRIES-max_partition_size*i, key_arr, KEY_LEN, true);
|
2010-01-23 02:13:59 +00:00
|
|
|
else
|
2010-01-26 03:37:07 +00:00
|
|
|
preprandstr(max_partition_size, key_arr, KEY_LEN, true);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
std::sort(key_arr->begin(), 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<key_v_t::iterator*> iters;
|
|
|
|
for(int i=0; i<list_size; i++)
|
|
|
|
{
|
|
|
|
iters.push_back(new key_v_t::iterator((*key_v_list)[i]->begin()));
|
|
|
|
}
|
|
|
|
|
|
|
|
int lc = 0;
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
int list_index = -1;
|
|
|
|
for(int i=0; i<list_size; i++)
|
|
|
|
{
|
|
|
|
if(*iters[i] == (*key_v_list)[i]->end())
|
|
|
|
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; i<list_size; i++)
|
|
|
|
{
|
|
|
|
(*key_v_list)[i]->clear();
|
|
|
|
delete (*key_v_list)[i];
|
|
|
|
delete iters[i];
|
|
|
|
}
|
|
|
|
key_v_list->clear();
|
|
|
|
delete key_v_list;
|
|
|
|
|
|
|
|
printf("key arr size: %d\n", key_arr->size());
|
|
|
|
|
|
|
|
if(key_arr->size() > NUM_ENTRIES)
|
|
|
|
key_arr->erase(key_arr->begin()+NUM_ENTRIES, key_arr->end());
|
|
|
|
|
|
|
|
NUM_ENTRIES=key_arr->size();
|
|
|
|
|
|
|
|
int xid = Tbegin();
|
|
|
|
|
|
|
|
merge_scheduler mscheduler;
|
2010-03-17 21:51:26 +00:00
|
|
|
logtable<datatuple> ltable(1000, 1000, 40);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
recordid table_root = ltable.allocTable(xid);
|
|
|
|
|
|
|
|
Tcommit(xid);
|
2010-02-25 19:03:20 +00:00
|
|
|
writelock(ltable.header_lock,0);
|
2010-01-23 02:13:59 +00:00
|
|
|
int lindex = mscheduler.addlogtable(<able);
|
|
|
|
ltable.setMergeData(mscheduler.getMergeData(lindex));
|
2010-02-25 19:03:20 +00:00
|
|
|
|
2010-01-23 02:13:59 +00:00
|
|
|
mscheduler.startlogtable(lindex);
|
2010-02-25 19:03:20 +00:00
|
|
|
unlock(ltable.header_lock);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
printf("Stage 1: Writing %d keys\n", NUM_ENTRIES);
|
|
|
|
|
|
|
|
struct timeval start_tv, stop_tv, ti_st, ti_end;
|
|
|
|
double insert_time = 0;
|
|
|
|
int delcount = 0, upcount = 0;
|
|
|
|
int64_t datasize = 0;
|
|
|
|
std::vector<pageid_t> dsp;
|
|
|
|
std::vector<int> del_list;
|
|
|
|
gettimeofday(&start_tv,0);
|
2010-01-27 23:34:33 +00:00
|
|
|
for(size_t i = 0; i < NUM_ENTRIES; i++)
|
2010-01-23 02:13:59 +00:00
|
|
|
{
|
|
|
|
//prepare the data
|
|
|
|
std::string ditem;
|
|
|
|
getnextdata(ditem, 8192);
|
2010-02-10 21:49:50 +00:00
|
|
|
|
|
|
|
//prepare the key
|
|
|
|
datatuple *newtuple = datatuple::create((*key_arr)[i].c_str(), (*key_arr)[i].length()+1, ditem.c_str(), ditem.length()+1);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2010-02-10 21:49:50 +00:00
|
|
|
datasize += newtuple->byte_length();
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
gettimeofday(&ti_st,0);
|
|
|
|
ltable.insertTuple(newtuple);
|
|
|
|
gettimeofday(&ti_end,0);
|
|
|
|
insert_time += tv_to_double(ti_end) - tv_to_double(ti_st);
|
|
|
|
|
2010-02-10 21:49:50 +00:00
|
|
|
datatuple::freetuple(newtuple);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
double rval = ((rand() % 100)+.0)/100;
|
|
|
|
if( rval < delete_freq) //delete a key
|
|
|
|
{
|
|
|
|
int del_index = i - (rand()%50); //delete one of the last inserted 50 elements
|
|
|
|
if(del_index >= 0 && std::find(del_list.begin(), del_list.end(), del_index) == del_list.end())
|
|
|
|
{
|
|
|
|
delcount++;
|
|
|
|
|
2010-02-10 21:49:50 +00:00
|
|
|
datatuple *deltuple = datatuple::create((*key_arr)[del_index].c_str(), (*key_arr)[del_index].length()+1);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
gettimeofday(&ti_st,0);
|
|
|
|
ltable.insertTuple(deltuple);
|
|
|
|
gettimeofday(&ti_end,0);
|
|
|
|
insert_time += tv_to_double(ti_end) - tv_to_double(ti_st);
|
|
|
|
|
2010-02-10 21:49:50 +00:00
|
|
|
datatuple::freetuple(deltuple);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
getnextdata(ditem, 512);
|
2010-02-10 21:49:50 +00:00
|
|
|
|
|
|
|
upcount++;
|
|
|
|
datatuple *uptuple = datatuple::create((*key_arr)[up_index].c_str(), (*key_arr)[up_index].length()+1,
|
|
|
|
ditem.c_str(), ditem.length()+1);
|
2010-01-23 02:13:59 +00:00
|
|
|
gettimeofday(&ti_st,0);
|
|
|
|
ltable.insertTuple(uptuple);
|
|
|
|
gettimeofday(&ti_end,0);
|
|
|
|
insert_time += tv_to_double(ti_end) - tv_to_double(ti_st);
|
|
|
|
|
2010-02-10 21:49:50 +00:00
|
|
|
datatuple::freetuple(uptuple);
|
2010-01-23 02:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
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");
|
|
|
|
printf("datasize: %lld\n", datasize);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
//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);
|
|
|
|
|
|
|
|
//find the key with the given tuple
|
|
|
|
datatuple *dt = ltable.findTuple(xid, rkey, keylen);
|
|
|
|
|
|
|
|
if(std::find(del_list.begin(), del_list.end(), i) == del_list.end())
|
|
|
|
{
|
|
|
|
assert(dt!=0);
|
|
|
|
assert(!dt->isDelete());
|
|
|
|
found_tuples++;
|
2010-02-10 21:49:50 +00:00
|
|
|
assert(dt->keylen() == (*key_arr)[ri].length()+1);
|
|
|
|
datatuple::freetuple(dt);
|
2010-01-23 02:13:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(dt!=0)
|
|
|
|
{
|
2010-02-10 21:49:50 +00:00
|
|
|
assert(dt->keylen() == (*key_arr)[ri].length()+1);
|
2010-01-23 02:13:59 +00:00
|
|
|
assert(dt->isDelete());
|
2010-02-10 21:49:50 +00:00
|
|
|
datatuple::freetuple(dt);
|
2010-01-23 02:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
2010-03-09 19:47:12 +00:00
|
|
|
diskTreeComponent::internalNodes::deinit_stasis();
|
2010-01-23 02:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test
|
|
|
|
*/
|
|
|
|
int main()
|
|
|
|
{
|
2010-02-10 21:49:50 +00:00
|
|
|
// insertProbeIter(25000);
|
2010-01-23 02:13:59 +00:00
|
|
|
insertProbeIter(400000);
|
|
|
|
/*
|
|
|
|
insertProbeIter(5000);
|
|
|
|
insertProbeIter(2500);
|
|
|
|
insertProbeIter(1000);
|
|
|
|
insertProbeIter(500);
|
|
|
|
insertProbeIter(1000);
|
|
|
|
insertProbeIter(100);
|
|
|
|
insertProbeIter(10);
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|