41 #if __cpp_lib_string_view
42 #include <string_view>
45 #include "libpmemkv.h"
71 #if __cpp_lib_string_view
72 using string_view = std::string_view;
90 const char *
data()
const noexcept;
91 std::size_t
size()
const noexcept;
132 OK = PMEMKV_STATUS_OK,
140 PMEMKV_STATUS_CONFIG_PARSING_ERROR,
142 PMEMKV_STATUS_CONFIG_TYPE_ERROR,
147 PMEMKV_STATUS_OUT_OF_MEMORY,
150 PMEMKV_STATUS_WRONG_ENGINE_NAME,
153 PMEMKV_STATUS_TRANSACTION_SCOPE_ERROR,
179 explicit config(pmemkv_config *cfg) noexcept;
189 template <
typename T>
191 const std::size_t number = 1) noexcept;
192 template <
typename T>
194 const std::string &key, T *value,
195 void (*deleter)(
void *) = [](T *value) {
delete value; }) noexcept;
198 status put_string(
const std::string &key,
const std::string &value) noexcept;
200 template <
typename T>
201 status get_data(
const std::string &key, T *&value, std::size_t &number)
const
203 template <
typename T>
206 status get_uint64(
const std::string &key, std::uint64_t &value)
const noexcept;
207 status get_int64(
const std::string &key, std::int64_t &value)
const noexcept;
208 status get_string(
const std::string &key, std::string &value)
const noexcept;
210 pmemkv_config *
release() noexcept;
235 db(
const db &other) =
delete;
236 db(
db &&other) noexcept;
241 status open(
const std::string &engine_name) noexcept;
244 void close() noexcept;
250 std::size_t &cnt) noexcept;
264 std::function<get_kv_function> f) noexcept;
295 this->_config = other._config;
296 other._config =
nullptr;
309 pmemkv_config_delete(this->_config);
311 this->_config = other._config;
312 other._config =
nullptr;
332 pmemkv_config_delete(this->
_config);
343 if (this->
_config ==
nullptr) {
344 this->
_config = pmemkv_config_new();
363 template <
typename T>
365 const std::size_t count) noexcept
370 return static_cast<status>(pmemkv_config_put_data(
371 this->_config, key.data(), (
void *)value, count *
sizeof(T)));
384 template <
typename T>
386 void (*deleter)(
void *)) noexcept
391 return static_cast<status>(pmemkv_config_put_object(this->_config, key.data(),
392 (
void *)value, deleter));
408 return static_cast<status>(
409 pmemkv_config_put_uint64(this->_config, key.data(), value));
425 return static_cast<status>(
426 pmemkv_config_put_int64(this->_config, key.data(), value));
438 const std::string &value) noexcept
443 return static_cast<status>(
444 pmemkv_config_put_string(this->_config, key.data(), value.data()));
457 template <
typename T>
459 std::size_t &count)
const noexcept
461 if (this->_config ==
nullptr)
465 auto s =
static_cast<status>(pmemkv_config_get_data(
466 this->_config, key.data(), (
const void **)&value, &size));
471 count = size /
sizeof(T);
485 template <
typename T>
488 if (this->_config ==
nullptr)
491 auto s =
static_cast<status>(
492 pmemkv_config_get_object(this->_config, key.data(), (
void **)&value));
508 if (this->_config ==
nullptr)
511 return static_cast<status>(
512 pmemkv_config_get_uint64(this->_config, key.data(), &value));
526 if (this->_config ==
nullptr)
529 return static_cast<status>(
530 pmemkv_config_get_int64(this->_config, key.data(), &value));
544 if (this->_config ==
nullptr)
549 auto s =
static_cast<status>(
550 pmemkv_config_get_string(this->_config, key.data(), &data));
573 #if !__cpp_lib_string_view
609 : _data(data), _size(std::char_traits<char>::length(data))
644 int ret = std::char_traits<char>::compare(data(), other.data(),
645 std::min(size(), other.size()));
648 if (size() < other.size())
650 if (size() > other.size())
662 static inline int call_get_kv_function(
const char *key,
size_t keybytes,
663 const char *value,
size_t valuebytes,
void *arg)
665 return (*
reinterpret_cast<std::function<get_kv_function> *
>(arg))(
669 static inline void call_get_v_function(
const char *value,
size_t valuebytes,
void *arg)
671 (*
reinterpret_cast<std::function<get_v_function> *
>(arg))(
672 string_view(value, valuebytes));
675 static inline void call_get_copy(
const char *v,
size_t vb,
void *arg)
677 auto c =
reinterpret_cast<std::string *
>(arg);
698 this->_db = other._db;
716 std::swap(this->_db, other._db);
730 return static_cast<status>(
731 pmemkv_open(engine_name.c_str(),
nullptr, &(this->_db)));
744 return static_cast<status>(
745 pmemkv_open(engine_name.c_str(), cfg.release(), &(this->_db)));
753 if (this->
_db !=
nullptr)
754 pmemkv_close(this->
_db);
776 return static_cast<status>(pmemkv_count_all(this->_db, &cnt));
791 return static_cast<status>(
792 pmemkv_count_above(this->_db, key.data(), key.size(), &cnt));
807 return static_cast<status>(
808 pmemkv_count_below(this->_db, key.data(), key.size(), &cnt));
823 std::size_t &cnt) noexcept
825 return static_cast<status>(pmemkv_count_between(
826 this->_db, key1.data(), key1.size(), key2.data(), key2.size(), &cnt));
843 return static_cast<status>(pmemkv_get_all(this->_db, callback, arg));
858 return static_cast<status>(pmemkv_get_all(this->_db, call_get_kv_function, &f));
880 return static_cast<status>(
881 pmemkv_get_above(this->_db, key.data(), key.size(), callback, arg));
900 return static_cast<status>(pmemkv_get_above(this->_db, key.data(), key.size(),
901 call_get_kv_function, &f));
923 return static_cast<status>(
924 pmemkv_get_below(this->_db, key.data(), key.size(), callback, arg));
943 return static_cast<status>(pmemkv_get_below(this->_db, key.data(), key.size(),
944 call_get_kv_function, &f));
967 return static_cast<status>(pmemkv_get_between(this->_db, key1.data(), key1.size(),
968 key2.data(), key2.size(), callback,
987 std::function<get_kv_function> f) noexcept
989 return static_cast<status>(pmemkv_get_between(this->_db, key1.data(), key1.size(),
990 key2.data(), key2.size(),
991 call_get_kv_function, &f));
1005 return static_cast<status>(pmemkv_exists(this->_db, key.data(), key.size()));
1025 return static_cast<status>(
1026 pmemkv_get(this->_db, key.data(), key.size(), callback, arg));
1042 return static_cast<status>(
1043 pmemkv_get(this->_db, key.data(), key.size(), call_get_v_function, &f));
1058 return static_cast<status>(
1059 pmemkv_get(this->_db, key.data(), key.size(), call_get_copy, value));
1073 return static_cast<status>(pmemkv_put(this->_db, key.data(), key.size(),
1074 value.data(), value.size()));
1087 return static_cast<status>(pmemkv_remove(this->_db, key.data(), key.size()));
1099 return std::string(pmemkv_errormsg());
1107 static inline std::string errormsg()
1109 return std::string(pmemkv_errormsg());