5#include <unordered_map>
8#if __cplusplus < 202002L
27 auto conn = std::make_shared<Connection>();
28 conn->connect(config);
34# if __cplusplus >= 201703L
44 if (!
m_config)
throw std::logic_error(
"No configuration provided.");
51# if __cplusplus >= 201703L
66 return m_env !=
nullptr;
75 auto tid = std::this_thread::get_id();
78 throw std::logic_error(
"Transaction already started for this thread.");
86 auto tid = std::this_thread::get_id();
89 throw std::logic_error(
"No transaction for this thread.");
97 auto tid = std::this_thread::get_id();
100 throw std::logic_error(
"No transaction for this thread.");
102 it->second->rollback();
121 if (
m_env && mdbx_env_close(
m_env) == MDBX_SUCCESS) {
130 int rc = mdbx_env_close(
m_env);
131 if (rc != MDBX_SUCCESS && use_throw) {
132 check_mdbx(rc,
"Failed to close environment");
139#if __cplusplus >= 201703L
140 namespace fs = std::filesystem;
145 mdbx_env_create(&
m_env),
146 "Failed to create environment"
150 mdbx_env_set_geometry(
159 "Failed to set environment geometry"
164 "Failed to set max databases"
167 int readers =
m_config->max_readers > 0
168 ?
static_cast<int>(
m_config->max_readers)
169 :
static_cast<int>(std::thread::hardware_concurrency()) * 2;
171 mdbx_env_set_maxreaders(
m_env, readers),
172 "Failed to set max readers"
175 MDBX_env_flags_t env_flags = MDBX_ACCEDE;
176 if (
m_config->no_subdir) env_flags |= MDBX_NOSUBDIR;
177 if (
m_config->sync_durable) env_flags |= MDBX_SYNC_DURABLE;
178 if (
m_config->read_only) env_flags |= MDBX_RDONLY;
179 if (!
m_config->readahead) env_flags |= MDBX_NORDAHEAD;
180 if (
m_config->writemap_mode) env_flags |= MDBX_WRITEMAP;
182 std::string pathname =
m_config->pathname;
184#if __cplusplus >= 201703L
185 pathname = (fs::u8path(
get_exec_dir()) / fs::u8path(pathname)).u8string();
196# if __cplusplus >= 201703L
197# if __cplusplus >= 202002L
198 fs::path file_path = fs::u8path(pathname);
200 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
201 std::wstring wide_path = converter.from_bytes(pathname);
202 fs::path file_path = fs::path(wide_path);
205 mdbx_env_openW(
m_env, file_path.c_str(), env_flags, 0664),
206 "Failed to open environment"
209 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
210 std::wstring wide_path = converter.from_bytes(pathname);
212 mdbx_env_openW(
m_env, wide_path.c_str(), env_flags, 0664),
213 "Failed to open environment"
219 mdbx_env_open(
m_env, pathname.c_str(), env_flags, 0664),
220 "Failed to open environment"
Parameters used by Connection to create the MDBX environment.
std::shared_ptr< Transaction > current_txn() const
Returns the transaction associated with the current thread.
bool is_connected() const
Checks whether the environment is currently open.
MDBX_env * m_env
Pointer to the MDBX environment handle.
Transaction transaction(TransactionMode mode=TransactionMode::WRITABLE)
Creates a RAII transaction object.
MDBX_env * env_handle() noexcept
Returns the environment handle.
void begin(TransactionMode mode=TransactionMode::WRITABLE)
Begins a manual transaction (must be committed or rolled back later).
void disconnect()
Disconnects from the MDBX environment and releases resources.
void db_init()
Initializes MDBX environment and opens a read-only transaction.
void rollback()
Rolls back the current manual transaction.
config_t m_config
Database configuration object.
Connection()=default
Default constructor.
void cleanup(bool use_throw=true)
Safely closes the environment and aborts transaction if needed.
void connect()
Connects to the database using the current configuration.
void configure(const Config &config)
Sets the MDBX configuration (must be called before connect()).
~Connection()
Destructor. Closes the MDBX environment and aborts any open transactions.
std::mutex m_mdbx_mutex
Mutex for thread-safe access.
void commit()
Commits the current manual transaction.
static std::shared_ptr< Connection > create(const Config &config)
Creates and connects a new shared Connection instance.
void initialize()
Initializes the environment and sets up read-only transaction.
std::unordered_map< std::thread::id, std::shared_ptr< Transaction > > m_transactions
Associates MDBX transactions with threads.
void create_directories(const std::string &path)
Creates directories recursively for the given path.
std::string get_exec_dir()
Retrieves the directory of the executable file.
bool is_absolute_path(const std::string &path)
Checks whether the given path is absolute (cross-platform).
void check_mdbx(int rc, const std::string &context)
Throws an MdbxException if MDBX return code indicates an error.
TransactionMode
Specifies the access mode of a transaction.