2020-09-15 02:02:03 +08:00
|
|
|
|
/// \file mdbx.h++
|
|
|
|
|
/// \brief The libmdbx C++ API header file (preliminary).
|
|
|
|
|
///
|
|
|
|
|
/// \author Copyright (c) 2020, Leonid Yuriev <leo@yuriev.ru>.
|
|
|
|
|
/// \copyright SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Tested with:
|
|
|
|
|
/// - LCC >= 1.23 (http://www.mcst.ru/lcc),
|
|
|
|
|
/// - GNU C++ >= 4.8,
|
|
|
|
|
/// - clang >= 4.0,
|
|
|
|
|
/// - MSVC >= 19.0 (Visual Studio 2015),
|
|
|
|
|
/// but 19.2x could hang due optimizer bug.
|
|
|
|
|
///
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#if (!defined(__cplusplus) || __cplusplus < 201103L) && \
|
2020-09-13 23:59:33 +08:00
|
|
|
|
!(defined( \
|
|
|
|
|
_MSC_VER) /* MSVC is mad and don't define __cplusplus properly */ \
|
|
|
|
|
&& _MSC_VER == 1900)
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#error "C++11 or better is required"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if (defined(_WIN32) || defined(_WIN64)) && MDBX_AVOID_CRT
|
|
|
|
|
#error "CRT is required for C++ API, the MDBX_AVOID_CRT option must be disabled"
|
|
|
|
|
#endif /* Windows */
|
|
|
|
|
|
|
|
|
|
#ifndef __has_include
|
|
|
|
|
#define __has_include(header) (0)
|
|
|
|
|
#endif /* __has_include */
|
|
|
|
|
|
2020-09-14 20:24:17 +08:00
|
|
|
|
#if __has_include(<version>)
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#include <version>
|
|
|
|
|
#endif /* <version> */
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
/* Disable min/max macros from C' headers */
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#ifndef NOMINMAX
|
|
|
|
|
#define NOMINMAX
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <algorithm> // for std::min/max
|
|
|
|
|
#include <cassert> // for assert()
|
|
|
|
|
#include <cstring> // for std::strlen, str:memcmp
|
|
|
|
|
#include <exception> // for std::exception_ptr
|
|
|
|
|
#include <ostream> // for std::ostream
|
2020-08-24 16:20:41 +08:00
|
|
|
|
#include <sstream> // for std::ostringstream
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#include <stdexcept> // for std::invalid_argument
|
|
|
|
|
#include <string> // for std::string
|
2020-08-24 16:20:41 +08:00
|
|
|
|
#include <type_traits> // for std::is_pod<>, etc.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
#include <vector> // for std::vector<> as template args
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
// Unused for now
|
|
|
|
|
// #if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
|
|
|
|
|
// #include <bit>
|
|
|
|
|
// #endif
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
#if defined(__cpp_lib_memory_resource) && __cpp_lib_memory_resource >= 201603L
|
|
|
|
|
#include <memory_resource>
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#if defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L
|
|
|
|
|
#include <string_view>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(__cpp_lib_filesystem) && __cpp_lib_filesystem >= 201703L
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "mdbx.h"
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
defined(__cpp_constexpr) && __cpp_constexpr >= 201603L && \
|
|
|
|
|
((defined(_MSC_VER) && _MSC_VER >= 1915) || \
|
|
|
|
|
(defined(__clang__) && __clang_major__ > 5) || \
|
|
|
|
|
(defined(__GNUC__) && __GNUC__ > 7) || \
|
|
|
|
|
(!defined(__GNUC__) && !defined(__clang__) && !defined(_MSC_VER)))
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_CXX17_CONSTEXPR constexpr
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#else
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_CXX17_CONSTEXPR inline
|
|
|
|
|
#endif /* MDBX_CXX17_CONSTEXPR */
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
#if defined(DOXYGEN) || defined(__cpp_lib_is_constant_evaluated) && \
|
|
|
|
|
__cpp_lib_is_constant_evaluated >= 201811L && \
|
|
|
|
|
defined(__cpp_lib_constexpr_string) && \
|
|
|
|
|
__cpp_lib_constexpr_string >= 201907L
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_CXX20_CONSTEXPR constexpr
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#else
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_CXX20_CONSTEXPR inline
|
|
|
|
|
#endif /* MDBX_CXX20_CONSTEXPR */
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#if defined(CONSTEXPR_ASSERT)
|
|
|
|
|
#define MDBX_CONSTEXPR_ASSERT(expr) CONSTEXPR_ASSERT(expr)
|
|
|
|
|
#elif defined NDEBUG
|
|
|
|
|
#define MDBX_CONSTEXPR_ASSERT(expr) void(0)
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#else
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_CONSTEXPR_ASSERT(expr) \
|
|
|
|
|
((expr) ? void(0) : [] { assert(!#expr); }())
|
|
|
|
|
#endif /* MDBX_CONSTEXPR_ASSERT */
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#ifndef MDBX_LIKELY
|
2020-08-24 16:20:41 +08:00
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__GNUC__) || __has_builtin(__builtin_expect)) && \
|
|
|
|
|
!defined(__COVERITY__)
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_LIKELY(cond) __builtin_expect(!!(cond), 1)
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#else
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_LIKELY(x) (x)
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#endif
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#endif /* MDBX_LIKELY */
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#ifndef MDBX_UNLIKELY
|
2020-08-24 16:20:41 +08:00
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__GNUC__) || __has_builtin(__builtin_expect)) && \
|
|
|
|
|
!defined(__COVERITY__)
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_UNLIKELY(cond) __builtin_expect(!!(cond), 0)
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#else
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_UNLIKELY(x) (x)
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#endif
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#endif /* MDBX_UNLIKELY */
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(__has_cpp_attribute(fallthrough) && \
|
2020-08-23 01:19:46 +08:00
|
|
|
|
(!defined(__clang__) || __clang__ > 4)) || \
|
|
|
|
|
__cplusplus >= 201703L
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_CXX17_FALLTHROUGH [[fallthrough]]
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#else
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_CXX17_FALLTHROUGH
|
|
|
|
|
#endif /* MDBX_CXX17_FALLTHROUGH */
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
#if defined(DOXYGEN) || __has_cpp_attribute(likely)
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_CXX20_LIKELY [[likely]]
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#else
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_CXX20_LIKELY
|
|
|
|
|
#endif /* MDBX_CXX20_LIKELY */
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#ifndef MDBX_CXX20_UNLIKELY
|
2020-08-24 16:20:41 +08:00
|
|
|
|
#if defined(DOXYGEN) || __has_cpp_attribute(unlikely)
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_CXX20_UNLIKELY [[unlikely]]
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#else
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#define MDBX_CXX20_UNLIKELY
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#endif
|
2020-09-14 21:40:46 +08:00
|
|
|
|
#endif /* MDBX_CXX20_UNLIKELY */
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
#pragma warning(push, 4)
|
|
|
|
|
#pragma warning(disable : 4251) /* 'std::FOO' needs to have dll-interface to \
|
|
|
|
|
be used by clients of 'mdbx::BAR' */
|
|
|
|
|
#pragma warning(disable : 4275) /* non dll-interface 'std::FOO' used as \
|
|
|
|
|
base for dll-interface 'mdbx::BAR' */
|
|
|
|
|
/* MSVC is mad and can generate this warning for its own intermediate
|
|
|
|
|
* automatically generated code, which becomes unreachable after some kinds of
|
2020-09-26 23:28:09 +08:00
|
|
|
|
* optimization (copy elision, etc). */
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#pragma warning(disable : 4702) /* unreachable code */
|
|
|
|
|
#endif /* _MSC_VER (warnings) */
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
/// \defgroup cxx_api C++ API
|
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
|
namespace mdbx {
|
|
|
|
|
|
2020-09-14 20:24:17 +08:00
|
|
|
|
// Functions whose signature depends on the `mdbx::byte` type
|
2020-08-23 01:19:46 +08:00
|
|
|
|
// must be strictly defined as inline!
|
|
|
|
|
#if defined(DOXYGEN) || (defined(__cpp_char8_t) && __cpp_char8_t >= 201811)
|
2020-09-16 06:17:47 +08:00
|
|
|
|
// Wanna using a non-aliasing type to release more power of an optimizer.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
using byte = char8_t;
|
|
|
|
|
#else
|
2020-09-16 06:17:47 +08:00
|
|
|
|
// Wanna not using std::byte since it doesn't add features,
|
|
|
|
|
// but add inconvenient restrictions.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
using byte = unsigned char;
|
|
|
|
|
#endif /* __cpp_char8_t >= 201811*/
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \copydoc MDBX_version_info
|
2020-08-23 01:19:46 +08:00
|
|
|
|
using version_info = ::MDBX_version_info;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns libmdbx version information.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const version_info &get_version() noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \copydoc MDBX_build_info
|
2020-08-23 01:19:46 +08:00
|
|
|
|
using build_info = ::MDBX_build_info;
|
2020-09-26 23:28:09 +08:00
|
|
|
|
/// \brief Returns libmdbx build information.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const build_info &get_build() noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief constexpr-compatible strlen().
|
2020-09-14 21:40:46 +08:00
|
|
|
|
static MDBX_CXX17_CONSTEXPR size_t strlen(const char *c_str) noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
struct slice;
|
|
|
|
|
class env;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
class env_managed;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
class txn;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
class txn_managed;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
class cursor;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
class cursor_managed;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Legacy default allocator
|
|
|
|
|
/// but it is recommended to use \ref polymorphic_allocator.
|
2020-09-01 18:28:29 +08:00
|
|
|
|
using legacy_allocator = ::std::string::allocator_type;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
defined(__cpp_lib_memory_resource) && __cpp_lib_memory_resource >= 201603L
|
2020-09-14 21:40:46 +08:00
|
|
|
|
/// \brief Default polymorphic allocator for modern code.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
using polymorphic_allocator = ::std::pmr::string::allocator_type;
|
|
|
|
|
#endif /* __cpp_lib_memory_resource >= 201603L */
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Default singe-byte string.
|
2020-09-01 18:28:29 +08:00
|
|
|
|
template <class ALLOCATOR = legacy_allocator>
|
2020-08-24 16:20:41 +08:00
|
|
|
|
using string = ::std::basic_string<char, ::std::char_traits<char>, ALLOCATOR>;
|
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
using filehandle = ::mdbx_filehandle_t;
|
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__cpp_lib_filesystem) && __cpp_lib_filesystem >= 201703L && \
|
|
|
|
|
(!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || \
|
|
|
|
|
__MAC_OS_X_VERSION_MIN_REQUIRED >= 101500))
|
|
|
|
|
using path = std::filesystem::path;
|
|
|
|
|
#elif defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
using path = ::std::wstring;
|
|
|
|
|
#else
|
|
|
|
|
using path = ::std::string;
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Transfers C++ exceptions thru C callbacks.
|
|
|
|
|
/// \details Implements saving exceptions before returning
|
|
|
|
|
/// from an C++'s environment to the intermediate C code and re-throwing after
|
|
|
|
|
/// returning from C to the C++'s environment.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
class LIBMDBX_API_TYPE exception_thunk {
|
|
|
|
|
::std::exception_ptr captured_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
exception_thunk() noexcept = default;
|
|
|
|
|
exception_thunk(const exception_thunk &) = delete;
|
|
|
|
|
exception_thunk(exception_thunk &&) = delete;
|
|
|
|
|
exception_thunk &operator=(const exception_thunk &) = delete;
|
|
|
|
|
exception_thunk &operator=(exception_thunk &&) = delete;
|
|
|
|
|
inline bool is_clean() const noexcept;
|
|
|
|
|
inline void capture() noexcept;
|
|
|
|
|
inline void rethrow_captured() const;
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Implements error information and throwing corresponding exceptions.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
class LIBMDBX_API_TYPE error {
|
|
|
|
|
MDBX_error_t code_;
|
|
|
|
|
inline error &operator=(MDBX_error_t error_code) noexcept;
|
|
|
|
|
|
|
|
|
|
public:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR error(MDBX_error_t error_code) noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error(const error &) = default;
|
|
|
|
|
error(error &&) = default;
|
|
|
|
|
error &operator=(const error &) = default;
|
|
|
|
|
error &operator=(error &&) = default;
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR friend bool operator==(const error &a,
|
|
|
|
|
const error &b) noexcept;
|
|
|
|
|
MDBX_CXX11_CONSTEXPR friend bool operator!=(const error &a,
|
|
|
|
|
const error &b) noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool is_success() const noexcept;
|
|
|
|
|
MDBX_CXX11_CONSTEXPR bool is_result_true() const noexcept;
|
|
|
|
|
MDBX_CXX11_CONSTEXPR bool is_result_false() const noexcept;
|
|
|
|
|
MDBX_CXX11_CONSTEXPR bool is_failure() const noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns error code.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR MDBX_error_t code() const noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns message for MDBX's errors only and "SYSTEM" for others.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const char *what() const noexcept;
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns message for any errors.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
::std::string message() const;
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns true for MDBX's errors.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool is_mdbx_error() const noexcept;
|
|
|
|
|
/// \brief Panics on unrecoverable errors inside destructors etc.
|
|
|
|
|
[[noreturn]] void panic(const char *context_where_when,
|
|
|
|
|
const char *func_who_what) const noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
[[noreturn]] void throw_exception() const;
|
|
|
|
|
[[noreturn]] static inline void throw_exception(int error_code);
|
|
|
|
|
inline void throw_on_failure() const;
|
|
|
|
|
inline void success_or_throw() const;
|
|
|
|
|
inline void success_or_throw(const exception_thunk &) const;
|
|
|
|
|
inline void panic_on_failure(const char *context_where,
|
|
|
|
|
const char *func_who) const noexcept;
|
|
|
|
|
inline void success_or_panic(const char *context_where,
|
|
|
|
|
const char *func_who) const noexcept;
|
|
|
|
|
static inline void throw_on_nullptr(const void *ptr, MDBX_error_t error_code);
|
2020-08-31 17:24:12 +08:00
|
|
|
|
static inline void success_or_throw(MDBX_error_t error_code);
|
|
|
|
|
static void success_or_throw(int error_code) {
|
|
|
|
|
success_or_throw(static_cast<MDBX_error_t>(error_code));
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
static inline void throw_on_failure(int error_code);
|
|
|
|
|
static inline bool boolean_or_throw(int error_code);
|
|
|
|
|
static inline void success_or_throw(int error_code, const exception_thunk &);
|
|
|
|
|
static inline void panic_on_failure(int error_code, const char *context_where,
|
|
|
|
|
const char *func_who) noexcept;
|
|
|
|
|
static inline void success_or_panic(int error_code, const char *context_where,
|
|
|
|
|
const char *func_who) noexcept;
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Base class for all libmdbx's exceptions that are corresponds
|
|
|
|
|
/// to libmdbx errors.
|
|
|
|
|
///
|
|
|
|
|
/// \see MDBX_error_t
|
2020-08-23 01:19:46 +08:00
|
|
|
|
class LIBMDBX_API_TYPE exception : public ::std::runtime_error {
|
|
|
|
|
using base = ::std::runtime_error;
|
2020-09-01 21:19:14 +08:00
|
|
|
|
::mdbx::error error_;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
public:
|
2020-09-01 21:19:14 +08:00
|
|
|
|
exception(const ::mdbx::error &) noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
exception(const exception &) = default;
|
|
|
|
|
exception(exception &&) = default;
|
|
|
|
|
exception &operator=(const exception &) = default;
|
|
|
|
|
exception &operator=(exception &&) = default;
|
|
|
|
|
virtual ~exception() noexcept;
|
2020-09-01 21:19:14 +08:00
|
|
|
|
const mdbx::error error() const noexcept { return error_; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Fatal exception that lead termination anyway
|
|
|
|
|
/// in dangerous unrecoverable cases.
|
2020-09-01 21:19:14 +08:00
|
|
|
|
class LIBMDBX_API_TYPE fatal : public exception {
|
|
|
|
|
using base = exception;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
public:
|
2020-09-01 21:19:14 +08:00
|
|
|
|
fatal(const ::mdbx::error &) noexcept;
|
|
|
|
|
fatal(const exception &src) noexcept : fatal(src.error()) {}
|
|
|
|
|
fatal(exception &&src) noexcept : fatal(src.error()) {}
|
2020-09-05 19:04:41 +08:00
|
|
|
|
fatal(const fatal &src) noexcept : fatal(src.error()) {}
|
|
|
|
|
fatal(fatal &&src) noexcept : fatal(src.error()) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
fatal &operator=(fatal &&) = default;
|
|
|
|
|
fatal &operator=(const fatal &) = default;
|
|
|
|
|
virtual ~fatal() noexcept;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define MDBX_DECLARE_EXCEPTION(NAME) \
|
|
|
|
|
struct LIBMDBX_API_TYPE NAME : public exception { \
|
2020-09-01 21:19:14 +08:00
|
|
|
|
NAME(const ::mdbx::error &); \
|
2020-08-23 01:19:46 +08:00
|
|
|
|
virtual ~NAME() noexcept; \
|
|
|
|
|
}
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(bad_map_id);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(bad_transaction);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(bad_value_size);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(db_corrupted);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(db_full);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(db_invalid);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(db_too_large);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(db_unable_extend);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(db_version_mismatch);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(db_wanna_write_for_recovery);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(incompatible_operation);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(internal_page_full);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(internal_problem);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(key_exists);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(key_mismatch);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(max_maps_reached);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(max_readers_reached);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(multivalue);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(no_data);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(not_found);
|
2020-09-26 23:28:09 +08:00
|
|
|
|
MDBX_DECLARE_EXCEPTION(operation_not_permitted);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
MDBX_DECLARE_EXCEPTION(permission_denied_or_not_writeable);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(reader_slot_busy);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(remote_media);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(something_busy);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(thread_mismatch);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(transaction_full);
|
|
|
|
|
MDBX_DECLARE_EXCEPTION(transaction_overlapping);
|
|
|
|
|
#undef MDBX_DECLARE_EXCEPTION
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
[[noreturn]] LIBMDBX_API void throw_too_small_target_buffer();
|
2020-08-23 01:19:46 +08:00
|
|
|
|
[[noreturn]] LIBMDBX_API void throw_max_length_exceeded();
|
2020-08-31 17:24:12 +08:00
|
|
|
|
[[noreturn]] LIBMDBX_API void throw_out_range();
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR size_t check_length(size_t bytes);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief References a data located outside the slice.
|
|
|
|
|
///
|
|
|
|
|
/// The `slice` is similar in many ways to `std::string_view`, but it
|
2020-09-14 21:40:46 +08:00
|
|
|
|
/// implements specific capabilities and manipulates with bytes but
|
|
|
|
|
/// not a characters.
|
2020-09-07 06:54:36 +08:00
|
|
|
|
///
|
|
|
|
|
/// \copydetails MDBX_val
|
2020-08-23 01:19:46 +08:00
|
|
|
|
struct LIBMDBX_API_TYPE slice : public ::MDBX_val {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \todo slice& operator<<(slice&, ...) for reading
|
|
|
|
|
/// \todo key-to-value (parse/unpack) functions
|
|
|
|
|
/// \todo template<class X> key(X); for decoding keys while reading
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
enum { max_length = MDBX_MAXDATASIZE };
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Create an empty slice.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR slice() noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Create a slice that refers to [0,bytes-1] of memory bytes pointed
|
|
|
|
|
/// by ptr.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR slice(const void *ptr, size_t bytes);
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Create a slice that refers to [begin,end] of memory bytes.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR slice(const void *begin, const void *end);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Create a slice that refers to text[0,strlen(text)-1].
|
2020-08-23 01:19:46 +08:00
|
|
|
|
template <size_t SIZE>
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR slice(const char (&text)[SIZE]) noexcept
|
2020-08-23 01:19:46 +08:00
|
|
|
|
: slice(text, SIZE - 1) {
|
2020-10-31 16:42:43 +08:00
|
|
|
|
MDBX_CONSTEXPR_ASSERT(SIZE > 0 && text[SIZE - 1] == '\0');
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Create a slice that refers to c_str[0,strlen(c_str)-1].
|
2020-09-14 21:40:46 +08:00
|
|
|
|
explicit MDBX_CXX17_CONSTEXPR slice(const char *c_str);
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Create a slice that refers to the contents of "str".
|
|
|
|
|
/* 'explicit' to avoid reference to the temporary std::string instance */
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class C, class T, class A>
|
2020-09-14 21:40:46 +08:00
|
|
|
|
explicit MDBX_CXX20_CONSTEXPR slice(const ::std::basic_string<C, T, A> &str)
|
2020-08-24 16:20:41 +08:00
|
|
|
|
: slice(str.data(), str.length() * sizeof(C)) {}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR slice(const MDBX_val &src);
|
|
|
|
|
MDBX_CXX11_CONSTEXPR slice(const slice &) noexcept = default;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Create a slice that refers to the same contents as "sv"
|
2020-08-23 01:19:46 +08:00
|
|
|
|
template <class C, class T>
|
2020-09-14 21:40:46 +08:00
|
|
|
|
explicit MDBX_CXX14_CONSTEXPR slice(const ::std::basic_string_view<C, T> &sv)
|
2020-09-07 06:54:36 +08:00
|
|
|
|
: slice(sv.data(), sv.data() + sv.length()) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#endif /* __cpp_lib_string_view >= 201606L */
|
|
|
|
|
|
|
|
|
|
inline slice(MDBX_val &&src);
|
|
|
|
|
inline slice(slice &&src) noexcept;
|
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
|
|
|
|
|
template <class C, class T>
|
2020-09-07 06:54:36 +08:00
|
|
|
|
slice(::std::basic_string_view<C, T> &&sv) : slice(sv) {
|
|
|
|
|
sv = {};
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
#endif /* __cpp_lib_string_view >= 201606L */
|
|
|
|
|
|
|
|
|
|
template <size_t SIZE>
|
2020-09-14 21:40:46 +08:00
|
|
|
|
static MDBX_CXX14_CONSTEXPR slice wrap(const char (&text)[SIZE]) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return slice(text);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
template <typename POD>
|
|
|
|
|
MDBX_CXX14_CONSTEXPR static slice wrap(const POD &pod) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
static_assert(::std::is_standard_layout<POD>::value &&
|
|
|
|
|
!std::is_pointer<POD>::value,
|
|
|
|
|
"Must be a standard layout type!");
|
|
|
|
|
return slice(&pod, sizeof(pod));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice &assign(const void *ptr, size_t bytes);
|
|
|
|
|
inline slice &assign(const slice &src) noexcept;
|
|
|
|
|
inline slice &assign(const ::MDBX_val &src);
|
|
|
|
|
inline slice &assign(slice &&src) noexcept;
|
|
|
|
|
inline slice &assign(::MDBX_val &&src);
|
|
|
|
|
inline slice &assign(const void *begin, const void *end);
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class C, class T, class A>
|
|
|
|
|
slice &assign(const ::std::basic_string<C, T, A> &str) {
|
|
|
|
|
return assign(str.data(), str.length() * sizeof(C));
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline slice &assign(const char *c_str);
|
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
|
|
|
|
|
template <class C, class T>
|
|
|
|
|
slice &assign(const ::std::basic_string_view<C, T> &view) {
|
|
|
|
|
return assign(view.begin(), view.end());
|
|
|
|
|
}
|
|
|
|
|
template <class C, class T>
|
|
|
|
|
slice &assign(::std::basic_string_view<C, T> &&view) {
|
|
|
|
|
assign(view);
|
|
|
|
|
view = {};
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
#endif /* __cpp_lib_string_view >= 201606L */
|
|
|
|
|
|
|
|
|
|
slice &operator=(const slice &) noexcept = default;
|
|
|
|
|
inline slice &operator=(slice &&src) noexcept;
|
|
|
|
|
inline slice &operator=(::MDBX_val &&src);
|
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
|
|
|
|
|
template <class C, class T>
|
|
|
|
|
slice &operator=(const ::std::basic_string_view<C, T> &view) {
|
|
|
|
|
return assign(view);
|
|
|
|
|
}
|
|
|
|
|
template <class C, class T>
|
|
|
|
|
slice &operator=(::std::basic_string_view<C, T> &&view) {
|
|
|
|
|
return assign(view);
|
|
|
|
|
}
|
|
|
|
|
#endif /* __cpp_lib_string_view >= 201606L */
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class C = char, class T = ::std::char_traits<C>,
|
2020-09-01 18:28:29 +08:00
|
|
|
|
class A = legacy_allocator>
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_CONSTEXPR ::std::basic_string<C, T, A>
|
2020-08-24 16:20:41 +08:00
|
|
|
|
string(const A &allocator = A()) const {
|
|
|
|
|
static_assert(sizeof(C) == 1, "Must be single byte characters");
|
|
|
|
|
return ::std::basic_string<C, T, A>(char_ptr(), length(), allocator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class C, class T, class A>
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_CONSTEXPR operator ::std::basic_string<C, T, A>() const {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return this->string<C, T, A>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Fills the buffer by hexadecimal data dump of slice content.
|
|
|
|
|
/// \throws std::length_error if given buffer is too small.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
char *to_hex(char *dest, size_t dest_size, bool uppercase = false,
|
|
|
|
|
unsigned wrap_width = 0) const;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the buffer size in bytes needed for hexadecimal data dump
|
|
|
|
|
/// of slice content.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR size_t
|
|
|
|
|
to_hex_bytes(unsigned wrap_width = 0) const noexcept {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
const size_t bytes = length() << 1;
|
|
|
|
|
return wrap_width ? bytes + bytes / wrap_width : bytes;
|
|
|
|
|
}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Fills the buffer with data converted from hexadecimal dump
|
|
|
|
|
/// from slice content.
|
|
|
|
|
/// \throws std::length_error if given buffer is too small.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
byte *from_hex(byte *dest, size_t dest_size,
|
|
|
|
|
bool ignore_spaces = false) const;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the buffer size in bytes needed for conversion
|
|
|
|
|
/// hexadecimal dump from slice content to data.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR size_t from_hex_bytes() const noexcept {
|
2020-09-13 23:59:33 +08:00
|
|
|
|
return length() >> 1;
|
|
|
|
|
}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Fills the buffer by [Base58](https://en.wikipedia.org/wiki/Base58)
|
|
|
|
|
/// data dump of slice content.
|
|
|
|
|
/// \throws std::length_error if given buffer is too small.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
char *to_base58(char *dest, size_t dest_size, unsigned wrap_width = 0) const;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the buffer size in bytes needed for
|
|
|
|
|
/// [Base58](https://en.wikipedia.org/wiki/Base58) data dump of slice content.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR size_t
|
2020-09-13 23:59:33 +08:00
|
|
|
|
to_base58_bytes(unsigned wrap_width = 0) const noexcept {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
const size_t bytes = length() / 8 * 11 + (length() % 8 * 43 + 31) / 32;
|
|
|
|
|
return wrap_width ? bytes + bytes / wrap_width : bytes;
|
2020-08-24 16:20:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Fills the buffer with data converted from
|
|
|
|
|
/// [Base58](https://en.wikipedia.org/wiki/Base58) dump from slice content.
|
|
|
|
|
/// \throws std::length_error if given buffer is too small.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
byte *from_base58(byte *dest, size_t dest_size,
|
|
|
|
|
bool ignore_spaces = false) const;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
2020-10-01 07:27:29 +08:00
|
|
|
|
/// \brief Returns the buffer size in bytes needed for conversion
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// [Base58](https://en.wikipedia.org/wiki/Base58) dump to data.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR size_t from_base58_bytes() const noexcept {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return length() / 11 * 8 + length() % 11 * 32 / 43;
|
2020-08-24 16:20:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Fills the buffer by [Base64](https://en.wikipedia.org/wiki/Base64)
|
|
|
|
|
/// data dump.
|
|
|
|
|
/// \throws std::length_error if given buffer is too small.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
char *to_base64(char *dest, size_t dest_size, unsigned wrap_width = 0) const;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the buffer size in bytes needed for
|
|
|
|
|
/// [Base64](https://en.wikipedia.org/wiki/Base64) data dump.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR size_t
|
2020-09-13 23:59:33 +08:00
|
|
|
|
to_base64_bytes(unsigned wrap_width = 0) const noexcept {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
const size_t bytes = (length() + 2) / 3 * 4;
|
|
|
|
|
return wrap_width ? bytes + bytes / wrap_width : bytes;
|
|
|
|
|
}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Fills the buffer with data converted from
|
|
|
|
|
/// [Base64](https://en.wikipedia.org/wiki/Base64) dump.
|
|
|
|
|
/// \throws std::length_error if given buffer is too small.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
byte *from_base64(byte *dest, size_t dest_size,
|
|
|
|
|
bool ignore_spaces = false) const;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the buffer size in bytes needed for conversion
|
|
|
|
|
/// [Base64](https://en.wikipedia.org/wiki/Base64) dump to data.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR size_t from_base64_bytes() const noexcept {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return (length() + 3) / 4 * 3;
|
2020-08-24 16:20:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns a string with a hexadecimal dump of the slice content.
|
2020-09-01 18:28:29 +08:00
|
|
|
|
template <class ALLOCATOR = legacy_allocator>
|
2020-08-24 16:20:41 +08:00
|
|
|
|
inline ::mdbx::string<ALLOCATOR>
|
|
|
|
|
hex_encode(bool uppercase = false,
|
|
|
|
|
const ALLOCATOR &allocator = ALLOCATOR()) const;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Decodes hexadecimal dump from the slice content into returned data
|
|
|
|
|
/// string.
|
2020-09-01 18:28:29 +08:00
|
|
|
|
template <class ALLOCATOR = legacy_allocator>
|
2020-08-24 16:20:41 +08:00
|
|
|
|
inline ::mdbx::string<ALLOCATOR>
|
|
|
|
|
hex_decode(const ALLOCATOR &allocator = ALLOCATOR()) const;
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns a string with a
|
|
|
|
|
/// [Base58](https://en.wikipedia.org/wiki/Base58) dump of the slice content.
|
2020-09-01 18:28:29 +08:00
|
|
|
|
template <class ALLOCATOR = legacy_allocator>
|
2020-08-24 16:20:41 +08:00
|
|
|
|
inline ::mdbx::string<ALLOCATOR>
|
|
|
|
|
base58_encode(const ALLOCATOR &allocator = ALLOCATOR()) const;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Decodes [Base58](https://en.wikipedia.org/wiki/Base58) dump
|
|
|
|
|
/// from the slice content into returned data string.
|
2020-09-01 18:28:29 +08:00
|
|
|
|
template <class ALLOCATOR = legacy_allocator>
|
2020-08-24 16:20:41 +08:00
|
|
|
|
inline ::mdbx::string<ALLOCATOR>
|
|
|
|
|
base58_decode(const ALLOCATOR &allocator = ALLOCATOR()) const;
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns a string with a
|
|
|
|
|
/// [Base64](https://en.wikipedia.org/wiki/Base64) dump of the slice content.
|
2020-09-01 18:28:29 +08:00
|
|
|
|
template <class ALLOCATOR = legacy_allocator>
|
2020-08-24 16:20:41 +08:00
|
|
|
|
inline ::mdbx::string<ALLOCATOR>
|
|
|
|
|
base64_encode(const ALLOCATOR &allocator = ALLOCATOR()) const;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Decodes [Base64](https://en.wikipedia.org/wiki/Base64) dump
|
|
|
|
|
/// from the slice content into returned data string.
|
2020-09-01 18:28:29 +08:00
|
|
|
|
template <class ALLOCATOR = legacy_allocator>
|
2020-08-24 16:20:41 +08:00
|
|
|
|
inline ::mdbx::string<ALLOCATOR>
|
|
|
|
|
base64_decode(const ALLOCATOR &allocator = ALLOCATOR()) const;
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Checks whether the content of the slice is printable.
|
|
|
|
|
/// \param [in] disable_utf8 By default if `disable_utf8` is `false` function
|
|
|
|
|
/// checks that content bytes are printable ASCII-7 characters or a valid UTF8
|
|
|
|
|
/// sequences. Otherwise, if if `disable_utf8` is `true` function checks that
|
|
|
|
|
/// content bytes are printable extended 8-bit ASCII codes.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION bool
|
2020-08-31 20:36:20 +08:00
|
|
|
|
is_printable(bool disable_utf8 = false) const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Checks whether the content of the slice is a hexadecimal dump.
|
|
|
|
|
/// \param [in] ignore_spaces If `true` function will skips spaces surrounding
|
|
|
|
|
/// (before, between and after) a encoded bytes. However, spaces should not
|
|
|
|
|
/// break a pair of characters encoding a single byte.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION bool
|
2020-08-31 17:24:12 +08:00
|
|
|
|
is_hex(bool ignore_spaces = false) const noexcept;
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION bool
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Checks whether the content of the slice is a
|
|
|
|
|
/// [Base58](https://en.wikipedia.org/wiki/Base58) dump.
|
|
|
|
|
/// \param [in] ignore_spaces If `true` function will skips spaces surrounding
|
|
|
|
|
/// (before, between and after) a encoded bytes. However, spaces should not
|
|
|
|
|
/// break a code group of characters.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
is_base58(bool ignore_spaces = false) const noexcept;
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION bool
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Checks whether the content of the slice is a
|
|
|
|
|
/// [Base64](https://en.wikipedia.org/wiki/Base64) dump.
|
|
|
|
|
/// \param [in] ignore_spaces If `true` function will skips spaces surrounding
|
|
|
|
|
/// (before, between and after) a encoded bytes. However, spaces should not
|
|
|
|
|
/// break a code group of characters.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
is_base64(bool ignore_spaces = false) const noexcept;
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Return a string_view that references the same data as this slice.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
template <class C, class T>
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR explicit
|
2020-09-13 23:59:33 +08:00
|
|
|
|
operator ::std::basic_string_view<C, T>() const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
static_assert(sizeof(C) == 1, "Must be single byte characters");
|
|
|
|
|
return ::std::basic_string_view<C, T>(char_ptr(), length());
|
|
|
|
|
}
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Return a string_view that references the same data as this slice.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class C = char, class T = ::std::char_traits<C>>
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR ::std::basic_string_view<C, T>
|
|
|
|
|
string_view() const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
static_assert(sizeof(C) == 1, "Must be single byte characters");
|
|
|
|
|
return ::std::basic_string_view<C, T>(char_ptr(), length());
|
|
|
|
|
}
|
|
|
|
|
#endif /* __cpp_lib_string_view >= 201606L */
|
|
|
|
|
|
|
|
|
|
inline void swap(slice &other) noexcept;
|
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
|
|
|
|
|
template <class C, class T>
|
|
|
|
|
void swap(::std::basic_string_view<C, T> &view) noexcept {
|
|
|
|
|
static_assert(sizeof(C) == 1, "Must be single byte characters");
|
|
|
|
|
const auto temp = ::std::basic_string_view<C, T>(*this);
|
|
|
|
|
*this = view;
|
|
|
|
|
view = temp;
|
|
|
|
|
}
|
|
|
|
|
#endif /* __cpp_lib_string_view >= 201606L */
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns casted to pointer to byte an address of data.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const byte *byte_ptr() const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns casted to pointer to char an address of data.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const char *char_ptr() const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Return a pointer to the beginning of the referenced data.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const void *data() const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the number of bytes.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR size_t length() const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Checks whether the slice is empty.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool empty() const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Checks whether the slice data pointer is nullptr.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool is_null() const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the number of bytes.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR size_t size() const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns true if slice is not empty.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR operator bool() const noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Depletes content of slice and make it invalid.
|
|
|
|
|
inline void invalidate() noexcept;
|
|
|
|
|
|
|
|
|
|
/// \brief Makes the slice empty and referencing to nothing.
|
|
|
|
|
inline void clear() noexcept;
|
|
|
|
|
|
|
|
|
|
/// \brief Drops the first "n" bytes from this slice.
|
|
|
|
|
/// \pre REQUIRES: `n <= size()`
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline void remove_prefix(size_t n) noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Drops the last "n" bytes from this slice.
|
|
|
|
|
/// \pre REQUIRES: `n <= size()`
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline void remove_suffix(size_t n) noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Drops the first "n" bytes from this slice.
|
|
|
|
|
/// \throws std::out_of_range if `n > size()`
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline void safe_remove_prefix(size_t n);
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Drops the last "n" bytes from this slice.
|
|
|
|
|
/// \throws std::out_of_range if `n > size()`
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline void safe_remove_suffix(size_t n);
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Checks if the data starts with the given prefix.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION inline bool
|
2020-08-23 01:19:46 +08:00
|
|
|
|
starts_with(const slice &prefix) const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Checks if the data ends with the given suffix.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION inline bool
|
2020-08-23 01:19:46 +08:00
|
|
|
|
ends_with(const slice &suffix) const noexcept;
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the nth byte in the referenced data.
|
|
|
|
|
/// \pre REQUIRES: `n < size()`
|
|
|
|
|
inline byte operator[](size_t n) const noexcept;
|
|
|
|
|
|
|
|
|
|
/// \brief Returns the nth byte in the referenced data with bounds checking.
|
|
|
|
|
/// \throws std::out_of_range if `n >= size()`
|
|
|
|
|
inline byte at(size_t n) const;
|
|
|
|
|
|
|
|
|
|
/// \brief Returns the first "n" bytes of the slice.
|
|
|
|
|
/// \pre REQUIRES: `n <= size()`
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline slice head(size_t n) const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the last "n" bytes of the slice.
|
|
|
|
|
/// \pre REQUIRES: `n <= size()`
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline slice tail(size_t n) const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the middle "n" bytes of the slice.
|
|
|
|
|
/// \pre REQUIRES: `from + n <= size()`
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline slice middle(size_t from, size_t n) const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the first "n" bytes of the slice.
|
|
|
|
|
/// \throws std::out_of_range if `n >= size()`
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline slice safe_head(size_t n) const;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the last "n" bytes of the slice.
|
|
|
|
|
/// \throws std::out_of_range if `n >= size()`
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline slice safe_tail(size_t n) const;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the middle "n" bytes of the slice.
|
|
|
|
|
/// \throws std::out_of_range if `from + n >= size()`
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline slice safe_middle(size_t from, size_t n) const;
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the hash value of referenced data.
|
|
|
|
|
/// \attention Function implementation and returned hash values may changed
|
|
|
|
|
/// version to version, and in future the t1ha3 will be used here. Therefore
|
|
|
|
|
/// values obtained from this function shouldn't be persisted anywhere.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX14_CONSTEXPR size_t
|
|
|
|
|
hash_value() const noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Three-way fast non-lexicographically length-based comparison.
|
|
|
|
|
/// \return value:
|
|
|
|
|
/// == 0 if "a" == "b",
|
|
|
|
|
/// < 0 if "a" shorter than "b",
|
|
|
|
|
/// > 0 if "a" longer than "b",
|
|
|
|
|
/// < 0 if "a" length-equal and lexicographically less than "b",
|
|
|
|
|
/// > 0 if "a" length-equal and lexicographically great than "b".
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION static inline intptr_t
|
2020-08-23 01:19:46 +08:00
|
|
|
|
compare_fast(const slice &a, const slice &b) noexcept;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Three-way lexicographically comparison.
|
|
|
|
|
/// \return value:
|
|
|
|
|
/// < 0 if "a" < "b",
|
|
|
|
|
/// == 0 if "a" == "b",
|
|
|
|
|
/// > 0 if "a" > "b".
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION static inline intptr_t
|
2020-08-23 01:19:46 +08:00
|
|
|
|
compare_lexicographically(const slice &a, const slice &b) noexcept;
|
|
|
|
|
friend inline bool operator==(const slice &a, const slice &b) noexcept;
|
|
|
|
|
friend inline bool operator<(const slice &a, const slice &b) noexcept;
|
|
|
|
|
friend inline bool operator>(const slice &a, const slice &b) noexcept;
|
|
|
|
|
friend inline bool operator<=(const slice &a, const slice &b) noexcept;
|
|
|
|
|
friend inline bool operator>=(const slice &a, const slice &b) noexcept;
|
|
|
|
|
friend inline bool operator!=(const slice &a, const slice &b) noexcept;
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Checks the slice is not refers to null address or has zero length.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool is_valid() const noexcept {
|
2020-09-01 18:07:05 +08:00
|
|
|
|
return !(iov_base == nullptr && iov_len != 0);
|
|
|
|
|
}
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Build an invalid slice which non-zero length and refers to null
|
|
|
|
|
/// address.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR static slice invalid() noexcept {
|
|
|
|
|
return slice(size_t(-1));
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
protected:
|
2020-09-26 23:28:09 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR slice(size_t invalid_length) noexcept
|
|
|
|
|
: ::MDBX_val({nullptr, invalid_length}) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief The chunk of data stored inside the buffer or located outside it.
|
2020-09-01 18:28:29 +08:00
|
|
|
|
template <class ALLOCATOR = legacy_allocator> class buffer {
|
2020-09-10 06:15:35 +08:00
|
|
|
|
friend class txn;
|
2020-08-24 16:20:41 +08:00
|
|
|
|
using silo = ::mdbx::string<ALLOCATOR>;
|
|
|
|
|
silo silo_;
|
2020-09-01 18:37:52 +08:00
|
|
|
|
::mdbx::slice slice_;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
void insulate() {
|
|
|
|
|
assert(is_reference());
|
|
|
|
|
silo_.assign(slice_.char_ptr(), slice_.length());
|
|
|
|
|
slice_.iov_base = const_cast<char *>(silo_.data());
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR const byte *
|
2020-08-24 16:20:41 +08:00
|
|
|
|
silo_begin() const noexcept {
|
|
|
|
|
return static_cast<const byte *>(static_cast<const void *>(silo_.data()));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR const byte *
|
2020-08-24 16:20:41 +08:00
|
|
|
|
silo_end() const noexcept {
|
|
|
|
|
return silo_begin() + silo_.capacity();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct data_preserver : public exception_thunk {
|
2020-09-26 06:40:11 +08:00
|
|
|
|
buffer data;
|
|
|
|
|
data_preserver(ALLOCATOR &allocator) : data(allocator) {}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
static int callback(void *context, MDBX_val *target, const void *src,
|
|
|
|
|
size_t bytes) noexcept;
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR operator MDBX_preserve_func() const noexcept {
|
2020-09-13 23:59:33 +08:00
|
|
|
|
return callback;
|
|
|
|
|
}
|
2020-09-26 06:40:11 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR operator const buffer &() const noexcept {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
MDBX_CXX11_CONSTEXPR operator buffer &() noexcept { return data; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \todo buffer& operator<<(buffer&, ...) for writing
|
2020-09-26 23:28:09 +08:00
|
|
|
|
/// \todo buffer& operator>>(buffer&, ...) for reading (delegated to slice)
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \todo template<class X> key(X) for encoding keys while writing
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
using allocator_type = ALLOCATOR;
|
|
|
|
|
enum : size_t {
|
|
|
|
|
max_length = MDBX_MAXDATASIZE,
|
2020-09-14 20:24:17 +08:00
|
|
|
|
default_shrink_threshold = 1024
|
2020-08-24 16:20:41 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the associated allocator.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_CONSTEXPR allocator_type get_allocator() const {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return silo_.get_allocator();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Checks whether data chunk stored inside the buffer, otherwise
|
|
|
|
|
/// buffer just refers to data located outside the buffer.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR bool
|
2020-08-24 16:20:41 +08:00
|
|
|
|
is_freestanding() const noexcept {
|
|
|
|
|
return size_t(byte_ptr() - silo_begin()) < silo_.capacity();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Checks whether the buffer just refers to data located outside
|
|
|
|
|
/// the buffer, rather than stores it.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR bool
|
|
|
|
|
is_reference() const noexcept {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return !is_freestanding();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the number of bytes that can be held in currently allocated
|
|
|
|
|
/// storage.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR size_t
|
|
|
|
|
capacity() const noexcept {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return is_freestanding() ? silo_.capacity() : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the number of bytes that available in currently allocated
|
2020-09-26 23:28:09 +08:00
|
|
|
|
/// storage ahead the currently beginning of data.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR size_t
|
|
|
|
|
headroom() const noexcept {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return is_freestanding() ? slice_.byte_ptr() - silo_begin() : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the number of bytes that available in currently allocated
|
2020-09-26 23:28:09 +08:00
|
|
|
|
/// storage after the currently data end.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR size_t
|
|
|
|
|
tailroom() const noexcept {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return is_freestanding() ? capacity() - headroom() - slice_.length() : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns casted to const pointer to byte an address of data.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const byte *byte_ptr() const noexcept {
|
2020-09-13 23:59:33 +08:00
|
|
|
|
return slice_.byte_ptr();
|
|
|
|
|
}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns casted to pointer to byte an address of data.
|
|
|
|
|
/// \pre REQUIRES: The buffer should store data chunk, but not referenced to
|
|
|
|
|
/// an external one.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR byte *byte_ptr() noexcept {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
assert(is_freestanding());
|
|
|
|
|
return const_cast<byte *>(slice_.byte_ptr());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief Returns casted to const pointer to char an address of data.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const char *char_ptr() const noexcept {
|
2020-09-13 23:59:33 +08:00
|
|
|
|
return slice_.char_ptr();
|
|
|
|
|
}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns casted to pointer to char an address of data.
|
|
|
|
|
/// \pre REQUIRES: The buffer should store data chunk, but not referenced to
|
|
|
|
|
/// an external one.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR char *char_ptr() noexcept {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
assert(is_freestanding());
|
|
|
|
|
return const_cast<char *>(slice_.char_ptr());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief Return a const pointer to the beginning of the referenced data.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const void *data() const noexcept {
|
|
|
|
|
return slice_.data();
|
|
|
|
|
}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Return a pointer to the beginning of the referenced data.
|
|
|
|
|
/// \pre REQUIRES: The buffer should store data chunk, but not referenced to
|
|
|
|
|
/// an external one.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR void *data() noexcept {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
assert(is_freestanding());
|
|
|
|
|
return const_cast<void *>(slice_.data());
|
|
|
|
|
}
|
2020-09-01 18:07:05 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the number of bytes.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR size_t
|
|
|
|
|
length() const noexcept {
|
|
|
|
|
return MDBX_CONSTEXPR_ASSERT(is_reference() ||
|
|
|
|
|
slice_.length() + headroom() ==
|
|
|
|
|
silo_.length()),
|
2020-08-24 16:20:41 +08:00
|
|
|
|
slice_.length();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void make_freestanding() {
|
|
|
|
|
if (is_reference())
|
|
|
|
|
insulate();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 18:37:52 +08:00
|
|
|
|
buffer(const ::mdbx::slice &src, bool make_reference,
|
2020-08-24 16:20:41 +08:00
|
|
|
|
const allocator_type &allocator = allocator_type())
|
|
|
|
|
: silo_(allocator), slice_(src) {
|
|
|
|
|
if (!make_reference)
|
|
|
|
|
insulate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer(const buffer &src, bool make_reference,
|
|
|
|
|
const allocator_type &allocator = allocator_type())
|
|
|
|
|
: buffer(src.slice_, make_reference, allocator) {}
|
|
|
|
|
|
|
|
|
|
buffer(const void *ptr, size_t bytes, bool make_reference,
|
|
|
|
|
const allocator_type &allocator = allocator_type())
|
2020-09-01 18:37:52 +08:00
|
|
|
|
: buffer(::mdbx::slice(ptr, bytes), make_reference, allocator) {}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
|
|
|
|
template <class C, class T, class A>
|
|
|
|
|
buffer(const ::std::basic_string<C, T, A> &str, bool make_reference,
|
|
|
|
|
const allocator_type &allocator = allocator_type())
|
2020-09-01 18:37:52 +08:00
|
|
|
|
: buffer(::mdbx::slice(str), make_reference, allocator) {}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
|
|
|
|
buffer(const char *c_str, bool make_reference,
|
|
|
|
|
const allocator_type &allocator = allocator_type())
|
2020-09-01 18:37:52 +08:00
|
|
|
|
: buffer(::mdbx::slice(c_str), make_reference, allocator) {}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class C, class T>
|
|
|
|
|
buffer(const ::std::basic_string_view<C, T> &view, bool make_reference,
|
|
|
|
|
const allocator_type &allocator = allocator_type())
|
2020-09-01 18:37:52 +08:00
|
|
|
|
: buffer(::mdbx::slice(view), make_reference, allocator) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#endif /* __cpp_lib_string_view >= 201606L */
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_CONSTEXPR
|
|
|
|
|
buffer(const ::mdbx::slice &src,
|
|
|
|
|
const allocator_type &allocator = allocator_type())
|
2020-08-24 16:20:41 +08:00
|
|
|
|
: silo_(src.char_ptr(), src.length(), allocator), slice_(silo_) {}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_CONSTEXPR
|
|
|
|
|
buffer(const buffer &src, const allocator_type &allocator = allocator_type())
|
2020-08-24 16:20:41 +08:00
|
|
|
|
: buffer(src.slice_, allocator) {}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_CONSTEXPR
|
|
|
|
|
buffer(const void *ptr, size_t bytes,
|
|
|
|
|
const allocator_type &allocator = allocator_type())
|
2020-09-01 18:37:52 +08:00
|
|
|
|
: buffer(::mdbx::slice(ptr, bytes), allocator) {}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
|
|
|
|
template <class C, class T, class A>
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_CONSTEXPR
|
|
|
|
|
buffer(const ::std::basic_string<C, T, A> &str,
|
|
|
|
|
const allocator_type &allocator = allocator_type())
|
2020-09-01 18:37:52 +08:00
|
|
|
|
: buffer(::mdbx::slice(str), allocator) {}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_CONSTEXPR
|
|
|
|
|
buffer(const char *c_str, const allocator_type &allocator = allocator_type())
|
2020-09-01 18:37:52 +08:00
|
|
|
|
: buffer(::mdbx::slice(c_str), allocator) {}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class C, class T>
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_CONSTEXPR
|
|
|
|
|
buffer(const ::std::basic_string_view<C, T> &view,
|
|
|
|
|
const allocator_type &allocator = allocator_type())
|
2020-09-01 18:37:52 +08:00
|
|
|
|
: buffer(::mdbx::slice(view), allocator) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#endif /* __cpp_lib_string_view >= 201606L */
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
inline buffer(size_t head_room, size_t tail_room,
|
|
|
|
|
const allocator_type &allocator = allocator_type());
|
|
|
|
|
|
|
|
|
|
inline buffer(size_t capacity,
|
|
|
|
|
const allocator_type &allocator = allocator_type());
|
|
|
|
|
|
2020-09-01 18:37:52 +08:00
|
|
|
|
inline buffer(size_t head_room, const ::mdbx::slice &src, size_t tail_room,
|
2020-08-24 16:20:41 +08:00
|
|
|
|
const allocator_type &allocator = allocator_type());
|
|
|
|
|
|
|
|
|
|
buffer(size_t head_room, const buffer &src, size_t tail_room,
|
|
|
|
|
const allocator_type &allocator = allocator_type())
|
|
|
|
|
: buffer(head_room, src.slice_, tail_room, allocator) {}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_CONSTEXPR
|
2020-08-24 16:20:41 +08:00
|
|
|
|
buffer(const allocator_type &allocator = allocator_type()) noexcept
|
|
|
|
|
: silo_(allocator) {}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline buffer(const txn &txn, const ::mdbx::slice &src,
|
2020-08-24 16:20:41 +08:00
|
|
|
|
const allocator_type &allocator = allocator_type());
|
|
|
|
|
|
|
|
|
|
buffer(buffer &&src) noexcept
|
|
|
|
|
: silo_(::std::move(src.silo_)), slice_(::std::move(src.slice_)) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
buffer(silo &&str) noexcept : silo_(::std::move(str)), slice_(silo_) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const ::mdbx::slice &slice() const noexcept {
|
|
|
|
|
return slice_;
|
|
|
|
|
}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR operator const ::mdbx::slice &() const noexcept {
|
2020-09-13 23:59:33 +08:00
|
|
|
|
return slice_;
|
|
|
|
|
}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
|
|
|
|
template <typename POD>
|
|
|
|
|
static buffer wrap(const POD &pod, bool make_reference = false,
|
|
|
|
|
const allocator_type &allocator = allocator_type()) {
|
2020-09-01 18:37:52 +08:00
|
|
|
|
return buffer(::mdbx::slice::wrap(pod), make_reference, allocator);
|
2020-08-24 16:20:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Reserves storage.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
inline void reserve(size_t wanna_headroom, size_t wanna_tailroom,
|
2020-09-14 20:24:17 +08:00
|
|
|
|
size_t shrink_threshold = default_shrink_threshold);
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
|
|
|
|
buffer &assign_reference(const void *ptr, size_t bytes) noexcept {
|
|
|
|
|
silo_.clear();
|
|
|
|
|
slice_.assign(ptr, bytes);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer &assign_freestanding(const void *ptr, size_t bytes) {
|
|
|
|
|
silo_.assign(static_cast<const typename silo::value_type *>(ptr),
|
|
|
|
|
check_length(bytes));
|
|
|
|
|
slice_.assign(silo_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void swap(buffer &other)
|
|
|
|
|
#if defined(__cpp_noexcept_function_type) && \
|
|
|
|
|
__cpp_noexcept_function_type >= 201510L
|
|
|
|
|
noexcept(
|
|
|
|
|
std::allocator_traits<ALLOCATOR>::propagate_on_container_swap::value
|
|
|
|
|
#if defined(__cpp_lib_allocator_traits_is_always_equal) && \
|
|
|
|
|
__cpp_lib_allocator_traits_is_always_equal >= 201411L
|
|
|
|
|
|| std::allocator_traits<ALLOCATOR>::is_always_equal::value
|
|
|
|
|
#endif /* __cpp_lib_allocator_traits_is_always_equal */
|
|
|
|
|
)
|
|
|
|
|
#endif /* __cpp_noexcept_function_type */
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
buffer &assign(buffer &&src)
|
|
|
|
|
#if defined(__cpp_noexcept_function_type) && \
|
|
|
|
|
__cpp_noexcept_function_type >= 201510L
|
|
|
|
|
noexcept(std::allocator_traits<
|
|
|
|
|
ALLOCATOR>::propagate_on_container_move_assignment::value
|
|
|
|
|
#if defined(__cpp_lib_allocator_traits_is_always_equal) && \
|
|
|
|
|
__cpp_lib_allocator_traits_is_always_equal >= 201411L
|
|
|
|
|
|| std::allocator_traits<ALLOCATOR>::is_always_equal::value
|
|
|
|
|
#endif /* __cpp_lib_allocator_traits_is_always_equal */
|
|
|
|
|
)
|
|
|
|
|
#endif /* __cpp_noexcept_function_type */
|
|
|
|
|
{
|
|
|
|
|
silo_.assign(::std::move(src.silo_));
|
|
|
|
|
slice_.assign(::std::move(src.slice_));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer &assign(silo &&src)
|
|
|
|
|
#if defined(__cpp_noexcept_function_type) && \
|
|
|
|
|
__cpp_noexcept_function_type >= 201510L
|
|
|
|
|
noexcept(std::allocator_traits<
|
|
|
|
|
ALLOCATOR>::propagate_on_container_move_assignment::value
|
|
|
|
|
#if defined(__cpp_lib_allocator_traits_is_always_equal) && \
|
|
|
|
|
__cpp_lib_allocator_traits_is_always_equal >= 201411L
|
|
|
|
|
|| std::allocator_traits<ALLOCATOR>::is_always_equal::value
|
|
|
|
|
#endif /* __cpp_lib_allocator_traits_is_always_equal */
|
|
|
|
|
)
|
|
|
|
|
#endif /* __cpp_noexcept_function_type */
|
|
|
|
|
{
|
|
|
|
|
return assign(buffer(::std::move(src)));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
static buffer clone(const buffer &src,
|
|
|
|
|
const allocator_type &allocator = allocator_type()) {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return buffer(src.headroom(), src.slice_, src.tailroom(), allocator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer &assign(const buffer &src, bool make_reference = false) {
|
|
|
|
|
return assign(src.slice_, make_reference);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer &assign(const void *ptr, size_t bytes, bool make_reference = false) {
|
|
|
|
|
return make_reference ? assign_reference(ptr, bytes)
|
|
|
|
|
: assign_freestanding(ptr, bytes);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 18:37:52 +08:00
|
|
|
|
buffer &assign(const ::mdbx::slice &src, bool make_reference = false) {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return assign(src.data(), src.length(), make_reference);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer &assign(const ::MDBX_val &src, bool make_reference = false) {
|
|
|
|
|
return assign(src.iov_base, src.iov_len, make_reference);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 18:37:52 +08:00
|
|
|
|
buffer &assign(::mdbx::slice &&src, bool make_reference = false) {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
assign(src.data(), src.length(), make_reference);
|
2020-09-07 06:54:36 +08:00
|
|
|
|
src.invalidate();
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer &assign(::MDBX_val &&src, bool make_reference = false) {
|
|
|
|
|
assign(src.iov_base, src.iov_len, make_reference);
|
|
|
|
|
src.iov_base = nullptr;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer &assign(const void *begin, const void *end,
|
|
|
|
|
bool make_reference = false) {
|
|
|
|
|
return assign(begin,
|
|
|
|
|
static_cast<const byte *>(end) -
|
|
|
|
|
static_cast<const byte *>(begin),
|
|
|
|
|
make_reference);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class C, class T, class A>
|
|
|
|
|
buffer &assign(const ::std::basic_string<C, T, A> &str,
|
|
|
|
|
bool make_reference = false) {
|
|
|
|
|
return assign(str.data(), str.length(), make_reference);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer &assign(const char *c_str, bool make_reference = false) {
|
|
|
|
|
return assign(c_str, ::mdbx::strlen(c_str), make_reference);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L
|
|
|
|
|
template <class C, class T>
|
|
|
|
|
buffer &assign(const ::std::basic_string_view<C, T> &view,
|
|
|
|
|
bool make_reference = false) {
|
|
|
|
|
return assign(view.data(), view.length(), make_reference);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class C, class T>
|
|
|
|
|
buffer &assign(::std::basic_string_view<C, T> &&view,
|
|
|
|
|
bool make_reference = false) {
|
|
|
|
|
assign(view.data(), view.length(), make_reference);
|
|
|
|
|
view = {};
|
|
|
|
|
return *this;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
#endif /* __cpp_lib_string_view >= 201606L */
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
buffer &operator=(const buffer &src) { return assign(src); }
|
|
|
|
|
|
|
|
|
|
buffer &operator=(buffer &&src) noexcept { return assign(::std::move(src)); }
|
|
|
|
|
|
|
|
|
|
buffer &operator=(silo &&src) noexcept { return assign(::std::move(src)); }
|
|
|
|
|
|
2020-09-01 18:37:52 +08:00
|
|
|
|
buffer &operator=(const ::mdbx::slice &src) { return assign(src); }
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-09-01 18:37:52 +08:00
|
|
|
|
buffer &operator=(::mdbx::slice &&src) { return assign(::std::move(src)); }
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class C, class T>
|
|
|
|
|
buffer &operator=(const ::std::basic_string_view<C, T> &view) noexcept {
|
|
|
|
|
return assign(view);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Return a string_view that references the data of this buffer.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class C = char, class T = ::std::char_traits<C>>
|
|
|
|
|
::std::basic_string_view<C, T> string_view() const noexcept {
|
|
|
|
|
return slice_.string_view<C, T>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Return a string_view that references the data of this buffer.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class C, class T>
|
|
|
|
|
operator ::std::basic_string_view<C, T>() const noexcept {
|
|
|
|
|
return string_view<C, T>();
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#endif /* __cpp_lib_string_view >= 201606L */
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Decodes hexadecimal dump from the given slice to the returned
|
|
|
|
|
/// buffer.
|
2020-09-01 18:37:52 +08:00
|
|
|
|
static buffer decode_hex(const ::mdbx::slice &hex,
|
2020-08-24 16:20:41 +08:00
|
|
|
|
const allocator_type &allocator = allocator_type()) {
|
|
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
|
return buffer(hex.hex_decode(allocator));
|
|
|
|
|
#else
|
|
|
|
|
silo data(hex.hex_decode(allocator));
|
|
|
|
|
return buffer(::std::move(data));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns a buffer with a hexadecimal dump of the given slice.
|
2020-09-01 18:37:52 +08:00
|
|
|
|
static buffer encode_hex(const ::mdbx::slice &data, bool uppercase = false,
|
2020-08-24 16:20:41 +08:00
|
|
|
|
const allocator_type &allocator = allocator_type()) {
|
|
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
|
return buffer(data.hex_encode(uppercase, allocator));
|
|
|
|
|
#else
|
|
|
|
|
silo hex(data.hex_encode(uppercase, allocator));
|
|
|
|
|
return buffer(::std::move(hex));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Decodes [Base58](https://en.wikipedia.org/wiki/Base58) dump from
|
|
|
|
|
/// the given slice to the returned buffer.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
static buffer
|
2020-09-01 18:37:52 +08:00
|
|
|
|
decode_base58(const ::mdbx::slice &base58,
|
2020-08-24 16:20:41 +08:00
|
|
|
|
const allocator_type &allocator = allocator_type()) {
|
|
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
|
return buffer(base58.base58_decode(allocator));
|
|
|
|
|
#else
|
|
|
|
|
silo data(base58.base58_decode(allocator));
|
|
|
|
|
return buffer(::std::move(data));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns a buffer with a
|
|
|
|
|
/// [Base58](https://en.wikipedia.org/wiki/Base58) dump of the given slice.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
static buffer
|
2020-09-01 18:37:52 +08:00
|
|
|
|
encode_base58(const ::mdbx::slice &data,
|
2020-08-24 16:20:41 +08:00
|
|
|
|
const allocator_type &allocator = allocator_type()) {
|
|
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
|
return buffer(data.base58_encode(allocator));
|
|
|
|
|
#else
|
|
|
|
|
silo base58(data.base58_encode(allocator));
|
|
|
|
|
return buffer(::std::move(base58));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Decodes [Base64](https://en.wikipedia.org/wiki/Base64) dump from
|
|
|
|
|
/// the given slice to the returned buffer.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
static buffer
|
2020-09-01 18:37:52 +08:00
|
|
|
|
decode_base64(const ::mdbx::slice &base64,
|
2020-08-24 16:20:41 +08:00
|
|
|
|
const allocator_type &allocator = allocator_type()) {
|
|
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
|
return buffer(base64.base64_decode(allocator));
|
|
|
|
|
#else
|
|
|
|
|
silo data(base64.base64_decode(allocator));
|
|
|
|
|
return buffer(::std::move(data));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns a buffer with a
|
|
|
|
|
/// [Base64](https://en.wikipedia.org/wiki/Base64) dump of the given slice.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
static buffer
|
2020-09-01 18:37:52 +08:00
|
|
|
|
encode_base64(const ::mdbx::slice &data,
|
2020-08-24 16:20:41 +08:00
|
|
|
|
const allocator_type &allocator = allocator_type()) {
|
|
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
|
return buffer(data.base64_encode(allocator));
|
|
|
|
|
#else
|
|
|
|
|
silo base64(data.base64_encode(allocator));
|
|
|
|
|
return buffer(::std::move(base64));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Checks whether the string is empty.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR bool empty() const noexcept {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return length() == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Checks whether the data pointer of the buffer is nullptr.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool is_null() const noexcept {
|
|
|
|
|
return data() == nullptr;
|
|
|
|
|
}
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the number of bytes.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX20_CONSTEXPR size_t size() const noexcept {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return length();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the hash value of the data.
|
|
|
|
|
/// \attention Function implementation and returned hash values may changed
|
|
|
|
|
/// version to version, and in future the t1ha3 will be used here. Therefore
|
|
|
|
|
/// values obtained from this function shouldn't be persisted anywhere.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX14_CONSTEXPR size_t
|
|
|
|
|
hash_value() const noexcept {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return slice_.hash_value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class C = char, class T = ::std::char_traits<C>,
|
2020-09-01 18:28:29 +08:00
|
|
|
|
class A = legacy_allocator>
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_CONSTEXPR ::std::basic_string<C, T, A>
|
2020-08-24 16:20:41 +08:00
|
|
|
|
string(const A &allocator = A()) const {
|
|
|
|
|
return slice_.string<C, T, A>(allocator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class C, class T, class A>
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_CONSTEXPR operator ::std::basic_string<C, T, A>() const {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return this->string<C, T, A>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Checks if the data starts with the given prefix.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION bool
|
2020-09-01 18:37:52 +08:00
|
|
|
|
starts_with(const ::mdbx::slice &prefix) const noexcept {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return slice_.starts_with(prefix);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Checks if the data ends with the given suffix.
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION bool
|
2020-09-01 18:37:52 +08:00
|
|
|
|
ends_with(const ::mdbx::slice &suffix) const noexcept {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return slice_.ends_with(suffix);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Clears the contents and storage.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
void clear() noexcept {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
slice_.clear();
|
2020-08-24 16:20:41 +08:00
|
|
|
|
silo_.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Reduces memory usage by freeing unused storage space.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
void shrink_to_fit(size_t threshold = 64) { reserve(0, 0, threshold); }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Drops the first "n" bytes from the data chunk.
|
|
|
|
|
/// \pre REQUIRES: `n <= size()`
|
2020-08-31 17:24:12 +08:00
|
|
|
|
void remove_prefix(size_t n) noexcept { slice_.remove_prefix(n); }
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Drops the last "n" bytes from the data chunk.
|
|
|
|
|
/// \pre REQUIRES: `n <= size()`
|
2020-08-31 17:24:12 +08:00
|
|
|
|
void remove_suffix(size_t n) noexcept { slice_.remove_suffix(n); }
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Drops the first "n" bytes from the data chunk.
|
|
|
|
|
/// \throws std::out_of_range if `n > size()`
|
2020-08-31 17:24:12 +08:00
|
|
|
|
void safe_remove_prefix(size_t n) { slice_.safe_remove_prefix(n); }
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Drops the last "n" bytes from the data chunk.
|
|
|
|
|
/// \throws std::out_of_range if `n > size()`
|
2020-08-31 17:24:12 +08:00
|
|
|
|
void safe_remove_suffix(size_t n) { slice_.safe_remove_suffix(n); }
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Accesses the specified byte of data chunk.
|
|
|
|
|
/// \pre REQUIRES: `n < size()`
|
|
|
|
|
byte operator[](size_t n) const noexcept { return slice_[n]; }
|
|
|
|
|
|
|
|
|
|
/// \brief Accesses the specified byte of data chunk.
|
|
|
|
|
/// \pre REQUIRES: `n < size()`
|
|
|
|
|
byte &operator[](size_t n) noexcept {
|
|
|
|
|
assert(n < size());
|
|
|
|
|
return byte_ptr()[n];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief Accesses the specified byte of data chunk with bounds checking.
|
|
|
|
|
/// \throws std::out_of_range if `n >= size()`
|
|
|
|
|
byte at(size_t n) const { return slice_.at(n); }
|
|
|
|
|
|
|
|
|
|
/// \brief Accesses the specified byte of data chunk with bounds checking.
|
|
|
|
|
/// \throws std::out_of_range if `n >= size()`
|
|
|
|
|
byte &at(size_t n) {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(n >= size()))
|
|
|
|
|
MDBX_CXX20_UNLIKELY throw_out_range();
|
2020-09-07 06:54:36 +08:00
|
|
|
|
return byte_ptr()[n];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief Returns the first "n" bytes of the data chunk.
|
|
|
|
|
/// \pre REQUIRES: `n <= size()`
|
2020-09-01 18:37:52 +08:00
|
|
|
|
::mdbx::slice head(size_t n) const noexcept { return slice_.head(n); }
|
2020-08-31 17:24:12 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the last "n" bytes of the data chunk.
|
|
|
|
|
/// \pre REQUIRES: `n <= size()`
|
2020-09-01 18:37:52 +08:00
|
|
|
|
::mdbx::slice tail(size_t n) const noexcept { return slice_.tail(n); }
|
2020-08-31 17:24:12 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the middle "n" bytes of the data chunk.
|
|
|
|
|
/// \pre REQUIRES: `from + n <= size()`
|
2020-09-01 18:37:52 +08:00
|
|
|
|
::mdbx::slice middle(size_t from, size_t n) const noexcept {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return slice_.middle(from, n);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the first "n" bytes of the data chunk.
|
|
|
|
|
/// \throws std::out_of_range if `n >= size()`
|
2020-09-01 18:37:52 +08:00
|
|
|
|
::mdbx::slice safe_head(size_t n) const { return slice_.safe_head(n); }
|
2020-08-31 17:24:12 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the last "n" bytes of the data chunk.
|
|
|
|
|
/// \throws std::out_of_range if `n >= size()`
|
2020-09-01 18:37:52 +08:00
|
|
|
|
::mdbx::slice safe_tail(size_t n) const { return slice_.safe_tail(n); }
|
2020-08-31 17:24:12 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Returns the middle "n" bytes of the data chunk.
|
|
|
|
|
/// \throws std::out_of_range if `from + n >= size()`
|
2020-09-01 18:37:52 +08:00
|
|
|
|
::mdbx::slice safe_middle(size_t from, size_t n) const {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return slice_.safe_middle(from, n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline buffer &append(const void *src, size_t bytes);
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
buffer &append(const ::mdbx::slice &chunk) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return append(chunk.data(), chunk.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline buffer &add_header(const void *src, size_t bytes);
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
buffer &add_header(const ::mdbx::slice &chunk) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return add_header(chunk.data(), chunk.size());
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
template <size_t SIZE>
|
|
|
|
|
static buffer key_from(const char (&text)[SIZE], bool make_reference = true) {
|
2020-09-01 18:37:52 +08:00
|
|
|
|
return buffer(::mdbx::slice(text), make_reference);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(DOXYGEN) || \
|
|
|
|
|
(defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L)
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class C, class T>
|
|
|
|
|
static buffer key_from(const ::std::basic_string_view<C, T> &src,
|
|
|
|
|
bool make_reference = false) {
|
|
|
|
|
return buffer(src, make_reference);
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#endif /* __cpp_lib_string_view >= 201606L */
|
2020-08-24 16:20:41 +08:00
|
|
|
|
|
|
|
|
|
static buffer key_from(const char *src, bool make_reference = false) {
|
|
|
|
|
return buffer(src, make_reference);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class C, class T, class A>
|
|
|
|
|
static buffer key_from(const ::std::basic_string<C, T, A> &src,
|
|
|
|
|
bool make_reference = false) {
|
|
|
|
|
return buffer(src, make_reference);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static buffer key_from(const silo &&src) noexcept {
|
|
|
|
|
return buffer(::std::move(src));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static buffer key_from(const double ieee754_64bit) {
|
|
|
|
|
return wrap(::mdbx_key_from_double(ieee754_64bit));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static buffer key_from(const double *ieee754_64bit) {
|
|
|
|
|
return wrap(::mdbx_key_from_ptrdouble(ieee754_64bit));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static buffer key_from(const uint64_t unsigned_int64) {
|
|
|
|
|
return wrap(unsigned_int64);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static buffer key_from(const int64_t signed_int64) {
|
|
|
|
|
return wrap(::mdbx_key_from_int64(signed_int64));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static buffer key_from_jsonInteger(const int64_t json_integer) {
|
|
|
|
|
return wrap(::mdbx_key_from_jsonInteger(json_integer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static buffer key_from(const float ieee754_32bit) {
|
|
|
|
|
return wrap(::mdbx_key_from_float(ieee754_32bit));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static buffer key_from(const float *ieee754_32bit) {
|
|
|
|
|
return wrap(::mdbx_key_from_ptrfloat(ieee754_32bit));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static buffer key_from(const uint32_t unsigned_int32) {
|
|
|
|
|
return wrap(unsigned_int32);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static buffer key_from(const int32_t signed_int32) {
|
|
|
|
|
return wrap(::mdbx_key_from_int32(signed_int32));
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Combines data slice with boolean flag to represent result of certain
|
|
|
|
|
/// operations.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
struct value_result {
|
|
|
|
|
slice value;
|
|
|
|
|
bool done;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
value_result(const slice &value, bool done) noexcept
|
|
|
|
|
: value(value), done(done) {}
|
|
|
|
|
value_result(const value_result &) noexcept = default;
|
|
|
|
|
value_result &operator=(const value_result &) noexcept = default;
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR operator bool() const noexcept {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
assert(!done || bool(value));
|
|
|
|
|
return done;
|
|
|
|
|
}
|
2020-08-31 17:24:12 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Combines pair of slices for key and value to represent result of
|
|
|
|
|
/// certain operations.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
struct pair {
|
2020-09-10 06:15:35 +08:00
|
|
|
|
slice key, value;
|
|
|
|
|
pair(const slice &key, const slice &value) noexcept
|
|
|
|
|
: key(key), value(value) {}
|
|
|
|
|
pair(const pair &) noexcept = default;
|
|
|
|
|
pair &operator=(const pair &) noexcept = default;
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR operator bool() const noexcept {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
assert(bool(key) == bool(value));
|
|
|
|
|
return key;
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Combines pair of slices for key and value with boolean flag to
|
|
|
|
|
/// represent result of certain operations.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
struct pair_result : public pair {
|
|
|
|
|
bool done;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
pair_result(const slice &key, const slice &value, bool done) noexcept
|
|
|
|
|
: pair(key, value), done(done) {}
|
|
|
|
|
pair_result(const pair_result &) noexcept = default;
|
|
|
|
|
pair_result &operator=(const pair_result &) noexcept = default;
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR operator bool() const noexcept {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
assert(!done || (bool(key) && bool(value)));
|
|
|
|
|
return done;
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Loop control constants for readers enumeration functor and other
|
|
|
|
|
/// cases. \see env::enumerate_readers()
|
|
|
|
|
enum loop_control { continue_loop = 0, exit_loop = INT32_MIN };
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Kinds of the keys and corresponding modes of comparing it.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
enum class key_mode {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
usual = MDBX_DB_DEFAULTS, ///< Usual variable length keys with byte-by-byte
|
|
|
|
|
///< lexicographic comparison like `std::memcmp()`.
|
|
|
|
|
reverse = MDBX_REVERSEKEY, ///< Variable length keys with byte-by-byte
|
|
|
|
|
///< lexicographic comparison in reverse order,
|
|
|
|
|
///< from the end of the keys to the beginning.
|
|
|
|
|
ordinal = MDBX_INTEGERKEY, ///< Keys are binary integers in native byte order,
|
|
|
|
|
///< either `uint32_t` or `uint64_t`, and will be
|
|
|
|
|
///< sorted as such. The keys must all be of the
|
|
|
|
|
///< same size and must be aligned while passing
|
|
|
|
|
///< as arguments.
|
|
|
|
|
msgpack = -1 ///< Keys are in [MessagePack](https://msgpack.org/)
|
|
|
|
|
///< format with appropriate comparison.
|
|
|
|
|
///< \note Not yet implemented and PRs are welcome.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Kind of the values and sorted multi-values with corresponding
|
|
|
|
|
/// comparison.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
enum class value_mode {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
single = MDBX_DB_DEFAULTS, ///< Usual single value for each key. In terms of
|
|
|
|
|
///< keys, they are unique.
|
|
|
|
|
multi =
|
|
|
|
|
MDBX_DUPSORT, ///< A more than one data value could be associated with
|
|
|
|
|
///< each key. Internally each key is stored once, and the
|
|
|
|
|
///< corresponding data values are sorted by byte-by-byte
|
|
|
|
|
///< lexicographic comparison like `std::memcmp()`.
|
|
|
|
|
///< In terms of keys, they are not unique, i.e. has
|
|
|
|
|
///< duplicates which are sorted by associated data values.
|
2020-10-11 01:07:00 +08:00
|
|
|
|
#if CONSTEXPR_ENUM_FLAGS_OPERATIONS || defined(DOXYGEN)
|
2020-09-07 06:54:36 +08:00
|
|
|
|
multi_reverse =
|
|
|
|
|
MDBX_DUPSORT |
|
|
|
|
|
MDBX_REVERSEDUP, ///< A more than one data value could be associated with
|
|
|
|
|
///< each key. Internally each key is stored once, and
|
|
|
|
|
///< the corresponding data values are sorted by
|
|
|
|
|
///< byte-by-byte lexicographic comparison in reverse
|
|
|
|
|
///< order, from the end of the keys to the beginning.
|
|
|
|
|
///< In terms of keys, they are not unique, i.e. has
|
|
|
|
|
///< duplicates which are sorted by associated data
|
|
|
|
|
///< values.
|
|
|
|
|
multi_samelength =
|
|
|
|
|
MDBX_DUPSORT |
|
|
|
|
|
MDBX_DUPFIXED, ///< A more than one data value could be associated with
|
|
|
|
|
///< each key, and all data values must be same length.
|
|
|
|
|
///< Internally each key is stored once, and the
|
|
|
|
|
///< corresponding data values are sorted by byte-by-byte
|
|
|
|
|
///< lexicographic comparison like `std::memcmp()`. In
|
|
|
|
|
///< terms of keys, they are not unique, i.e. has
|
|
|
|
|
///< duplicates which are sorted by associated data values.
|
|
|
|
|
multi_ordinal =
|
|
|
|
|
MDBX_DUPSORT | MDBX_DUPFIXED |
|
|
|
|
|
MDBX_INTEGERDUP, ///< A more than one data value could be associated with
|
|
|
|
|
///< each key, and all data values are binary integers in
|
|
|
|
|
///< native byte order, either `uint32_t` or `uint64_t`,
|
|
|
|
|
///< and will be sorted as such. Internally each key is
|
|
|
|
|
///< stored once, and the corresponding data values are
|
|
|
|
|
///< sorted. In terms of keys, they are not unique, i.e.
|
|
|
|
|
///< has duplicates which are sorted by associated data
|
|
|
|
|
///< values.
|
|
|
|
|
multi_reverse_samelength =
|
|
|
|
|
MDBX_DUPSORT | MDBX_REVERSEDUP |
|
|
|
|
|
MDBX_DUPFIXED, ///< A more than one data value could be associated with
|
|
|
|
|
///< each key, and all data values must be same length.
|
|
|
|
|
///< Internally each key is stored once, and the
|
|
|
|
|
///< corresponding data values are sorted by byte-by-byte
|
|
|
|
|
///< lexicographic comparison in reverse order, from the
|
|
|
|
|
///< end of the keys to the beginning. In terms of keys,
|
|
|
|
|
///< they are not unique, i.e. has duplicates which are
|
|
|
|
|
///< sorted by associated data values.
|
|
|
|
|
msgpack = -1 ///< A more than one data value could be associated with each
|
|
|
|
|
///< key. Values are in [MessagePack](https://msgpack.org/)
|
|
|
|
|
///< format with appropriate comparison. Internally each key is
|
|
|
|
|
///< stored once, and the corresponding data values are sorted.
|
|
|
|
|
///< In terms of keys, they are not unique, i.e. has duplicates
|
|
|
|
|
///< which are sorted by associated data values.
|
|
|
|
|
///< \note Not yet implemented and PRs are welcome.
|
2020-10-11 01:07:00 +08:00
|
|
|
|
#else
|
|
|
|
|
multi_reverse = uint32_t(MDBX_DUPSORT) | uint32_t(MDBX_REVERSEDUP),
|
|
|
|
|
multi_samelength = uint32_t(MDBX_DUPSORT) | uint32_t(MDBX_DUPFIXED),
|
|
|
|
|
multi_ordinal = uint32_t(MDBX_DUPSORT) | uint32_t(MDBX_DUPFIXED) |
|
|
|
|
|
uint32_t(MDBX_INTEGERDUP),
|
|
|
|
|
multi_reverse_samelength = uint32_t(MDBX_DUPSORT) |
|
|
|
|
|
uint32_t(MDBX_REVERSEDUP) | uint32_t(MDBX_DUPFIXED)
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief A handle for an individual database (key-value spaces) in the
|
|
|
|
|
/// environment.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \see txn::open_map() \see txn::create_map()
|
|
|
|
|
/// \see txn::clear_map() \see txn::drop_map()
|
|
|
|
|
/// \see txn::get_handle_info() \see txn::get_map_stat()
|
|
|
|
|
/// \see env::close_amp()
|
|
|
|
|
/// \see cursor::map()
|
2020-08-23 01:19:46 +08:00
|
|
|
|
struct LIBMDBX_API_TYPE map_handle {
|
|
|
|
|
MDBX_dbi dbi{0};
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR map_handle() noexcept {}
|
|
|
|
|
MDBX_CXX11_CONSTEXPR map_handle(MDBX_dbi dbi) noexcept : dbi(dbi) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
map_handle(const map_handle &) noexcept = default;
|
|
|
|
|
map_handle &operator=(const map_handle &) noexcept = default;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
operator bool() const noexcept { return dbi != 0; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
using flags = ::MDBX_db_flags_t;
|
|
|
|
|
using state = ::MDBX_dbi_state_t;
|
|
|
|
|
struct LIBMDBX_API_TYPE info {
|
|
|
|
|
map_handle::flags flags;
|
|
|
|
|
map_handle::state state;
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR info(map_handle::flags flags,
|
|
|
|
|
map_handle::state state) noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
info(const info &) noexcept = default;
|
|
|
|
|
info &operator=(const info &) noexcept = default;
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR ::mdbx::key_mode key_mode() const noexcept;
|
|
|
|
|
MDBX_CXX11_CONSTEXPR ::mdbx::value_mode value_mode() const noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Key-value pairs put mode.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
enum put_mode {
|
2020-09-10 06:15:35 +08:00
|
|
|
|
insert = MDBX_NOOVERWRITE, ///< Insert only unique keys.
|
|
|
|
|
upsert = MDBX_UPSERT, ///< Insert or update.
|
|
|
|
|
update = MDBX_CURRENT, ///< Update existing, don't insert new.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Unmanaged database environment.
|
|
|
|
|
///
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// Like other unmanaged classes, `env` allows copying and assignment for
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// instances, but does not destroys the represented underlying object from the
|
|
|
|
|
/// own class destructor.
|
|
|
|
|
///
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// An environment supports multiple key-value sub-databases (aka key-value
|
|
|
|
|
/// spaces or tables), all residing in the same shared-memory map.
|
|
|
|
|
class LIBMDBX_API_TYPE env {
|
|
|
|
|
friend class txn;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
MDBX_env *handle_{nullptr};
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR env(MDBX_env *ptr) noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
public:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR env() noexcept = default;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
env(const env &) noexcept = default;
|
|
|
|
|
inline env &operator=(env &&other) noexcept;
|
|
|
|
|
inline env(env &&other) noexcept;
|
|
|
|
|
inline ~env() noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR operator bool() const noexcept;
|
|
|
|
|
MDBX_CXX14_CONSTEXPR operator const MDBX_env *() const;
|
|
|
|
|
MDBX_CXX14_CONSTEXPR operator MDBX_env *();
|
|
|
|
|
friend MDBX_CXX11_CONSTEXPR bool operator==(const env &a,
|
|
|
|
|
const env &b) noexcept;
|
|
|
|
|
friend MDBX_CXX11_CONSTEXPR bool operator!=(const env &a,
|
|
|
|
|
const env &b) noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// Database geometry for size management.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
struct LIBMDBX_API_TYPE geometry {
|
2020-09-01 06:59:21 +08:00
|
|
|
|
enum : int64_t {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
default_value = -1, ///< Means "keep current or use default"
|
|
|
|
|
minimal_value = 0, ///< Means "minimal acceptable"
|
2020-09-26 23:28:09 +08:00
|
|
|
|
maximal_value = INTPTR_MAX, ///< Means "maximal acceptable"
|
2020-09-07 06:54:36 +08:00
|
|
|
|
kB = 1000, ///< \f$10^{3}\f$ bytes
|
|
|
|
|
MB = kB * 1000, ///< \f$10^{6}\f$ bytes
|
|
|
|
|
GB = MB * 1000, ///< \f$10^{9}\f$ bytes
|
|
|
|
|
TB = GB * 1000, ///< \f$10^{12}\f$ bytes
|
|
|
|
|
PB = TB * 1000, ///< \f$10^{15}\f$ bytes
|
|
|
|
|
EB = PB * 1000, ///< \f$10^{18}\f$ bytes
|
|
|
|
|
KiB = 1024, ///< \f$2^{10}\f$ bytes
|
|
|
|
|
MiB = KiB << 10, ///< \f$2^{20}\f$ bytes
|
|
|
|
|
GiB = MiB << 10, ///< \f$2^{30}\f$ bytes
|
|
|
|
|
TiB = GiB << 10, ///< \f$2^{40}\f$ bytes
|
|
|
|
|
PiB = TiB << 10, ///< \f$2^{50}\f$ bytes
|
|
|
|
|
EiB = PiB << 10, ///< \f$2^{60}\f$ bytes
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
2020-09-01 06:59:21 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Tagged type for output to std::ostream
|
2020-09-01 06:59:21 +08:00
|
|
|
|
struct size {
|
|
|
|
|
intptr_t bytes;
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR size(intptr_t bytes) noexcept : bytes(bytes) {}
|
|
|
|
|
MDBX_CXX11_CONSTEXPR operator intptr_t() const noexcept { return bytes; }
|
2020-09-01 06:59:21 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief The lower bound of database size in bytes.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
intptr_t size_lower{minimal_value};
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief The size in bytes to setup the database size for now.
|
|
|
|
|
/// \details It is recommended always pass \ref default_value in this
|
|
|
|
|
/// argument except some special cases.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
intptr_t size_now{default_value};
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief The upper bound of database size in bytes.
|
|
|
|
|
/// \details It is recommended to avoid change upper bound while database is
|
|
|
|
|
/// used by other processes or threaded (i.e. just pass \ref default_value
|
|
|
|
|
/// in this argument except absolutely necessary). Otherwise you must be
|
|
|
|
|
/// ready for \ref MDBX_UNABLE_EXTEND_MAPSIZE error(s), unexpected pauses
|
|
|
|
|
/// during remapping and/or system errors like "address busy", and so on. In
|
|
|
|
|
/// other words, there is no way to handle a growth of the upper bound
|
|
|
|
|
/// robustly because there may be a lack of appropriate system resources
|
|
|
|
|
/// (which are extremely volatile in a multi-process multi-threaded
|
|
|
|
|
/// environment).
|
2020-08-23 01:19:46 +08:00
|
|
|
|
intptr_t size_upper{maximal_value};
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief The growth step in bytes, must be greater than zero to allow the
|
|
|
|
|
/// database to grow.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
intptr_t growth_step{default_value};
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief The shrink threshold in bytes, must be greater than zero to allow
|
|
|
|
|
/// the database to shrink.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
intptr_t shrink_threshold{default_value};
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief The database page size for new database creation
|
|
|
|
|
/// or \ref default_value otherwise.
|
|
|
|
|
/// \details Must be power of 2 in the range between \ref MDBX_MIN_PAGESIZE
|
|
|
|
|
/// and \ref MDBX_MAX_PAGESIZE.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
intptr_t pagesize{default_value};
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline geometry &make_fixed(intptr_t size) noexcept;
|
|
|
|
|
inline geometry &make_dynamic(intptr_t lower = minimal_value,
|
|
|
|
|
intptr_t upper = maximal_value) noexcept;
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Operation mode.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
enum mode {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
readonly, ///< \copydoc MDBX_RDONLY
|
|
|
|
|
write_file_io, // don't available on OpenBSD
|
|
|
|
|
write_mapped_io ///< \copydoc MDBX_WRITEMAP
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Durability level.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
enum durability {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
robust_synchronous, ///< \copydoc MDBX_SYNC_DURABLE
|
|
|
|
|
half_synchronous_weak_last, ///< \copydoc MDBX_NOMETASYNC
|
|
|
|
|
lazy_weak_tail, ///< \copydoc MDBX_SAFE_NOSYNC
|
|
|
|
|
whole_fragile ///< \copydoc MDBX_UTTERLY_NOSYNC
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Garbage reclaiming options.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
struct LIBMDBX_API_TYPE reclaiming_options {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \copydoc MDBX_LIFORECLAIM
|
2020-08-23 01:19:46 +08:00
|
|
|
|
bool lifo{false};
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \copydoc MDBX_COALESCE
|
2020-08-23 01:19:46 +08:00
|
|
|
|
bool coalesce{false};
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR reclaiming_options() noexcept {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
reclaiming_options(MDBX_env_flags_t) noexcept;
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Operate options.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
struct LIBMDBX_API_TYPE operate_options {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \copydoc MDBX_NOTLS
|
2020-08-23 01:19:46 +08:00
|
|
|
|
bool orphan_read_transactions{false};
|
|
|
|
|
bool nested_write_transactions{false};
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \copydoc MDBX_EXCLUSIVE
|
2020-08-23 01:19:46 +08:00
|
|
|
|
bool exclusive{false};
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \copydoc MDBX_NORDAHEAD
|
2020-08-23 01:19:46 +08:00
|
|
|
|
bool disable_readahead{false};
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \copydoc MDBX_NOMEMINIT
|
2020-08-23 01:19:46 +08:00
|
|
|
|
bool disable_clear_memory{false};
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR operate_options() noexcept {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
operate_options(MDBX_env_flags_t) noexcept;
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Operate parameters.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
struct LIBMDBX_API_TYPE operate_parameters {
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief The maximum number of named databases for the environment.
|
|
|
|
|
/// Zero means default value.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
unsigned max_maps{0};
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief The maximum number of threads/reader slots for the environment.
|
|
|
|
|
/// Zero means default value.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
unsigned max_readers{0};
|
2020-09-10 06:15:35 +08:00
|
|
|
|
env::mode mode{write_mapped_io};
|
|
|
|
|
env::durability durability{robust_synchronous};
|
|
|
|
|
env::reclaiming_options reclaiming;
|
|
|
|
|
env::operate_options options;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR operate_parameters() noexcept {}
|
2020-09-07 06:54:36 +08:00
|
|
|
|
MDBX_env_flags_t make_flags(bool accede = true, ///< \copydoc MDBX_ACCEDE
|
2020-08-23 01:19:46 +08:00
|
|
|
|
bool use_subdirectory = false) const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
static env::mode mode_from_flags(MDBX_env_flags_t) noexcept;
|
|
|
|
|
static env::durability durability_from_flags(MDBX_env_flags_t) noexcept;
|
|
|
|
|
inline static env::reclaiming_options
|
2020-08-23 01:19:46 +08:00
|
|
|
|
reclaiming_from_flags(MDBX_env_flags_t flags) noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline static env::operate_options
|
2020-08-23 01:19:46 +08:00
|
|
|
|
options_from_flags(MDBX_env_flags_t flags) noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
operate_parameters(const env &);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns current operation parameters.
|
|
|
|
|
inline env::operate_parameters get_operation_parameters() const;
|
|
|
|
|
/// \brief Returns current operation mode.
|
|
|
|
|
inline env::mode get_mode() const;
|
|
|
|
|
/// \brief Returns current durability mode.
|
|
|
|
|
inline env::durability get_durability() const;
|
|
|
|
|
/// \brief Returns current reclaiming options.
|
|
|
|
|
inline env::reclaiming_options get_reclaiming() const;
|
|
|
|
|
/// \brief Returns current operate options.
|
|
|
|
|
inline env::operate_options get_options() const;
|
|
|
|
|
|
|
|
|
|
/// \brief Returns `true` for a freshly created database,
|
|
|
|
|
/// but `false` if at least one transaction was committed.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
bool is_pristine() const;
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Checks whether the database is empty.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
bool is_empty() const;
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns default page size for current system/platform.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
static size_t default_pagesize() noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
struct limits {
|
|
|
|
|
limits() = delete;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the minimal database page size in bytes.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
static inline size_t pagesize_min() noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the maximal database page size in bytes.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
static inline size_t pagesize_max() noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the minimal database size in bytes for specified page
|
|
|
|
|
/// size.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
static inline size_t dbsize_min(intptr_t pagesize);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the maximal database size in bytes for specified page
|
|
|
|
|
/// size.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
static inline size_t dbsize_max(intptr_t pagesize);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the minimal key size in bytes for specified database
|
|
|
|
|
/// flags.
|
2020-09-07 06:54:36 +08:00
|
|
|
|
static inline size_t key_min(MDBX_db_flags_t flags) noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the minimal key size in bytes for specified keys mode.
|
2020-09-07 06:54:36 +08:00
|
|
|
|
static inline size_t key_min(key_mode mode) noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the maximal key size in bytes for specified page size and
|
|
|
|
|
/// database flags.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
static inline size_t key_max(intptr_t pagesize, MDBX_db_flags_t flags);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the maximal key size in bytes for specified page size and
|
|
|
|
|
/// keys mode.
|
2020-09-07 06:54:36 +08:00
|
|
|
|
static inline size_t key_max(intptr_t pagesize, key_mode mode);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the maximal key size in bytes for given environment and
|
|
|
|
|
/// database flags.
|
|
|
|
|
static inline size_t key_max(const env &, MDBX_db_flags_t flags);
|
|
|
|
|
/// \brief Returns the maximal key size in bytes for given environment and
|
|
|
|
|
/// keys mode.
|
|
|
|
|
static inline size_t key_max(const env &, key_mode mode);
|
|
|
|
|
/// \brief Returns the minimal values size in bytes for specified database
|
|
|
|
|
/// flags.
|
2020-09-07 06:54:36 +08:00
|
|
|
|
static inline size_t value_min(MDBX_db_flags_t flags) noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the minimal values size in bytes for specified values
|
|
|
|
|
/// mode.
|
|
|
|
|
static inline size_t value_min(value_mode) noexcept;
|
|
|
|
|
/// \brief Returns the maximal value size in bytes for specified page size
|
|
|
|
|
/// and database flags.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
static inline size_t value_max(intptr_t pagesize, MDBX_db_flags_t flags);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the maximal value size in bytes for specified page size
|
|
|
|
|
/// and values mode.
|
|
|
|
|
static inline size_t value_max(intptr_t pagesize, value_mode);
|
|
|
|
|
/// \brief Returns the maximal value size in bytes for given environment and
|
|
|
|
|
/// database flags.
|
|
|
|
|
static inline size_t value_max(const env &, MDBX_db_flags_t flags);
|
|
|
|
|
/// \brief Returns the maximal value size in bytes for specified page size
|
|
|
|
|
/// and values mode.
|
|
|
|
|
static inline size_t value_max(const env &, value_mode);
|
|
|
|
|
/// \brief Returns the maximal write transaction size (i.e. limit for
|
|
|
|
|
/// summary volume of dirty pages) in bytes for specified page size.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
static inline size_t transaction_size_max(intptr_t pagesize);
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the minimal database size in bytes for the environment.
|
|
|
|
|
size_t dbsize_min() const { return limits::dbsize_min(this->get_pagesize()); }
|
|
|
|
|
/// \brief Returns the maximal database size in bytes for the environment.
|
|
|
|
|
size_t dbsize_max() const { return limits::dbsize_max(this->get_pagesize()); }
|
|
|
|
|
/// \brief Returns the minimal key size in bytes for specified keys mode.
|
|
|
|
|
size_t key_min(key_mode mode) const noexcept { return limits::key_min(mode); }
|
|
|
|
|
/// \brief Returns the maximal key size in bytes for specified keys mode.
|
|
|
|
|
size_t key_max(key_mode mode) const { return limits::key_max(*this, mode); }
|
|
|
|
|
/// \brief Returns the minimal value size in bytes for specified values mode.
|
|
|
|
|
size_t value_min(value_mode mode) const noexcept {
|
|
|
|
|
return limits::value_min(mode);
|
|
|
|
|
}
|
|
|
|
|
/// \brief Returns the maximal value size in bytes for specified values mode.
|
|
|
|
|
size_t value_max(value_mode mode) const {
|
|
|
|
|
return limits::value_max(*this, mode);
|
|
|
|
|
}
|
|
|
|
|
/// \brief Returns the maximal write transaction size (i.e. limit for summary
|
|
|
|
|
/// volume of dirty pages) in bytes.
|
|
|
|
|
size_t transaction_size_max() const {
|
|
|
|
|
return limits::transaction_size_max(this->get_pagesize());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief Make a copy (backup) of an existing environment to the specified
|
|
|
|
|
/// path.
|
|
|
|
|
env ©(const path &destination, bool compactify,
|
|
|
|
|
bool force_dynamic_size = false);
|
|
|
|
|
|
|
|
|
|
/// \brief Copy an environment to the specified file descriptor.
|
|
|
|
|
env ©(filehandle fd, bool compactify, bool force_dynamic_size = false);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-10-10 23:31:15 +08:00
|
|
|
|
/// \brief Deletion modes for \ref remove().
|
|
|
|
|
enum remove_mode {
|
|
|
|
|
/// \brief Just delete the environment's files and directory if any.
|
|
|
|
|
/// \note On POSIX systems, processes already working with the database will
|
|
|
|
|
/// continue to work without interference until it close the environment.
|
|
|
|
|
/// \note On Windows, the behavior of `just_remove` is different
|
|
|
|
|
/// because the system does not support deleting files that are currently
|
|
|
|
|
/// memory mapped.
|
|
|
|
|
just_remove = MDBX_ENV_JUST_DELETE,
|
|
|
|
|
/// \brief Make sure that the environment is not being used by other
|
|
|
|
|
/// processes, or return an error otherwise.
|
|
|
|
|
ensure_unused = MDBX_ENV_ENSURE_UNUSED,
|
|
|
|
|
/// \brief Wait until other processes closes the environment before
|
|
|
|
|
/// deletion.
|
|
|
|
|
wait_for_unused = MDBX_ENV_WAIT_FOR_UNUSED
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// \brief Removes the environment's files in a proper and multiprocess-safe
|
|
|
|
|
/// way.
|
|
|
|
|
static bool remove(const path &, const remove_mode mode = just_remove);
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Statistics for a database in the MDBX environment.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
using stat = ::MDBX_stat;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Information about the environment.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
using info = ::MDBX_envinfo;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns snapshot statistics about the MDBX environment.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline stat get_stat() const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns pagesize of this MDBX environment.
|
|
|
|
|
size_t get_pagesize() const { return get_stat().ms_psize; }
|
|
|
|
|
|
|
|
|
|
/// \brief Return snapshot information about the MDBX environment.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline info get_info() const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Return statistics about the MDBX environment accordingly to the
|
|
|
|
|
/// specified transaction.
|
|
|
|
|
inline stat get_stat(const txn &) const;
|
|
|
|
|
|
|
|
|
|
/// \brief Return information about the MDBX environment accordingly to the
|
|
|
|
|
/// specified transaction.
|
|
|
|
|
inline info get_info(const txn &) const;
|
|
|
|
|
|
|
|
|
|
/// \brief Returns the file descriptor for the DXB file of MDBX environment.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline filehandle get_filehandle() const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Return the path that was used for opening the environment.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
path get_path() const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// Returns environment flags.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline MDBX_env_flags_t get_flags() const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the maximum number of threads/reader slots for the
|
|
|
|
|
/// environment.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline unsigned max_readers() const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the maximum number of named databases for the environment.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline unsigned max_maps() const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Returns the application context associated with the environment.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline void *get_context() const noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Sets the application context associated with the environment.
|
|
|
|
|
inline env &set_context(void *);
|
|
|
|
|
|
|
|
|
|
/// \brief Sets threshold to force flush the data buffers to disk, for
|
|
|
|
|
/// non-sync durability modes.
|
|
|
|
|
///
|
|
|
|
|
/// The threshold value affects all processes which operates with given
|
|
|
|
|
/// environment until the last process close environment or a new value will
|
|
|
|
|
/// be settled.
|
|
|
|
|
/// Data is always written to disk when \ref txn_managed::commit() is called,
|
|
|
|
|
/// but the operating system may keep it buffered. MDBX always flushes the OS
|
|
|
|
|
/// buffers upon commit as well, unless the environment was opened with \ref
|
|
|
|
|
/// whole_fragile, \ref lazy_weak_tail or in part \ref
|
|
|
|
|
/// half_synchronous_weak_last. The default is 0, than mean no any threshold
|
|
|
|
|
/// checked, and no additional flush will be made.
|
|
|
|
|
///
|
|
|
|
|
inline env &set_sync_threshold(size_t bytes);
|
|
|
|
|
|
2020-09-26 23:28:09 +08:00
|
|
|
|
/// \brief Sets relative period since the last unsteady commit to force flush
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// the data buffers to disk, for non-sync durability modes.
|
|
|
|
|
///
|
|
|
|
|
/// The relative period value affects all processes which operates with given
|
|
|
|
|
/// environment until the last process close environment or a new value will
|
|
|
|
|
/// be settled.
|
|
|
|
|
/// Data is always written to disk when \ref txn_managed::commit() is called,
|
|
|
|
|
/// but the operating system may keep it buffered. MDBX always flushes the OS
|
|
|
|
|
/// buffers upon commit as well, unless the environment was opened with \ref
|
|
|
|
|
/// whole_fragile, \ref lazy_weak_tail or in part \ref
|
|
|
|
|
/// half_synchronous_weak_last. Settled period don't checked asynchronously,
|
|
|
|
|
/// but only by the \ref txn_managed::commit() and \ref env::sync_to_disk()
|
|
|
|
|
/// functions. Therefore, in cases where transactions are committed
|
|
|
|
|
/// infrequently and/or irregularly, polling by \ref env::poll_sync_to_disk()
|
|
|
|
|
/// may be a reasonable solution to timeout enforcement. The default is 0,
|
|
|
|
|
/// than mean no any timeout checked, and no additional flush will be made.
|
|
|
|
|
///
|
|
|
|
|
/// \param [in] seconds_16dot16 The period in 1/65536 of second when a
|
2020-09-26 23:28:09 +08:00
|
|
|
|
/// synchronous flush would be made since the last unsteady commit.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env &set_sync_period(unsigned seconds_16dot16);
|
|
|
|
|
|
2020-09-26 23:28:09 +08:00
|
|
|
|
/// \brief Sets relative period since the last unsteady commit to force flush
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// the data buffers to disk, for non-sync durability modes.
|
|
|
|
|
///
|
|
|
|
|
/// The relative period value affects all processes which operates with given
|
|
|
|
|
/// environment until the last process close environment or a new value will
|
|
|
|
|
/// be settled.
|
|
|
|
|
/// Data is always written to disk when \ref txn_managed::commit() is called,
|
|
|
|
|
/// but the operating system may keep it buffered. MDBX always flushes the OS
|
|
|
|
|
/// buffers upon commit as well, unless the environment was opened with \ref
|
|
|
|
|
/// whole_fragile, \ref lazy_weak_tail or in part \ref
|
|
|
|
|
/// half_synchronous_weak_last. Settled period don't checked asynchronously,
|
|
|
|
|
/// but only by the \ref txn_managed::commit() and \ref env::sync_to_disk()
|
|
|
|
|
/// functions. Therefore, in cases where transactions are committed
|
|
|
|
|
/// infrequently and/or irregularly, polling by \ref env::poll_sync_to_disk()
|
|
|
|
|
/// may be a reasonable solution to timeout enforcement. The default is 0,
|
|
|
|
|
/// than mean no any timeout checked, and no additional flush will be made.
|
|
|
|
|
///
|
|
|
|
|
/// \param [in] seconds The period in second when a synchronous flush would
|
2020-09-26 23:28:09 +08:00
|
|
|
|
/// be made since the last unsteady commit.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env &set_sync_period(double seconds);
|
|
|
|
|
|
|
|
|
|
/// \brief Alter environment flags.
|
|
|
|
|
inline env &alter_flags(MDBX_env_flags_t flags, bool on_off);
|
|
|
|
|
|
|
|
|
|
/// \brief Set all size-related parameters of environment.
|
|
|
|
|
inline env &set_geometry(const geometry &size);
|
|
|
|
|
|
|
|
|
|
/// \brief Flush the environment data buffers.
|
|
|
|
|
/// \return `True` if sync done or no data to sync, or `false` if the
|
|
|
|
|
/// environment is busy by other thread or none of the thresholds are reached.
|
|
|
|
|
inline bool sync_to_disk(bool force = true, bool nonblock = false);
|
|
|
|
|
|
|
|
|
|
/// \brief Performs non-blocking polling of sync-to-disk thresholds.
|
|
|
|
|
/// \return `True` if sync done or no data to sync, or `false` if the
|
|
|
|
|
/// environment is busy by other thread or none of the thresholds are reached.
|
|
|
|
|
bool poll_sync_to_disk() { return sync_to_disk(false, true); }
|
|
|
|
|
|
|
|
|
|
/// \brief Close a key-value map (aka sub-database) handle. Normally
|
|
|
|
|
/// unnecessary.
|
|
|
|
|
///
|
|
|
|
|
/// Closing a database handle is not necessary, but lets \ref txn::open_map()
|
|
|
|
|
/// reuse the handle value. Usually it's better to set a bigger
|
|
|
|
|
/// \ref env::operate_parameters::max_maps, unless that value would be
|
|
|
|
|
/// large.
|
|
|
|
|
///
|
|
|
|
|
/// \note Use with care.
|
|
|
|
|
/// This call is synchronized via mutex with other calls \ref close_map(), but
|
|
|
|
|
/// NOT with other transactions running by other threads. The "next" version
|
2020-09-30 01:14:08 +08:00
|
|
|
|
/// of libmdbx (\ref MithrilDB) will solve this issue.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
///
|
|
|
|
|
/// Handles should only be closed if no other threads are going to reference
|
|
|
|
|
/// the database handle or one of its cursors any further. Do not close a
|
|
|
|
|
/// handle if an existing transaction has modified its database. Doing so can
|
|
|
|
|
/// cause misbehavior from database corruption to errors like
|
|
|
|
|
/// \ref MDBX_BAD_DBI (since the DB name is gone).
|
|
|
|
|
inline void close_map(const map_handle &);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-10-20 20:42:50 +08:00
|
|
|
|
/// \brief Reader information
|
2020-08-23 01:19:46 +08:00
|
|
|
|
struct reader_info {
|
2020-09-10 06:15:35 +08:00
|
|
|
|
int slot; ///< The reader lock table slot number.
|
|
|
|
|
mdbx_pid_t pid; ///< The reader process ID.
|
|
|
|
|
mdbx_tid_t thread; ///< The reader thread ID.
|
|
|
|
|
uint64_t transaction_id; ///< The ID of the transaction being read,
|
2020-09-26 23:28:09 +08:00
|
|
|
|
///< i.e. the MVCC-snapshot number.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
uint64_t transaction_lag; ///< The lag from a recent MVCC-snapshot,
|
|
|
|
|
///< i.e. the number of committed write
|
|
|
|
|
/// transactions since the current read
|
|
|
|
|
/// transaction started.
|
|
|
|
|
size_t bytes_used; ///< The number of last used page in the MVCC-snapshot
|
|
|
|
|
///< which being read, i.e. database file can't shrinked
|
|
|
|
|
///< beyond this.
|
|
|
|
|
size_t bytes_retained; ///< The total size of the database pages that
|
|
|
|
|
///< were retired by committed write transactions
|
|
|
|
|
///< after the reader's MVCC-snapshot, i.e. the space
|
|
|
|
|
///< which would be freed after the Reader releases
|
|
|
|
|
///< the MVCC-snapshot for reuse by completion read
|
|
|
|
|
///< transaction.
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR reader_info(int slot, mdbx_pid_t pid,
|
|
|
|
|
mdbx_tid_t thread, uint64_t txnid,
|
|
|
|
|
uint64_t lag, size_t used,
|
|
|
|
|
size_t retained) noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Enumerate readers.
|
|
|
|
|
///
|
|
|
|
|
/// The VISITOR class must have `int operator(const reader_info&, int serial)`
|
|
|
|
|
/// which should return \ref continue_loop (zero) to continue enumeration,
|
|
|
|
|
/// or any non-zero value to exit.
|
|
|
|
|
///
|
|
|
|
|
/// \returns The last value returned from visitor' functor.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
template <typename VISITOR> inline int enumerate_readers(VISITOR &visitor);
|
|
|
|
|
|
2020-09-26 23:28:09 +08:00
|
|
|
|
/// \brief Checks for stale readers in the lock table and
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// return number of cleared slots.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline unsigned check_readers();
|
|
|
|
|
|
2020-09-30 00:24:57 +08:00
|
|
|
|
/// \brief Sets a Handle-Slow-Readers callback to resolve database
|
|
|
|
|
/// full/overflow issue due to a reader(s) which prevents the old data from
|
|
|
|
|
/// being recycled.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
///
|
|
|
|
|
/// Such callback will be triggered in a case where there is not enough free
|
|
|
|
|
/// space in the database due to long read transaction(s) which impedes
|
2020-09-26 23:28:09 +08:00
|
|
|
|
/// reusing the pages of an old MVCC snapshot(s).
|
2020-09-10 06:15:35 +08:00
|
|
|
|
///
|
2020-09-30 00:24:57 +08:00
|
|
|
|
/// Using this callback you can choose how to resolve the situation:
|
|
|
|
|
/// - abort the write transaction with an error;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// - wait for the read transaction(s) to complete;
|
|
|
|
|
/// - notify a thread performing a long-lived read transaction
|
|
|
|
|
/// and wait for an effect;
|
|
|
|
|
/// - kill the thread or whole process that performs the long-lived read
|
|
|
|
|
/// transaction;
|
|
|
|
|
///
|
|
|
|
|
/// \see long-lived-read
|
2020-09-30 00:24:57 +08:00
|
|
|
|
inline env &set_HandleSlowReaders(MDBX_hsr_func *);
|
|
|
|
|
|
|
|
|
|
/// \brief Returns the current Handle-Slow-Readers callback used to resolve
|
|
|
|
|
/// database full/overflow issue due to a reader(s) which prevents the old
|
|
|
|
|
/// data from being recycled.
|
|
|
|
|
/// \see set_HandleSlowReaders()
|
|
|
|
|
inline MDBX_hsr_func *get_HandleSlowReaders() const noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Starts read (read-only) transaction.
|
|
|
|
|
inline txn_managed start_read() const;
|
2020-09-30 01:14:08 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Creates but not start read transaction.
|
|
|
|
|
inline txn_managed prepare_read() const;
|
2020-09-30 01:14:08 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Starts write (read-write) transaction.
|
|
|
|
|
inline txn_managed start_write(bool dont_wait = false);
|
2020-09-30 01:14:08 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Tries to start write (read-write) transaction without blocking.
|
|
|
|
|
inline txn_managed try_start_write();
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Managed database environment.
|
|
|
|
|
///
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// As other managed classes, `env_managed` destroys the represented underlying
|
|
|
|
|
/// object from the own class destructor, but disallows copying and assignment
|
|
|
|
|
/// for instances.
|
2020-09-07 06:54:36 +08:00
|
|
|
|
///
|
|
|
|
|
/// An environment supports multiple key-value databases (aka key-value spaces
|
|
|
|
|
/// or tables), all residing in the same shared-memory map.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
class LIBMDBX_API_TYPE env_managed : public env {
|
|
|
|
|
using inherited = env;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
/// delegated constructor for RAII
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR env_managed(MDBX_env *ptr) noexcept : inherited(ptr) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
void setup(unsigned max_maps, unsigned max_readers = 0);
|
|
|
|
|
|
|
|
|
|
public:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR env_managed() noexcept = default;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Open existing database.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
env_managed(const path &, const operate_parameters &, bool accede = true);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Additional parameters for creating a new database.
|
2020-09-07 06:54:36 +08:00
|
|
|
|
struct create_parameters {
|
2020-09-10 06:15:35 +08:00
|
|
|
|
env::geometry geometry;
|
2020-09-07 06:54:36 +08:00
|
|
|
|
mdbx_mode_t file_mode_bits{0640};
|
|
|
|
|
bool use_subdirectory{false};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// \brief Create new or open existing database.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
env_managed(const path &, const create_parameters &,
|
|
|
|
|
const operate_parameters &, bool accede = true);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Explicitly closes the environment and release the memory map.
|
|
|
|
|
///
|
|
|
|
|
/// Only a single thread may call this function. All transactions, databases,
|
|
|
|
|
/// and cursors must already be closed before calling this function. Attempts
|
|
|
|
|
/// to use any such handles after calling this function will cause a
|
|
|
|
|
/// `SIGSEGV`. The environment handle will be freed and must not be used again
|
|
|
|
|
/// after this call.
|
|
|
|
|
///
|
|
|
|
|
/// \param [in] dont_sync A dont'sync flag, if non-zero the last checkpoint
|
|
|
|
|
/// will be kept "as is" and may be still "weak" in the \ref lazy_weak_tail
|
|
|
|
|
/// or \ref whole_fragile modes. Such "weak" checkpoint will be ignored
|
|
|
|
|
/// on opening next time, and transactions since the last non-weak checkpoint
|
|
|
|
|
/// (meta-page update) will rolledback for consistency guarantee.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
void close(bool dont_sync = false);
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
env_managed(env_managed &&) = default;
|
|
|
|
|
env_managed &operator=(env_managed &&) = default;
|
|
|
|
|
env_managed(const env_managed &) = delete;
|
|
|
|
|
env_managed &operator=(const env_managed &) = delete;
|
|
|
|
|
virtual ~env_managed() noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Unmanaged database transaction.
|
|
|
|
|
///
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// Like other unmanaged classes, `txn` allows copying and assignment for
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// instances, but does not destroys the represented underlying object from the
|
|
|
|
|
/// own class destructor.
|
|
|
|
|
///
|
|
|
|
|
/// All database operations require a transaction handle. Transactions may be
|
|
|
|
|
/// read-only or read-write.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
class LIBMDBX_API_TYPE txn {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
protected:
|
2020-09-10 06:15:35 +08:00
|
|
|
|
friend class cursor;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
MDBX_txn *handle_{nullptr};
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR txn(MDBX_txn *ptr) noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
public:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR txn() noexcept = default;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
txn(const txn &) noexcept = default;
|
|
|
|
|
inline txn &operator=(txn &&other) noexcept;
|
|
|
|
|
inline txn(txn &&other) noexcept;
|
|
|
|
|
inline ~txn() noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR operator bool() const noexcept;
|
|
|
|
|
MDBX_CXX14_CONSTEXPR operator const MDBX_txn *() const;
|
|
|
|
|
MDBX_CXX14_CONSTEXPR operator MDBX_txn *();
|
|
|
|
|
friend MDBX_CXX11_CONSTEXPR bool operator==(const txn &a,
|
|
|
|
|
const txn &b) noexcept;
|
|
|
|
|
friend MDBX_CXX11_CONSTEXPR bool operator!=(const txn &a,
|
|
|
|
|
const txn &b) noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns the transaction's environment.
|
|
|
|
|
inline ::mdbx::env env() const noexcept;
|
|
|
|
|
/// \brief Returns transaction's flags.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline MDBX_txn_flags_t flags() const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Return the transaction's ID.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline uint64_t id() const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Checks whether the given data is on a dirty page.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline bool is_dirty(const void *ptr) const;
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Checks whether the transaction is read-only.
|
|
|
|
|
bool is_readonly() const { return (flags() & MDBX_TXN_RDONLY) != 0; }
|
|
|
|
|
|
|
|
|
|
/// \brief Checks whether the transaction is read-write.
|
|
|
|
|
bool is_readwrite() const { return (flags() & MDBX_TXN_RDONLY) == 0; }
|
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
using info = ::MDBX_txn_info;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns information about the MDBX transaction.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline info get_info(bool scan_reader_lock_table = false) const;
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns maximal write transaction size (i.e. limit for summary
|
|
|
|
|
/// volume of dirty pages) in bytes.
|
|
|
|
|
size_t size_max() const { return env().transaction_size_max(); }
|
|
|
|
|
|
|
|
|
|
/// \brief Returns current write transaction size (i.e.summary volume of dirty
|
|
|
|
|
/// pages) in bytes.
|
|
|
|
|
size_t size_current() const {
|
|
|
|
|
assert(is_readwrite());
|
2020-09-13 23:06:14 +08:00
|
|
|
|
return size_t(get_info().txn_space_dirty);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Reset a read-only transaction.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline void reset_reading();
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Renew a read-only transaction.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline void renew_reading();
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Start nested write transaction.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
txn_managed start_nested();
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-15 07:32:04 +08:00
|
|
|
|
/// \brief Opens cursor for specified key-value map handle.
|
|
|
|
|
inline cursor_managed open_cursor(map_handle map);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Open existing key-value map.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline map_handle open_map(
|
|
|
|
|
const char *name,
|
|
|
|
|
const ::mdbx::key_mode key_mode = ::mdbx::key_mode::usual,
|
2020-09-07 06:54:36 +08:00
|
|
|
|
const ::mdbx::value_mode value_mode = ::mdbx::value_mode::single) const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Open existing key-value map.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline map_handle open_map(
|
|
|
|
|
const ::std::string &name,
|
|
|
|
|
const ::mdbx::key_mode key_mode = ::mdbx::key_mode::usual,
|
2020-09-07 06:54:36 +08:00
|
|
|
|
const ::mdbx::value_mode value_mode = ::mdbx::value_mode::single) const;
|
|
|
|
|
|
|
|
|
|
/// \brief Create new or open existing key-value map.
|
|
|
|
|
inline map_handle
|
|
|
|
|
create_map(const char *name,
|
|
|
|
|
const ::mdbx::key_mode key_mode = ::mdbx::key_mode::usual,
|
|
|
|
|
const ::mdbx::value_mode value_mode = ::mdbx::value_mode::single);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Create new or open existing key-value map.
|
2020-09-07 06:54:36 +08:00
|
|
|
|
inline map_handle
|
|
|
|
|
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);
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Drops key-value map using handle.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline void drop_map(map_handle map);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Drops key-value map using name.
|
|
|
|
|
/// \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 char *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.
|
|
|
|
|
inline bool drop_map(const ::std::string &name, bool throw_if_absent = false);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Clear key-value map.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline void clear_map(map_handle map);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \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 char *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.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline bool clear_map(const ::std::string &name,
|
2020-09-10 06:15:35 +08:00
|
|
|
|
bool throw_if_absent = false);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
using map_stat = ::MDBX_stat;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns statistics for a sub-database.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline map_stat get_map_stat(map_handle map) const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns depth (bitmask) information of nested dupsort (multi-value)
|
|
|
|
|
/// B+trees for given database.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline uint32_t get_tree_deepmask(map_handle map) const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Returns information about key-value map (aka sub-database) handle.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline map_handle::info get_handle_info(map_handle map) const;
|
|
|
|
|
|
|
|
|
|
using canary = ::MDBX_canary;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Set integers markers (aka "canary") associated with the
|
|
|
|
|
/// environment.
|
|
|
|
|
inline txn &put_canary(const canary &);
|
|
|
|
|
/// \brief Returns fours integers markers (aka "canary") associated with the
|
|
|
|
|
/// environment.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline canary get_canary() const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
|
|
|
|
/// Reads sequence generator associated with a key-value map (aka
|
|
|
|
|
/// sub-database).
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline uint64_t sequence(map_handle map) const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Reads and increment sequence generator associated with a key-value
|
|
|
|
|
/// map (aka sub-database).
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline uint64_t sequence(map_handle map, uint64_t increment);
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Compare two keys according to a particular key-value map (aka
|
|
|
|
|
/// sub-database).
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline int compare_keys(map_handle map, const slice &a,
|
|
|
|
|
const slice &b) const noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Compare two values according to a particular key-value map (aka
|
|
|
|
|
/// sub-database).
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline int compare_values(map_handle map, const slice &a,
|
|
|
|
|
const slice &b) const noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Compare keys of two pairs according to a particular key-value map
|
|
|
|
|
/// (aka sub-database).
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline int compare_keys(map_handle map, const pair &a,
|
|
|
|
|
const pair &b) const noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Compare values of two pairs according to a particular key-value map
|
|
|
|
|
/// (aka sub-database).
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline int compare_values(map_handle map, const pair &a,
|
|
|
|
|
const pair &b) const noexcept;
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Get value by key from a key-value map (aka sub-database).
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline slice get(map_handle map, const slice &key) const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Get first of multi-value and values count by key from a key-value
|
|
|
|
|
/// multimap (aka sub-database).
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline slice get(map_handle map, slice key, size_t &values_count) const;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Get value by key from a key-value map (aka sub-database).
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline slice get(map_handle map, const slice &key,
|
2020-09-10 06:15:35 +08:00
|
|
|
|
const slice &value_at_absence) const;
|
|
|
|
|
/// \brief Get first of multi-value and values count by key from a key-value
|
|
|
|
|
/// multimap (aka sub-database).
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline slice get(map_handle map, slice key, size_t &values_count,
|
2020-09-10 06:15:35 +08:00
|
|
|
|
const slice &value_at_absence) const;
|
|
|
|
|
/// \brief Get value for equal or great key from a database.
|
|
|
|
|
/// \return Bundle of key-value pair and boolean flag,
|
|
|
|
|
/// which will be `true` if the exact key was found and `false` otherwise.
|
|
|
|
|
inline pair_result get_equal_or_great(map_handle map, const slice &key) const;
|
|
|
|
|
/// \brief Get value for equal or great key from a database.
|
|
|
|
|
/// \return Bundle of key-value pair and boolean flag,
|
|
|
|
|
/// which will be `true` if the exact key was found and `false` otherwise.
|
|
|
|
|
inline pair_result get_equal_or_great(map_handle map, const slice &key,
|
|
|
|
|
const slice &value_at_absence) const;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline MDBX_error_t put(map_handle map, const slice &key, slice *value,
|
|
|
|
|
MDBX_put_flags_t flags) noexcept;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void put(map_handle map, const slice &key, slice value,
|
|
|
|
|
put_mode mode) noexcept;
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline void insert(map_handle map, const slice &key, slice value);
|
|
|
|
|
inline value_result try_insert(map_handle map, const slice &key, slice value);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline slice insert_reserve(map_handle map, const slice &key,
|
|
|
|
|
size_t value_length);
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline value_result try_insert_reserve(map_handle map, const slice &key,
|
|
|
|
|
size_t value_length);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
inline void upsert(map_handle map, const slice &key, const slice &value);
|
|
|
|
|
inline slice upsert_reserve(map_handle map, const slice &key,
|
|
|
|
|
size_t value_length);
|
|
|
|
|
|
|
|
|
|
inline void update(map_handle map, const slice &key, const slice &value);
|
|
|
|
|
inline bool try_update(map_handle map, const slice &key, const slice &value);
|
|
|
|
|
inline slice update_reserve(map_handle map, const slice &key,
|
|
|
|
|
size_t value_length);
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline value_result try_update_reserve(map_handle map, const slice &key,
|
|
|
|
|
size_t value_length);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
inline bool erase(map_handle map, const slice &key);
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Removes the particular multi-value entry of the key.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline bool erase(map_handle map, const slice &key, const slice &value);
|
2020-09-07 06:54:36 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Replaces the particular multi-value of the key with a new value.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline void replace(map_handle map, const slice &key, slice old_value,
|
|
|
|
|
const slice &new_value);
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Removes and return a value of the key.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline buffer<ALLOCATOR> extract(map_handle map, const slice &key,
|
|
|
|
|
const ALLOCATOR &allocator = ALLOCATOR());
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Replaces and returns a value of the key with new one.
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline buffer<ALLOCATOR> replace(map_handle map, const slice &key,
|
|
|
|
|
const slice &new_value,
|
|
|
|
|
const ALLOCATOR &allocator = ALLOCATOR());
|
|
|
|
|
|
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline buffer<ALLOCATOR>
|
|
|
|
|
replace_reserve(map_handle map, const slice &key, slice &new_value,
|
|
|
|
|
const ALLOCATOR &allocator = ALLOCATOR());
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Adding a key-value pair, provided that ascending order of the keys
|
|
|
|
|
/// and (optionally) values are preserved.
|
2020-08-31 17:24:12 +08:00
|
|
|
|
///
|
|
|
|
|
/// Instead of splitting the full b+tree pages, the data will be placed on new
|
|
|
|
|
/// ones. Thus appending is about two times faster than insertion, and the
|
|
|
|
|
/// pages will be filled in completely mostly but not half as after splitting
|
|
|
|
|
/// ones. On the other hand, any subsequent insertion or update with an
|
|
|
|
|
/// increase in the length of the value will be twice as slow, since it will
|
|
|
|
|
/// require splitting already filled pages.
|
|
|
|
|
///
|
|
|
|
|
/// \param [in] multivalue_order_preserved
|
|
|
|
|
/// If `multivalue_order_preserved == true` then the same rules applied for
|
|
|
|
|
/// to pages of nested b+tree of multimap's values.
|
|
|
|
|
inline void append(map_handle map, const slice &key, const slice &value,
|
|
|
|
|
bool multivalue_order_preserved = true);
|
|
|
|
|
|
|
|
|
|
size_t put_multiple(map_handle map, const slice &key,
|
|
|
|
|
const size_t value_length, const void *values_array,
|
|
|
|
|
size_t values_count, put_mode mode,
|
|
|
|
|
bool allow_partial = false);
|
|
|
|
|
template <typename VALUE>
|
|
|
|
|
void put_multiple(map_handle map, const slice &key,
|
|
|
|
|
const std::vector<VALUE> &vector, put_mode mode) {
|
|
|
|
|
put_multiple(map, key, sizeof(VALUE), vector.data(), vector.size(), mode,
|
|
|
|
|
false);
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
inline ptrdiff_t estimate(map_handle map, pair from, pair to) const;
|
|
|
|
|
inline ptrdiff_t estimate(map_handle map, slice from, slice to) const;
|
|
|
|
|
inline ptrdiff_t estimate_from_first(map_handle map, slice to) const;
|
|
|
|
|
inline ptrdiff_t estimate_to_last(map_handle map, slice from) const;
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Managed database transaction.
|
|
|
|
|
///
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// As other managed classes, `txn_managed` destroys the represented underlying
|
|
|
|
|
/// object from the own class destructor, but disallows copying and assignment
|
|
|
|
|
/// for instances.
|
2020-09-07 06:54:36 +08:00
|
|
|
|
///
|
|
|
|
|
/// All database operations require a transaction handle. Transactions may be
|
|
|
|
|
/// read-only or read-write.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
class LIBMDBX_API_TYPE txn_managed : public txn {
|
|
|
|
|
using inherited = txn;
|
|
|
|
|
friend class env;
|
|
|
|
|
friend class txn;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
/// delegated constructor for RAII
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR txn_managed(MDBX_txn *ptr) noexcept : inherited(ptr) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
public:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR txn_managed() noexcept = default;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
txn_managed(txn_managed &&) = default;
|
|
|
|
|
txn_managed &operator=(txn_managed &&) = default;
|
|
|
|
|
txn_managed(const txn_managed &) = delete;
|
|
|
|
|
txn_managed &operator=(const txn_managed &) = delete;
|
|
|
|
|
~txn_managed() noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Abandon all the operations of the transaction instead of saving
|
|
|
|
|
/// them.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
void abort();
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// \brief Commit all the operations of a transaction into the database.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
void commit();
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Unmanaged cursor.
|
|
|
|
|
///
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// Like other unmanaged classes, `cursor` allows copying and assignment for
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// instances, but does not destroys the represented underlying object from the
|
|
|
|
|
/// own class destructor.
|
|
|
|
|
///
|
|
|
|
|
/// \copydetails MDBX_cursor
|
2020-09-10 06:15:35 +08:00
|
|
|
|
class LIBMDBX_API_TYPE cursor {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
protected:
|
|
|
|
|
MDBX_cursor *handle_{nullptr};
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR cursor(MDBX_cursor *ptr) noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
public:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR cursor() noexcept = default;
|
2020-09-10 06:15:35 +08:00
|
|
|
|
cursor(const cursor &) noexcept = default;
|
|
|
|
|
inline cursor &operator=(cursor &&other) noexcept;
|
|
|
|
|
inline cursor(cursor &&other) noexcept;
|
|
|
|
|
inline ~cursor() noexcept;
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR operator bool() const noexcept;
|
|
|
|
|
MDBX_CXX14_CONSTEXPR operator const MDBX_cursor *() const;
|
|
|
|
|
MDBX_CXX14_CONSTEXPR operator MDBX_cursor *();
|
|
|
|
|
friend MDBX_CXX11_CONSTEXPR bool operator==(const cursor &a,
|
|
|
|
|
const cursor &b) noexcept;
|
|
|
|
|
friend MDBX_CXX11_CONSTEXPR bool operator!=(const cursor &a,
|
|
|
|
|
const cursor &b) noexcept;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
enum move_operation {
|
|
|
|
|
first = MDBX_FIRST,
|
|
|
|
|
last = MDBX_LAST,
|
|
|
|
|
next = MDBX_NEXT,
|
|
|
|
|
previous = MDBX_PREV,
|
|
|
|
|
get_current = MDBX_GET_CURRENT,
|
|
|
|
|
|
|
|
|
|
multi_prevkey_lastvalue = MDBX_PREV_NODUP,
|
|
|
|
|
multi_currentkey_firstvalue = MDBX_FIRST_DUP,
|
|
|
|
|
multi_currentkey_prevvalue = MDBX_PREV_DUP,
|
|
|
|
|
multi_currentkey_nextvalue = MDBX_NEXT_DUP,
|
|
|
|
|
multi_currentkey_lastvalue = MDBX_LAST_DUP,
|
|
|
|
|
multi_nextkey_firstvalue = MDBX_NEXT_NODUP,
|
|
|
|
|
|
|
|
|
|
multi_find_pair = MDBX_GET_BOTH,
|
|
|
|
|
multi_exactkey_lowerboundvalue = MDBX_GET_BOTH_RANGE,
|
|
|
|
|
|
|
|
|
|
find_key = MDBX_SET,
|
|
|
|
|
key_exact = MDBX_SET_KEY,
|
|
|
|
|
key_lowerbound = MDBX_SET_RANGE
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct move_result : public pair_result {
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline move_result(const cursor &cursor, bool throw_notfound);
|
|
|
|
|
inline move_result(cursor &cursor, move_operation operation,
|
2020-08-23 01:19:46 +08:00
|
|
|
|
bool throw_notfound);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline move_result(cursor &cursor, move_operation operation,
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const slice &key, bool throw_notfound);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline move_result(cursor &cursor, move_operation operation,
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const slice &key, const slice &value,
|
|
|
|
|
bool throw_notfound);
|
|
|
|
|
move_result(const move_result &) noexcept = default;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
protected:
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline bool move(move_operation operation, MDBX_val *key, MDBX_val *value,
|
2020-08-23 01:19:46 +08:00
|
|
|
|
bool throw_notfound) const
|
|
|
|
|
/* fake const, i.e. for some operations */;
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline ptrdiff_t estimate(move_operation operation, MDBX_val *key,
|
|
|
|
|
MDBX_val *value) const;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
inline move_result move(move_operation operation, bool throw_notfound);
|
|
|
|
|
inline move_result to_first(bool throw_notfound = true);
|
|
|
|
|
inline move_result to_previous(bool throw_notfound = true);
|
|
|
|
|
inline move_result to_previous_last_multi(bool throw_notfound = true);
|
|
|
|
|
inline move_result to_current_first_multi(bool throw_notfound = true);
|
|
|
|
|
inline move_result to_current_prev_multi(bool throw_notfound = true);
|
|
|
|
|
inline move_result current(bool throw_notfound = true) const;
|
|
|
|
|
inline move_result to_current_next_multi(bool throw_notfound = true);
|
|
|
|
|
inline move_result to_current_last_multi(bool throw_notfound = true);
|
|
|
|
|
inline move_result to_next_first_multi(bool throw_notfound = true);
|
|
|
|
|
inline move_result to_next(bool throw_notfound = true);
|
|
|
|
|
inline move_result to_last(bool throw_notfound = true);
|
|
|
|
|
|
|
|
|
|
inline move_result move(move_operation operation, const slice &key,
|
|
|
|
|
bool throw_notfound);
|
|
|
|
|
inline move_result find(const slice &key, bool throw_notfound = true);
|
|
|
|
|
inline move_result lower_bound(const slice &key, bool throw_notfound = true);
|
|
|
|
|
|
|
|
|
|
inline move_result move(move_operation operation, const slice &key,
|
|
|
|
|
const slice &value, bool throw_notfound);
|
|
|
|
|
inline move_result find_multivalue(const slice &key, const slice &value,
|
|
|
|
|
bool throw_notfound = true);
|
|
|
|
|
inline move_result lower_bound_multivalue(const slice &key,
|
|
|
|
|
const slice &value,
|
|
|
|
|
bool throw_notfound = false);
|
|
|
|
|
|
|
|
|
|
inline bool seek(const slice &key);
|
|
|
|
|
inline bool move(move_operation operation, slice &key, slice &value,
|
|
|
|
|
bool throw_notfound);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Return count of duplicates for current key.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline size_t count_multivalue() const;
|
|
|
|
|
|
|
|
|
|
inline bool eof() const;
|
|
|
|
|
inline bool on_first() const;
|
|
|
|
|
inline bool on_last() const;
|
|
|
|
|
inline ptrdiff_t estimate(slice key, slice value) const;
|
|
|
|
|
inline ptrdiff_t estimate(slice key) const;
|
|
|
|
|
inline ptrdiff_t estimate(move_operation operation) const;
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-15 07:32:04 +08:00
|
|
|
|
/// \brief Renew/bind a cursor with a new transaction and previously used
|
|
|
|
|
/// key-value map handle.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void renew(::mdbx::txn &txn);
|
2020-09-15 07:32:04 +08:00
|
|
|
|
|
|
|
|
|
/// \brief Bind/renew a cursor with a new transaction and specified key-value
|
|
|
|
|
/// map handle.
|
|
|
|
|
inline void bind(::mdbx::txn &txn, ::mdbx::map_handle map_handle);
|
|
|
|
|
|
2020-09-16 06:17:47 +08:00
|
|
|
|
/// \brief Returns the cursor's transaction.
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ::mdbx::txn txn() const;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline map_handle map() const;
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline operator ::mdbx::txn() const { return txn(); }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline operator ::mdbx::map_handle() const { return map(); }
|
|
|
|
|
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline MDBX_error_t put(const slice &key, slice *value,
|
|
|
|
|
MDBX_put_flags_t flags) noexcept;
|
|
|
|
|
inline void insert(const slice &key, slice value);
|
|
|
|
|
inline value_result try_insert(const slice &key, slice value);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline slice insert_reserve(const slice &key, size_t value_length);
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline value_result try_insert_reserve(const slice &key, size_t value_length);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
inline void upsert(const slice &key, const slice &value);
|
|
|
|
|
inline slice upsert_reserve(const slice &key, size_t value_length);
|
|
|
|
|
|
|
|
|
|
inline void update(const slice &key, const slice &value);
|
|
|
|
|
inline bool try_update(const slice &key, const slice &value);
|
|
|
|
|
inline slice update_reserve(const slice &key, size_t value_length);
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline value_result try_update_reserve(const slice &key, size_t value_length);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
inline bool erase(bool whole_multivalue = false);
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
/// \brief Managed cursor.
|
|
|
|
|
///
|
2020-09-10 06:15:35 +08:00
|
|
|
|
/// As other managed classes, `cursor_managed` destroys the represented
|
|
|
|
|
/// underlying object from the own class destructor, but disallows copying and
|
|
|
|
|
/// assignment for instances.
|
2020-09-07 06:54:36 +08:00
|
|
|
|
///
|
|
|
|
|
/// \copydetails MDBX_cursor
|
2020-09-10 06:15:35 +08:00
|
|
|
|
class LIBMDBX_API_TYPE cursor_managed : public cursor {
|
|
|
|
|
using inherited = cursor;
|
|
|
|
|
friend class txn;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
/// delegated constructor for RAII
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR cursor_managed(MDBX_cursor *ptr) noexcept
|
|
|
|
|
: inherited(ptr) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
public:
|
2020-09-15 07:32:04 +08:00
|
|
|
|
/// \brief Creates a new managed cursor with underlying object.
|
|
|
|
|
inline cursor_managed();
|
2020-09-10 06:15:35 +08:00
|
|
|
|
|
2020-09-15 07:32:04 +08:00
|
|
|
|
/// \brief Explicitly closes the cursor.
|
2020-08-23 01:19:46 +08:00
|
|
|
|
void close();
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
cursor_managed(cursor_managed &&) = default;
|
|
|
|
|
cursor_managed &operator=(cursor_managed &&) = default;
|
|
|
|
|
cursor_managed(const cursor_managed &) = delete;
|
|
|
|
|
cursor_managed &operator=(const cursor_managed &) = delete;
|
|
|
|
|
~cursor_managed() noexcept { ::mdbx_cursor_close(handle_); }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-14 20:24:17 +08:00
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &, const slice &);
|
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &, const pair &);
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline ::std::ostream &operator<<(::std::ostream &out,
|
2020-09-14 20:24:17 +08:00
|
|
|
|
const buffer<ALLOCATOR> &it) {
|
2020-09-01 06:59:21 +08:00
|
|
|
|
return (it.is_freestanding()
|
|
|
|
|
? out << "buf-" << it.headroom() << "." << it.tailroom()
|
|
|
|
|
: out << "ref-")
|
2020-09-01 18:37:52 +08:00
|
|
|
|
<< it.slice();
|
2020-08-24 16:20:41 +08:00
|
|
|
|
}
|
2020-09-01 06:59:21 +08:00
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &,
|
2020-09-14 20:24:17 +08:00
|
|
|
|
const env::geometry::size &);
|
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &, const env::geometry &);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &,
|
2020-09-14 20:24:17 +08:00
|
|
|
|
const env::operate_parameters &);
|
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &, const env::mode &);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &,
|
2020-09-14 20:24:17 +08:00
|
|
|
|
const env::durability &);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &,
|
2020-09-14 20:24:17 +08:00
|
|
|
|
const env::reclaiming_options &);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &,
|
2020-09-14 20:24:17 +08:00
|
|
|
|
const env::operate_options &);
|
2020-09-07 06:54:36 +08:00
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &,
|
2020-09-14 20:24:17 +08:00
|
|
|
|
const env_managed::create_parameters &);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &,
|
|
|
|
|
const MDBX_log_level_t &);
|
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &,
|
|
|
|
|
const MDBX_debug_flags_t &);
|
2020-09-14 20:24:17 +08:00
|
|
|
|
LIBMDBX_API ::std::ostream &operator<<(::std::ostream &, const error &);
|
2020-09-01 06:59:21 +08:00
|
|
|
|
inline ::std::ostream &operator<<(::std::ostream &out,
|
|
|
|
|
const MDBX_error_t &errcode) {
|
2020-09-14 20:24:17 +08:00
|
|
|
|
return out << error(errcode);
|
2020-09-01 06:59:21 +08:00
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
//
|
|
|
|
|
// Inline body of the libmdbx C++ API (preliminary draft)
|
|
|
|
|
//
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const version_info &get_version() noexcept {
|
2020-09-13 23:59:33 +08:00
|
|
|
|
return ::mdbx_version;
|
|
|
|
|
}
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const build_info &get_build() noexcept {
|
|
|
|
|
return ::mdbx_build;
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
static MDBX_CXX17_CONSTEXPR size_t strlen(const char *c_str) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#if defined(__cpp_lib_is_constant_evaluated) && \
|
|
|
|
|
__cpp_lib_is_constant_evaluated >= 201811L
|
|
|
|
|
if (::std::is_constant_evaluated()) {
|
|
|
|
|
for (size_t i = 0; c_str; ++i)
|
|
|
|
|
if (!c_str[i])
|
|
|
|
|
return i;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif /* __cpp_lib_is_constant_evaluated >= 201811 */
|
|
|
|
|
#if defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606L
|
|
|
|
|
return c_str ? ::std::string_view(c_str).length() : 0;
|
|
|
|
|
#else
|
|
|
|
|
return c_str ? ::std::strlen(c_str) : 0;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR size_t check_length(size_t bytes) {
|
|
|
|
|
if (MDBX_UNLIKELY(bytes > size_t(MDBX_MAXDATASIZE)))
|
|
|
|
|
MDBX_CXX20_UNLIKELY throw_max_length_exceeded();
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool exception_thunk::is_clean() const noexcept { return !captured_; }
|
|
|
|
|
|
|
|
|
|
inline void exception_thunk::capture() noexcept {
|
|
|
|
|
assert(is_clean());
|
|
|
|
|
captured_ = ::std::current_exception();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void exception_thunk::rethrow_captured() const {
|
|
|
|
|
if (captured_)
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY ::std::rethrow_exception(captured_);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR error::error(MDBX_error_t error_code) noexcept
|
2020-09-13 23:59:33 +08:00
|
|
|
|
: code_(error_code) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
inline error &error::operator=(MDBX_error_t error_code) noexcept {
|
|
|
|
|
code_ = error_code;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool operator==(const error &a, const error &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return a.code_ == b.code_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool operator!=(const error &a, const error &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool error::is_success() const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return code_ == MDBX_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool error::is_result_true() const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return code_ == MDBX_RESULT_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool error::is_result_false() const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return code_ == MDBX_RESULT_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool error::is_failure() const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return code_ != MDBX_SUCCESS && code_ != MDBX_RESULT_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR MDBX_error_t error::code() const noexcept { return code_; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool error::is_mdbx_error() const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return (code() >= MDBX_FIRST_LMDB_ERRCODE &&
|
|
|
|
|
code() <= MDBX_LAST_LMDB_ERRCODE) ||
|
|
|
|
|
(code() >= MDBX_FIRST_ADDED_ERRCODE &&
|
2020-09-26 23:28:09 +08:00
|
|
|
|
code() <= MDBX_LAST_ADDED_ERRCODE);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void error::throw_exception(int error_code) {
|
|
|
|
|
const error trouble(static_cast<MDBX_error_t>(error_code));
|
|
|
|
|
trouble.throw_exception();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void error::throw_on_failure() const {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(is_failure()))
|
|
|
|
|
MDBX_CXX20_UNLIKELY throw_exception();
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void error::success_or_throw() const {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(!is_success()))
|
|
|
|
|
MDBX_CXX20_UNLIKELY throw_exception();
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void error::success_or_throw(const exception_thunk &thunk) const {
|
|
|
|
|
assert(thunk.is_clean() || code() != MDBX_SUCCESS);
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(!is_success())) {
|
|
|
|
|
MDBX_CXX20_UNLIKELY if (!thunk.is_clean()) thunk.rethrow_captured();
|
2020-08-23 01:19:46 +08:00
|
|
|
|
else throw_exception();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void error::panic_on_failure(const char *context_where,
|
|
|
|
|
const char *func_who) const noexcept {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(is_failure()))
|
|
|
|
|
MDBX_CXX20_UNLIKELY panic(context_where, func_who);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void error::success_or_panic(const char *context_where,
|
|
|
|
|
const char *func_who) const noexcept {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(!is_success()))
|
|
|
|
|
MDBX_CXX20_UNLIKELY panic(context_where, func_who);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void error::throw_on_nullptr(const void *ptr, MDBX_error_t error_code) {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(ptr == nullptr))
|
|
|
|
|
MDBX_CXX20_UNLIKELY error(error_code).throw_exception();
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void error::throw_on_failure(int error_code) {
|
|
|
|
|
error rc(static_cast<MDBX_error_t>(error_code));
|
|
|
|
|
rc.throw_on_failure();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline void error::success_or_throw(MDBX_error_t error_code) {
|
|
|
|
|
error rc(error_code);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
rc.success_or_throw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool error::boolean_or_throw(int error_code) {
|
|
|
|
|
switch (error_code) {
|
|
|
|
|
case MDBX_RESULT_FALSE:
|
|
|
|
|
return false;
|
|
|
|
|
case MDBX_RESULT_TRUE:
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY throw_exception(error_code);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void error::success_or_throw(int error_code,
|
|
|
|
|
const exception_thunk &thunk) {
|
|
|
|
|
error rc(static_cast<MDBX_error_t>(error_code));
|
|
|
|
|
rc.success_or_throw(thunk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void error::panic_on_failure(int error_code, const char *context_where,
|
|
|
|
|
const char *func_who) noexcept {
|
|
|
|
|
error rc(static_cast<MDBX_error_t>(error_code));
|
|
|
|
|
rc.panic_on_failure(context_where, func_who);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void error::success_or_panic(int error_code, const char *context_where,
|
|
|
|
|
const char *func_who) noexcept {
|
|
|
|
|
error rc(static_cast<MDBX_error_t>(error_code));
|
|
|
|
|
rc.success_or_panic(context_where, func_who);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR slice::slice() noexcept : ::MDBX_val({nullptr, 0}) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR slice::slice(const void *ptr, size_t bytes)
|
2020-08-23 01:19:46 +08:00
|
|
|
|
: ::MDBX_val({const_cast<void *>(ptr), check_length(bytes)}) {}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR slice::slice(const void *begin, const void *end)
|
2020-08-23 01:19:46 +08:00
|
|
|
|
: slice(begin, static_cast<const byte *>(end) -
|
|
|
|
|
static_cast<const byte *>(begin)) {}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX17_CONSTEXPR slice::slice(const char *c_str)
|
2020-08-23 01:19:46 +08:00
|
|
|
|
: slice(c_str, ::mdbx::strlen(c_str)) {}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR slice::slice(const MDBX_val &src)
|
2020-08-23 01:19:46 +08:00
|
|
|
|
: slice(src.iov_base, src.iov_len) {}
|
|
|
|
|
|
|
|
|
|
inline slice::slice(MDBX_val &&src) : slice(src) { src.iov_base = nullptr; }
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
inline slice::slice(slice &&src) noexcept : slice(src) { src.invalidate(); }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
inline slice &slice::assign(const void *ptr, size_t bytes) {
|
|
|
|
|
iov_base = const_cast<void *>(ptr);
|
|
|
|
|
iov_len = check_length(bytes);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice &slice::assign(const slice &src) noexcept {
|
|
|
|
|
iov_base = src.iov_base;
|
|
|
|
|
iov_len = src.iov_len;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice &slice::assign(const ::MDBX_val &src) {
|
|
|
|
|
return assign(src.iov_base, src.iov_len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slice &slice::assign(slice &&src) noexcept {
|
|
|
|
|
assign(src);
|
2020-09-07 06:54:36 +08:00
|
|
|
|
src.invalidate();
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice &slice::assign(::MDBX_val &&src) {
|
|
|
|
|
assign(src.iov_base, src.iov_len);
|
|
|
|
|
src.iov_base = nullptr;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice &slice::assign(const void *begin, const void *end) {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return assign(begin, static_cast<const byte *>(end) -
|
|
|
|
|
static_cast<const byte *>(begin));
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice &slice::assign(const char *c_str) {
|
|
|
|
|
return assign(c_str, ::mdbx::strlen(c_str));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice &slice::operator=(slice &&src) noexcept {
|
|
|
|
|
return assign(::std::move(src));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice &slice::operator=(::MDBX_val &&src) {
|
|
|
|
|
return assign(::std::move(src));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void slice::swap(slice &other) noexcept {
|
|
|
|
|
const auto temp = *this;
|
|
|
|
|
*this = other;
|
|
|
|
|
other = temp;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const mdbx::byte *slice::byte_ptr() const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return static_cast<const byte *>(iov_base);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const char *slice::char_ptr() const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return static_cast<const char *>(iov_base);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR const void *slice::data() const noexcept {
|
|
|
|
|
return iov_base;
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR size_t slice::length() const noexcept { return iov_len; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool slice::empty() const noexcept {
|
|
|
|
|
return length() == 0;
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool slice::is_null() const noexcept {
|
2020-09-13 23:59:33 +08:00
|
|
|
|
return data() == nullptr;
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR size_t slice::size() const noexcept { return length(); }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR slice::operator bool() const noexcept {
|
|
|
|
|
return !is_null();
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
inline void slice::invalidate() noexcept { iov_base = nullptr; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
inline void slice::clear() noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
iov_base = nullptr;
|
|
|
|
|
iov_len = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void slice::remove_prefix(size_t n) noexcept {
|
|
|
|
|
assert(n <= size());
|
|
|
|
|
iov_base = static_cast<byte *>(iov_base) + n;
|
|
|
|
|
iov_len -= n;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline void slice::safe_remove_prefix(size_t n) {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(n > size()))
|
|
|
|
|
MDBX_CXX20_UNLIKELY throw_out_range();
|
2020-08-31 17:24:12 +08:00
|
|
|
|
remove_prefix(n);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline void slice::remove_suffix(size_t n) noexcept {
|
|
|
|
|
assert(n <= size());
|
|
|
|
|
iov_len -= n;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline void slice::safe_remove_suffix(size_t n) {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(n > size()))
|
|
|
|
|
MDBX_CXX20_UNLIKELY throw_out_range();
|
2020-08-31 17:24:12 +08:00
|
|
|
|
remove_suffix(n);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline bool slice::starts_with(const slice &prefix) const noexcept {
|
|
|
|
|
return length() >= prefix.length() &&
|
|
|
|
|
::std::memcmp(data(), prefix.data(), prefix.length()) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool slice::ends_with(const slice &suffix) const noexcept {
|
|
|
|
|
return length() >= suffix.length() &&
|
|
|
|
|
::std::memcmp(byte_ptr() + length() - suffix.length(), suffix.data(),
|
|
|
|
|
suffix.length()) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION MDBX_CXX14_CONSTEXPR size_t
|
2020-08-23 01:19:46 +08:00
|
|
|
|
slice::hash_value() const noexcept {
|
|
|
|
|
size_t h = length() * 3977471;
|
|
|
|
|
for (size_t i = 0; i < length(); ++i)
|
|
|
|
|
h = (h ^ static_cast<const uint8_t *>(data())[i]) * 1664525 + 1013904223;
|
|
|
|
|
return h ^ 3863194411 * (h >> 11);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 06:54:36 +08:00
|
|
|
|
inline byte slice::operator[](size_t n) const noexcept {
|
|
|
|
|
assert(n < size());
|
|
|
|
|
return byte_ptr()[n];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline byte slice::at(size_t n) const {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(n >= size()))
|
|
|
|
|
MDBX_CXX20_UNLIKELY throw_out_range();
|
2020-09-07 06:54:36 +08:00
|
|
|
|
return byte_ptr()[n];
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 17:24:12 +08:00
|
|
|
|
inline slice slice::head(size_t n) const noexcept {
|
|
|
|
|
assert(n <= size());
|
|
|
|
|
return slice(data(), n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice slice::tail(size_t n) const noexcept {
|
|
|
|
|
assert(n <= size());
|
|
|
|
|
return slice(char_ptr() + size() - n, n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice slice::middle(size_t from, size_t n) const noexcept {
|
|
|
|
|
assert(from + n <= size());
|
|
|
|
|
return slice(char_ptr() + from, n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice slice::safe_head(size_t n) const {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(n > size()))
|
|
|
|
|
MDBX_CXX20_UNLIKELY throw_out_range();
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return head(n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice slice::safe_tail(size_t n) const {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(n > size()))
|
|
|
|
|
MDBX_CXX20_UNLIKELY throw_out_range();
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return tail(n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline slice slice::safe_middle(size_t from, size_t n) const {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(n > max_length))
|
|
|
|
|
MDBX_CXX20_UNLIKELY throw_max_length_exceeded();
|
|
|
|
|
if (MDBX_UNLIKELY(from + n > size()))
|
|
|
|
|
MDBX_CXX20_UNLIKELY throw_out_range();
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return middle(from, n);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
inline intptr_t slice::compare_fast(const slice &a, const slice &b) noexcept {
|
|
|
|
|
const intptr_t diff = a.length() - b.length();
|
|
|
|
|
return diff ? diff
|
|
|
|
|
: (a.data() == b.data())
|
|
|
|
|
? 0
|
|
|
|
|
: ::std::memcmp(a.data(), b.data(), a.length());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline intptr_t slice::compare_lexicographically(const slice &a,
|
|
|
|
|
const slice &b) noexcept {
|
|
|
|
|
const intptr_t diff =
|
|
|
|
|
::std::memcmp(a.data(), b.data(), ::std::min(a.length(), b.length()));
|
|
|
|
|
return diff ? diff : intptr_t(a.length() - b.length());
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION inline bool operator==(const slice &a,
|
|
|
|
|
const slice &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return slice::compare_fast(a, b) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION inline bool operator<(const slice &a,
|
|
|
|
|
const slice &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return slice::compare_lexicographically(a, b) < 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION inline bool operator>(const slice &a,
|
|
|
|
|
const slice &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return slice::compare_lexicographically(a, b) > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION inline bool operator<=(const slice &a,
|
|
|
|
|
const slice &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return slice::compare_lexicographically(a, b) <= 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION inline bool operator>=(const slice &a,
|
|
|
|
|
const slice &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return slice::compare_lexicographically(a, b) >= 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_NOTHROW_PURE_FUNCTION inline bool operator!=(const slice &a,
|
|
|
|
|
const slice &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return slice::compare_fast(a, b) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline ::mdbx::string<ALLOCATOR>
|
|
|
|
|
slice::hex_encode(bool uppercase, const ALLOCATOR &allocator) const {
|
|
|
|
|
::mdbx::string<ALLOCATOR> result(allocator);
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_LIKELY(length() > 0)) {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
result.reserve(to_hex_bytes());
|
|
|
|
|
result.resize(to_hex(const_cast<char *>(result.data()), result.capacity()) -
|
|
|
|
|
result.data(),
|
|
|
|
|
uppercase);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline ::mdbx::string<ALLOCATOR>
|
|
|
|
|
slice::hex_decode(const ALLOCATOR &allocator) const {
|
|
|
|
|
::mdbx::string<ALLOCATOR> result(allocator);
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_LIKELY(length() > 0)) {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
result.reserve(from_hex_bytes());
|
|
|
|
|
result.resize(
|
2020-08-31 17:24:12 +08:00
|
|
|
|
from_hex(static_cast<byte *>(
|
|
|
|
|
static_cast<void *>(const_cast<char *>(result.data()))),
|
|
|
|
|
result.capacity()) -
|
|
|
|
|
static_cast<const byte *>(static_cast<const void *>(result.data())));
|
2020-08-24 16:20:41 +08:00
|
|
|
|
}
|
|
|
|
|
return result;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline ::mdbx::string<ALLOCATOR>
|
|
|
|
|
slice::base58_encode(const ALLOCATOR &allocator) const {
|
|
|
|
|
::mdbx::string<ALLOCATOR> result(allocator);
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_LIKELY(length() > 0)) {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
result.reserve(to_base58_bytes());
|
|
|
|
|
result.resize(
|
|
|
|
|
to_base58(const_cast<char *>(result.data()), result.capacity()) -
|
|
|
|
|
result.data());
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline ::mdbx::string<ALLOCATOR>
|
|
|
|
|
slice::base58_decode(const ALLOCATOR &allocator) const {
|
|
|
|
|
::mdbx::string<ALLOCATOR> result(allocator);
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_LIKELY(length() > 0)) {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
result.reserve(from_base58_bytes());
|
|
|
|
|
result.resize(
|
2020-08-31 17:24:12 +08:00
|
|
|
|
from_base58(static_cast<byte *>(
|
|
|
|
|
static_cast<void *>(const_cast<char *>(result.data()))),
|
|
|
|
|
result.capacity()) -
|
|
|
|
|
static_cast<const byte *>(static_cast<const void *>(result.data())));
|
2020-08-24 16:20:41 +08:00
|
|
|
|
}
|
|
|
|
|
return result;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline ::mdbx::string<ALLOCATOR>
|
|
|
|
|
slice::base64_encode(const ALLOCATOR &allocator) const {
|
|
|
|
|
::mdbx::string<ALLOCATOR> result(allocator);
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_LIKELY(length() > 0)) {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
result.reserve(to_base64_bytes());
|
|
|
|
|
result.resize(
|
|
|
|
|
to_base64(const_cast<char *>(result.data()), result.capacity()) -
|
|
|
|
|
result.data());
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline ::mdbx::string<ALLOCATOR>
|
|
|
|
|
slice::base64_decode(const ALLOCATOR &allocator) const {
|
|
|
|
|
::mdbx::string<ALLOCATOR> result(allocator);
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_LIKELY(length() > 0)) {
|
2020-08-24 16:20:41 +08:00
|
|
|
|
result.reserve(from_base64_bytes());
|
|
|
|
|
result.resize(
|
2020-08-31 17:24:12 +08:00
|
|
|
|
from_base64(static_cast<byte *>(
|
|
|
|
|
static_cast<void *>(const_cast<char *>(result.data()))),
|
|
|
|
|
result.capacity()) -
|
|
|
|
|
static_cast<const byte *>(static_cast<const void *>(result.data())));
|
2020-08-24 16:20:41 +08:00
|
|
|
|
}
|
|
|
|
|
return result;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR map_handle::info::info(map_handle::flags flags,
|
|
|
|
|
map_handle::state state) noexcept
|
2020-08-23 01:19:46 +08:00
|
|
|
|
: flags(flags), state(state) {}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR ::mdbx::key_mode
|
|
|
|
|
map_handle::info::key_mode() const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return ::mdbx::key_mode(flags & (MDBX_REVERSEKEY | MDBX_INTEGERKEY));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR ::mdbx::value_mode
|
2020-09-13 23:59:33 +08:00
|
|
|
|
map_handle::info::value_mode() const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return ::mdbx::value_mode(flags & (MDBX_DUPSORT | MDBX_REVERSEDUP |
|
|
|
|
|
MDBX_DUPFIXED | MDBX_INTEGERDUP));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR env::env(MDBX_env *ptr) noexcept : handle_(ptr) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env &env::operator=(env &&other) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
handle_ = other.handle_;
|
|
|
|
|
other.handle_ = nullptr;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::env(env &&other) noexcept : handle_(other.handle_) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
other.handle_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::~env() noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
handle_ = reinterpret_cast<MDBX_env *>(uintptr_t(0xDeadBeef));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR env::operator bool() const noexcept {
|
2020-09-13 23:59:33 +08:00
|
|
|
|
return handle_ != nullptr;
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR env::operator const MDBX_env *() const { return handle_; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR env::operator MDBX_env *() { return handle_; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool operator==(const env &a, const env &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return a.handle_ == b.handle_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool operator!=(const env &a, const env &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return a.handle_ != b.handle_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::geometry &env::geometry::make_fixed(intptr_t size) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
size_lower = size_now = size_upper = size;
|
|
|
|
|
growth_step = shrink_threshold = 0;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::geometry &env::geometry::make_dynamic(intptr_t lower,
|
|
|
|
|
intptr_t upper) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
size_now = size_lower = lower;
|
|
|
|
|
size_upper = upper;
|
|
|
|
|
growth_step = shrink_threshold = default_value;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::reclaiming_options env::operate_parameters::reclaiming_from_flags(
|
2020-08-23 01:19:46 +08:00
|
|
|
|
MDBX_env_flags_t flags) noexcept {
|
|
|
|
|
return reclaiming_options(flags);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::operate_options
|
|
|
|
|
env::operate_parameters::options_from_flags(MDBX_env_flags_t flags) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return operate_options(flags);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::pagesize_min() noexcept { return MDBX_MIN_PAGESIZE; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::pagesize_max() noexcept { return MDBX_MAX_PAGESIZE; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::dbsize_min(intptr_t pagesize) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const intptr_t result = mdbx_limits_dbsize_min(pagesize);
|
|
|
|
|
if (result < 0)
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(MDBX_EINVAL);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return static_cast<size_t>(result);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::dbsize_max(intptr_t pagesize) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const intptr_t result = mdbx_limits_dbsize_max(pagesize);
|
|
|
|
|
if (result < 0)
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(MDBX_EINVAL);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return static_cast<size_t>(result);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::key_min(MDBX_db_flags_t flags) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return (flags & MDBX_INTEGERKEY) ? 4 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::key_min(key_mode mode) noexcept {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
return key_min(MDBX_db_flags_t(mode));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::key_max(intptr_t pagesize, MDBX_db_flags_t flags) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const intptr_t result = mdbx_limits_keysize_max(pagesize, flags);
|
|
|
|
|
if (result < 0)
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(MDBX_EINVAL);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return static_cast<size_t>(result);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::key_max(intptr_t pagesize, key_mode mode) {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
return key_max(pagesize, MDBX_db_flags_t(mode));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::key_max(const env &env, MDBX_db_flags_t flags) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const intptr_t result = mdbx_env_get_maxkeysize_ex(env, flags);
|
|
|
|
|
if (result < 0)
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(MDBX_EINVAL);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return static_cast<size_t>(result);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::key_max(const env &env, key_mode mode) {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
return key_max(env, MDBX_db_flags_t(mode));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::value_min(MDBX_db_flags_t flags) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return (flags & MDBX_INTEGERDUP) ? 4 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::value_min(value_mode mode) noexcept {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
return value_min(MDBX_db_flags_t(mode));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::value_max(intptr_t pagesize, MDBX_db_flags_t flags) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const intptr_t result = mdbx_limits_valsize_max(pagesize, flags);
|
|
|
|
|
if (result < 0)
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(MDBX_EINVAL);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return static_cast<size_t>(result);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::value_max(intptr_t pagesize, value_mode mode) {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
return value_max(pagesize, MDBX_db_flags_t(mode));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::value_max(const env &env, MDBX_db_flags_t flags) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const intptr_t result = mdbx_env_get_maxvalsize_ex(env, flags);
|
|
|
|
|
if (result < 0)
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(MDBX_EINVAL);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return static_cast<size_t>(result);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::value_max(const env &env, value_mode mode) {
|
2020-09-07 06:54:36 +08:00
|
|
|
|
return value_max(env, MDBX_db_flags_t(mode));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t env::limits::transaction_size_max(intptr_t pagesize) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const intptr_t result = mdbx_limits_txnsize_max(pagesize);
|
|
|
|
|
if (result < 0)
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(MDBX_EINVAL);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return static_cast<size_t>(result);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::operate_parameters env::get_operation_parameters() const {
|
|
|
|
|
return env::operate_parameters(*this);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::mode env::get_mode() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return operate_parameters::mode_from_flags(get_flags());
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::durability env::get_durability() const {
|
|
|
|
|
return env::operate_parameters::durability_from_flags(get_flags());
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::reclaiming_options env::get_reclaiming() const {
|
|
|
|
|
return env::operate_parameters::reclaiming_from_flags(get_flags());
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::operate_options env::get_options() const {
|
|
|
|
|
return env::operate_parameters::options_from_flags(get_flags());
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::stat env::get_stat() const {
|
|
|
|
|
env::stat r;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_env_stat_ex(handle_, nullptr, &r, sizeof(r)));
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::stat env::get_stat(const txn &txn) const {
|
|
|
|
|
env::stat r;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_env_stat_ex(handle_, txn, &r, sizeof(r)));
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::info env::get_info() const {
|
|
|
|
|
env::info r;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_env_info_ex(handle_, nullptr, &r, sizeof(r)));
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env::info env::get_info(const txn &txn) const {
|
|
|
|
|
env::info r;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_env_info_ex(handle_, txn, &r, sizeof(r)));
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline filehandle env::get_filehandle() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
filehandle fd;
|
|
|
|
|
error::success_or_throw(::mdbx_env_get_fd(handle_, &fd));
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline MDBX_env_flags_t env::get_flags() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
unsigned bits;
|
|
|
|
|
error::success_or_throw(::mdbx_env_get_flags(handle_, &bits));
|
|
|
|
|
return MDBX_env_flags_t(bits);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline unsigned env::max_readers() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
unsigned r;
|
|
|
|
|
error::success_or_throw(::mdbx_env_get_maxreaders(handle_, &r));
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline unsigned env::max_maps() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
unsigned r;
|
|
|
|
|
error::success_or_throw(::mdbx_env_get_maxdbs(handle_, &r));
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void *env::get_context() const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return mdbx_env_get_userctx(handle_);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env &env::set_context(void *ptr) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_env_set_userctx(handle_, ptr));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env &env::set_sync_threshold(size_t bytes) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_env_set_syncbytes(handle_, bytes));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env &env::set_sync_period(unsigned seconds_16dot16) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_env_set_syncperiod(handle_, seconds_16dot16));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env &env::set_sync_period(double seconds) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return set_sync_period(unsigned(seconds * 65536));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env &env::alter_flags(MDBX_env_flags_t flags, bool on_off) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_env_set_flags(handle_, flags, on_off));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline env &env::set_geometry(const geometry &geo) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_env_set_geometry(
|
|
|
|
|
handle_, geo.size_lower, geo.size_now, geo.size_upper, geo.growth_step,
|
|
|
|
|
geo.shrink_threshold, geo.pagesize));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool env::sync_to_disk(bool force, bool nonblock) {
|
|
|
|
|
const int err = ::mdbx_env_sync_ex(handle_, force, nonblock);
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS /* flush done */:
|
|
|
|
|
case MDBX_RESULT_TRUE /* no data pending for flush to disk */:
|
|
|
|
|
return true;
|
|
|
|
|
case MDBX_BUSY /* the environment is used by other thread */:
|
|
|
|
|
return false;
|
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void env::close_map(const map_handle &handle) {
|
|
|
|
|
error::success_or_throw(::mdbx_dbi_close(*this, handle.dbi));
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR
|
|
|
|
|
env::reader_info::reader_info(int slot, mdbx_pid_t pid, mdbx_tid_t thread,
|
|
|
|
|
uint64_t txnid, uint64_t lag, size_t used,
|
|
|
|
|
size_t retained) noexcept
|
2020-08-23 01:19:46 +08:00
|
|
|
|
: slot(slot), pid(pid), thread(thread), transaction_id(txnid),
|
|
|
|
|
transaction_lag(lag), bytes_used(used), bytes_retained(retained) {}
|
|
|
|
|
|
|
|
|
|
template <typename VISITOR>
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline int env::enumerate_readers(VISITOR &visitor) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
struct reader_visitor_thunk : public exception_thunk {
|
|
|
|
|
VISITOR &visitor_;
|
|
|
|
|
static int cb(void *ctx, int number, int slot, mdbx_pid_t pid,
|
|
|
|
|
mdbx_tid_t thread, uint64_t txnid, uint64_t lag, size_t used,
|
|
|
|
|
size_t retained) noexcept {
|
|
|
|
|
reader_visitor_thunk *thunk = static_cast<reader_visitor_thunk *>(ctx);
|
|
|
|
|
assert(thunk->is_clean());
|
|
|
|
|
try {
|
|
|
|
|
const reader_info info(slot, pid, thread, txnid, lag, used, retained);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
return loop_control(thunk->visitor_(info, number));
|
2020-08-23 01:19:46 +08:00
|
|
|
|
} catch (... /* capture any exception to rethrow it over C code */) {
|
|
|
|
|
thunk->capture();
|
2020-09-10 06:15:35 +08:00
|
|
|
|
return loop_control::exit_loop;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR reader_visitor_thunk(VISITOR &visitor) noexcept
|
2020-08-23 01:19:46 +08:00
|
|
|
|
: visitor_(visitor) {}
|
|
|
|
|
};
|
|
|
|
|
reader_visitor_thunk thunk(visitor);
|
|
|
|
|
const auto rc = ::mdbx_reader_list(*this, thunk.cb, &thunk);
|
2020-09-26 23:28:09 +08:00
|
|
|
|
thunk.rethrow_captured();
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline unsigned env::check_readers() {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
int dead_count;
|
|
|
|
|
error::throw_on_failure(::mdbx_reader_check(*this, &dead_count));
|
|
|
|
|
assert(dead_count >= 0);
|
|
|
|
|
return static_cast<unsigned>(dead_count);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 00:24:57 +08:00
|
|
|
|
inline env &env::set_HandleSlowReaders(MDBX_hsr_func *cb) {
|
|
|
|
|
error::success_or_throw(::mdbx_env_set_hsr(handle_, cb));
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 00:24:57 +08:00
|
|
|
|
inline MDBX_hsr_func *env::get_HandleSlowReaders() const noexcept {
|
|
|
|
|
return ::mdbx_env_get_hsr(handle_);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline txn_managed env::start_read() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
::MDBX_txn *ptr;
|
|
|
|
|
error::success_or_throw(
|
|
|
|
|
::mdbx_txn_begin(handle_, nullptr, MDBX_TXN_RDONLY, &ptr));
|
|
|
|
|
assert(ptr != nullptr);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
return txn_managed(ptr);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline txn_managed env::prepare_read() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
::MDBX_txn *ptr;
|
|
|
|
|
error::success_or_throw(
|
|
|
|
|
::mdbx_txn_begin(handle_, nullptr, MDBX_TXN_RDONLY_PREPARE, &ptr));
|
|
|
|
|
assert(ptr != nullptr);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
return txn_managed(ptr);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline txn_managed env::start_write(bool dont_wait) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
::MDBX_txn *ptr;
|
|
|
|
|
error::success_or_throw(::mdbx_txn_begin(
|
|
|
|
|
handle_, nullptr, dont_wait ? MDBX_TXN_TRY : MDBX_TXN_READWRITE, &ptr));
|
|
|
|
|
assert(ptr != nullptr);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
return txn_managed(ptr);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline txn_managed env::try_start_write() { return start_write(true); }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR txn::txn(MDBX_txn *ptr) noexcept : handle_(ptr) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline txn &txn::operator=(txn &&other) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
handle_ = other.handle_;
|
|
|
|
|
other.handle_ = nullptr;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline txn::txn(txn &&other) noexcept : handle_(other.handle_) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
other.handle_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline txn::~txn() noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
handle_ = reinterpret_cast<MDBX_txn *>(uintptr_t(0xDeadBeef));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR txn::operator bool() const noexcept {
|
2020-09-13 23:59:33 +08:00
|
|
|
|
return handle_ != nullptr;
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR txn::operator const MDBX_txn *() const { return handle_; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR txn::operator MDBX_txn *() { return handle_; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool operator==(const txn &a, const txn &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return a.handle_ == b.handle_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool operator!=(const txn &a, const txn &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return a.handle_ != b.handle_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool txn::is_dirty(const void *ptr) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
int err = ::mdbx_is_dirty(handle_, ptr);
|
|
|
|
|
switch (err) {
|
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
case MDBX_RESULT_TRUE:
|
|
|
|
|
return true;
|
|
|
|
|
case MDBX_RESULT_FALSE:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ::mdbx::env txn::env() const noexcept { return ::mdbx_txn_env(handle_); }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline MDBX_txn_flags_t txn::flags() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const int bits = mdbx_txn_flags(handle_);
|
|
|
|
|
error::throw_on_failure((bits != -1) ? MDBX_SUCCESS : MDBX_BAD_TXN);
|
|
|
|
|
return static_cast<MDBX_txn_flags_t>(bits);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline uint64_t txn::id() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const uint64_t txnid = mdbx_txn_id(handle_);
|
|
|
|
|
error::throw_on_failure(txnid ? MDBX_SUCCESS : MDBX_BAD_TXN);
|
|
|
|
|
return txnid;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void txn::reset_reading() {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_txn_reset(handle_));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void txn::renew_reading() {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_txn_renew(handle_));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline txn::info txn::get_info(bool scan_reader_lock_table) const {
|
|
|
|
|
txn::info r;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_txn_info(handle_, &r, scan_reader_lock_table));
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-15 07:32:04 +08:00
|
|
|
|
inline cursor_managed txn::open_cursor(map_handle map) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
MDBX_cursor *ptr;
|
|
|
|
|
error::success_or_throw(::mdbx_cursor_open(handle_, map.dbi, &ptr));
|
2020-09-10 06:15:35 +08:00
|
|
|
|
return cursor_managed(ptr);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline ::mdbx::map_handle
|
2020-09-10 06:15:35 +08:00
|
|
|
|
txn::open_map(const char *name, const ::mdbx::key_mode key_mode,
|
|
|
|
|
const ::mdbx::value_mode value_mode) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
::mdbx::map_handle map;
|
|
|
|
|
error::success_or_throw(::mdbx_dbi_open(
|
|
|
|
|
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
|
2020-09-10 06:15:35 +08:00
|
|
|
|
txn::open_map(const ::std::string &name, const ::mdbx::key_mode key_mode,
|
|
|
|
|
const ::mdbx::value_mode value_mode) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return open_map(name.c_str(), key_mode, value_mode);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ::mdbx::map_handle txn::create_map(const char *name,
|
|
|
|
|
const ::mdbx::key_mode key_mode,
|
|
|
|
|
const ::mdbx::value_mode value_mode) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
::mdbx::map_handle map;
|
|
|
|
|
error::success_or_throw(::mdbx_dbi_open(
|
|
|
|
|
handle_, name,
|
|
|
|
|
MDBX_CREATE | MDBX_db_flags_t(key_mode) | MDBX_db_flags_t(value_mode),
|
|
|
|
|
&map.dbi));
|
|
|
|
|
assert(map.dbi != 0);
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ::mdbx::map_handle txn::create_map(const ::std::string &name,
|
|
|
|
|
const ::mdbx::key_mode key_mode,
|
|
|
|
|
const ::mdbx::value_mode value_mode) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return create_map(name.c_str(), key_mode, value_mode);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void txn::drop_map(map_handle map) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_drop(handle_, map.dbi, true));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool txn::drop_map(const ::std::string &name, bool throw_if_absent) {
|
|
|
|
|
return drop_map(name.c_str(), throw_if_absent);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void txn::clear_map(map_handle map) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_drop(handle_, map.dbi, false));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool txn::clear_map(const ::std::string &name, bool throw_if_absent) {
|
|
|
|
|
return clear_map(name.c_str(), throw_if_absent);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline txn::map_stat txn::get_map_stat(map_handle map) const {
|
|
|
|
|
txn::map_stat r;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_dbi_stat(handle_, map.dbi, &r, sizeof(r)));
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline uint32_t txn::get_tree_deepmask(map_handle map) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
uint32_t r;
|
|
|
|
|
error::success_or_throw(::mdbx_dbi_dupsort_depthmask(handle_, map.dbi, &r));
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline map_handle::info txn::get_handle_info(map_handle map) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
unsigned flags, state;
|
|
|
|
|
error::success_or_throw(
|
|
|
|
|
::mdbx_dbi_flags_ex(handle_, map.dbi, &flags, &state));
|
|
|
|
|
return map_handle::info(MDBX_db_flags_t(flags), MDBX_dbi_state_t(state));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline txn &txn::put_canary(const txn::canary &canary) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_canary_put(handle_, &canary));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline txn::canary txn::get_canary() const {
|
|
|
|
|
txn::canary r;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_canary_get(handle_, &r));
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline uint64_t txn::sequence(map_handle map) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
uint64_t result;
|
|
|
|
|
error::success_or_throw(::mdbx_dbi_sequence(handle_, map.dbi, &result, 0));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline uint64_t txn::sequence(map_handle map, uint64_t increment) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
uint64_t result;
|
|
|
|
|
error::success_or_throw(
|
|
|
|
|
::mdbx_dbi_sequence(handle_, map.dbi, &result, increment));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline int txn::compare_keys(map_handle map, const slice &a,
|
|
|
|
|
const slice &b) const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return ::mdbx_cmp(handle_, map.dbi, &a, &b);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline int txn::compare_values(map_handle map, const slice &a,
|
|
|
|
|
const slice &b) const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return ::mdbx_dcmp(handle_, map.dbi, &a, &b);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline int txn::compare_keys(map_handle map, const pair &a,
|
|
|
|
|
const pair &b) const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return compare_keys(map, a.key, b.key);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline int txn::compare_values(map_handle map, const pair &a,
|
|
|
|
|
const pair &b) const noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return compare_values(map, a.value, b.value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline slice txn::get(map_handle map, const slice &key) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
slice result;
|
|
|
|
|
error::success_or_throw(::mdbx_get(handle_, map.dbi, &key, &result));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline slice txn::get(map_handle map, slice key, size_t &values_count) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
slice result;
|
|
|
|
|
error::success_or_throw(
|
|
|
|
|
::mdbx_get_ex(handle_, map.dbi, &key, &result, &values_count));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline slice txn::get(map_handle map, const slice &key,
|
|
|
|
|
const slice &value_at_absence) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
slice result;
|
|
|
|
|
const int err = ::mdbx_get(handle_, map.dbi, &key, &result);
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
|
|
|
|
return result;
|
|
|
|
|
case MDBX_NOTFOUND:
|
2020-09-10 06:15:35 +08:00
|
|
|
|
return value_at_absence;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline slice txn::get(map_handle map, slice key, size_t &values_count,
|
|
|
|
|
const slice &value_at_absence) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
slice result;
|
|
|
|
|
const int err = ::mdbx_get_ex(handle_, map.dbi, &key, &result, &values_count);
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
|
|
|
|
return result;
|
|
|
|
|
case MDBX_NOTFOUND:
|
2020-09-10 06:15:35 +08:00
|
|
|
|
return value_at_absence;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline pair_result txn::get_equal_or_great(map_handle map,
|
|
|
|
|
const slice &key) const {
|
|
|
|
|
pair result(key, slice());
|
|
|
|
|
bool exact = !error::boolean_or_throw(
|
2020-08-23 01:19:46 +08:00
|
|
|
|
::mdbx_get_equal_or_great(handle_, map.dbi, &result.key, &result.value));
|
2020-09-10 06:15:35 +08:00
|
|
|
|
return pair_result(result.key, result.value, exact);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline pair_result
|
|
|
|
|
txn::get_equal_or_great(map_handle map, const slice &key,
|
|
|
|
|
const slice &value_at_absence) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
pair result{key, slice()};
|
|
|
|
|
const int err =
|
|
|
|
|
::mdbx_get_equal_or_great(handle_, map.dbi, &result.key, &result.value);
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
2020-09-10 06:15:35 +08:00
|
|
|
|
return pair_result{result.key, result.value, true};
|
|
|
|
|
case MDBX_RESULT_TRUE:
|
|
|
|
|
return pair_result{result.key, result.value, false};
|
2020-08-23 01:19:46 +08:00
|
|
|
|
case MDBX_NOTFOUND:
|
2020-09-10 06:15:35 +08:00
|
|
|
|
return pair_result{key, value_at_absence, false};
|
2020-08-23 01:19:46 +08:00
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline MDBX_error_t txn::put(map_handle map, const slice &key, slice *value,
|
|
|
|
|
MDBX_put_flags_t flags) noexcept {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return MDBX_error_t(::mdbx_put(handle_, map.dbi, &key, value, flags));
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void txn::put(map_handle map, const slice &key, slice value,
|
|
|
|
|
put_mode mode) noexcept {
|
|
|
|
|
error::success_or_throw(put(map, key, &value, MDBX_put_flags_t(mode)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void txn::insert(map_handle map, const slice &key, slice value) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
error::success_or_throw(
|
|
|
|
|
put(map, key, &value /* takes the present value in case MDBX_KEYEXIST */,
|
|
|
|
|
MDBX_put_flags_t(put_mode::insert)));
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline value_result txn::try_insert(map_handle map, const slice &key,
|
|
|
|
|
slice value) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const int err =
|
2020-08-31 17:24:12 +08:00
|
|
|
|
put(map, key, &value /* takes the present value in case MDBX_KEYEXIST */,
|
|
|
|
|
MDBX_put_flags_t(put_mode::insert));
|
2020-08-23 01:19:46 +08:00
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return value_result{slice(), true};
|
2020-08-23 01:19:46 +08:00
|
|
|
|
case MDBX_KEYEXIST:
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return value_result{value, false};
|
2020-08-23 01:19:46 +08:00
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline slice txn::insert_reserve(map_handle map, const slice &key,
|
|
|
|
|
size_t value_length) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
slice result(nullptr, value_length);
|
|
|
|
|
error::success_or_throw(
|
|
|
|
|
put(map, key, &result /* takes the present value in case MDBX_KEYEXIST */,
|
|
|
|
|
MDBX_put_flags_t(put_mode::insert) | MDBX_RESERVE));
|
|
|
|
|
return result;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline value_result txn::try_insert_reserve(map_handle map, const slice &key,
|
|
|
|
|
size_t value_length) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
slice result(nullptr, value_length);
|
2020-08-31 17:24:12 +08:00
|
|
|
|
const int err =
|
|
|
|
|
put(map, key, &result /* takes the present value in case MDBX_KEYEXIST */,
|
|
|
|
|
MDBX_put_flags_t(put_mode::insert) | MDBX_RESERVE);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return value_result{result, true};
|
2020-08-23 01:19:46 +08:00
|
|
|
|
case MDBX_KEYEXIST:
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return value_result{result, false};
|
2020-08-23 01:19:46 +08:00
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void txn::upsert(map_handle map, const slice &key, const slice &value) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
error::success_or_throw(put(map, key, const_cast<slice *>(&value),
|
|
|
|
|
MDBX_put_flags_t(put_mode::upsert)));
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline slice txn::upsert_reserve(map_handle map, const slice &key,
|
|
|
|
|
size_t value_length) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
slice result(nullptr, value_length);
|
|
|
|
|
error::success_or_throw(put(
|
|
|
|
|
map, key, &result, MDBX_put_flags_t(put_mode::upsert) | MDBX_RESERVE));
|
|
|
|
|
return result;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void txn::update(map_handle map, const slice &key, const slice &value) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
error::success_or_throw(put(map, key, const_cast<slice *>(&value),
|
|
|
|
|
MDBX_put_flags_t(put_mode::update)));
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool txn::try_update(map_handle map, const slice &key,
|
|
|
|
|
const slice &value) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
const int err = put(map, key, const_cast<slice *>(&value),
|
|
|
|
|
MDBX_put_flags_t(put_mode::update));
|
2020-08-23 01:19:46 +08:00
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
|
|
|
|
return true;
|
|
|
|
|
case MDBX_NOTFOUND:
|
|
|
|
|
return false;
|
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline slice txn::update_reserve(map_handle map, const slice &key,
|
|
|
|
|
size_t value_length) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
slice result(nullptr, value_length);
|
|
|
|
|
error::success_or_throw(put(
|
|
|
|
|
map, key, &result, MDBX_put_flags_t(put_mode::update) | MDBX_RESERVE));
|
|
|
|
|
return result;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline value_result txn::try_update_reserve(map_handle map, const slice &key,
|
|
|
|
|
size_t value_length) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
slice result(nullptr, value_length);
|
|
|
|
|
const int err =
|
2020-08-31 17:24:12 +08:00
|
|
|
|
put(map, key, &result, MDBX_put_flags_t(put_mode::update) | MDBX_RESERVE);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return value_result{result, true};
|
|
|
|
|
case MDBX_NOTFOUND:
|
|
|
|
|
return value_result{slice(), false};
|
2020-08-23 01:19:46 +08:00
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool txn::erase(map_handle map, const slice &key) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const int err = ::mdbx_del(handle_, map.dbi, &key, nullptr);
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
|
|
|
|
return true;
|
|
|
|
|
case MDBX_NOTFOUND:
|
|
|
|
|
return false;
|
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool txn::erase(map_handle map, const slice &key, const slice &value) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const int err = ::mdbx_del(handle_, map.dbi, &key, &value);
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
|
|
|
|
return true;
|
|
|
|
|
case MDBX_NOTFOUND:
|
|
|
|
|
return false;
|
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void txn::replace(map_handle map, const slice &key, slice old_value,
|
|
|
|
|
const slice &new_value) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_replace_ex(
|
|
|
|
|
handle_, map.dbi, &key, const_cast<slice *>(&new_value), &old_value,
|
|
|
|
|
MDBX_CURRENT | MDBX_NOOVERWRITE, nullptr, nullptr));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline buffer<ALLOCATOR> txn::extract(map_handle map, const slice &key,
|
|
|
|
|
const ALLOCATOR &allocator) {
|
2020-09-26 06:40:11 +08:00
|
|
|
|
typename buffer<ALLOCATOR>::data_preserver result(allocator);
|
2020-08-24 16:20:41 +08:00
|
|
|
|
error::success_or_throw(::mdbx_replace_ex(handle_, map.dbi, &key, nullptr,
|
2020-09-26 06:40:11 +08:00
|
|
|
|
&result.slice_, MDBX_CURRENT,
|
|
|
|
|
result, &result),
|
|
|
|
|
result);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline buffer<ALLOCATOR> txn::replace(map_handle map, const slice &key,
|
|
|
|
|
const slice &new_value,
|
|
|
|
|
const ALLOCATOR &allocator) {
|
2020-09-26 06:40:11 +08:00
|
|
|
|
typename buffer<ALLOCATOR>::data_preserver result(allocator);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(
|
|
|
|
|
::mdbx_replace_ex(handle_, map.dbi, &key, const_cast<slice *>(&new_value),
|
2020-09-26 06:40:11 +08:00
|
|
|
|
&result.slice_, MDBX_CURRENT, result, &result),
|
|
|
|
|
result);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline buffer<ALLOCATOR> txn::replace_reserve(map_handle map, const slice &key,
|
|
|
|
|
slice &new_value,
|
|
|
|
|
const ALLOCATOR &allocator) {
|
2020-09-26 06:40:11 +08:00
|
|
|
|
typename buffer<ALLOCATOR>::data_preserver result(allocator);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(
|
|
|
|
|
::mdbx_replace_ex(handle_, map.dbi, &key, &new_value, &result.slice_,
|
2020-09-26 06:40:11 +08:00
|
|
|
|
MDBX_CURRENT | MDBX_RESERVE, result, &result),
|
|
|
|
|
result);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void txn::append(map_handle map, const slice &key, const slice &value,
|
|
|
|
|
bool multivalue_order_preserved) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
error::success_or_throw(::mdbx_put(
|
|
|
|
|
handle_, map.dbi, const_cast<slice *>(&key), const_cast<slice *>(&value),
|
|
|
|
|
multivalue_order_preserved ? (MDBX_APPEND | MDBX_APPENDDUP)
|
|
|
|
|
: MDBX_APPEND));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t txn::put_multiple(map_handle map, const slice &key,
|
|
|
|
|
const size_t value_length,
|
|
|
|
|
const void *values_array, size_t values_count,
|
|
|
|
|
put_mode mode, bool allow_partial) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
MDBX_val args[2] = {{const_cast<void *>(values_array), value_length},
|
|
|
|
|
{nullptr, values_count}};
|
|
|
|
|
const int err = ::mdbx_put(handle_, map.dbi, const_cast<slice *>(&key), args,
|
|
|
|
|
MDBX_put_flags_t(mode) | MDBX_MULTIPLE);
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_LIKELY break;
|
2020-08-31 17:24:12 +08:00
|
|
|
|
case MDBX_KEYEXIST:
|
|
|
|
|
if (allow_partial)
|
|
|
|
|
break;
|
|
|
|
|
mdbx_txn_break(handle_);
|
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-31 17:24:12 +08:00
|
|
|
|
}
|
|
|
|
|
return args[1].iov_len /* done item count */;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ptrdiff_t txn::estimate(map_handle map, pair from, pair to) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
ptrdiff_t result;
|
|
|
|
|
error::success_or_throw(mdbx_estimate_range(
|
|
|
|
|
handle_, map.dbi, &from.key, &from.value, &to.key, &to.value, &result));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ptrdiff_t txn::estimate(map_handle map, slice from, slice to) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
ptrdiff_t result;
|
|
|
|
|
error::success_or_throw(mdbx_estimate_range(handle_, map.dbi, &from, nullptr,
|
|
|
|
|
&to, nullptr, &result));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ptrdiff_t txn::estimate_from_first(map_handle map, slice to) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
ptrdiff_t result;
|
|
|
|
|
error::success_or_throw(mdbx_estimate_range(handle_, map.dbi, nullptr,
|
|
|
|
|
nullptr, &to, nullptr, &result));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ptrdiff_t txn::estimate_to_last(map_handle map, slice from) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
ptrdiff_t result;
|
|
|
|
|
error::success_or_throw(mdbx_estimate_range(handle_, map.dbi, &from, nullptr,
|
|
|
|
|
nullptr, nullptr, &result));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR cursor::cursor(MDBX_cursor *ptr) noexcept : handle_(ptr) {}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor &cursor::operator=(cursor &&other) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
handle_ = other.handle_;
|
|
|
|
|
other.handle_ = nullptr;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::cursor(cursor &&other) noexcept : handle_(other.handle_) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
other.handle_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::~cursor() noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
handle_ = reinterpret_cast<MDBX_cursor *>(uintptr_t(0xDeadBeef));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR cursor::operator bool() const noexcept {
|
2020-09-13 23:59:33 +08:00
|
|
|
|
return handle_ != nullptr;
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR cursor::operator const MDBX_cursor *() const {
|
|
|
|
|
return handle_;
|
|
|
|
|
}
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX14_CONSTEXPR cursor::operator MDBX_cursor *() { return handle_; }
|
2020-08-23 01:19:46 +08:00
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool operator==(const cursor &a,
|
|
|
|
|
const cursor &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return a.handle_ == b.handle_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX11_CONSTEXPR bool operator!=(const cursor &a,
|
|
|
|
|
const cursor &b) noexcept {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return a.handle_ != b.handle_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result::move_result(const cursor &cursor,
|
|
|
|
|
bool throw_notfound)
|
|
|
|
|
: pair_result(key, value, false) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
done = cursor.move(get_current, &key, &value, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result::move_result(cursor &cursor,
|
|
|
|
|
move_operation operation,
|
|
|
|
|
bool throw_notfound)
|
|
|
|
|
: pair_result(key, value, false) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
done = cursor.move(operation, &key, &value, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result::move_result(cursor &cursor,
|
|
|
|
|
move_operation operation,
|
|
|
|
|
const slice &key, bool throw_notfound)
|
|
|
|
|
: pair_result(key, slice(), false) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
this->done = cursor.move(operation, &this->key, &this->value, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result::move_result(cursor &cursor,
|
|
|
|
|
move_operation operation,
|
|
|
|
|
const slice &key, const slice &value,
|
|
|
|
|
bool throw_notfound)
|
|
|
|
|
: pair_result(key, value, false) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
this->done = cursor.move(operation, &this->key, &this->value, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool cursor::move(move_operation operation, MDBX_val *key,
|
|
|
|
|
MDBX_val *value, bool throw_notfound) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const int err =
|
|
|
|
|
::mdbx_cursor_get(handle_, key, value, MDBX_cursor_op(operation));
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_LIKELY return true;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
case MDBX_NOTFOUND:
|
|
|
|
|
if (!throw_notfound)
|
|
|
|
|
return false;
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX17_FALLTHROUGH /* fallthrough */;
|
2020-08-23 01:19:46 +08:00
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ptrdiff_t cursor::estimate(move_operation operation, MDBX_val *key,
|
|
|
|
|
MDBX_val *value) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
ptrdiff_t result;
|
|
|
|
|
error::success_or_throw(::mdbx_estimate_move(
|
|
|
|
|
*this, key, value, MDBX_cursor_op(operation), &result));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ptrdiff_t estimate(const cursor &from, const cursor &to) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
ptrdiff_t result;
|
|
|
|
|
error::success_or_throw(mdbx_estimate_distance(from, to, &result));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::move(move_operation operation,
|
|
|
|
|
bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move_result(*this, operation, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::to_first(bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(first, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::to_previous(bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(previous, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::to_previous_last_multi(bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(multi_prevkey_lastvalue, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::to_current_first_multi(bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(multi_currentkey_firstvalue, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::to_current_prev_multi(bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(multi_currentkey_prevvalue, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::current(bool throw_notfound) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move_result(*this, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::to_current_next_multi(bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(multi_currentkey_nextvalue, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::to_current_last_multi(bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(multi_currentkey_lastvalue, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::to_next_first_multi(bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(multi_nextkey_firstvalue, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::to_next(bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(next, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::to_last(bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(last, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::move(move_operation operation,
|
|
|
|
|
const slice &key, bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move_result(*this, operation, key, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::find(const slice &key, bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(key_exact, key, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::lower_bound(const slice &key,
|
|
|
|
|
bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(key_lowerbound, key, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::move(move_operation operation,
|
|
|
|
|
const slice &key, const slice &value,
|
|
|
|
|
bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move_result(*this, operation, key, value, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::find_multivalue(const slice &key,
|
|
|
|
|
const slice &value,
|
|
|
|
|
bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(key_exact, key, value, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline cursor::move_result cursor::lower_bound_multivalue(const slice &key,
|
|
|
|
|
const slice &value,
|
|
|
|
|
bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(multi_exactkey_lowerboundvalue, key, value, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool cursor::seek(const slice &key) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(find_key, const_cast<slice *>(&key), nullptr, false);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool cursor::move(move_operation operation, slice &key, slice &value,
|
|
|
|
|
bool throw_notfound) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return move(operation, &key, &value, throw_notfound);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline size_t cursor::count_multivalue() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
size_t result;
|
|
|
|
|
error::success_or_throw(::mdbx_cursor_count(*this, &result));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool cursor::eof() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return error::boolean_or_throw(::mdbx_cursor_eof(*this));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool cursor::on_first() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return error::boolean_or_throw(::mdbx_cursor_on_first(*this));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool cursor::on_last() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return error::boolean_or_throw(::mdbx_cursor_on_last(*this));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ptrdiff_t cursor::estimate(slice key, slice value) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return estimate(multi_exactkey_lowerboundvalue, &key, &value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ptrdiff_t cursor::estimate(slice key) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
return estimate(key_lowerbound, &key, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline ptrdiff_t cursor::estimate(move_operation operation) const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
slice unused_key;
|
|
|
|
|
return estimate(operation, &unused_key, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void cursor::renew(::mdbx::txn &txn) {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::success_or_throw(::mdbx_cursor_renew(txn, handle_));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-15 07:32:04 +08:00
|
|
|
|
inline void cursor::bind(::mdbx::txn &txn, ::mdbx::map_handle map_handle) {
|
|
|
|
|
error::success_or_throw(::mdbx_cursor_bind(txn, handle_, map_handle.dbi));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline txn cursor::txn() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
MDBX_txn *txn = ::mdbx_cursor_txn(handle_);
|
|
|
|
|
error::throw_on_nullptr(txn, MDBX_EINVAL);
|
2020-09-10 06:15:35 +08:00
|
|
|
|
return ::mdbx::txn(txn);
|
2020-08-23 01:19:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline map_handle cursor::map() const {
|
2020-08-23 01:19:46 +08:00
|
|
|
|
const MDBX_dbi dbi = ::mdbx_cursor_dbi(handle_);
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(dbi > MDBX_MAX_DBI))
|
2020-08-23 01:19:46 +08:00
|
|
|
|
error::throw_exception(MDBX_EINVAL);
|
|
|
|
|
return map_handle(dbi);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline MDBX_error_t cursor::put(const slice &key, slice *value,
|
|
|
|
|
MDBX_put_flags_t flags) noexcept {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
return MDBX_error_t(::mdbx_cursor_put(handle_, &key, value, flags));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void cursor::insert(const slice &key, slice value) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
error::success_or_throw(
|
|
|
|
|
put(key, &value /* takes the present value in case MDBX_KEYEXIST */,
|
|
|
|
|
MDBX_put_flags_t(put_mode::insert)));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline value_result cursor::try_insert(const slice &key, slice value) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
const int err =
|
|
|
|
|
put(key, &value /* takes the present value in case MDBX_KEYEXIST */,
|
|
|
|
|
MDBX_put_flags_t(put_mode::insert));
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
|
|
|
|
return value_result{slice(), true};
|
|
|
|
|
case MDBX_KEYEXIST:
|
|
|
|
|
return value_result{value, false};
|
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-31 17:24:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline slice cursor::insert_reserve(const slice &key, size_t value_length) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
slice result(nullptr, value_length);
|
|
|
|
|
error::success_or_throw(
|
|
|
|
|
put(key, &result /* takes the present value in case MDBX_KEYEXIST */,
|
|
|
|
|
MDBX_put_flags_t(put_mode::insert) | MDBX_RESERVE));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline value_result cursor::try_insert_reserve(const slice &key,
|
|
|
|
|
size_t value_length) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
slice result(nullptr, value_length);
|
|
|
|
|
const int err =
|
|
|
|
|
put(key, &result /* takes the present value in case MDBX_KEYEXIST */,
|
|
|
|
|
MDBX_put_flags_t(put_mode::insert) | MDBX_RESERVE);
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
|
|
|
|
return value_result{result, true};
|
|
|
|
|
case MDBX_KEYEXIST:
|
|
|
|
|
return value_result{result, false};
|
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-31 17:24:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void cursor::upsert(const slice &key, const slice &value) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
error::success_or_throw(put(key, const_cast<slice *>(&value),
|
|
|
|
|
MDBX_put_flags_t(put_mode::upsert)));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline slice cursor::upsert_reserve(const slice &key, size_t value_length) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
slice result(nullptr, value_length);
|
|
|
|
|
error::success_or_throw(
|
|
|
|
|
put(key, &result, MDBX_put_flags_t(put_mode::upsert) | MDBX_RESERVE));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline void cursor::update(const slice &key, const slice &value) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
error::success_or_throw(put(key, const_cast<slice *>(&value),
|
|
|
|
|
MDBX_put_flags_t(put_mode::update)));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool cursor::try_update(const slice &key, const slice &value) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
const int err =
|
|
|
|
|
put(key, const_cast<slice *>(&value), MDBX_put_flags_t(put_mode::update));
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
|
|
|
|
return true;
|
|
|
|
|
case MDBX_NOTFOUND:
|
|
|
|
|
return false;
|
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-31 17:24:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline slice cursor::update_reserve(const slice &key, size_t value_length) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
slice result(nullptr, value_length);
|
|
|
|
|
error::success_or_throw(
|
|
|
|
|
put(key, &result, MDBX_put_flags_t(put_mode::update) | MDBX_RESERVE));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline value_result cursor::try_update_reserve(const slice &key,
|
|
|
|
|
size_t value_length) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
slice result(nullptr, value_length);
|
|
|
|
|
const int err =
|
|
|
|
|
put(key, &result, MDBX_put_flags_t(put_mode::update) | MDBX_RESERVE);
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
|
|
|
|
return value_result{result, true};
|
|
|
|
|
case MDBX_NOTFOUND:
|
|
|
|
|
return value_result{slice(), false};
|
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-31 17:24:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline bool cursor::erase(bool whole_multivalue) {
|
2020-08-31 17:24:12 +08:00
|
|
|
|
const int err = ::mdbx_cursor_del(handle_, whole_multivalue ? MDBX_ALLDUPS
|
|
|
|
|
: MDBX_CURRENT);
|
|
|
|
|
switch (err) {
|
|
|
|
|
case MDBX_SUCCESS:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_LIKELY return true;
|
2020-08-31 17:24:12 +08:00
|
|
|
|
case MDBX_NOTFOUND:
|
|
|
|
|
return false;
|
|
|
|
|
default:
|
2020-09-14 21:40:46 +08:00
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(err);
|
2020-08-31 17:24:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-15 07:32:04 +08:00
|
|
|
|
inline cursor_managed::cursor_managed()
|
2020-09-30 01:58:09 +08:00
|
|
|
|
: cursor_managed(::mdbx_cursor_create(nullptr)) {
|
2020-09-15 07:32:04 +08:00
|
|
|
|
if (MDBX_UNLIKELY(!handle_))
|
|
|
|
|
MDBX_CXX20_UNLIKELY error::throw_exception(MDBX_ENOMEM);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
2020-09-10 06:15:35 +08:00
|
|
|
|
inline buffer<ALLOCATOR>::buffer(const txn &txn, const ::mdbx::slice &src,
|
2020-08-24 16:20:41 +08:00
|
|
|
|
const ALLOCATOR &allocator)
|
|
|
|
|
: buffer(src, !txn.is_dirty(src.data()), allocator) {}
|
|
|
|
|
|
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline buffer<ALLOCATOR>::buffer(size_t head_room, size_t tail_room,
|
|
|
|
|
const ALLOCATOR &allocator)
|
|
|
|
|
: silo_(allocator) {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(head_room > max_length || tail_room > max_length ||
|
2020-08-24 16:20:41 +08:00
|
|
|
|
head_room + tail_room > max_length))
|
|
|
|
|
throw_max_length_exceeded();
|
|
|
|
|
silo_.reserve(head_room + tail_room);
|
|
|
|
|
silo_.append(head_room, '\0');
|
|
|
|
|
slice_.iov_base = const_cast<char *>(silo_.data());
|
|
|
|
|
assert(slice_.iov_len == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline buffer<ALLOCATOR>::buffer(size_t capacity, const ALLOCATOR &allocator)
|
|
|
|
|
: silo_(allocator) {
|
|
|
|
|
silo_.reserve(check_length(capacity));
|
|
|
|
|
slice_.iov_base = const_cast<char *>(silo_.data());
|
|
|
|
|
assert(slice_.iov_len == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class ALLOCATOR>
|
2020-09-01 18:37:52 +08:00
|
|
|
|
inline buffer<ALLOCATOR>::buffer(size_t head_room, const ::mdbx::slice &src,
|
2020-08-24 16:20:41 +08:00
|
|
|
|
size_t tail_room, const ALLOCATOR &allocator)
|
|
|
|
|
: silo_(allocator) {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(head_room > max_length || tail_room > max_length ||
|
2020-08-24 16:20:41 +08:00
|
|
|
|
head_room + tail_room > max_length - slice_.length()))
|
|
|
|
|
throw_max_length_exceeded();
|
|
|
|
|
silo_.reserve(head_room + src.length() + tail_room);
|
|
|
|
|
silo_.append(head_room, '\0');
|
|
|
|
|
silo_.append(src.char_ptr(), src.length());
|
|
|
|
|
slice_.iov_base = const_cast<char *>(silo_.data());
|
|
|
|
|
slice_.iov_len = src.length();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline void buffer<ALLOCATOR>::reserve(size_t wanna_headroom,
|
|
|
|
|
size_t wanna_tailroom,
|
|
|
|
|
size_t shrink_threshold) {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(
|
2020-08-24 16:20:41 +08:00
|
|
|
|
wanna_headroom > max_length || wanna_tailroom > max_length ||
|
|
|
|
|
wanna_headroom + wanna_tailroom > max_length - slice_.length()))
|
|
|
|
|
throw_max_length_exceeded();
|
|
|
|
|
|
|
|
|
|
wanna_headroom = std::min(std::max(headroom(), wanna_headroom),
|
|
|
|
|
wanna_headroom + shrink_threshold);
|
|
|
|
|
wanna_tailroom = std::min(std::max(tailroom(), wanna_tailroom),
|
|
|
|
|
wanna_tailroom + shrink_threshold);
|
|
|
|
|
const auto wanna_capacity = wanna_headroom + slice_.length() + wanna_tailroom;
|
|
|
|
|
if (is_reference() || slice_.empty()) {
|
|
|
|
|
silo_.reserve(wanna_capacity);
|
|
|
|
|
silo_.resize(wanna_headroom);
|
|
|
|
|
silo_.append(slice_.char_ptr(), slice_.length());
|
|
|
|
|
} else {
|
|
|
|
|
const auto was_headroom = headroom();
|
|
|
|
|
if (was_headroom > wanna_headroom)
|
|
|
|
|
silo_.erase(wanna_headroom, was_headroom - wanna_headroom);
|
|
|
|
|
silo_.reserve(wanna_capacity);
|
|
|
|
|
if (was_headroom < wanna_headroom)
|
|
|
|
|
silo_.insert(was_headroom, wanna_headroom - was_headroom, '\0');
|
|
|
|
|
}
|
|
|
|
|
slice_.iov_base = const_cast<byte *>(silo_begin()) + wanna_headroom;
|
|
|
|
|
assert(headroom() >= wanna_headroom &&
|
|
|
|
|
headroom() <= wanna_headroom + shrink_threshold);
|
|
|
|
|
assert(tailroom() >= wanna_tailroom &&
|
|
|
|
|
tailroom() <= wanna_tailroom + shrink_threshold);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 17:24:12 +08:00
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline buffer<ALLOCATOR> &buffer<ALLOCATOR>::append(const void *src,
|
|
|
|
|
size_t bytes) {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(tailroom() < check_length(bytes)))
|
2020-08-31 17:24:12 +08:00
|
|
|
|
reserve(0, bytes);
|
|
|
|
|
std::memcpy(static_cast<char *>(slice_.iov_base) + size(), src, bytes);
|
|
|
|
|
slice_.iov_len += bytes;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline buffer<ALLOCATOR> &buffer<ALLOCATOR>::add_header(const void *src,
|
|
|
|
|
size_t bytes) {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(headroom() < check_length(bytes)))
|
2020-08-31 17:24:12 +08:00
|
|
|
|
reserve(bytes, 0);
|
|
|
|
|
slice_.iov_base =
|
|
|
|
|
std::memcpy(static_cast<char *>(slice_.iov_base) - bytes, src, bytes);
|
|
|
|
|
slice_.iov_len += bytes;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 16:20:41 +08:00
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline void buffer<ALLOCATOR>::swap(buffer &other)
|
|
|
|
|
#if defined(__cpp_noexcept_function_type) && \
|
|
|
|
|
__cpp_noexcept_function_type >= 201510L
|
|
|
|
|
noexcept(
|
|
|
|
|
std::allocator_traits<ALLOCATOR>::propagate_on_container_swap::value
|
|
|
|
|
#if defined(__cpp_lib_allocator_traits_is_always_equal) && \
|
|
|
|
|
__cpp_lib_allocator_traits_is_always_equal >= 201411L
|
|
|
|
|
|| std::allocator_traits<ALLOCATOR>::is_always_equal::value
|
|
|
|
|
#endif /* __cpp_lib_allocator_traits_is_always_equal */
|
|
|
|
|
)
|
|
|
|
|
#endif /* __cpp_noexcept_function_type */
|
|
|
|
|
{
|
|
|
|
|
if /* checking the equality of allocators to avoid UB */
|
|
|
|
|
#if defined(__cpp_if_constexpr) && __cpp_if_constexpr >= 201606L
|
|
|
|
|
constexpr
|
|
|
|
|
#endif
|
|
|
|
|
(!std::allocator_traits<ALLOCATOR>::propagate_on_container_swap::value
|
|
|
|
|
#if defined(__cpp_lib_allocator_traits_is_always_equal) && \
|
|
|
|
|
__cpp_lib_allocator_traits_is_always_equal >= 201411L
|
|
|
|
|
&& !std::allocator_traits<ALLOCATOR>::is_always_equal::value
|
|
|
|
|
#endif /* __cpp_lib_allocator_traits_is_always_equal */
|
|
|
|
|
) {
|
2020-09-14 21:40:46 +08:00
|
|
|
|
if (MDBX_UNLIKELY(silo_.get_allocator() != other.silo_.get_allocator()))
|
2020-08-24 16:20:41 +08:00
|
|
|
|
throw std::bad_alloc();
|
|
|
|
|
}
|
|
|
|
|
silo_.swap(other.silo_);
|
|
|
|
|
slice_.swap(other.slice_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class ALLOCATOR>
|
|
|
|
|
inline int buffer<ALLOCATOR>::data_preserver::callback(void *context,
|
|
|
|
|
MDBX_val *target,
|
|
|
|
|
const void *src,
|
|
|
|
|
size_t bytes) noexcept {
|
2020-09-26 06:40:11 +08:00
|
|
|
|
auto self = static_cast<data_preserver *>(context);
|
2020-08-24 16:20:41 +08:00
|
|
|
|
assert(self->is_clean());
|
2020-09-26 06:40:11 +08:00
|
|
|
|
assert(&self->data.slice_ == target);
|
|
|
|
|
(void)target;
|
2020-08-24 16:20:41 +08:00
|
|
|
|
try {
|
2020-09-26 06:40:11 +08:00
|
|
|
|
self->data.assign(src, bytes, false);
|
2020-08-24 16:20:41 +08:00
|
|
|
|
return MDBX_RESULT_FALSE;
|
|
|
|
|
} catch (... /* capture any exception to rethrow it over C code */) {
|
|
|
|
|
self->capture();
|
|
|
|
|
return MDBX_RESULT_TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
} // namespace mdbx
|
|
|
|
|
|
2020-09-14 20:24:17 +08:00
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
namespace std {
|
|
|
|
|
|
|
|
|
|
inline string to_string(const mdbx::slice &value) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << value;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class ALLOCATOR>
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const mdbx::buffer<ALLOCATOR> &buffer) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << buffer;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const mdbx::pair &value) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << value;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const mdbx::env::geometry &value) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << value;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const mdbx::env::operate_parameters &value) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << value;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const mdbx::env::mode &value) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << value;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const mdbx::env::durability &value) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << value;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const mdbx::env::reclaiming_options &value) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << value;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const mdbx::env::operate_options &value) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << value;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const mdbx::env_managed::create_parameters &value) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << value;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const ::MDBX_log_level_t &value) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << value;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const ::MDBX_debug_flags_t &value) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << value;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const mdbx::error &value) {
|
|
|
|
|
ostringstream out;
|
2020-09-14 20:24:17 +08:00
|
|
|
|
out << value;
|
|
|
|
|
return out.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 23:36:05 +08:00
|
|
|
|
inline string to_string(const ::MDBX_error_t &errcode) {
|
|
|
|
|
return to_string(mdbx::error(errcode));
|
2020-09-14 20:24:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 22:21:12 +08:00
|
|
|
|
template <> struct hash<mdbx::slice> {
|
|
|
|
|
MDBX_CXX14_CONSTEXPR size_t
|
|
|
|
|
operator()(mdbx::slice const &slice) const noexcept {
|
|
|
|
|
return slice.hash_value();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
} // namespace std
|
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
#pragma warning(pop)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/// @} end of C++ API
|