OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
log_store.hpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef OPENCBDC_TX_SRC_RAFT_LOG_STORE_H_
7#define OPENCBDC_TX_SRC_RAFT_LOG_STORE_H_
8
10
11#include <leveldb/db.h>
12#include <libnuraft/log_store.hxx>
13#include <mutex>
14
15namespace cbdc::raft {
17 class log_store : public nuraft::log_store {
18 public:
19 log_store() = default;
20 ~log_store() override = default;
21
22 log_store(const log_store& other) = delete;
23 auto operator=(const log_store& other) -> log_store& = delete;
24
25 log_store(log_store&& other) noexcept;
26 auto operator=(log_store&& other) noexcept -> log_store&;
27
31 [[nodiscard]] auto load(const std::string& db_dir) -> bool;
32
35 [[nodiscard]] auto next_slot() const -> uint64_t override;
36
39 [[nodiscard]] auto start_index() const -> uint64_t override;
40
44 [[nodiscard]] auto last_entry() const
45 -> nuraft::ptr<nuraft::log_entry> override;
46
50 auto append(nuraft::ptr<nuraft::log_entry>& entry)
51 -> uint64_t override;
52
56 void write_at(uint64_t index,
57 nuraft::ptr<nuraft::log_entry>& entry) override;
58
61 = nuraft::ptr<std::vector<nuraft::ptr<nuraft::log_entry>>>;
62
67 [[nodiscard]] auto log_entries(uint64_t start, uint64_t end)
68 -> log_entries_t override;
69
74 [[nodiscard]] auto entry_at(uint64_t index)
75 -> nuraft::ptr<nuraft::log_entry> override;
76
81 [[nodiscard]] auto term_at(uint64_t index) -> uint64_t override;
82
87 [[nodiscard]] auto pack(uint64_t index, int32_t cnt)
88 -> nuraft::ptr<nuraft::buffer> override;
89
94 void apply_pack(uint64_t index, nuraft::buffer& pack) override;
95
100 auto compact(uint64_t last_log_index) -> bool override;
101
104 auto flush() -> bool override;
105
106 private:
107 std::unique_ptr<leveldb::DB> m_db{};
108 mutable std::mutex m_db_mut{};
109 uint64_t m_next_idx{};
110 uint64_t m_start_idx{};
111
112 leveldb::ReadOptions m_read_opt;
113 leveldb::WriteOptions m_write_opt;
114
115 index_comparator m_cmp;
116 };
117}
118
119#endif // OPENCBDC_TX_SRC_RAFT_LOG_STORE_H_
Buffer to store and retrieve byte data.
Definition buffer.hpp:15
NuRaft log_store implementation using LevelDB.
Definition log_store.hpp:17
log_store(const log_store &other)=delete
auto next_slot() const -> uint64_t override
Return the log index of the next empty log entry.
Definition log_store.cpp:66
auto term_at(uint64_t index) -> uint64_t override
Return the log term associated with the log entry at the given index.
auto load(const std::string &db_dir) -> bool
Load the log store from the given LevelDB database directory.
Definition log_store.cpp:40
auto pack(uint64_t index, int32_t cnt) -> nuraft::ptr< nuraft::buffer > override
Serialize the given number of log entries from the given index.
~log_store() override=default
log_store(log_store &&other) noexcept
auto flush() -> bool override
Flush any buffered writes to disk.
auto start_index() const -> uint64_t override
Return the first log index stored by the log store.
Definition log_store.cpp:71
void apply_pack(uint64_t index, nuraft::buffer &pack) override
Deserialize the given log entries and write them starting at the given log index.
void write_at(uint64_t index, nuraft::ptr< nuraft::log_entry > &entry) override
Write a log entry at the given index.
auto compact(uint64_t last_log_index) -> bool override
Delete log entries from the start of the log up to the given log index.
auto operator=(const log_store &other) -> log_store &=delete
auto last_entry() const -> nuraft::ptr< nuraft::log_entry > override
Return the last log entry in the log store.
Definition log_store.cpp:85
auto append(nuraft::ptr< nuraft::log_entry > &entry) -> uint64_t override
Append the given log entry to the end of the log.
nuraft::ptr< std::vector< nuraft::ptr< nuraft::log_entry > > > log_entries_t
List of log entries.
Definition log_store.hpp:60
auto entry_at(uint64_t index) -> nuraft::ptr< nuraft::log_entry > override
Return the log entry at the given index.
auto log_entries(uint64_t start, uint64_t end) -> log_entries_t override
Return the log entries in the given range of indices.
auto operator=(log_store &&other) noexcept -> log_store &