-
Notifications
You must be signed in to change notification settings - Fork 17
/
tpccdb.cc
150 lines (134 loc) · 5.13 KB
/
tpccdb.cc
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
147
148
149
150
#include "tpccdb.h"
#include <cassert>
#include "stlutil.h"
void Address::copy(char* street1, char* street2, char* city, char* state, char* zip,
const char* src_street1, const char* src_street2, const char* src_city,
const char* src_state, const char* src_zip) {
// TODO: Just do one copy since all the fields should be contiguous?
memcpy(street1, src_street1, MAX_STREET+1);
memcpy(street2, src_street2, MAX_STREET+1);
memcpy(city, src_city, MAX_CITY+1);
memcpy(state, src_state, STATE+1);
memcpy(zip, src_zip, ZIP+1);
}
// Non-integral constants must be defined in a .cc file according to the standard
const char Customer::GOOD_CREDIT[] = "GC";
const char Customer::BAD_CREDIT[] = "BC";
const char NewOrderOutput::INVALID_ITEM_STATUS[] = "Item number is not valid";
TPCCUndo::~TPCCUndo() {
STLDeleteValues(&modified_warehouses_);
STLDeleteValues(&modified_districts_);
STLDeleteValues(&modified_customers_);
STLDeleteValues(&modified_stock_);
STLDeleteValues(&modified_orders_);
STLDeleteValues(&modified_order_lines_);
STLDeleteElements(&deleted_new_orders_);
}
template <typename T>
static void copyIfNeeded(typename std::unordered_map<T*, T*>* map, T* source) {
typedef typename std::unordered_map<T*, T*> MapType;
std::pair<typename MapType::iterator, bool> result = map->insert(
typename MapType::value_type(source, NULL));
if (result.second) {
// we did the insert: copy the value
assert(result.first->second == NULL);
result.first->second = new T(*source);
} else {
assert(result.first->second != NULL);
}
}
void TPCCUndo::save(Warehouse* w) {
copyIfNeeded(&modified_warehouses_, w);
}
void TPCCUndo::save(District* d) {
copyIfNeeded(&modified_districts_, d);
}
void TPCCUndo::save(Customer* c) {
copyIfNeeded(&modified_customers_, c);
}
void TPCCUndo::save(Stock* s) {
copyIfNeeded(&modified_stock_, s);
}
void TPCCUndo::save(Order* o) {
copyIfNeeded(&modified_orders_, o);
}
void TPCCUndo::save(OrderLine* ol) {
copyIfNeeded(&modified_order_lines_, ol);
}
void TPCCUndo::inserted(const Order* o) {
assert(inserted_orders_.find(o) == inserted_orders_.end());
inserted_orders_.insert(o);
}
void TPCCUndo::inserted(const OrderLine* ol) {
assert(inserted_order_lines_.find(ol) == inserted_order_lines_.end());
inserted_order_lines_.insert(ol);
}
void TPCCUndo::inserted(const NewOrder* no) {
assert(inserted_new_orders_.find(no) == inserted_new_orders_.end());
inserted_new_orders_.insert(no);
}
void TPCCUndo::inserted(const History* h) {
assert(inserted_history_.find(h) == inserted_history_.end());
inserted_history_.insert(h);
}
void TPCCUndo::deleted(NewOrder* no) {
assert(deleted_new_orders_.find(no) == deleted_new_orders_.end());
deleted_new_orders_.insert(no);
}
void TPCCUndo::applied() {
deleted_new_orders_.clear();
}
TPCCDB::WarehouseSet TPCCDB::newOrderRemoteWarehouses(int32_t home_warehouse,
const std::vector<NewOrderItem>& items) {
WarehouseSet out;
for (size_t i = 0; i < items.size(); ++i) {
if (items[i].ol_supply_w_id != home_warehouse) {
out.insert(items[i].ol_supply_w_id);
}
}
return out;
}
void TPCCDB::newOrderCombine(const std::vector<int32_t>& remote_quantities,
NewOrderOutput* output) {
assert(remote_quantities.size() == output->items.size());
for (size_t i = 0; i < remote_quantities.size(); ++i) {
if (remote_quantities[i] != INVALID_QUANTITY) {
assert(output->items[i].s_quantity == 0);
output->items[i].s_quantity = remote_quantities[i];
}
}
}
void TPCCDB::newOrderCombine(const std::vector<int32_t>& remote_quantities,
std::vector<int32_t>* output) {
assert(remote_quantities.size() == output->size());
for (size_t i = 0; i < remote_quantities.size(); ++i) {
if (remote_quantities[i] != INVALID_QUANTITY) {
assert((*output)[i] == INVALID_QUANTITY);
(*output)[i] = remote_quantities[i];
}
}
}
// TODO: These macros are copied from tpcctables.cc. Is there a way to share them?
#define COPY_ADDRESS(src, dest, prefix) \
Address::copy( \
dest->prefix ## street_1, dest->prefix ## street_2, dest->prefix ## city, \
dest->prefix ## state, dest->prefix ## zip,\
src.prefix ## street_1, src.prefix ## street_2, src.prefix ## city, \
src.prefix ## state, src.prefix ## zip)
#define COPY_STRING(dest, src, field) memcpy(dest->field, src.field, sizeof(src.field))
// Copy the customer fields from remote into local
void TPCCDB::paymentCombine(const PaymentOutput& remote, PaymentOutput* home) {
home->c_credit_lim = remote.c_credit_lim;
home->c_discount = remote.c_discount;
home->c_balance = remote.c_balance;
COPY_STRING(home, remote, c_first);
COPY_STRING(home, remote, c_middle);
COPY_STRING(home, remote, c_last);
COPY_ADDRESS(remote, home, c_);
COPY_STRING(home, remote, c_phone);
COPY_STRING(home, remote, c_since);
COPY_STRING(home, remote, c_credit);
COPY_STRING(home, remote, c_data);
}
#undef COPY_STRING
#undef COPY_ADDRESS