Commit 653a5974 authored by Gaurav Kukreja's avatar Gaurav Kukreja

fakedb: Optimized by using map.

parent 10a80e18
CC=g++
CCFLAGS=-O0
CCFLAGS=-O0 -fno-stack-protector -std=c++11
fakedb: main.cpp data_migrate.cpp schema_methods.cpp neworder.cpp oltp.cpp schema_constructors.cpp data_migrate.h schema.h oltp.h neworder.h
SOURCES=main.cpp data_migrate.cpp schema_methods.cpp neworder.cpp oltp.cpp schema_constructors.cpp
INCLUDE=include/data_migrate.h include/main.h include/neworder.h include/oltp.h include/schema.h
fakedb: $(SOURCES) $(INCLUDE)
$(CC) $(CCFLAGS) $^ -o $@
clean:
rm -rf fakedb
\ No newline at end of file
rm -rf fakedb
......@@ -2,10 +2,12 @@
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <list>
#include <vector>
#include <map>
#include <tuple>
#include <stdio.h>
#include "schema.h"
#include "include/schema.h"
//#include "data_migrate.h"
using namespace std;
......@@ -20,28 +22,36 @@ char ORDER_TBL_FILE[40]="./../../tables/tpcc_order.tbl";
char STOCK_TBL_FILE[40]="./../../tables/tpcc_stock.tbl";
char WAREHOUSE_TBL_FILE[40]="./../../tables/tpcc_warehouse.tbl";
list<customer> customer_list;
list<district> district_list;
list<history> history_list;
list<item> item_list;
list<neworder> neworder_list;
list<orderline> orderline_list;
list<order> order_list;
list<stock> stock_list;
list<warehouse> warehouse_list;
vector<customer> customer_vect;
map<tuple<int,int,int>, uint64_t> customer_map; //primary key (c_w_id,c_d_id,c_id)
vector<district> district_vect;
map<tuple<int,int>,uint64_t> district_map; //primary key (d_w_id,d_id)
vector<history> history_vect;
//map<tuple<int>,uint64_t> history_map; //primary_key ()
vector<item> item_vect;
map<tuple<int>,uint64_t> item_map; // primary key (i_id)
vector<neworder> neworder_vect;
map<tuple<int,int,int>,uint64_t> neworder_map; //primary key (no_w_id,no_d_id,no_o_id)
vector<orderline> orderline_vect;
map<tuple<int,int,int,int>,uint64_t> orderline_map; //primary key (ol_w_id,ol_d_id,ol_o_id,ol_number)
vector<order> order_vect;
map<tuple<int,int,int>,uint64_t> order_map; //primary key (o_w_id,o_d_id,o_id)
vector<stock> stock_vect;
map<tuple<int,int>,uint64_t> stock_map; // primary key (s_w_id,s_i_id)
vector<warehouse> warehouse_vect;
map<tuple<int>,uint64_t> warehouse_map; //primary key (w_id)
int load_customer_from_file() {
FILE *customer_tbl;
int count=0;
customer_tbl=fopen(CUSTOMER_TBL_FILE, "r");
customer temp_customer;
while(!feof(customer_tbl)) {
customer temp_customer;
if(temp_customer.parse(&customer_tbl)==0)
{
customer_list.push_front(temp_customer);
count++;
temp_customer.insert_new();
}
else
{
......@@ -49,7 +59,7 @@ int load_customer_from_file() {
}
}
printf("No. of entries = %d :: %s\n",count,__func__);
printf("No. of entries = %lld :: %s\n",temp_customer.count,__func__);
fclose(customer_tbl);
return 0;
......@@ -57,16 +67,15 @@ int load_customer_from_file() {
int load_district_from_file() {
FILE *district_tbl;
int count=0;
district_tbl=fopen(DISTRICT_TBL_FILE, "r");
district temp_district;
while(!feof(district_tbl)) {
district temp_district;
if(temp_district.parse(&district_tbl)==0)
{
district_list.push_front(temp_district);
count++;
temp_district.insert_new();
}
else
{
......@@ -74,7 +83,7 @@ int load_district_from_file() {
}
}
printf("No. of entries = %d :: %s\n",count,__func__);
printf("No. of entries = %lld :: %s\n",temp_district.count,__func__);
fclose(district_tbl);
return 0;
......@@ -82,16 +91,15 @@ int load_district_from_file() {
int load_history_from_file() {
FILE *history_tbl;
int count=0;
history_tbl=fopen(HISTORY_TBL_FILE, "r");
history temp_history;
while(!feof(history_tbl)) {
history temp_history;
if(temp_history.parse(&history_tbl)==0)
{
history_list.push_front(temp_history);
count++;
temp_history.insert_new();
}
else
{
......@@ -99,7 +107,7 @@ int load_history_from_file() {
}
}
printf("No. of entries = %d :: %s\n",count,__func__);
printf("No. of entries = %lld :: %s\n",temp_history.count,__func__);
fclose(history_tbl);
return 0;
......@@ -107,16 +115,15 @@ int load_history_from_file() {
int load_item_from_file() {
FILE *item_tbl;
int count=0;
item_tbl=fopen(ITEM_TBL_FILE, "r");
item temp_item;
while(!feof(item_tbl)) {
item temp_item;
if(temp_item.parse(&item_tbl)==0)
{
item_list.push_front(temp_item);
count++;
temp_item.insert_new();
}
else
{
......@@ -124,7 +131,7 @@ int load_item_from_file() {
}
}
printf("No. of entries = %d :: %s\n",count,__func__);
printf("No. of entries = %lld :: %s\n",temp_item.count,__func__);
fclose(item_tbl);
return 0;
......@@ -132,16 +139,15 @@ int load_item_from_file() {
int load_neworder_from_file() {
FILE *neworder_tbl;
int count=0;
neworder_tbl=fopen(NEWORDER_TBL_FILE, "r");
neworder temp_neworder;
while(!feof(neworder_tbl)) {
neworder temp_neworder;
if(temp_neworder.parse(&neworder_tbl)==0)
{
neworder_list.push_front(temp_neworder);
count++;
temp_neworder.insert_new();
}
else
{
......@@ -149,7 +155,7 @@ int load_neworder_from_file() {
}
}
printf("No. of entries = %d :: %s\n",count,__func__);
printf("No. of entries = %lld :: %s\n",temp_neworder.count,__func__);
fclose(neworder_tbl);
return 0;
......@@ -157,16 +163,15 @@ int load_neworder_from_file() {
int load_order_from_file() {
FILE *order_tbl;
int count=0;
order_tbl=fopen(ORDER_TBL_FILE, "r");
order temp_order;
while(!feof(order_tbl)) {
order temp_order;
if(temp_order.parse(&order_tbl)==0)
{
order_list.push_front(temp_order);
count++;
temp_order.insert_new();
}
else
{
......@@ -174,7 +179,7 @@ int load_order_from_file() {
}
}
printf("No. of entries = %d :: %s\n",count,__func__);
printf("No. of entries = %lld :: %s\n",temp_order.count,__func__);
fclose(order_tbl);
return 0;
......@@ -182,16 +187,15 @@ int load_order_from_file() {
int load_orderline_from_file() {
FILE *orderline_tbl;
int count=0;
orderline_tbl=fopen(ORDERLINE_TBL_FILE, "r");
orderline temp_orderline;
while(!feof(orderline_tbl)) {
orderline temp_orderline;
if(temp_orderline.parse(&orderline_tbl)==0)
{
orderline_list.push_front(temp_orderline);
count++;
temp_orderline.insert_new();
}
else
{
......@@ -199,7 +203,7 @@ int load_orderline_from_file() {
}
}
printf("No. of entries = %d :: %s\n",count,__func__);
printf("No. of entries = %lld :: %s\n",temp_orderline.count,__func__);
fclose(orderline_tbl);
return 0;
......@@ -207,16 +211,15 @@ int load_orderline_from_file() {
int load_stock_from_file() {
FILE *stock_tbl;
int count=0;
stock_tbl=fopen(STOCK_TBL_FILE, "r");
stock temp_stock;
while(!feof(stock_tbl)) {
stock temp_stock;
if(temp_stock.parse(&stock_tbl)==0)
{
stock_list.push_front(temp_stock);
count++;
temp_stock.insert_new();
}
else
{
......@@ -224,7 +227,7 @@ int load_stock_from_file() {
}
}
printf("No. of entries = %d :: %s\n",count,__func__);
printf("No. of entries = %lld :: %s\n",temp_stock.count,__func__);
fclose(stock_tbl);
return 0;
......@@ -232,16 +235,15 @@ int load_stock_from_file() {
int load_warehouse_from_file() {
FILE *warehouse_tbl;
int count=0;
warehouse_tbl=fopen(WAREHOUSE_TBL_FILE, "r");
warehouse temp_warehouse;
while(!feof(warehouse_tbl)) {
warehouse temp_warehouse;
if(temp_warehouse.parse(&warehouse_tbl)==0)
{
warehouse_list.push_front(temp_warehouse);
count++;
temp_warehouse.insert_new();
}
else
{
......@@ -249,7 +251,7 @@ int load_warehouse_from_file() {
}
}
printf("No. of entries = %d :: %s\n",count,__func__);
printf("No. of entries = %lld :: %s\n",temp_warehouse.count,__func__);
fclose(warehouse_tbl);
return 0;
......
#ifndef _DATA_MIGRATE_H_
#define _DATA_MIGRATE_H_
#include <list>
#include "schema.h"
using namespace std;
int load_customer_from_file();
int load_district_from_file();
int load_history_from_file();
int load_item_from_file();
int load_neworder_from_file();
int load_order_from_file();
int load_orderline_from_file();
int load_stock_from_file();
int load_warehouse_from_file();
extern list<customer> customer_list;
extern list<district> district_list;
extern list<history> history_list;
extern list<item> item_list;
extern list<neworder> neworder_list;
extern list<orderline> orderline_list;
extern list<order> order_list;
extern list<stock> stock_list;
extern list<warehouse> warehouse_list;
#endif //_DATA_MIGRATE_H_
\ No newline at end of file
#ifndef _DATA_MIGRATE_H_
#define _DATA_MIGRATE_H_
#include <vector>
#include <map>
#include <tuple>
#include "schema.h"
using namespace std;
int load_customer_from_file();
int load_district_from_file();
int load_history_from_file();
int load_item_from_file();
int load_neworder_from_file();
int load_order_from_file();
int load_orderline_from_file();
int load_stock_from_file();
int load_warehouse_from_file();
extern vector<customer> customer_vect;
extern map<tuple<int,int,int>, uint64_t> customer_map; //primary key (c_w_id,c_d_id,c_id)
extern vector<district> district_vect;
extern map<tuple<int,int>,uint64_t> district_map; //primary key (d_w_id,d_id)
extern vector<history> history_vect;
extern map<tuple<int>,uint64_t> history_map; //primary_key ()
extern vector<item> item_vect;
extern map<tuple<int>,uint64_t> item_map; // primary key (i_id)
extern vector<neworder> neworder_vect;
extern map<tuple<int,int,int>,uint64_t> neworder_map; //primary key (no_w_id,no_d_id,no_o_id)
extern vector<orderline> orderline_vect;
extern map<tuple<int,int,int,int>,uint64_t> orderline_map; //primary key (ol_w_id,ol_d_id,ol_o_id,ol_number)
extern vector<order> order_vect;
extern map<tuple<int,int,int>,uint64_t> order_map; //primary key (o_w_id,o_d_id,o_id)
extern vector<stock> stock_vect;
extern map<tuple<int,int>,uint64_t> stock_map; // primary key (s_w_id,s_i_id)
extern vector<warehouse> warehouse_vect;
extern map<tuple<int>,uint64_t> warehouse_map; //primary key (w_id)
#endif //_DATA_MIGRATE_H_
\ No newline at end of file
......@@ -9,6 +9,8 @@
class warehouse {
public:
static uint64_t count;
int insert_new();
int w_id; // integer not null,
char w_name[11]; // varchar(10) not null,
char w_street_1[21]; // varchar(20) not null,
......@@ -18,12 +20,16 @@ class warehouse {
char w_zip[10]; // varchar(9) not null,
uint64_t w_tax;
uint64_t w_ytd;
//primary key (w_id)
int parse(FILE **fp);
};
class district {
public:
static uint64_t count;
int insert_new();
int d_id;
int d_w_id;
char d_name[11];
......@@ -41,8 +47,12 @@ class district {
class customer {
public:
static uint64_t count;
int insert_new();
int c_id;
int c_d_id;
int c_w_id;
......@@ -71,10 +81,14 @@ class customer {
//create index customer_wdl on customer(c_w_id,c_d_id,c_last,c_first);
class history {
public:
static uint64_t count;
int insert_new();
int h_c_id;
int h_c_d_id;
int h_c_w_id;
......@@ -83,14 +97,18 @@ class history {
uint64_t h_date;
uint64_t h_amount;
char h_data[25];
int parse(FILE **fp);
};
class neworder {
public:
static uint64_t count;
int insert_new();
int no_o_id;
int no_d_id;
int no_w_id;
......@@ -104,8 +122,12 @@ class neworder {
class order {
public:
static uint64_t count;
int insert_new();
int o_id;
int o_d_id;
int o_w_id;
......@@ -125,10 +147,14 @@ class order {
//create index order_wdc on "order"(o_w_id,o_d_id,o_c_id,o_id);
class orderline {
public:
static uint64_t count;
int insert_new();
int ol_o_id;
int ol_d_id;
int ol_w_id;
......@@ -148,8 +174,12 @@ class orderline {
class item {
public:
static uint64_t count;
int insert_new();
int i_id; // integer not null,
int i_im_id; // integer not null,
char i_name[25]; // varchar(24) not null,
......@@ -159,8 +189,12 @@ class item {
int parse(FILE **fp);
};
class stock {
public:
static uint64_t count;
int insert_new();
int s_i_id; // integer not null,
int s_w_id; // integer not null,
uint64_t s_quantity; // numeric(4,0) not null,
......@@ -184,4 +218,6 @@ class stock {
#endif //_SCHEMA_H_
\ No newline at end of file
......@@ -3,13 +3,13 @@
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <list>
#include <vector>
#include <ctime>
#include <sys/time.h>
#include "schema.h"
#include "data_migrate.h"
#include "oltp.h"
#include "include/schema.h"
#include "include/data_migrate.h"
#include "include/oltp.h"
using namespace std;
......@@ -44,9 +44,9 @@ int main(int argc, char* argv[]) {
int choice;
timeval start_time, end_time, time_taken;
list<warehouse>::iterator i;
vector<warehouse>::iterator i;
for(i=warehouse_list.begin(); i !=warehouse_list.end(); ++i)
for(i=warehouse_vect.begin(); i !=warehouse_vect.end(); ++i)
display_warehouse(*i);
//cout << *i << " "; // print with overloaded operator
......@@ -59,7 +59,7 @@ int main(int argc, char* argv[]) {
time_taken.tv_usec = end_time.tv_usec - start_time.tv_usec;
cout << "Time Taken " << time_taken.tv_sec << " s " << time_taken.tv_usec << " us\n";
cout << "newOrder operations per second are " << 1000/time_taken.tv_sec << endl;
cout << "newOrder operations per second are " << 1000000/(time_taken.tv_sec*1000+time_taken.tv_usec/1000) << endl;
return 0;
}
\ No newline at end of file
#include "schema.h"
#include "data_migrate.h"
#include "include/schema.h"
#include "include/data_migrate.h"
#include <list>
#include <vector>
#include <map>
#include <tuple>
using namespace std;
void newOrder(int w_id, int d_id, int c_id, int items, int supware[15], int itemid[15], int qty[15], uint64_t datetime) {
list<warehouse>::iterator ware_iter;
list<customer>::iterator cust_iter;
list<district>::iterator dist_iter;
//vector<warehouse>::iterator ware_vec_iter;
warehouse ware_vec_iter;
//vector<customer>::iterator cust_vec_iter;
customer cust_vec_iter;
//vector<district>::iterator dist_vec_iter;
district dist_vec_iter;
//printf("w_id is %d\n",w_id);
for(ware_iter=warehouse_list.begin(); (ware_iter!=warehouse_list.end() && (ware_iter->w_id!=w_id)); ++ware_iter);
for(cust_iter=customer_list.begin(); (cust_iter!=customer_list.end() && (cust_iter->c_w_id!=w_id && cust_iter->c_d_id!=d_id && cust_iter->c_id!=c_id)); ++cust_iter);
for(dist_iter=district_list.begin(); cust_iter!=customer_list.end() && (dist_iter->d_w_id!=w_id && dist_iter->d_id!=d_id); ++dist_iter);
//for(ware_vec_iter=warehouse_vect.begin(); (ware_vec_iter!=warehouse_vect.end() && (ware_vec_iter.w_id!=w_id)); ++ware_vec_iter);
ware_vec_iter = warehouse_vect.at(warehouse_map.find(make_tuple(w_id))->second);
//printf("Warehouse found %d\n",ware_vec_iter.w_id);
//for(cust_vec_iter=customer_vect.begin(); (cust_vec_iter!=customer_vect.end() && (cust_vec_iter.c_w_id!=w_id && cust_vec_iter.c_d_id!=d_id && cust_vec_iter.c_id!=c_id)); ++cust_vec_iter);
cust_vec_iter = customer_vect.at(customer_map.find(make_tuple(w_id,d_id,c_id))->second);
//for(dist_vec_iter=district_vect.begin(); cust_vec_iter!=customer_vect.end() && (dist_vec_iter.d_w_id!=w_id && dist_vec_iter.d_id!=d_id); ++dist_vec_iter);
dist_vec_iter = district_vect.at(district_map.find(make_tuple(w_id,d_id))->second);
int all_local = 1;
for(int i=0;i<items;i++) {
......@@ -23,59 +32,64 @@ void newOrder(int w_id, int d_id, int c_id, int items, int supware[15], int item
all_local=0;
}
int o_id=dist_iter->d_next_o_id; //d_next_o_id as o_id
int o_id=dist_vec_iter.d_next_o_id; //d_next_o_id as o_id
order *order_new=new order(o_id,d_id,w_id,c_id,datetime,0,items,all_local);
order_list.push_front(*order_new);
order order_new(o_id,d_id,w_id,c_id,datetime,0,items,all_local);
order_vect.push_back(order_new);
neworder *neworder_new=new neworder(o_id,d_id,w_id);
neworder_list.push_front(*neworder_new);
neworder neworder_new(o_id,d_id,w_id);
neworder_vect.push_back(neworder_new);
for(int i=0;i<items;i++) {
list<item>::iterator item_iter;
list<stock>::iterator stock_iter;
for(item_iter=item_list.begin();item_iter!=item_list.end() && item_iter->i_id!=itemid[i];++item_iter);
for(stock_iter=stock_list.begin();stock_iter!=stock_list.end() && (stock_iter->s_w_id!=supware[i] && stock_iter->s_i_id!=itemid[i]);++stock_iter);
//vector<item>::iterator item_vec_iter;
item item_vec_iter;
//vector<stock>::iterator stock_vec_iter;
stock stock_vec_iter;
//for(item_vec_iter=item_vect.begin();item_vec_iter!=item_vect.end() && item_vec_iter.i_id!=itemid[i];++item_vec_iter);
item_vec_iter = item_vect.at(item_map.find(make_tuple(itemid[i]))->second);
//for(stock_vec_iter=stock_vect.begin();stock_vec_iter!=stock_vect.end() && (stock_vec_iter.s_w_id!=supware[i] && stock_vec_iter.s_i_id!=itemid[i]);++stock_vec_iter);
stock_vec_iter = stock_vect.at(stock_map.find(make_tuple(supware[i],itemid[i]))->second);
if(stock_iter->s_quantity > qty[i])
stock_iter->s_quantity = stock_iter->s_quantity - qty[i];
if(stock_vec_iter.s_quantity > qty[i])
stock_vec_iter.s_quantity = stock_vec_iter.s_quantity - qty[i];
else
stock_iter->s_quantity = stock_iter->s_quantity + 91 - qty[i];
stock_vec_iter.s_quantity = stock_vec_iter.s_quantity + 91 - qty[i];
if(supware[i] != w_id)
stock_iter->s_remote_cnt = stock_iter->s_remote_cnt +1;
stock_vec_iter.s_remote_cnt = stock_vec_iter.s_remote_cnt +1;
else
stock_iter->s_order_cnt = stock_iter->s_order_cnt +1;
stock_vec_iter.s_order_cnt = stock_vec_iter.s_order_cnt +1;
uint64_t ol_amount=qty[i]*(item_iter->i_price)*(1.0+(ware_iter->w_tax)*(1.0-(cust_iter->c_discount)));
uint64_t ol_amount=qty[i]*(item_vec_iter.i_price)*(1.0+(ware_vec_iter.w_tax)*(1.0-(cust_vec_iter.c_discount)));
char s_dist[25];
switch(d_id) {
case 1: strcpy(stock_iter->s_dist_01,s_dist);
case 1: strcpy(stock_vec_iter.s_dist_01,s_dist);
break;
case 2: strcpy(stock_iter->s_dist_02,s_dist);
case 2: strcpy(stock_vec_iter.s_dist_02,s_dist);
break;
case 3: strcpy(stock_iter->s_dist_03,s_dist);
case 3: strcpy(stock_vec_iter.s_dist_03,s_dist);
break;
case 4: strcpy(stock_iter->s_dist_04,s_dist);
case 4: strcpy(stock_vec_iter.s_dist_04,s_dist);
break;
case 5: strcpy(stock_iter->s_dist_05,s_dist);
case 5: strcpy(stock_vec_iter.s_dist_05,s_dist);
break;
case 6: strcpy(stock_iter->s_dist_06,s_dist);
case 6: strcpy(stock_vec_iter.s_dist_06,s_dist);
break;
case 7: strcpy(stock_iter->s_dist_07,s_dist);
case 7: strcpy(stock_vec_iter.s_dist_07,s_dist);
break;
case 8: strcpy(stock_iter->s_dist_08,s_dist);
case 8: strcpy(stock_vec_iter.s_dist_08,s_dist);
break;
case 9: strcpy(stock_iter->s_dist_09,s_dist);
case 9: strcpy(stock_vec_iter.s_dist_09,s_dist);
break;
case 10: strcpy(stock_iter->s_dist_10,s_dist);
case 10: strcpy(stock_vec_iter.s_dist_10,s_dist);
break;
default: break;
}
orderline *orderline_new=new orderline(o_id,d_id,w_id,i+1,itemid[i],supware[i],0,qty[i],ol_amount,s_dist);
orderline_list.push_front(*orderline_new);
orderline orderline_new(o_id,d_id,w_id,i+1,itemid[i],supware[i],0,qty[i],ol_amount,s_dist);
orderline_new.insert_new();
}
}
......
create transaction newOrder ( integer w_id, integer d_id, integer c_id, integer items, array(15) integer supware, array(15) integer itemid, array(15) integer qty, timestamp datetime)
{
select w_tax from warehouse w where w.w_id=w_id;
select c_discount from customer c where c_w_id=w_id and c_d_id=d_id and c.c_id=c_id;
select d_next_o_id as o_id,d_tax from district d where d_w_id=w_id and d.d_id=d_id;
update district set d_next_o_id=o_id+1 where d_w_id=w_id and district.d_id=d_id;
var integer all_local = 1;
forsequence (index between 0 and items-1) {
if (w_id<>supware[index])
all_local=0;
}
insert into "order" values (o_id,d_id,w_id,c_id,datetime,0,items,all_local);
insert into neworder values (o_id,d_id,w_id);
forsequence (index between 0 and items-1) {
select i_price from item where i_id=itemid[index];
select s_quantity,s_remote_cnt,s_order_cnt,case d_id when 1 then s_dist_01 when 2 then s_dist_02 when 3 then s_dist_03 when 4 then s_dist_04 when 5 then s_dist_05 when 6 then s_dist_06 when 7 then s_dist_07 when 8 then s_dist_08 when 9 then s_dist_09 when 10 then s_dist_10 end as s_dist from stock where s_w_id=supware[index] and s_i_id=itemid[index];
if (s_quantity>qty[index]) {
update stock set s_quantity=s_quantity-qty[index] where s_w_id=supware[index] and s_i_id=itemid[index];
} else {
update stock set s_quantity=s_quantity+91-qty[index] where s_w_id=supware[index] and s_i_id=itemid[index];
}
if (supware[index]<>w_id) {
update stock set s_remote_cnt=s_remote_cnt+1 where s_w_id=w_id and s_i_id=itemid[index];
} else {
update stock set s_order_cnt=s_order_cnt+1 where s_w_id=w_id and s_i_id=itemid[index];
}
var numeric(6,2) ol_amount=qty[index]*i_price*(1.0+w_tax+d_tax)*(1.0-c_discount);
insert into orderline values (o_id,d_id,w_id,index+1,itemid[index],supware[index],0,qty[index],ol_amount,s_dist);
}
commit;
};
......@@ -2,7 +2,7 @@
#include <ctime> //for time_t
#include <string.h> //For strcpy and strcat
#include <cstdlib> //for random
#include "neworder.h" //for neworder()
#include "include/neworder.h" //for neworder()
#include <stdio.h> // DEBUG for printf
using namespace std;
......
create constant integer warehouses = 5;
create function varchar(20) genName(integer id)
{
var array(10) varchar(5) parts = {'BAR','OUGHT','ABLE','PRI','PRES','ESE','ANTI','CALLY','ATION','EING'};
return parts[(id/100)%10]+parts[(id/10)%10]+parts[id%10];
};
create function numeric(6,2) randomnumeric62(numeric(6,2) min,numeric(6,2) max)
{
return min+((cast (random%cast ((max-min)*100) as integer) as numeric(6,2))/100);
};
create function integer nurand(integer A,integer x,integer y)
{
return ((((random%A)|(random%(y-x+1)+x))+42)%(y-x+1))+x;
};
create function integer urand(integer min,integer max)
{
return (random%(max-min+1))+min;
};
create function integer urandexcept(integer min,integer max,integer v)
{
if (max<=min)
return min;
var integer r=(random%(max-min))+min;
if (r>=v)
return r+1; else
return r;
};
create function void newOrderRandom(timestamp now,integer w_id)
{
var integer d_id=execute urand(1,10);
var integer c_id=execute nurand(1023,1,3000);
var integer ol_cnt=execute urand(5,15);
var array(15) integer supware;
var array(15) integer itemid;
var array(15) integer qty;
forsequence (index between 0 and ol_cnt-1) {
if (execute urand(1,100)>1)
supware[index]=w_id; else
supware[index]=execute urandexcept(1,warehouses,w_id);
itemid[index]=execute nurand(8191,1,100000);
qty[index]=execute urand(1,10);
}
forsequence (index between ol_cnt and 14) {
supware[index]=w_id;
}
execute newOrder(w_id,d_id,c_id,ol_cnt,supware,itemid,qty,now);
};
create function void paymentRandom(timestamp now,integer w_id)
{
var integer d_id=execute urand(1,10);
var integer c_w_id;
var integer c_d_id;
if (execute urand(1,100)<=85) {
c_w_id=w_id;
c_d_id=d_id;
} else {
c_w_id=execute urandexcept(1,warehouses,w_id);
c_d_id=execute urand(1,10);
}
var numeric(6,2) h_amount=execute randomnumeric62(1.00,5000.00);
var timestamp h_date=now;
if (execute urand(1,100)<=60) {
execute paymentByName(w_id,d_id,c_w_id,c_d_id,execute genName(execute nurand(255,0,999)),h_date,h_amount,now);
} else {
execute paymentById(w_id,d_id,c_w_id,c_d_id,execute nurand(1023,1,3000),h_date,h_amount,now);
}
};
create function void ostatRandom(integer w_id)
{
var integer d_id=execute urand(1,10);
if (execute urand(1,100)<=60) {
execute ostatByName(w_id,d_id,execute genName(execute nurand(255,0,999)));
} else {
execute ostatById(w_id,d_id,execute nurand(1023,1,3000));
}
};
create function void deliveryRandom(timestamp now,integer w_id)
{
var integer carrier_id=execute urand(1,10);
execute delivery(w_id,carrier_id,now);
};
create function void slevRandom(integer w_id)
{
var integer d_id=execute urand(1,10);
var integer threshold=execute urand(10,20);
execute slev(w_id,d_id,threshold);
};
create procedure integer partitionedOltp(timestamp now,integer w_id)
{
var integer choice=execute urand(1,1000);
-- Payment?
if (choice<=430) {
execute paymentRandom(now,w_id);
return 1;
}
choice=choice-430;
-- Order status?
if (choice<=40) {
execute ostatRandom(w_id);
return 2;
}
choice=choice-40;
-- Delivery?
if (choice<=45) {
execute deliveryRandom(now,w_id);
return 3;
}
choice=choice-45;
-- Stock level?
if (choice<=40) {
execute slevRandom(w_id);
return 40;
}
-- Default to new order
execute newOrderRandom(now,w_id);
return 0;
};
create procedure integer oltp(timestamp now)
{
return execute partitionedOltp(now,execute urand(1,warehouses));
};
create table warehouse (
w_id integer not null,
w_name varchar(10) not null,
w_street_1 varchar(20) not null,
w_street_2 varchar(20) not null,
w_city varchar(20) not null,
w_state char(2) not null,
w_zip char(9) not null,
w_tax numeric(4,4) not null,
w_ytd numeric(12,2) not null,
primary key (w_id)
);
create table district (
d_id integer not null,
d_w_id integer not null,
d_name varchar(10) not null,
d_street_1 varchar(20) not null,
d_street_2 varchar(20) not null,
d_city varchar(20) not null,
d_state char(2) not null,
d_zip char(9) not null,
d_tax numeric(4,4) not null,
d_ytd numeric(12,2) not null,
d_next_o_id integer not null,
primary key (d_w_id,d_id)
);
create table customer (
c_id integer not null,
c_d_id integer not null,
c_w_id integer not null,
c_first varchar(16) not null,
c_middle char(2) not null,
c_last varchar(16) not null,
c_street_1 varchar(20) not null,
c_street_2 varchar(20) not null,
c_city varchar(20) not null,
c_state char(2) not null,
c_zip char(9) not null,
c_phone char(16) not null,
c_since timestamp not null,
c_credit char(2) not null,
c_credit_lim numeric(12,2) not null,
c_discount numeric(4,4) not null,
c_balance numeric(12,2) not null,
c_ytd_paymenr numeric(12,2) not null,
c_payment_cnt numeric(4,0) not null,
c_delivery_cnt numeric(4,0) not null,
c_data varchar(500) not null,
primary key (c_w_id,c_d_id,c_id)
);
create index customer_wdl on customer(c_w_id,c_d_id,c_last,c_first);
create table history (
h_c_id integer not null,
h_c_d_id integer not null,
h_c_w_id integer not null,
h_d_id integer not null,
h_w_id integer not null,
h_date timestamp not null,
h_amount numeric(6,2) not null,
h_data varchar(24) not null
);
create table neworder (
no_o_id integer not null,
no_d_id integer not null,
no_w_id integer not null,
primary key (no_w_id,no_d_id,no_o_id)
);
create table "order" (
o_id integer not null,
o_d_id integer not null,
o_w_id integer not null,
o_c_id integer not null,
o_entry_d timestamp not null,
o_carrier_id integer not null,
o_ol_cnt numeric(2,0) not null,
o_all_local numeric(1,0) not null,
primary key (o_w_id,o_d_id,o_id)
);
create index order_wdc on "order"(o_w_id,o_d_id,o_c_id,o_id);
create table orderline (
ol_o_id integer not null,
ol_d_id integer not null,
ol_w_id integer not null,
ol_number integer not null,
ol_i_id integer not null,
ol_supply_w_id integer not null,
ol_delivery_d timestamp not null,
ol_quantity numeric(2,0) not null,
ol_amount numeric(6,2) not null,
ol_dist_info char(24) not null,
primary key (ol_w_id,ol_d_id,ol_o_id,ol_number)
);
create table item (
i_id integer not null,
i_im_id integer not null,
i_name varchar(24) not null,
i_price numeric(5,2) not null,
i_data varchar(50) not null,
primary key (i_id)
);
create table stock (
s_i_id integer not null,
s_w_id integer not null,
s_quantity numeric(4,0) not null,
s_dist_01 char(24) not null,
s_dist_02 char(24) not null,
s_dist_03 char(24) not null,
s_dist_04 char(24) not null,
s_dist_05 char(24) not null,
s_dist_06 char(24) not null,
s_dist_07 char(24) not null,
s_dist_08 char(24) not null,
s_dist_09 char(24) not null,
s_dist_10 char(24) not null,
s_ytd numeric(8,0) not null,
s_order_cnt numeric(4,0) not null,
s_remote_cnt numeric(4,0) not null,
s_data varchar(50) not null,
primary key (s_w_id,s_i_id)
);
#include "schema.h"
#include "include/schema.h"
orderline::orderline() {}
......
......@@ -3,7 +3,22 @@
#include <stdlib.h>
#include <string.h>
#include "schema.h"
#include <map>
#include <tuple>
#include <vector>
#include "include/data_migrate.h"
#include "include/schema.h"
uint64_t warehouse::count=0;
int warehouse::insert_new() {
warehouse_vect.push_back(*this);
warehouse_map.insert(make_pair(make_tuple(w_id), count));
count++;
}
int warehouse::parse(FILE **fp) {
char token[25];
......@@ -62,6 +77,16 @@ int warehouse::parse(FILE **fp) {
return 0;
}
uint64_t district::count=0;
int district::insert_new() {
district_vect.push_back(*this);
district_map.insert(make_pair(make_tuple(d_w_id,d_id), count));
count++;
}
int district::parse(FILE **fp) {
char token[25];
int i,j;
......@@ -123,6 +148,16 @@ int district::parse(FILE **fp) {
return 0;
}
uint64_t customer::count=0;
int customer::insert_new() {
customer_vect.push_back(*this);
customer_map.insert(make_pair(make_tuple(c_w_id, c_d_id, c_id), count));
count++;
}
int customer::parse(FILE **fp) {
char token[501];
int i,j;
......@@ -204,6 +239,16 @@ int customer::parse(FILE **fp) {
return 0;
}
uint64_t history::count=0;
int history::insert_new() {
history_vect.push_back(*this);
//history_map.insert(make_pair(make_tuple(), count));
count++;
}
int history::parse(FILE **fp) {
char token[25];
int i,j;
......@@ -259,6 +304,16 @@ int history::parse(FILE **fp) {
return 0;
}
uint64_t neworder::count=0;
int neworder::insert_new() {
neworder_vect.push_back(*this);
neworder_map.insert(make_pair(make_tuple(no_w_id, no_d_id, no_o_id), count));
count++;
}
int neworder::parse(FILE **fp) {
char token[10];
int i,j;
......@@ -304,6 +359,16 @@ int neworder::parse(FILE **fp) {
return 0;
}
uint64_t order::count=0;
int order::insert_new() {
order_vect.push_back(*this);
order_map.insert(make_pair(make_tuple(o_w_id, o_d_id, o_id), count));
count++;
}
int order::parse(FILE **fp) {
char token[25];
int i,j;
......@@ -359,6 +424,16 @@ int order::parse(FILE **fp) {
return 0;
}
uint64_t orderline::count=0;
int orderline::insert_new() {
orderline_vect.push_back(*this);
orderline_map.insert(make_pair(make_tuple(ol_w_id, ol_d_id, ol_o_id, ol_number), count));
count++;
}
int orderline::parse(FILE **fp) {
char token[25];
int i,j;
......@@ -418,6 +493,16 @@ int orderline::parse(FILE **fp) {
return 0;
}
uint64_t item::count=0;
int item::insert_new() {
item_vect.push_back(*this);
item_map.insert(make_pair(make_tuple(i_id), count));
count++;
}
int item::parse(FILE **fp) {
char token[51];
int i,j;
......@@ -467,6 +552,16 @@ int item::parse(FILE **fp) {
return 0;
}
uint64_t stock::count=0;
int stock::insert_new() {
stock_vect.push_back(*this);
stock_map.insert(make_pair(make_tuple(s_w_id, s_i_id), count));
count++;
}
int stock::parse(FILE **fp) {
char token[51];
int i,j;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment