From 49c6e14b3060281cabe2c28cab525f2f1fa94fef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D0=BE=D0=BD=D0=B8=D0=B4=20=D0=AE=D1=80=D1=8C?= =?UTF-8?q?=D0=B5=D0=B2=20=28Leonid=20Yuriev=29?= Date: Sun, 30 Jun 2024 14:35:42 +0300 Subject: [PATCH] =?UTF-8?q?mdbx++:=20=D1=80=D0=B0=D1=81=D1=88=D0=B8=D1=80?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20API=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4?= =?UTF-8?q?=D0=B0=D0=BC=D0=B8=20=D0=BF=D1=80=D0=B8=D0=BD=D0=B8=D0=BC=D0=B0?= =?UTF-8?q?=D1=8E=D1=89=D0=B8=D0=BC=D0=B8=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=20subDb=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20`mdbx::slice`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mdbx.h++ | 166 +++++++++++++++++++++++++-------------------------- src/mdbx.c++ | 33 +++------- 2 files changed, 90 insertions(+), 109 deletions(-) diff --git a/mdbx.h++ b/mdbx.h++ index 2d982041..9f52793d 100644 --- a/mdbx.h++ +++ b/mdbx.h++ @@ -4384,11 +4384,18 @@ public: const ::std::string &name, const ::mdbx::key_mode key_mode = ::mdbx::key_mode::usual, const ::mdbx::value_mode value_mode = ::mdbx::value_mode::single) const; + /// \brief Open existing key-value map. + inline map_handle open_map( + const ::mdbx::slice &name, + const ::mdbx::key_mode key_mode = ::mdbx::key_mode::usual, + const ::mdbx::value_mode value_mode = ::mdbx::value_mode::single) const; /// \brief Open existing key-value map. inline map_handle open_map_accede(const char *name) const; /// \brief Open existing key-value map. inline map_handle open_map_accede(const ::std::string &name) const; + /// \brief Open existing key-value map. + inline map_handle open_map_accede(const ::mdbx::slice &name) const; /// \brief Create new or open existing key-value map. inline map_handle @@ -4400,6 +4407,11 @@ public: create_map(const ::std::string &name, const ::mdbx::key_mode key_mode = ::mdbx::key_mode::usual, const ::mdbx::value_mode value_mode = ::mdbx::value_mode::single); + /// \brief Create new or open existing key-value map. + inline map_handle + create_map(const ::mdbx::slice &name, + const ::mdbx::key_mode key_mode = ::mdbx::key_mode::usual, + const ::mdbx::value_mode value_mode = ::mdbx::value_mode::single); /// \brief Drops key-value map using handle. inline void drop_map(map_handle map); @@ -4411,6 +4423,10 @@ public: /// \return `True` if the key-value map existed and was deleted, either /// `false` if the key-value map did not exist and there is nothing to delete. inline bool drop_map(const ::std::string &name, bool throw_if_absent = false); + /// \brief Drop key-value map. + /// \return `True` if the key-value map existed and was deleted, either + /// `false` if the key-value map did not exist and there is nothing to delete. + bool drop_map(const ::mdbx::slice &name, bool throw_if_absent = false); /// \brief Clear key-value map. inline void clear_map(map_handle map); @@ -4421,12 +4437,17 @@ public: /// `false` if the key-value map did not exist and there is nothing to clear. inline bool clear_map(const ::std::string &name, bool throw_if_absent = false); + /// \return `True` if the key-value map existed and was cleared, either + /// `false` if the key-value map did not exist and there is nothing to clear. + bool clear_map(const ::mdbx::slice &name, bool throw_if_absent = false); /// \brief Переименовывает таблицу ключ-значение. inline void rename_map(map_handle map, const char *new_name); /// \brief Переименовывает таблицу ключ-значение. inline void rename_map(map_handle map, const ::std::string &new_name); /// \brief Переименовывает таблицу ключ-значение. + inline void rename_map(map_handle map, const ::mdbx::slice &new_name); + /// \brief Переименовывает таблицу ключ-значение. /// \return `True` если таблица существует и была переименована, либо /// `false` в случае отсутствия исходной таблицы. bool rename_map(const char *old_name, const char *new_name, @@ -4436,6 +4457,11 @@ public: /// `false` в случае отсутствия исходной таблицы. bool rename_map(const ::std::string &old_name, const ::std::string &new_name, bool throw_if_absent = false); + /// \brief Переименовывает таблицу ключ-значение. + /// \return `True` если таблица существует и была переименована, либо + /// `false` в случае отсутствия исходной таблицы. + bool rename_map(const ::mdbx::slice &old_name, const ::mdbx::slice &new_name, + bool throw_if_absent = false); #if defined(DOXYGEN) || \ (defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L) @@ -4444,21 +4470,29 @@ public: inline map_handle open_map( const ::std::string_view &name, const ::mdbx::key_mode key_mode = ::mdbx::key_mode::usual, - const ::mdbx::value_mode value_mode = ::mdbx::value_mode::single) const; + const ::mdbx::value_mode value_mode = ::mdbx::value_mode::single) const { + return open_map(::mdbx::slice(name), key_mode, value_mode); + } /// \brief Open existing key-value map. inline map_handle open_map_accede(const ::std::string_view &name) const; /// \brief Create new or open existing key-value map. inline map_handle create_map(const ::std::string_view &name, const ::mdbx::key_mode key_mode = ::mdbx::key_mode::usual, - const ::mdbx::value_mode value_mode = ::mdbx::value_mode::single); + const ::mdbx::value_mode value_mode = ::mdbx::value_mode::single) { + return create_map(::mdbx::slice(name), key_mode, value_mode); + } /// \brief Drop key-value map. /// \return `True` if the key-value map existed and was deleted, either /// `false` if the key-value map did not exist and there is nothing to delete. - bool drop_map(const ::std::string_view &name, bool throw_if_absent = false); + bool drop_map(const ::std::string_view &name, bool throw_if_absent = false) { + return drop_map(::mdbx::slice(name), throw_if_absent); + } /// \return `True` if the key-value map existed and was cleared, either /// `false` if the key-value map did not exist and there is nothing to clear. - bool clear_map(const ::std::string_view &name, bool throw_if_absent = false); + bool clear_map(const ::std::string_view &name, bool throw_if_absent = false) { + return clear_map(::mdbx::slice(name), throw_if_absent); + } /// \brief Переименовывает таблицу ключ-значение. inline void rename_map(map_handle map, const ::std::string_view &new_name); /// \brief Переименовывает таблицу ключ-значение. @@ -4466,8 +4500,10 @@ public: /// `false` в случае отсутствия исходной таблицы. bool rename_map(const ::std::string_view &old_name, const ::std::string_view &new_name, - bool throw_if_absent = false); - + bool throw_if_absent = false) { + return rename_map(::mdbx::slice(old_name), ::mdbx::slice(new_name), + throw_if_absent); + } #endif /* __cpp_lib_string_view >= 201606L */ using map_stat = ::MDBX_stat; @@ -6427,6 +6463,17 @@ inline size_t txn::release_all_cursors(bool unbind) const { return size_t(err); } +inline ::mdbx::map_handle +txn::open_map(const ::mdbx::slice &name, const ::mdbx::key_mode key_mode, + const ::mdbx::value_mode value_mode) const { + ::mdbx::map_handle map; + error::success_or_throw(::mdbx_dbi_open2( + handle_, name, MDBX_db_flags_t(key_mode) | MDBX_db_flags_t(value_mode), + &map.dbi)); + assert(map.dbi != 0); + return map; +} + inline ::mdbx::map_handle txn::open_map(const char *name, const ::mdbx::key_mode key_mode, const ::mdbx::value_mode value_mode) const { @@ -6438,6 +6485,15 @@ txn::open_map(const char *name, const ::mdbx::key_mode key_mode, return map; } +inline ::mdbx::map_handle +txn::open_map_accede(const ::mdbx::slice &name) const { + ::mdbx::map_handle map; + error::success_or_throw( + ::mdbx_dbi_open2(handle_, name, MDBX_DB_ACCEDE, &map.dbi)); + assert(map.dbi != 0); + return map; +} + inline ::mdbx::map_handle txn::open_map_accede(const char *name) const { ::mdbx::map_handle map; error::success_or_throw( @@ -6446,6 +6502,18 @@ inline ::mdbx::map_handle txn::open_map_accede(const char *name) const { return map; } +inline ::mdbx::map_handle txn::create_map(const ::mdbx::slice &name, + const ::mdbx::key_mode key_mode, + const ::mdbx::value_mode value_mode) { + ::mdbx::map_handle map; + error::success_or_throw(::mdbx_dbi_open2( + handle_, name, + MDBX_CREATE | MDBX_db_flags_t(key_mode) | MDBX_db_flags_t(value_mode), + &map.dbi)); + assert(map.dbi != 0); + return map; +} + inline ::mdbx::map_handle txn::create_map(const char *name, const ::mdbx::key_mode key_mode, const ::mdbx::value_mode value_mode) { @@ -6470,109 +6538,39 @@ inline void txn::rename_map(map_handle map, const char *new_name) { error::success_or_throw(::mdbx_dbi_rename(handle_, map, new_name)); } -#if defined(DOXYGEN) || \ - (defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L) - -inline ::mdbx::map_handle -txn::open_map(const ::std::string_view &name, const ::mdbx::key_mode key_mode, - const ::mdbx::value_mode value_mode) const { - ::mdbx::map_handle map; - error::success_or_throw(::mdbx_dbi_open2( - handle_, ::mdbx::slice(name), - MDBX_db_flags_t(key_mode) | MDBX_db_flags_t(value_mode), &map.dbi)); - assert(map.dbi != 0); - return map; -} - -inline ::mdbx::map_handle -txn::open_map_accede(const ::std::string_view &name) const { - ::mdbx::map_handle map; - error::success_or_throw( - ::mdbx_dbi_open2(handle_, ::mdbx::slice(name), MDBX_DB_ACCEDE, &map.dbi)); - assert(map.dbi != 0); - return map; -} - -inline ::mdbx::map_handle txn::create_map(const ::std::string_view &name, - const ::mdbx::key_mode key_mode, - const ::mdbx::value_mode value_mode) { - ::mdbx::map_handle map; - error::success_or_throw(::mdbx_dbi_open2( - handle_, ::mdbx::slice(name), - MDBX_CREATE | MDBX_db_flags_t(key_mode) | MDBX_db_flags_t(value_mode), - &map.dbi)); - assert(map.dbi != 0); - return map; -} - -inline void txn::rename_map(map_handle map, - const ::std::string_view &new_name) { - error::success_or_throw( - ::mdbx_dbi_rename2(handle_, map, ::mdbx::slice(new_name))); +inline void txn::rename_map(map_handle map, const ::mdbx::slice &new_name) { + error::success_or_throw(::mdbx_dbi_rename2(handle_, map, new_name)); } inline ::mdbx::map_handle txn::open_map(const ::std::string &name, const ::mdbx::key_mode key_mode, const ::mdbx::value_mode value_mode) const { - return open_map(::std::string_view(name), key_mode, value_mode); + return open_map(::mdbx::slice(name), key_mode, value_mode); } inline ::mdbx::map_handle txn::open_map_accede(const ::std::string &name) const { - return open_map_accede(::std::string_view(name)); + return open_map_accede(::mdbx::slice(name)); } inline ::mdbx::map_handle txn::create_map(const ::std::string &name, const ::mdbx::key_mode key_mode, const ::mdbx::value_mode value_mode) { - return create_map(::std::string_view(name), key_mode, value_mode); + return create_map(::mdbx::slice(name), key_mode, value_mode); } inline bool txn::drop_map(const ::std::string &name, bool throw_if_absent) { - return drop_map(::std::string_view(name), throw_if_absent); + return drop_map(::mdbx::slice(name), throw_if_absent); } inline bool txn::clear_map(const ::std::string &name, bool throw_if_absent) { - return clear_map(::std::string_view(name), throw_if_absent); + return clear_map(::mdbx::slice(name), throw_if_absent); } inline void txn::rename_map(map_handle map, const ::std::string &new_name) { - return rename_map(map, ::std::string_view(new_name)); + return rename_map(map, ::mdbx::slice(new_name)); } -#else - -inline ::mdbx::map_handle -txn::open_map(const ::std::string &name, const ::mdbx::key_mode key_mode, - const ::mdbx::value_mode value_mode) const { - return open_map(name.c_str(), key_mode, value_mode); -} - -inline ::mdbx::map_handle -txn::open_map_accede(const ::std::string &name) const { - return open_map_accede(name.c_str()); -} - -inline ::mdbx::map_handle txn::create_map(const ::std::string &name, - const ::mdbx::key_mode key_mode, - const ::mdbx::value_mode value_mode) { - return create_map(name.c_str(), key_mode, value_mode); -} - -inline bool txn::drop_map(const ::std::string &name, bool throw_if_absent) { - return drop_map(name.c_str(), throw_if_absent); -} - -inline bool txn::clear_map(const ::std::string &name, bool throw_if_absent) { - return clear_map(name.c_str(), throw_if_absent); -} - -inline void txn::rename_map(map_handle map, const ::std::string &new_name) { - return rename_map(map, new_name.c_str()); -} - -#endif /* __cpp_lib_string_view >= 201606L */ - inline txn::map_stat txn::get_map_stat(map_handle map) const { txn::map_stat r; error::success_or_throw(::mdbx_dbi_stat(handle_, map.dbi, &r, sizeof(r))); diff --git a/src/mdbx.c++ b/src/mdbx.c++ index 7c27f1d8..583b46a4 100644 --- a/src/mdbx.c++ +++ b/src/mdbx.c++ @@ -1618,13 +1618,9 @@ __cold bool txn::rename_map(const char *old_name, const char *new_name, } } -#if defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L - -__cold bool txn::drop_map(const ::std::string_view &name, - bool throw_if_absent) { +__cold bool txn::drop_map(const ::mdbx::slice &name, bool throw_if_absent) { map_handle map; - const int err = - ::mdbx_dbi_open2(handle_, mdbx::slice(name), MDBX_DB_ACCEDE, &map.dbi); + const int err = ::mdbx_dbi_open2(handle_, name, MDBX_DB_ACCEDE, &map.dbi); switch (err) { case MDBX_SUCCESS: drop_map(map); @@ -1639,11 +1635,9 @@ __cold bool txn::drop_map(const ::std::string_view &name, } } -__cold bool txn::clear_map(const ::std::string_view &name, - bool throw_if_absent) { +__cold bool txn::clear_map(const ::mdbx::slice &name, bool throw_if_absent) { map_handle map; - const int err = - ::mdbx_dbi_open2(handle_, mdbx::slice(name), MDBX_DB_ACCEDE, &map.dbi); + const int err = ::mdbx_dbi_open2(handle_, name, MDBX_DB_ACCEDE, &map.dbi); switch (err) { case MDBX_SUCCESS: clear_map(map); @@ -1658,12 +1652,11 @@ __cold bool txn::clear_map(const ::std::string_view &name, } } -__cold bool txn::rename_map(const ::std::string_view &old_name, - const ::std::string_view &new_name, +__cold bool txn::rename_map(const ::mdbx::slice &old_name, + const ::mdbx::slice &new_name, bool throw_if_absent) { map_handle map; - const int err = ::mdbx_dbi_open2(handle_, mdbx::slice(old_name), - MDBX_DB_ACCEDE, &map.dbi); + const int err = ::mdbx_dbi_open2(handle_, old_name, MDBX_DB_ACCEDE, &map.dbi); switch (err) { case MDBX_SUCCESS: rename_map(map, new_name); @@ -1681,20 +1674,10 @@ __cold bool txn::rename_map(const ::std::string_view &old_name, __cold bool txn::rename_map(const ::std::string &old_name, const ::std::string &new_name, bool throw_if_absent) { - return rename_map(::std::string_view(old_name), ::std::string_view(new_name), + return rename_map(::mdbx::slice(old_name), ::mdbx::slice(new_name), throw_if_absent); } -#else - -__cold bool txn::rename_map(const ::std::string &old_name, - const ::std::string &new_name, - bool throw_if_absent) { - return rename_map(old_name.c_str(), new_name.c_str(), throw_if_absent); -} - -#endif /* __cpp_lib_string_view >= 201606L */ - //------------------------------------------------------------------------------ void cursor_managed::close() {