ready to write query

This commit is contained in:
2024-05-23 14:11:53 +00:00
parent 050eb43c38
commit e6ef7f08c5
3 changed files with 105 additions and 5 deletions

View File

@ -8,6 +8,7 @@
#endif #endif
#include <vector> #include <vector>
#include "data.h" #include "data.h"
#include "stop_register.hpp"
#include "storage/disk_map.hpp" #include "storage/disk_map.hpp"
#include "utils.h" #include "utils.h"
class TicketSystemEngine { class TicketSystemEngine {
@ -30,11 +31,12 @@ class TicketSystemEngine {
DiskMap<hash_t, StationNameData> station_name_data_storage; DiskMap<hash_t, StationNameData> station_name_data_storage;
DiskMap<hash_t, TicketPriceData> ticket_price_data_storage; DiskMap<hash_t, TicketPriceData> ticket_price_data_storage;
DiskMap<hash_t, CoreTrainData> core_train_data_storage; DiskMap<hash_t, CoreTrainData> core_train_data_storage;
typedef std::pair<hash_t, uint8_t> seats_index_t;
DiskMap<seats_index_t, SeatsData> seats_data_storage;
/** /**
* @brief transaction data system * @brief transaction data system
* @details This part is responsible for storing: * @details This part is responsible for storing:
* - Remaining seat numbers: using HashedTrainID + train number as the index, with a B+ tree point to an array
* - Stop information: using the station + HashedTrainID as the index, with a B+ tree point to the train's sales start * - Stop information: using the station + HashedTrainID as the index, with a B+ tree point to the train's sales start
* and end dates, minutes required from the starting station to the current station, and the current station's stop * and end dates, minutes required from the starting station to the current station, and the current station's stop
* time * time
@ -44,9 +46,7 @@ class TicketSystemEngine {
* - User purchase history: using HashedUserID as the index, with a LinkedStack attached, with a simple skip list-like * - User purchase history: using HashedUserID as the index, with a LinkedStack attached, with a simple skip list-like
* optimization, storing IDs pointing to order information * optimization, storing IDs pointing to order information
*/ */
// TODO StopRegister stop_register;
typedef std::pair<hash_t, uint8_t> seats_index_t;
DiskMap<seats_index_t, SeatsData> seats_data_storage;
void PrepareExit(); void PrepareExit();
@ -62,7 +62,8 @@ class TicketSystemEngine {
data_directory + "/ticket_price.val"), data_directory + "/ticket_price.val"),
core_train_data_storage("core_train.idx", data_directory + "/core_train.idx", "core_train.val", core_train_data_storage("core_train.idx", data_directory + "/core_train.idx", "core_train.val",
data_directory + "/core_train.val"), data_directory + "/core_train.val"),
seats_data_storage("seats.idx", data_directory + "/seats.idx", "seats.val", data_directory + "/seats.val") {} seats_data_storage("seats.idx", data_directory + "/seats.idx", "seats.val", data_directory + "/seats.val"),
stop_register("stop_register.idx", data_directory + "/stop_register.idx") {}
std::string Execute(const std::string &command); std::string Execute(const std::string &command);
// User system // User system

View File

@ -0,0 +1,84 @@
#ifndef STOP_REGISTER_HPP
#define STOP_REGISTER_HPP
#include <vector>
#include "basic_defs.h"
#include "data.h"
#include "storage/bpt.hpp"
#include "storage/buffer_pool_manager.h"
#include "storage/driver.h"
typedef std::pair<hash_t, hash_t> stop_register_t; // The first is station hash, the second is train hash
struct MinimalTrainRecord {
uint16_t saleDate_beg : 7, saleDate_end : 7, vis_time_offset : 13, type : 1;
};
static_assert(sizeof(MinimalTrainRecord) == sizeof(default_numeric_index_t));
class StopRegister : public DataDriverBase {
std::string bpt_file_identifier;
std::string bpt_file_path;
DiskManager *bpt_disk_manager;
BufferPoolManager *bpt_bpm;
BPlusTreeIndexer<stop_register_t, std::less<stop_register_t>> *bpt_indexer;
public:
// for satety, all the copy/move operations are deleted, please manage it using pointer
StopRegister &operator=(const StopRegister &) = delete;
StopRegister(const StopRegister &) = delete;
StopRegister &operator=(StopRegister &&) = delete;
StopRegister(StopRegister &&) = delete;
inline StopRegister(std::string bpt_file_identifier_, std::string bpt_file_path_)
: bpt_file_identifier(std::move(bpt_file_identifier_)), bpt_file_path(std::move(bpt_file_path_)) {
bpt_disk_manager = new DiskManager(bpt_file_path);
bpt_bpm = new BufferPoolManager(100, 5, bpt_disk_manager);
bpt_indexer = new BPlusTreeIndexer<stop_register_t, std::less<stop_register_t>>(bpt_bpm);
}
inline ~StopRegister() {
delete bpt_indexer;
delete bpt_bpm;
delete bpt_disk_manager;
}
inline virtual sjtu::vector<FileEntry> ListFiles() override {
sjtu::vector<FileEntry> res;
res.push_back({bpt_file_identifier, bpt_file_path, bpt_disk_manager});
return res;
}
inline virtual void LockDownForCheckOut() override {
delete bpt_indexer;
delete bpt_bpm;
delete bpt_disk_manager;
bpt_indexer = nullptr;
bpt_bpm = nullptr;
bpt_disk_manager = nullptr;
}
inline virtual void Flush() override {
if (bpt_indexer == nullptr) return;
bpt_indexer->Flush();
}
inline void AddStopInfo(hash_t station_hash, hash_t train_hash, uint16_t true_saleDate_beg,
uint16_t true_saleDate_end, uint16_t arrive_time_offset, uint16_t leave_time_offset) {
MinimalTrainRecord record_arrive, record_leave;
const static int June_1st_2024 = 152;
record_arrive.saleDate_beg = true_saleDate_beg - June_1st_2024;
record_arrive.saleDate_end = true_saleDate_end - June_1st_2024;
record_arrive.vis_time_offset = arrive_time_offset;
record_arrive.type = 0;
record_leave.saleDate_beg = true_saleDate_beg - June_1st_2024;
record_leave.saleDate_end = true_saleDate_end - June_1st_2024;
record_leave.vis_time_offset = leave_time_offset;
record_leave.type = 1;
if (arrive_time_offset != uint16_t(-1))
bpt_indexer->Put({station_hash, train_hash}, *reinterpret_cast<default_numeric_index_t *>(&record_arrive));
if (leave_time_offset != uint16_t(-1))
bpt_indexer->Put({station_hash, train_hash}, *reinterpret_cast<default_numeric_index_t *>(&record_leave));
}
inline void GetShareItems(std::vector<hash_t> &A, std::vector<hash_t> &B, std::vector<hash_t> &res) {
// TODO
}
inline void QueryDirectTrains(uint32_t date, hash_t from_station_ID, hash_t to_station_ID, std::vector<hash_t> &res) {
std::vector<hash_t> valid_trains_pass_from, valid_trains_pass_to;
res.clear();
// TODO
GetShareItems(valid_trains_pass_from, valid_trains_pass_to, res);
}
};
#endif

View File

@ -247,6 +247,21 @@ std::string TicketSystemEngine::ReleaseTrain(const std::string &command) {
core_train_data.is_released = 1; core_train_data.is_released = 1;
core_train_data_storage.Put(train_id_hash, core_train_data); core_train_data_storage.Put(train_id_hash, core_train_data);
// TODO: update data to transaction system // TODO: update data to transaction system
int vis_time_offset = 0;
for (int i = 0; i < core_train_data.stationNum; i++) {
uint16_t arrive_time_offset = -1;
uint16_t leave_time_offset = -1;
if (i != 0) {
vis_time_offset += core_train_data.travelTime[i - 1];
arrive_time_offset = vis_time_offset;
}
if (i != core_train_data.stationNum - 1) {
if (i != 0) vis_time_offset += core_train_data.stopoverTime[i];
leave_time_offset = vis_time_offset;
}
stop_register.AddStopInfo(core_train_data.stations_hash[i], train_id_hash, core_train_data.saleDate_beg,
core_train_data.saleDate_end, arrive_time_offset, leave_time_offset);
}
response_stream << '[' << command_id << "] 0"; response_stream << '[' << command_id << "] 0";
return response_stream.str(); return response_stream.str();
} }