write query_order

This commit is contained in:
2024-05-24 15:26:47 +00:00
parent dd0487e834
commit f3dd078e65
2 changed files with 42 additions and 7 deletions

View File

@ -146,7 +146,7 @@ class TransactionManager : public DataDriverBase {
}
inline void AddOrder(std::string trainID, std::string from_station_name, std::string to_station_name, uint8_t status,
uint32_t leave_time_stamp, uint32_t arrive_time_stamp, uint32_t num, uint64_t total_price,
uint8_t running_date_offset) {
uint8_t running_date_offset, std::string username) {
TransactionData tmp;
strcpy(tmp.trainID, trainID.c_str());
strcpy(tmp.from_station_name, from_station_name.c_str());
@ -158,6 +158,7 @@ class TransactionManager : public DataDriverBase {
tmp.total_price = total_price;
b_plus_tree_value_index_t data_id = data_storage->write(tmp);
hash_t train_ID_hash = SplitMix64Hash(trainID);
hash_t user_ID_hash = SplitMix64Hash(username);
if (status == 0) {
queue_index_t queue_index_for_query;
queue_index_for_query.train_ID_hash = train_ID_hash;
@ -176,7 +177,7 @@ class TransactionManager : public DataDriverBase {
queue_indexer->Put(queue_index, data_id);
}
order_history_index_t order_history_index_for_query;
order_history_index_for_query.user_ID_hash = train_ID_hash;
order_history_index_for_query.user_ID_hash = user_ID_hash;
order_history_index_for_query.id = order_history_index_special_id;
int new_id;
{
@ -185,7 +186,7 @@ class TransactionManager : public DataDriverBase {
it_order_history_query.SetValue(new_id);
} // to release the lock
order_history_index_t order_history_index;
order_history_index.user_ID_hash = train_ID_hash;
order_history_index.user_ID_hash = user_ID_hash;
order_history_index.id = new_id;
order_history_indexer->Put(order_history_index, data_id);
}
@ -244,6 +245,7 @@ class TransactionManager : public DataDriverBase {
}
inline void FetchTransactionData(b_plus_tree_value_index_t idx, TransactionData &data) {
// warning: the validity of idx is not checked
LOG->debug("fetching transaction data with idx {}", idx);
data_storage->read(data, idx);
}
};

View File

@ -257,13 +257,13 @@ std::string TicketSystemEngine::BuyTicket(const std::string &command) {
}
transaction_manager.AddOrder(train_id, from, to, 0, info.leave_time_stamp, info.arrive_time_stamp, ticket_num,
total_price * (unsigned long long)ticket_num,
info.actual_start_date - info.saleDate_beg);
info.actual_start_date - info.saleDate_beg, user_name);
response_stream << "[" << command_id << "] queue";
return response_stream.str();
}
transaction_manager.AddOrder(train_id, from, to, 1, info.leave_time_stamp, info.arrive_time_stamp, ticket_num,
total_price * (unsigned long long)ticket_num,
info.actual_start_date - info.saleDate_beg);
total_price * (unsigned long long)ticket_num, info.actual_start_date - info.saleDate_beg,
user_name);
for (int j = from_station_id; j < to_station_id; j++) {
seats_data.seat[j] -= ticket_num;
}
@ -288,7 +288,40 @@ std::string TicketSystemEngine::QueryOrder(const std::string &command) {
}
}
}
response_stream << "[" << command_id << "] QueryOrder";
hash_t user_ID_hash = SplitMix64Hash(user_name);
if (online_users.find(user_ID_hash) == online_users.end()) {
response_stream << "[" << command_id << "] -1";
return response_stream.str();
}
std::vector<b_plus_tree_value_index_t> his_idxs;
transaction_manager.FetchFullUserOrderHistory(user_ID_hash, his_idxs);
size_t len = his_idxs.size();
TransactionData txn_data;
response_stream << "[" << command_id << "] " << len;
for (size_t i = 0; i < len; i++) {
transaction_manager.FetchTransactionData(his_idxs[i], txn_data);
response_stream << "\n[";
if (txn_data.status == 0) {
response_stream << "pending] ";
} else if (txn_data.status == 1) {
response_stream << "success] ";
} else {
response_stream << "refunded] ";
}
response_stream << txn_data.trainID << " " << txn_data.from_station_name << " ";
int leave_month, leave_day, leave_hour, leave_minute;
RetrieveReadableTimeStamp(txn_data.leave_time_stamp, leave_month, leave_day, leave_hour, leave_minute);
response_stream << std::setw(2) << std::setfill('0') << leave_month << '-' << std::setw(2) << std::setfill('0')
<< leave_day << ' ' << std::setw(2) << std::setfill('0') << leave_hour << ':' << std::setw(2)
<< std::setfill('0') << leave_minute;
response_stream << " -> " << txn_data.to_station_name << " ";
int arrive_month, arrive_day, arrive_hour, arrive_minute;
RetrieveReadableTimeStamp(txn_data.arrive_time_stamp, arrive_month, arrive_day, arrive_hour, arrive_minute);
response_stream << std::setw(2) << std::setfill('0') << arrive_month << '-' << std::setw(2) << std::setfill('0')
<< arrive_day << ' ' << std::setw(2) << std::setfill('0') << arrive_hour << ':' << std::setw(2)
<< std::setfill('0') << arrive_minute;
response_stream << " " << txn_data.total_price / txn_data.num << " " << txn_data.num;
}
return response_stream.str();
}