├── src
├── useless.md
├── logging
│ ├── useless.md
│ ├── singleton.h
│ ├── log_recorder.h
│ ├── blocking_queue.h
│ ├── logger.h
│ ├── logger.cpp
│ └── log_recorder.cpp
├── conn_data.h
├── test
│ ├── dotest.h
│ ├── test_config.h
│ ├── test.h
│ └── test_client.h
├── persistence.cpp
├── persistence.h
├── recovery.h
├── cmd_recoder.h
├── kv_database.h
├── main.cpp
├── utils.h
├── transaction.h
├── config.h
├── store_client.h
├── kvstore.pro
├── tcp_server.h
├── kv_database.cpp
├── tcp_connection.h
├── config.cpp.autosave
├── cmd_recoder.cpp
├── store_server.h
├── tcp_server.cpp
├── config.cpp
├── recovery.cpp
├── tcp_connection.cpp
├── store_client.cpp
├── cmd_protocol.h
├── store_server.cpp
└── kvstore.pro.user
└── README.md
/src/useless.md:
--------------------------------------------------------------------------------
1 | 1
2 |
--------------------------------------------------------------------------------
/src/logging/useless.md:
--------------------------------------------------------------------------------
1 | 1
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # [c++]使用boost.asio写的简单键值对缓存
2 | 支持put,del, get三个命令。
3 | 用use切换数据库。
4 |
5 | 支持密码。
6 |
7 | 支持持久化。
8 |
9 | 支持事务。
10 |
11 |
配置文件形式如下
12 | persistence:on
13 | password:"mima"
14 |
15 |
16 |
--------------------------------------------------------------------------------
/src/conn_data.h:
--------------------------------------------------------------------------------
1 | #ifndef CONN_DATA
2 | #define CONN_DATA
3 | #include
4 | #include "transaction.h"
5 |
6 | namespace kvstore {
7 |
8 | struct ConnData {
9 | ConnData() : db(0), buf(), transaction(), login(false) {}
10 | std::size_t db;
11 | std::string buf;
12 | Transaction transaction;
13 | bool login;
14 | };
15 | //buf高水位
16 | }
17 | #endif // CONN_DATA
18 |
19 |
--------------------------------------------------------------------------------
/src/test/dotest.h:
--------------------------------------------------------------------------------
1 | #include
2 | #include "test.h"
3 | #include "test/test_config.h"
4 | #include "../store_server.h"
5 | #include "../store_client.h"
6 | #include "test/test_client.h"
7 | using namespace kvstore;
8 | namespace test {
9 |
10 | static void do_test(boost::asio::io_service& ioservice) {
11 | test_config();
12 | test_client_transaction3();
13 | //test_client_transaction2();
14 | //test_client_transaction();
15 | }
16 |
17 | }//namespace
18 |
--------------------------------------------------------------------------------
/src/logging/singleton.h:
--------------------------------------------------------------------------------
1 | #ifndef SINGLETON_H
2 | #define SINGLETON_H
3 | namespace logging {
4 |
5 | template
6 | class Singleton {
7 | public:
8 | static T& get_instance() {
9 | static T instance_;
10 | return instance_;
11 | }
12 |
13 | ~Singleton() { }
14 |
15 | private:
16 | Singleton() {}
17 | Singleton(const Singleton &other);
18 | Singleton & operator=(const Singleton &other);
19 | };
20 | }
21 | #endif // SINGLETON_H
22 |
23 |
--------------------------------------------------------------------------------
/src/persistence.cpp:
--------------------------------------------------------------------------------
1 | #include "persistence.h"
2 |
3 | #include "cmd_protocol.h"
4 | namespace kvstore {
5 | Persistence::Persistence(StoreServer& server):
6 | server_(server),
7 | cmdRecoder_()
8 | {
9 |
10 | }
11 |
12 | int Persistence::record(const std::string &cmd, int databaseid) {
13 | return cmdRecoder_.record(cmd, databaseid);
14 | }
15 |
16 | int Persistence::recover() {
17 | Recovery recovery;
18 | return recovery.recover(server_);
19 | }
20 |
21 |
22 | } //namespace
23 |
24 |
--------------------------------------------------------------------------------
/src/persistence.h:
--------------------------------------------------------------------------------
1 | #ifndef PERSISTENCE_H
2 | #define PERSISTENCE_H
3 | //#include "store_server.h"
4 | #include "cmd_recoder.h"
5 | #include "recovery.h"
6 | namespace kvstore {
7 | class StoreServer;
8 |
9 | class Persistence
10 | {
11 | public:
12 | Persistence(StoreServer& server);
13 | int record(const std::string& cmd, int databaseid);
14 | int recover();
15 |
16 | private:
17 | StoreServer& server_;
18 | CmdRecoder cmdRecoder_;
19 | };
20 |
21 | } //namepsace
22 |
23 | #endif // PERSISTENCE_H
24 |
--------------------------------------------------------------------------------
/src/recovery.h:
--------------------------------------------------------------------------------
1 | #ifndef RECOVERY_H
2 | #define RECOVERY_H
3 | #include "kv_database.h"
4 | #include
5 | namespace kvstore {
6 | class StoreServer;
7 | class Recovery {
8 | public:
9 | Recovery();
10 | int recover(StoreServer &server);
11 |
12 | private:
13 | int execute_cmds(StoreServer &server, std::size_t db,const std::vector& cmds);
14 | int read_file(const std::string& filename, std::vector& cmds);
15 | private:
16 |
17 | };
18 |
19 | } //namespace
20 |
21 | #endif // RECOVERY_H
22 |
--------------------------------------------------------------------------------
/src/cmd_recoder.h:
--------------------------------------------------------------------------------
1 | #ifndef CMDRECODER_H
2 | #define CMDRECODER_H
3 | #include
4 | #include
5 | #include
6 | namespace kvstore {
7 |
8 | class CmdRecoder
9 | {
10 | public:
11 | CmdRecoder(std::size_t dbnum);
12 | CmdRecoder();
13 | int record(const std::string& cmd, int databaseid);
14 | private:
15 | int init();
16 | private:
17 | std::size_t dbnum_;
18 | std::vector dbfiles_;
19 | };
20 | static const char* CMD_SAVE_PATH_FOLDER = "cmd_save/";
21 |
22 | }
23 | #endif // CMDRECODER_H
24 |
--------------------------------------------------------------------------------
/src/logging/log_recorder.h:
--------------------------------------------------------------------------------
1 | #ifndef LOG_RECORDER_H
2 | #define LOG_RECORDER_H
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include "blocking_queue.h"
9 | #include "logger.h"
10 |
11 | namespace logging {
12 | class LogRecorder {
13 | public:
14 | LogRecorder();
15 | void submit(std::string msg);
16 | void stop();
17 | static Logger::OutputCallback callback();
18 |
19 | private:
20 | void create_dir_and_files();
21 | void customer_thread_function();
22 | void adjust_filename();
23 | private:
24 | BlockingQueue queue_;
25 | std::atomic_bool quit_;
26 | };
27 |
28 | } // namespace
29 | #endif // LOG_RECORDER_H
30 |
--------------------------------------------------------------------------------
/src/kv_database.h:
--------------------------------------------------------------------------------
1 | #ifndef KVDATABASE_H
2 | #define KVDATABASE_H
3 |
4 | #include
5 | //#include