-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtpcctables.h
More file actions
146 lines (122 loc) · 7.08 KB
/
tpcctables.h
File metadata and controls
146 lines (122 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef TPCCTABLES_H__
#define TPCCTABLES_H__
#include <map>
#include <set>
#include <vector>
#include "btree.h"
#include "tpccdb.h"
class CustomerByNameOrdering {
public:
bool operator()(const Customer* a, const Customer* b);
};
// Stores all the tables in TPC-C
class TPCCTables : public TPCCDB {
public:
virtual ~TPCCTables();
virtual int32_t stockLevel(int32_t warehouse_id, int32_t district_id, int32_t threshold);
virtual void orderStatus(int32_t warehouse_id, int32_t district_id, int32_t customer_id,
OrderStatusOutput* output);
virtual void orderStatus(int32_t warehouse_id, int32_t district_id, const char* c_last,
OrderStatusOutput* output);
virtual bool newOrder(int32_t warehouse_id, int32_t district_id, int32_t customer_id,
const std::vector<NewOrderItem>& items, const char* now,
NewOrderOutput* output, TPCCUndo** undo);
virtual bool newOrderHome(int32_t warehouse_id, int32_t district_id, int32_t customer_id,
const std::vector<NewOrderItem>& items, const char* now,
NewOrderOutput* output, TPCCUndo** undo);
virtual bool newOrderRemote(int32_t home_warehouse, int32_t remote_warehouse,
const std::vector<NewOrderItem>& items, std::vector<int32_t>* out_quantities,
TPCCUndo** undo);
virtual void payment(int32_t warehouse_id, int32_t district_id, int32_t c_warehouse_id,
int32_t c_district_id, int32_t customer_id, float h_amount, const char* now,
PaymentOutput* output, TPCCUndo** undo);
virtual void payment(int32_t warehouse_id, int32_t district_id, int32_t c_warehouse_id,
int32_t c_district_id, const char* c_last, float h_amount, const char* now,
PaymentOutput* output, TPCCUndo** undo);
virtual void paymentHome(int32_t warehouse_id, int32_t district_id, int32_t c_warehouse_id,
int32_t c_district_id, int32_t c_id, float h_amount, const char* now,
PaymentOutput* output, TPCCUndo** undo);
virtual void paymentRemote(int32_t warehouse_id, int32_t district_id, int32_t c_warehouse_id,
int32_t c_district_id, int32_t c_id, float h_amount, PaymentOutput* output,
TPCCUndo** undo);
virtual void paymentRemote(int32_t warehouse_id, int32_t district_id, int32_t c_warehouse_id,
int32_t c_district_id, const char* c_last, float h_amount, PaymentOutput* output,
TPCCUndo** undo);
virtual void delivery(int32_t warehouse_id, int32_t carrier_id, const char* now,
std::vector<DeliveryOrderInfo>* orders, TPCCUndo** undo);
virtual bool hasWarehouse(int32_t warehouse_id) { return findWarehouse(warehouse_id) != NULL; }
virtual void applyUndo(TPCCUndo* undo);
virtual void freeUndo(TPCCUndo* undo) { assert(undo != NULL); delete undo; }
void reserveItems(int size) { items_.reserve(size); }
// Copies item into the item table.
void insertItem(const Item& item);
Item* findItem(int32_t i_id);
void insertWarehouse(const Warehouse& warehouse);
Warehouse* findWarehouse(int32_t w_id);
void insertStock(const Stock& stock);
Stock* findStock(int32_t w_id, int32_t s_id);
void insertDistrict(const District& district);
District* findDistrict(int32_t w_id, int32_t d_id);
void insertCustomer(const Customer& customer);
Customer* findCustomer(int32_t w_id, int32_t d_id, int32_t c_id);
// Finds all customers that match (w_id, d_id, *, c_last), taking the n/2th one (rounded up).
Customer* findCustomerByName(int32_t w_id, int32_t d_id, const char* c_last);
// Stores order in the database. Returns a pointer to the database's tuple.
Order* insertOrder(const Order& order);
Order* findOrder(int32_t w_id, int32_t d_id, int32_t o_id);
Order* findLastOrderByCustomer(int32_t w_id, int32_t d_id, int32_t c_id);
// Stores orderline in the database. Returns a pointer to the database's tuple.
OrderLine* insertOrderLine(const OrderLine& orderline);
OrderLine* findOrderLine(int32_t w_id, int32_t d_id, int32_t o_id, int32_t number);
// Creates a new order in the database. Returns a pointer to the database's tuple.
NewOrder* insertNewOrder(int32_t w_id, int32_t d_id, int32_t o_id);
NewOrder* findNewOrder(int32_t w_id, int32_t d_id, int32_t o_id);
const std::vector<const History*>& history() const { return history_; }
// Stores order in the database. Returns a pointer to the database's tuple.
History* insertHistory(const History& history);
static const int KEYS_PER_INTERNAL = 8;
static const int KEYS_PER_LEAF = 8;
private:
static const int STOCK_LEVEL_ORDERS = 20;
// Loads each item from the items table. Returns true if they are all found.
bool findAndValidateItems(const std::vector<NewOrderItem>& items,
std::vector<Item*>* item_tuples);
// Implements order status transaction after the customer tuple has been located.
void internalOrderStatus(Customer* customer, OrderStatusOutput* output);
// Implements payment transaction after the customer tuple has been located.
void internalPaymentRemote(int32_t warehouse_id, int32_t district_id, Customer* c,
float h_amount, PaymentOutput* output, TPCCUndo** undo);
// Erases order from the database. NOTE: This is only for undoing transactions.
void eraseOrder(const Order* order);
// Erases order_line from the database. NOTE: This is only for undoing transactions.
void eraseOrderLine(const OrderLine* order_line);
// Erases new_order from the database. NOTE: This is only for undoing transactions.
void eraseNewOrder(const NewOrder* new_order);
// Erases history from the database. NOTE: This is only for undoing transactions.
void eraseHistory(const History* history);
// Allocates an undo buffer if needed, storing the pointer in *undo.
void allocateUndo(TPCCUndo** undo) {
if (undo != NULL && *undo == NULL) {
*undo = new TPCCUndo();
}
}
// TODO: Use a data structure that supports deletes, appends, and sparse ranges.
// Using a vector instead of a BPlusTree reduced the new order run time by 3.65us. This was an
// improvement of 12%. It also saved 4141kB of RSS.
std::vector<Item> items_;
BPlusTree<int32_t, Warehouse*, KEYS_PER_INTERNAL, KEYS_PER_LEAF> warehouses_;
BPlusTree<int32_t, Stock*, KEYS_PER_INTERNAL, KEYS_PER_LEAF> stock_;
BPlusTree<int32_t, District*, KEYS_PER_INTERNAL, KEYS_PER_LEAF> districts_;
BPlusTree<int32_t, Customer*, KEYS_PER_INTERNAL, KEYS_PER_LEAF> customers_;
typedef std::set<Customer*, CustomerByNameOrdering> CustomerByNameSet;
CustomerByNameSet customers_by_name_;
BPlusTree<int32_t, Order*, KEYS_PER_INTERNAL, KEYS_PER_LEAF> orders_;
// TODO: Tune the size of this tree for the bigger keys?
BPlusTree<int64_t, Order*, KEYS_PER_INTERNAL, KEYS_PER_LEAF> orders_by_customer_;
BPlusTree<int32_t, OrderLine*, KEYS_PER_INTERNAL, KEYS_PER_LEAF> orderlines_;
// TODO: Implement btree lower_bound?
typedef std::map<int64_t, NewOrder*> NewOrderMap;
NewOrderMap neworders_;
std::vector<const History*> history_;
};
#endif