write add_train
This commit is contained in:
@ -38,11 +38,15 @@ option(ENABLE_ADVANCED_FEATURE "Enable advanced features" OFF)
|
|||||||
option(OJ_TEST_BPT "Enable OJ test for B+ Tree" OFF)
|
option(OJ_TEST_BPT "Enable OJ test for B+ Tree" OFF)
|
||||||
option(OJ_TEST_BACKEND "Enable OJ test for backend" ON)
|
option(OJ_TEST_BACKEND "Enable OJ test for backend" ON)
|
||||||
option(ENABLE_TEST_POINTS "Enable test points" OFF)
|
option(ENABLE_TEST_POINTS "Enable test points" OFF)
|
||||||
|
option(DISABLE_COUT_CACHE "Disable the cache of std::cout" OFF)
|
||||||
|
|
||||||
# 如果 ENABLE_ADVANCED_FEATURE 选项为 ON,则定义 ENABLE_ADVANCED_FEATURE 宏
|
# 如果 ENABLE_ADVANCED_FEATURE 选项为 ON,则定义 ENABLE_ADVANCED_FEATURE 宏
|
||||||
if (ENABLE_ADVANCED_FEATURE)
|
if (ENABLE_ADVANCED_FEATURE)
|
||||||
add_definitions(-DENABLE_ADVANCED_FEATURE)
|
add_definitions(-DENABLE_ADVANCED_FEATURE)
|
||||||
endif()
|
endif()
|
||||||
|
if (DISABLE_COUT_CACHE)
|
||||||
|
add_definitions(-DDISABLE_COUT_CACHE)
|
||||||
|
endif()
|
||||||
|
|
||||||
include(FetchContent)
|
include(FetchContent)
|
||||||
|
|
||||||
|
@ -105,6 +105,7 @@ std::string TicketSystemEngine::Exit(const std::string &command) {
|
|||||||
LOG->debug("command id: {}", command_id);
|
LOG->debug("command id: {}", command_id);
|
||||||
std::stringstream response_stream;
|
std::stringstream response_stream;
|
||||||
response_stream << '[' << command_id << "] bye";
|
response_stream << '[' << command_id << "] bye";
|
||||||
|
its_time_to_exit = true;
|
||||||
return response_stream.str();
|
return response_stream.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,9 +15,11 @@ struct StationNameData {
|
|||||||
char name[100][40];
|
char name[100][40];
|
||||||
};
|
};
|
||||||
static_assert(sizeof(StationNameData) == 4000);
|
static_assert(sizeof(StationNameData) == 4000);
|
||||||
|
struct TicketPriceData {
|
||||||
|
uint32_t price[99];
|
||||||
|
};
|
||||||
|
|
||||||
// waring: this struct is extremely large, later DiskManager should be optimized to handle this
|
struct CoreTrainData {
|
||||||
struct FullTrainData {
|
|
||||||
char trainID[21];
|
char trainID[21];
|
||||||
uint8_t stationNum;
|
uint8_t stationNum;
|
||||||
hash_t stations_hash[100];
|
hash_t stations_hash[100];
|
||||||
@ -25,12 +27,8 @@ struct FullTrainData {
|
|||||||
uint16_t startTime : 12;
|
uint16_t startTime : 12;
|
||||||
uint16_t saleDate_beg : 10, saleDate_end : 10;
|
uint16_t saleDate_beg : 10, saleDate_end : 10;
|
||||||
uint8_t type : 6;
|
uint8_t type : 6;
|
||||||
|
uint8_t is_released : 1;
|
||||||
uint16_t travelTime[100];
|
uint16_t travelTime[100];
|
||||||
uint16_t stopoverTime[100];
|
uint16_t stopoverTime[100];
|
||||||
};
|
};
|
||||||
struct TicketPriceData {};
|
|
||||||
|
|
||||||
class TrainDataDrive : public DataDriverBase {};
|
|
||||||
|
|
||||||
class TransactionDataDrive : public DataDriverBase {};
|
|
||||||
#endif
|
#endif
|
@ -14,6 +14,7 @@ class TicketSystemEngine {
|
|||||||
#ifdef ENABLE_ADVANCED_FEATURE
|
#ifdef ENABLE_ADVANCED_FEATURE
|
||||||
SnapShotManager snapshot_manager;
|
SnapShotManager snapshot_manager;
|
||||||
#endif
|
#endif
|
||||||
|
bool its_time_to_exit = false;
|
||||||
std::string data_directory;
|
std::string data_directory;
|
||||||
std::map<hash_t, uint8_t> online_users;
|
std::map<hash_t, uint8_t> online_users;
|
||||||
|
|
||||||
@ -26,7 +27,9 @@ class TicketSystemEngine {
|
|||||||
/**
|
/**
|
||||||
* @brief train data system
|
* @brief train data system
|
||||||
*/
|
*/
|
||||||
DiskMap<hash_t, StationNameData> full_station_name_data;
|
DiskMap<hash_t, StationNameData> station_name_data_storage;
|
||||||
|
DiskMap<hash_t, TicketPriceData> ticket_price_data_storage;
|
||||||
|
DiskMap<hash_t, CoreTrainData> core_train_data_storage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief transaction data system
|
* @brief transaction data system
|
||||||
@ -46,12 +49,17 @@ class TicketSystemEngine {
|
|||||||
void PrepareExit();
|
void PrepareExit();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
const bool *its_time_to_exit_ptr = &its_time_to_exit;
|
||||||
inline TicketSystemEngine(std::string data_directory)
|
inline TicketSystemEngine(std::string data_directory)
|
||||||
: data_directory(data_directory),
|
: data_directory(data_directory),
|
||||||
user_data("user_data.idx", data_directory + "/user_data.idx", "user_data.val",
|
user_data("user_data.idx", data_directory + "/user_data.idx", "user_data.val",
|
||||||
data_directory + "/user_data.val"),
|
data_directory + "/user_data.val"),
|
||||||
full_station_name_data("station_name.idx", data_directory + "/station_name.idx", "station_name.val",
|
station_name_data_storage("station_name.idx", data_directory + "/station_name.idx", "station_name.val",
|
||||||
data_directory + "/station_name.val") {}
|
data_directory + "/station_name.val"),
|
||||||
|
ticket_price_data_storage("ticket_price.idx", data_directory + "/ticket_price.idx", "ticket_price.val",
|
||||||
|
data_directory + "/ticket_price.val"),
|
||||||
|
core_train_data_storage("core_train.idx", data_directory + "/core_train.idx", "core_train.val",
|
||||||
|
data_directory + "/core_train.val") {}
|
||||||
std::string Execute(const std::string &command);
|
std::string Execute(const std::string &command);
|
||||||
|
|
||||||
// User system
|
// User system
|
||||||
|
@ -112,6 +112,10 @@ int main(int argc, char *argv[]) {
|
|||||||
std::string cmd;
|
std::string cmd;
|
||||||
while (std::getline(std::cin, cmd)) {
|
while (std::getline(std::cin, cmd)) {
|
||||||
std::cout << engine.Execute(cmd) << '\n';
|
std::cout << engine.Execute(cmd) << '\n';
|
||||||
|
#ifdef DISABLE_COUT_CACHE
|
||||||
|
std::cout.flush();
|
||||||
|
#endif
|
||||||
|
if (*engine.its_time_to_exit_ptr) break;
|
||||||
}
|
}
|
||||||
#ifdef ENABLE_ADVANCED_FEATURE
|
#ifdef ENABLE_ADVANCED_FEATURE
|
||||||
}
|
}
|
||||||
|
@ -131,7 +131,39 @@ std::string TicketSystemEngine::AddTrain(const std::string &command) {
|
|||||||
RetrieveReadableDate(saleDate_begin).second, saleDate_end, RetrieveReadableDate(saleDate_end).first,
|
RetrieveReadableDate(saleDate_begin).second, saleDate_end, RetrieveReadableDate(saleDate_end).first,
|
||||||
RetrieveReadableDate(saleDate_end).second);
|
RetrieveReadableDate(saleDate_end).second);
|
||||||
LOG->debug("type: {}", type);
|
LOG->debug("type: {}", type);
|
||||||
response_stream << '[' << command_id << "] AddTrain";
|
hash_t train_id_hash = SplitMix64Hash(trainID);
|
||||||
|
if (ticket_price_data_storage.HasKey(train_id_hash)) {
|
||||||
|
response_stream << '[' << command_id << "] -1";
|
||||||
|
return response_stream.str();
|
||||||
|
}
|
||||||
|
TicketPriceData ticket_price_data;
|
||||||
|
for (int i = 0; i < stationNum - 1; i++) ticket_price_data.price[i] = prices[i];
|
||||||
|
ticket_price_data_storage.Put(train_id_hash, ticket_price_data);
|
||||||
|
CoreTrainData core_train_data;
|
||||||
|
core_train_data.is_released = 0;
|
||||||
|
strcpy(core_train_data.trainID, trainID.c_str());
|
||||||
|
core_train_data.stationNum = stationNum;
|
||||||
|
for (int i = 0; i < stationNum; i++) {
|
||||||
|
core_train_data.stations_hash[i] = SplitMix64Hash(stations[i]);
|
||||||
|
}
|
||||||
|
core_train_data.seatNum = seatNum;
|
||||||
|
core_train_data.startTime = startTime;
|
||||||
|
for (int i = 0; i < stationNum - 1; i++) {
|
||||||
|
core_train_data.travelTime[i] = travelTimes[i];
|
||||||
|
core_train_data.stopoverTime[i] = stopoverTimes[i];
|
||||||
|
}
|
||||||
|
core_train_data.saleDate_beg = saleDate_begin;
|
||||||
|
core_train_data.saleDate_end = saleDate_end;
|
||||||
|
core_train_data.type = type[0] - 'A';
|
||||||
|
core_train_data_storage.Put(train_id_hash, core_train_data);
|
||||||
|
StationNameData station_name_data;
|
||||||
|
for (int i = 0; i < stationNum; i++) {
|
||||||
|
size_t len = stations[i].length();
|
||||||
|
for (int j = 0; j < len; j++) station_name_data.name[i][j] = stations[i][j];
|
||||||
|
if (len < 40) station_name_data.name[i][len] = '\0';
|
||||||
|
}
|
||||||
|
station_name_data_storage.Put(train_id_hash, station_name_data);
|
||||||
|
response_stream << '[' << command_id << "] 0";
|
||||||
return response_stream.str();
|
return response_stream.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user