upd: ready to write the database and engine

This commit is contained in:
2023-12-12 15:46:43 +00:00
parent 824145ec39
commit d9d32c5e24
8 changed files with 65 additions and 64 deletions

View File

@ -12,6 +12,12 @@
#include <string>
#include <thread>
#include <vector>
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 <typename... Args>
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<std::string> &ret);
class UserItemClass {
public:
char user_id[31], password[31], user_name[31];
unsigned char privilege;
};
#endif // PROTECTOR_UTILITY_H

View File

@ -0,0 +1,28 @@
#ifndef PROTECTOR_DATABASE_HPP
#define PROTECTOR_DATABASE_HPP
#include <string>
#include <vector>
#include "bs-utility.h"
#include "drivearray.hpp"
#include "key2index.hpp"
class UserDataBase {
DriveArray<UserItemClass> 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

View File

@ -1,53 +0,0 @@
#ifndef BPT_DATABASE_HPP
#define BPT_DATABASE_HPP
#include <string>
#include <vector>
#include "drivearray.hpp"
#include "key2index.hpp"
template <class StorageType>
class DriveMultiMap_string {
private:
String2Index Indexer;
DriveArray<StorageType, 2, 100> 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<StorageType> Find(const std::string &key) noexcept {
if (!is_open) return {};
std::vector<StorageType> ret;
std::vector<int> 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<int> 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

View File

@ -3,12 +3,16 @@
#include <stack>
#include <string>
#include <vector>
#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<std::string> Execute(const std::string &cmd, std::stack<std::string> &login_stack);
BookStoreEngineClass(std::string __config_dir);
std::vector<std::string> Execute(const std::string &cmd,
std::stack<std::string> &login_stack);
};
#endif // PROTECTOR_ENGINE_H

View File

@ -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<std::mutex> 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;

View File

@ -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<std::string, SessionClass> session_map;
}
}

6
backend/src/database.cpp Normal file
View File

@ -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");
}

View File

@ -4,6 +4,11 @@
#include <string>
#include "bs-utility.h"
BookStoreEngineClass::BookStoreEngineClass(std::string __config_dir) {
config_dir = __config_dir;
user_data_base.Open(config_dir + "user");
}
std::vector<std::string> BookStoreEngineClass::Execute(
const std::string &cmd, std::stack<std::string> &login_stack) {
if (cmd == "quit" || cmd == "exit") {