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,
133 UNKNOWN_ERROR = PMEMKV_STATUS_UNKNOWN_ERROR,
134 NOT_FOUND = PMEMKV_STATUS_NOT_FOUND,
135 NOT_SUPPORTED = PMEMKV_STATUS_NOT_SUPPORTED,
137 INVALID_ARGUMENT = PMEMKV_STATUS_INVALID_ARGUMENT,
139 CONFIG_PARSING_ERROR =
140 PMEMKV_STATUS_CONFIG_PARSING_ERROR,
142 PMEMKV_STATUS_CONFIG_TYPE_ERROR,
144 STOPPED_BY_CB = PMEMKV_STATUS_STOPPED_BY_CB,
147 PMEMKV_STATUS_OUT_OF_MEMORY,
150 PMEMKV_STATUS_WRONG_ENGINE_NAME,
152 TRANSACTION_SCOPE_ERROR =
153 PMEMKV_STATUS_TRANSACTION_SCOPE_ERROR,
181 explicit config(pmemkv_config *cfg) noexcept;
191 template <
typename T>
192 status put_data(
const std::string &key,
const T *value,
193 const std::size_t number = 1) noexcept;
194 template <typename T>
196 const std::
string &key, T *value,
197 void (*deleter)(
void *) = [](T *value) {
delete value; }) noexcept;
198 status put_uint64(
const std::string &key, std::uint64_t value) noexcept;
199 status put_int64(
const std::string &key, std::int64_t value) noexcept;
200 status put_string(
const std::string &key,
const std::string &value) noexcept;
202 template <
typename T>
203 status get_data(
const std::string &key, T *&value, std::size_t &number)
const
205 template <
typename T>
206 status get_object(
const std::string &key, T *&value)
const noexcept;
208 status get_uint64(
const std::string &key, std::uint64_t &value)
const noexcept;
209 status get_int64(
const std::string &key, std::int64_t &value)
const noexcept;
210 status get_string(
const std::string &key, std::string &value)
const noexcept;
212 pmemkv_config *release() noexcept;
217 pmemkv_config *_config;
238 db(
db &&other) noexcept;
243 status open(
const std::string &engine_name) noexcept;
244 status open(
const std::string &engine_name,
config &&cfg) noexcept;
246 void close() noexcept;
248 status count_all(std::
size_t &cnt) noexcept;
254 std::
size_t &cnt) noexcept;
288 status defrag(
double start_percent = 0,
double amount_percent = 100);
290 std::
string errormsg();
301 this->_config =
nullptr;
310 this->_config = other._config;
311 other._config =
nullptr;
324 pmemkv_config_delete(this->_config);
326 this->_config = other._config;
327 other._config =
nullptr;
347 pmemkv_config_delete(this->_config);
358 if (this->_config ==
nullptr) {
359 this->_config = pmemkv_config_new();
361 if (this->_config ==
nullptr)
378 template <
typename T>
380 const std::size_t count) noexcept
383 return status::UNKNOWN_ERROR;
385 return static_cast<status>(pmemkv_config_put_data(
386 this->_config, key.data(), (
void *)value, count *
sizeof(T)));
399 template <
typename T>
401 void (*deleter)(
void *)) noexcept
404 return status::UNKNOWN_ERROR;
406 return static_cast<status>(pmemkv_config_put_object(this->_config, key.data(),
407 (
void *)value, deleter));
421 return status::UNKNOWN_ERROR;
423 return static_cast<status>(
424 pmemkv_config_put_uint64(this->_config, key.data(), value));
438 return status::UNKNOWN_ERROR;
440 return static_cast<status>(
441 pmemkv_config_put_int64(this->_config, key.data(), value));
453 const std::string &value) noexcept
456 return status::UNKNOWN_ERROR;
458 return static_cast<status>(
459 pmemkv_config_put_string(this->_config, key.data(), value.data()));
472 template <
typename T>
474 std::size_t &count)
const noexcept
476 if (this->_config ==
nullptr)
477 return status::NOT_FOUND;
480 auto s =
static_cast<status>(pmemkv_config_get_data(
481 this->_config, key.data(), (
const void **)&value, &
size));
486 count =
size /
sizeof(T);
500 template <
typename T>
503 if (this->_config ==
nullptr)
504 return status::NOT_FOUND;
506 auto s =
static_cast<status>(
507 pmemkv_config_get_object(this->_config, key.data(), (
void **)&value));
523 if (this->_config ==
nullptr)
524 return status::NOT_FOUND;
526 return static_cast<status>(
527 pmemkv_config_get_uint64(this->_config, key.data(), &value));
541 if (this->_config ==
nullptr)
542 return status::NOT_FOUND;
544 return static_cast<status>(
545 pmemkv_config_get_int64(this->_config, key.data(), &value));
559 if (this->_config ==
nullptr)
560 return status::NOT_FOUND;
564 auto s =
static_cast<status>(
565 pmemkv_config_get_string(this->_config, key.data(), &
data));
583 auto c = this->_config;
584 this->_config =
nullptr;
588 #if !__cpp_lib_string_view
624 : _data(data), _size(std::char_traits<char>::length(data))
659 int ret = std::char_traits<char>::compare(data(), other.data(),
660 std::min(size(), other.size()));
663 if (size() < other.size())
665 if (size() > other.size())
677 static inline int call_get_kv_function(
const char *key,
size_t keybytes,
678 const char *value,
size_t valuebytes,
void *arg)
680 return (*
reinterpret_cast<std::function<get_kv_function> *
>(arg))(
684 static inline void call_get_v_function(
const char *value,
size_t valuebytes,
void *arg)
686 (*
reinterpret_cast<std::function<get_v_function> *
>(arg))(
687 string_view(value, valuebytes));
690 static inline void call_get_copy(
const char *v,
size_t vb,
void *arg)
692 auto c =
reinterpret_cast<std::string *
>(arg);
713 this->_db = other._db;
731 std::swap(this->_db, other._db);
745 return static_cast<status>(
746 pmemkv_open(engine_name.c_str(),
nullptr, &(this->_db)));
759 return static_cast<status>(
760 pmemkv_open(engine_name.c_str(), cfg.release(), &(this->_db)));
768 if (this->
_db !=
nullptr)
769 pmemkv_close(this->
_db);
791 return static_cast<status>(pmemkv_count_all(this->_db, &cnt));
806 return static_cast<status>(
807 pmemkv_count_above(this->_db, key.data(), key.size(), &cnt));
822 return static_cast<status>(
823 pmemkv_count_equal_above(this->_db, key.data(), key.size(), &cnt));
838 return static_cast<status>(
839 pmemkv_count_equal_below(this->_db, key.data(), key.size(), &cnt));
854 return static_cast<status>(
855 pmemkv_count_below(this->_db, key.data(), key.size(), &cnt));
870 std::size_t &cnt) noexcept
872 return static_cast<status>(pmemkv_count_between(
873 this->_db, key1.data(), key1.size(), key2.data(), key2.size(), &cnt));
890 return static_cast<status>(pmemkv_get_all(this->_db, callback, arg));
905 return static_cast<status>(pmemkv_get_all(this->_db, call_get_kv_function, &f));
927 return static_cast<status>(
928 pmemkv_get_above(this->_db, key.data(), key.size(), callback, arg));
947 return static_cast<status>(pmemkv_get_above(this->_db, key.data(), key.size(),
948 call_get_kv_function, &f));
971 return static_cast<status>(
972 pmemkv_get_equal_above(this->_db, key.data(), key.size(), callback, arg));
991 std::function<get_kv_function> f) noexcept
993 return static_cast<status>(pmemkv_get_equal_above(
994 this->_db, key.data(), key.size(), call_get_kv_function, &f));
1017 return static_cast<status>(
1018 pmemkv_get_equal_below(this->_db, key.data(), key.size(), callback, arg));
1037 std::function<get_kv_function> f) noexcept
1039 return static_cast<status>(pmemkv_get_equal_below(
1040 this->_db, key.data(), key.size(), call_get_kv_function, &f));
1062 return static_cast<status>(
1063 pmemkv_get_below(this->_db, key.data(), key.size(), callback, arg));
1082 return static_cast<status>(pmemkv_get_below(this->_db, key.data(), key.size(),
1083 call_get_kv_function, &f));
1106 return static_cast<status>(pmemkv_get_between(this->_db, key1.data(), key1.size(),
1107 key2.data(), key2.size(), callback,
1126 std::function<get_kv_function> f) noexcept
1128 return static_cast<status>(pmemkv_get_between(this->_db, key1.data(), key1.size(),
1129 key2.data(), key2.size(),
1130 call_get_kv_function, &f));
1144 return static_cast<status>(pmemkv_exists(this->_db, key.data(), key.size()));
1164 return static_cast<status>(
1165 pmemkv_get(this->_db, key.data(), key.size(), callback, arg));
1181 return static_cast<status>(
1182 pmemkv_get(this->_db, key.data(), key.size(), call_get_v_function, &f));
1197 return static_cast<status>(
1198 pmemkv_get(this->_db, key.data(), key.size(), call_get_copy, value));
1212 return static_cast<status>(pmemkv_put(this->_db, key.data(), key.size(),
1213 value.data(), value.size()));
1226 return static_cast<status>(pmemkv_remove(this->_db, key.data(), key.size()));
1241 return static_cast<status>(
1242 pmemkv_defrag(this->
_db, start_percent, amount_percent));
1254 return std::string(pmemkv_errormsg());
1262 static inline std::string errormsg()
1264 return std::string(pmemkv_errormsg());