From d9d32c5e24735d6b8c9b22760aced0525685e4cc Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Tue, 12 Dec 2023 15:46:43 +0000 Subject: [PATCH] upd: ready to write the database and engine --- backend/include/bs-utility.h | 17 +++++++++--- backend/include/database.h | 28 +++++++++++++++++++ backend/include/database.hpp | 53 ------------------------------------ backend/include/engine.h | 8 ++++-- backend/src/bs-utility.cpp | 10 ++++--- backend/src/builtin-cli.cpp | 2 +- backend/src/database.cpp | 6 ++++ backend/src/engine.cpp | 5 ++++ 8 files changed, 65 insertions(+), 64 deletions(-) create mode 100644 backend/include/database.h delete mode 100644 backend/include/database.hpp create mode 100644 backend/src/database.cpp diff --git a/backend/include/bs-utility.h b/backend/include/bs-utility.h index c4bcca3..f42ef35 100644 --- a/backend/include/bs-utility.h +++ b/backend/include/bs-utility.h @@ -12,6 +12,12 @@ #include #include #include + +namespace BookStore_ZYM { +extern std::mutex debug_Print_Mutex; +extern bool shut_down; +} // namespace BookStore_ZYM + class FatalError : public std::exception { public: FatalError(const char *__message, int __code) @@ -94,10 +100,7 @@ class SessionClass { std::string SessionToken; std::string OuthorizationKey; }; -namespace BookStore_ZYM { -extern std::mutex debug_Print_Mutex; -extern bool shut_down; -} // namespace BookStore_ZYM + void debugPrint(); template void debugPrint(Args... args) { @@ -110,4 +113,10 @@ void debugPrint(Args... args) { void Respond(BlockingStringStream *output_ptr, std::string SessionToken, std::string OperationToken, std::string AuthenticationKey, const std::vector &ret); + +class UserItemClass { + public: + char user_id[31], password[31], user_name[31]; + unsigned char privilege; +}; #endif // PROTECTOR_UTILITY_H \ No newline at end of file diff --git a/backend/include/database.h b/backend/include/database.h new file mode 100644 index 0000000..3253c1d --- /dev/null +++ b/backend/include/database.h @@ -0,0 +1,28 @@ +#ifndef PROTECTOR_DATABASE_HPP +#define PROTECTOR_DATABASE_HPP +#include +#include + +#include "bs-utility.h" +#include "drivearray.hpp" +#include "key2index.hpp" + +class UserDataBase { + DriveArray full_user_data; + String2Index user_name2index; + + public: + UserDataBase() = default; + void Open(std::string file_name); + bool PAM(const std::string &user_id, const std::string &password); +}; +class BookDataBase { + ; +}; +class FinanceDataBase { + ; +}; +class OperationLogDataBase { + ; +}; +#endif // PROTECTOR_DATABASE_HPP \ No newline at end of file diff --git a/backend/include/database.hpp b/backend/include/database.hpp deleted file mode 100644 index 5e3ebbb..0000000 --- a/backend/include/database.hpp +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef BPT_DATABASE_HPP -#define BPT_DATABASE_HPP -#include -#include - -#include "drivearray.hpp" -#include "key2index.hpp" - -template -class DriveMultiMap_string { - private: - String2Index Indexer; - DriveArray Storage; - bool is_open = false; - - public: - DriveMultiMap_string() = default; - void OpenFile(const std::string __file_name) noexcept { - Indexer.OpenFile(__file_name + ".idx"); - Storage.OpenFile(__file_name + ".dat"); - is_open = true; - } - std::vector Find(const std::string &key) noexcept { - if (!is_open) return {}; - std::vector ret; - std::vector idxs = std::move(Indexer.Find(key)); - for (auto idx : idxs) { - StorageType tmp; - Storage.read(tmp, idx); - ret.push_back(tmp); - } - return std::move(ret); - } - void Delete(const std::string &key, const StorageType &value) noexcept { - if (!is_open) return; - std::vector idxs = std::move(Indexer.Find(key)); - for (auto idx : idxs) { - StorageType tmp; - Storage.read(tmp, idx); - if (tmp == value) { - Storage.Delete(idx); - Indexer.Delete(key, idx); - return; - } - } - } - void Insert(const std::string &key, StorageType &value) noexcept { - if (!is_open) return; - int idx = Storage.write(value); - Indexer.Insert(key, idx); - } -}; -#endif // BPT_DATABASE_HPP \ No newline at end of file diff --git a/backend/include/engine.h b/backend/include/engine.h index 20d0072..3790c92 100644 --- a/backend/include/engine.h +++ b/backend/include/engine.h @@ -3,12 +3,16 @@ #include #include #include + +#include "database.h" class BookStoreEngineClass { std::string config_dir; + UserDataBase user_data_base; public: BookStoreEngineClass() = delete; - BookStoreEngineClass(std::string config_dir) : config_dir(config_dir) {} - std::vector Execute(const std::string &cmd, std::stack &login_stack); + BookStoreEngineClass(std::string __config_dir); + std::vector Execute(const std::string &cmd, + std::stack &login_stack); }; #endif // PROTECTOR_ENGINE_H \ No newline at end of file diff --git a/backend/src/bs-utility.cpp b/backend/src/bs-utility.cpp index 14d8d95..7f03bc4 100644 --- a/backend/src/bs-utility.cpp +++ b/backend/src/bs-utility.cpp @@ -1,5 +1,10 @@ #include "bs-utility.h" +namespace BookStore_ZYM { +std::mutex debug_Print_Mutex; +bool shut_down = false; +} // namespace BookStore_ZYM + BlockingStringStream &BlockingStringStream::getline(std::string &str, char delim) { std::unique_lock lock(mutex); @@ -43,10 +48,7 @@ void ReadWriteLock::endWrite() { is_writing = false; cv.notify_all(); // 唤醒所有等待的读操作和写操作 } -namespace BookStore_ZYM { -std::mutex debug_Print_Mutex; -bool shut_down = false; -} // namespace BookStore_ZYM + void debugPrint() { BookStore_ZYM::debug_Print_Mutex.lock(); std::cerr << std::endl; diff --git a/backend/src/builtin-cli.cpp b/backend/src/builtin-cli.cpp index 43f5baa..5ed1d3b 100644 --- a/backend/src/builtin-cli.cpp +++ b/backend/src/builtin-cli.cpp @@ -22,7 +22,7 @@ void BookStoreMain(bool is_server, std::string config_dir) { if (BookStore_ZYM::shut_down) return; } } else { - throw FatalError("Not implemented yet", 1); + throw FatalError("Server mode has not been implemented yet", 1); std::unordered_map session_map; } } \ No newline at end of file diff --git a/backend/src/database.cpp b/backend/src/database.cpp new file mode 100644 index 0000000..1a8d0d8 --- /dev/null +++ b/backend/src/database.cpp @@ -0,0 +1,6 @@ +#include "database.h" + +void UserDataBase::Open(std::string file_name) { + full_user_data.OpenFile(file_name + ".full"); + user_name2index.OpenFile(file_name + ".n2i"); +} \ No newline at end of file diff --git a/backend/src/engine.cpp b/backend/src/engine.cpp index e61b48b..849db28 100644 --- a/backend/src/engine.cpp +++ b/backend/src/engine.cpp @@ -4,6 +4,11 @@ #include #include "bs-utility.h" + +BookStoreEngineClass::BookStoreEngineClass(std::string __config_dir) { + config_dir = __config_dir; + user_data_base.Open(config_dir + "user"); +} std::vector BookStoreEngineClass::Execute( const std::string &cmd, std::stack &login_stack) { if (cmd == "quit" || cmd == "exit") {