setting up structure

This commit is contained in:
2024-05-21 06:35:55 +00:00
parent 735b0cb966
commit 5ae88e3312
10 changed files with 274 additions and 9 deletions

View File

@ -10,7 +10,11 @@
extern const std::string main_version;
extern const std::string build_version;
extern std::shared_ptr<spdlog::logger> logger_ptr;
extern const bool global_log_enabled;
#ifndef ENABLE_ADVANCED_FEATURE
constexpr bool global_log_enabled = false;
#else
constexpr bool global_log_enabled = true;
#endif
extern const bool optimize_enabled;
#define LOG if constexpr (global_log_enabled) if (logger_ptr) logger_ptr
#endif

4
src/include/data.h Normal file
View File

@ -0,0 +1,4 @@
#ifndef DATA_H
#define DATA_H
#endif

47
src/include/engine.h Normal file
View File

@ -0,0 +1,47 @@
#ifndef ENGINE_H
#define ENGINE_H
#include <map>
#include <string>
#ifdef ENABLE_ADVANCED_FEATURE
#include "dataguard/dataguard.h"
#include "dataguard/snapshot.h"
#endif
#include <vector>
#include "utils.h"
class TicketSystemEngine {
#ifdef ENABLE_ADVANCED_FEATURE
SnapShotManager snapshot_manager;
#endif
std::string data_directory;
std::map<hash_t, bool> online_users;
void PrepareExit();
public:
inline TicketSystemEngine(std::string data_directory) : data_directory(data_directory) {}
std::string Execute(const std::string &command);
// 用户相关函数
std::string AddUser(const std::string &command);
std::string LoginUser(const std::string &command);
std::string LogoutUser(const std::string &command);
std::string QueryProfile(const std::string &command);
std::string ModifyProfile(const std::string &command);
// 车次相关函数
std::string AddTrain(const std::string &command);
std::string DeleteTrain(const std::string &command);
std::string ReleaseTrain(const std::string &command);
std::string QueryTrain(const std::string &command);
// 订单相关函数
std::string BuyTicket(const std::string &command);
std::string QueryOrder(const std::string &command);
std::string RefundTicket(const std::string &command);
// 其他函数
std::string QueryTransfer(const std::string &command);
std::string QueryTicket(const std::string &command);
std::string Clean();
std::string Exit();
};
#endif

59
src/include/utils.h Normal file
View File

@ -0,0 +1,59 @@
#ifndef UTILS_H
#define UTILS_H
#include <cstdint>
#include <string>
#include <string_view>
typedef uint64_t hash_t;
inline hash_t SplitMix64Hash(const std::string &str) noexcept {
// constexpr static char salt1[10] = "mL;]-=eT";
// constexpr static char salt2[10] = "9B<mF_me";
constexpr static char inner_salt[17] = "si9aW@zl#2$3%4^!";
/* Reference: http://xorshift.di.unimi.it/splitmix64.c */
// str = salt1 + str + salt2;
hash_t ret = 0;
int i = 0;
for (; i + 8 <= str.length(); i += 8) {
ret ^= *reinterpret_cast<const hash_t *>(str.c_str() + i);
ret ^= *reinterpret_cast<const hash_t *>(inner_salt + (i & 15));
ret += 0x9e3779b97f4a7c15;
ret = (ret ^ (ret >> 30)) * 0xbf58476d1ce4e5b9;
ret = (ret ^ (ret >> 27)) * 0x94d049bb133111eb;
ret ^= ret >> 31;
}
for (; i < str.length(); ++i) {
ret ^= str[i];
ret ^= inner_salt[i & 15];
ret += 0x9e3779b97f4a7c15;
ret = (ret ^ (ret >> 30)) * 0xbf58476d1ce4e5b9;
ret = (ret ^ (ret >> 27)) * 0x94d049bb133111eb;
ret ^= ret >> 31;
}
return ret;
}
inline hash_t SplitMix64Hash(const std::string_view &str) noexcept {
// constexpr static char salt1[10] = "mL;]-=eT";
// constexpr static char salt2[10] = "9B<mF_me";
constexpr static char inner_salt[17] = "si9aW@zl#2$3%4^!";
/* Reference: http://xorshift.di.unimi.it/splitmix64.c */
// str = salt1 + str + salt2;
hash_t ret = 0;
int i = 0;
for (; i + 8 <= str.length(); i += 8) {
ret ^= *reinterpret_cast<const hash_t *>(str.data() + i);
ret ^= *reinterpret_cast<const hash_t *>(inner_salt + (i & 15));
ret += 0x9e3779b97f4a7c15;
ret = (ret ^ (ret >> 30)) * 0xbf58476d1ce4e5b9;
ret = (ret ^ (ret >> 27)) * 0x94d049bb133111eb;
ret ^= ret >> 31;
}
for (; i < str.length(); ++i) {
ret ^= str[i];
ret ^= inner_salt[i & 15];
ret += 0x9e3779b97f4a7c15;
ret = (ret ^ (ret >> 30)) * 0xbf58476d1ce4e5b9;
ret = (ret ^ (ret >> 27)) * 0x94d049bb133111eb;
ret ^= ret >> 31;
}
return ret;
}
#endif