finish writing transaction manager, ready to fulfill transacntion system

This commit is contained in:
2024-05-24 14:04:15 +00:00
parent 4a9c47e03d
commit fc5a66eed7
2 changed files with 96 additions and 1 deletions

View File

@ -6,14 +6,16 @@
#include "data.h"
#include "storage/bpt.hpp"
#include "storage/buffer_pool_manager.h"
#include "storage/config.h"
#include "storage/disk_manager.h"
#include "storage/driver.h"
#include "storage/single_value_storage.hpp"
#include "utils.h"
struct TransactionData {
char trainID[21];
char from_station_name[41];
char to_station_name[41];
uint8_t status;
uint8_t status; // 0: in queue, 1: success, 2: refunded
uint32_t leave_time_stamp;
uint32_t arrive_time_stamp;
uint32_t num;
@ -126,5 +128,98 @@ class TransactionManager : public DataDriverBase {
queue_indexer->Flush();
order_history_indexer->Flush();
}
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) {
TransactionData tmp;
strcpy(tmp.trainID, trainID.c_str());
strcpy(tmp.from_station_name, from_station_name.c_str());
strcpy(tmp.to_station_name, to_station_name.c_str());
tmp.status = status;
tmp.leave_time_stamp = leave_time_stamp;
tmp.arrive_time_stamp = arrive_time_stamp;
tmp.num = num;
tmp.total_price = total_price;
b_plus_tree_value_index_t data_id = data_storage->write(tmp);
hash_t train_ID_hash = SplitMix64Hash(trainID);
if (status == 0) {
queue_index_t queue_index_for_query;
queue_index_for_query.train_ID_hash = train_ID_hash;
queue_index_for_query.running_offset = running_date_offset;
queue_index_for_query.id = queue_index_specai_id;
int new_id;
{
auto it_queue_query = queue_indexer->lower_bound(queue_index_for_query);
new_id = it_queue_query.GetValue() + 1;
it_queue_query.SetValue(new_id);
} // to release the lock
queue_index_t queue_index;
queue_index.train_ID_hash = queue_index_for_query.train_ID_hash;
queue_index.running_offset = running_date_offset;
queue_index.id = new_id;
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.id = order_history_index_special_id;
int new_id;
{
auto it_order_history_query = order_history_indexer->lower_bound(order_history_index_for_query);
new_id = it_order_history_query.GetValue() + 1;
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.id = new_id;
order_history_indexer->Put(order_history_index, data_id);
}
inline void FetchQueue(hash_t train_ID_hash, uint8_t running_date_offset,
std::vector<b_plus_tree_value_index_t> &res) {
// warning: the validity of train_ID_hash is not checked
queue_index_t queue_index_for_query;
queue_index_for_query.train_ID_hash = train_ID_hash;
queue_index_for_query.running_offset = running_date_offset;
queue_index_for_query.id = queue_index_specai_id;
auto it = queue_indexer->lower_bound_const(queue_index_for_query);
int total_num = it.GetValue();
res.resize(total_num);
for (int i = 0; i < total_num; i++) {
++it;
res[i] = it.GetValue();
}
}
inline void FetchFullUserOrderHistory(hash_t user_ID_hash, std::vector<b_plus_tree_value_index_t> &res) {
// warning: the validity of user_ID_hash is not checked
order_history_index_t order_history_index_for_query;
order_history_index_for_query.user_ID_hash = user_ID_hash;
order_history_index_for_query.id = order_history_index_special_id;
auto it = order_history_indexer->lower_bound_const(order_history_index_for_query);
int total_num = it.GetValue();
res.resize(total_num);
for (int i = 0; i < total_num; i++) {
++it;
res[i] = it.GetValue();
}
}
inline b_plus_tree_value_index_t FetchSingleUserOrderHistory(hash_t user_ID_hash, int n, bool &success) {
// warning: the validity of user_ID_hash is not checked
order_history_index_t order_history_index_for_query;
order_history_index_for_query.user_ID_hash = user_ID_hash;
order_history_index_for_query.id = order_history_index_special_id;
auto it = order_history_indexer->lower_bound_const(order_history_index_for_query);
int total_num = it.GetValue();
if (n > total_num) {
success = false;
return 0;
}
order_history_index_for_query.id = total_num - n + 1;
auto dat_it = order_history_indexer->lower_bound_const(order_history_index_for_query);
success = true;
return dat_it.GetValue();
}
inline void FetchTransactionData(b_plus_tree_value_index_t idx, TransactionData &data) {
// warning: the validity of idx is not checked
data_storage->read(data, idx);
}
};
#endif