|
| db () noexcept |
| Default constructor with uninitialized database. More...
|
|
status | open (const std::string &engine_name, config &&cfg=config{}) noexcept |
| Opens the pmemkv database with specified config. More...
|
|
void | close () noexcept |
| Closes pmemkv database. More...
|
|
status | count_all (std::size_t &cnt) noexcept |
| It returns number of currently stored elements in pmem::kv::db. More...
|
|
status | count_above (string_view key, std::size_t &cnt) noexcept |
| It returns number of currently stored elements in pmem::kv::db, whose keys are greater than the given key. More...
|
|
status | count_equal_above (string_view key, std::size_t &cnt) noexcept |
| It returns number of currently stored elements in pmem::kv::db, whose keys are greater than or equal to the given key. More...
|
|
status | count_equal_below (string_view key, std::size_t &cnt) noexcept |
| It returns number of currently stored elements in pmem::kv::db, whose keys are lower than or equal to the given key. More...
|
|
status | count_below (string_view key, std::size_t &cnt) noexcept |
| It returns number of currently stored elements in pmem::kv::db, whose keys are less than the given key. More...
|
|
status | count_between (string_view key1, string_view key2, std::size_t &cnt) noexcept |
| It returns number of currently stored elements in pmem::kv::db, whose keys are greater than the key1 and less than the key2. More...
|
|
status | get_all (get_kv_callback *callback, void *arg) noexcept |
| Executes (C-like) callback function for every record stored in pmem::kv::db. More...
|
|
status | get_all (std::function< get_kv_function > f) noexcept |
| Executes function for every record stored in pmem::kv::db. More...
|
|
status | get_above (string_view key, get_kv_callback *callback, void *arg) noexcept |
| Executes (C-like) callback function for every record stored in pmem::kv::db, whose keys are greater than the given key. More...
|
|
status | get_above (string_view key, std::function< get_kv_function > f) noexcept |
| Executes function for every record stored in pmem::kv::db, whose keys are greater than the given key. More...
|
|
status | get_equal_above (string_view key, get_kv_callback *callback, void *arg) noexcept |
| Executes (C-like) callback function for every record stored in pmem::kv::db, whose keys are greater than or equal to the given key. More...
|
|
status | get_equal_above (string_view key, std::function< get_kv_function > f) noexcept |
| Executes function for every record stored in pmem::kv::db, whose keys are greater than or equal to the given key. More...
|
|
status | get_equal_below (string_view key, get_kv_callback *callback, void *arg) noexcept |
| Executes (C-like) callback function for every record stored in pmem::kv::db, whose keys are lower than or equal to the given key. More...
|
|
status | get_equal_below (string_view key, std::function< get_kv_function > f) noexcept |
| Executes function for every record stored in pmem::kv::db, whose keys are lower than or equal to the given key. More...
|
|
status | get_below (string_view key, get_kv_callback *callback, void *arg) noexcept |
| Executes (C-like) callback function for every record stored in pmem::kv::db, whose keys are lower than the given key. More...
|
|
status | get_below (string_view key, std::function< get_kv_function > f) noexcept |
| Executes function for every record stored in pmem::kv::db, whose keys are less than the given key. More...
|
|
status | get_between (string_view key1, string_view key2, get_kv_callback *callback, void *arg) noexcept |
| Executes (C-like) callback function for every record stored in pmem::kv::db, whose keys are greater than the key1 and less than the key2. More...
|
|
status | get_between (string_view key1, string_view key2, std::function< get_kv_function > f) noexcept |
| Executes function for every record stored in pmem::kv::db, whose keys are greater than the key1 and less than the key2. More...
|
|
status | exists (string_view key) noexcept |
| Checks existence of record with given key. More...
|
|
status | get (string_view key, get_v_callback *callback, void *arg) noexcept |
| Executes (C-like) callback function for record with given key. More...
|
|
status | get (string_view key, std::function< get_v_function > f) noexcept |
| Executes function for record with given key. More...
|
|
status | get (string_view key, std::string *value) noexcept |
| Gets value copy of record with given key. More...
|
|
status | put (string_view key, string_view value) noexcept |
| Inserts a key-value pair into pmemkv database. More...
|
|
status | remove (string_view key) noexcept |
| Removes from database record with given key. More...
|
|
status | defrag (double start_percent=0, double amount_percent=100) |
| Defragments approximately 'amount_percent' percent of elements in the database starting from 'start_percent' percent of elements. More...
|
|
result< tx > | tx_begin () noexcept |
| Starts a pmemkv transaction. More...
|
|
result< read_iterator > | new_read_iterator () |
| Returns new read iterator in pmem::kv::result. More...
|
|
result< write_iterator > | new_write_iterator () |
| Returns new write iterator in pmem::kv::result. More...
|
|
std::string | errormsg () |
| Returns a human readable string describing the last error. More...
|
|
Main pmemkv class, it provides functions to operate on data in database.
Database class for creating, opening and closing pmemkv's data file. It provides functions to write, read & remove data, count elements stored and check for existence of an element based on its key.
Note: It does not explicitly provide upper_bound/lower_bound functions. If you want to obtain an element(s) above or below the selected key, you can use pmem::kv::get_above() or pmem::kv::get_below(). See descriptions of these functions for details.
Example of basic usage:
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <sstream>
#define ASSERT(expr) \
do { \
if (!(expr)) \
std::cout << pmemkv_errormsg() << std::endl; \
assert(expr); \
} while (0)
#define LOG(msg) std::cout << msg << std::endl
const uint64_t SIZE = 1024UL * 1024UL * 1024UL;
int main(int argc, char *argv[])
{
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " file\n";
exit(1);
}
LOG("Creating config");
ASSERT(s == status::OK);
ASSERT(s == status::OK);
ASSERT(s == status::OK);
LOG("Opening pmemkv database with 'cmap' engine");
ASSERT(kv != nullptr);
s = kv->
open(
"cmap", std::move(cfg));
ASSERT(s == status::OK);
LOG("Putting new key");
s = kv->
put(
"key1",
"value1");
ASSERT(s == status::OK);
size_t cnt;
ASSERT(s == status::OK && cnt == 1);
LOG("Reading key back");
std::string value;
s = kv->
get(
"key1", &value);
ASSERT(s == status::OK && value == "value1");
LOG("Iterating existing keys");
s = kv->
put(
"key2",
"value2");
ASSERT(s == status::OK);
s = kv->
put(
"key3",
"value3");
ASSERT(s == status::OK);
LOG(" visited: " << k.data());
return 0;
});
LOG("Defragmenting the database");
ASSERT(s == status::OK);
LOG("Removing existing key");
ASSERT(s == status::OK);
ASSERT(s == status::NOT_FOUND);
std::cout << s << std::endl;
std::ostringstream oss;
oss << s;
assert(oss.str() == "NOT_FOUND (2)");
LOG("Closing database");
delete kv;
return 0;
}
Example with existing database:
int main(int argc, char *argv[])
{
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " pool\n";
exit(1);
}
LOG("Creating config");
config cfg;
status s = cfg.put_path(argv[1]);
ASSERT(s == status::OK);
LOG("Opening pmemkv database with 'cmap' engine");
ASSERT(kv != nullptr);
s = kv->open("cmap", std::move(cfg));
ASSERT(s == status::OK);
LOG("Putting new key");
s = kv->put("key1", "value1");
ASSERT(s == status::OK);
size_t cnt;
s = kv->count_all(cnt);
ASSERT(s == status::OK && cnt == 1);
LOG("Reading key back");
std::string value;
s = kv->get("key1", &value);
ASSERT(s == status::OK && value == "value1");
LOG("Iterating existing keys");
s = kv->put("key2", "value2");
ASSERT(s == status::OK);
s = kv->put("key3", "value3");
ASSERT(s == status::OK);
LOG(" visited: " << k.data());
return 0;
});
LOG("Removing existing key");
s = kv->remove("key1");
ASSERT(s == status::OK);
s = kv->exists("key1");
ASSERT(s == status::NOT_FOUND);
LOG("Closing database");
delete kv;
return 0;
}
Example for pmemkv's database supporting multiple engines:
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <libpmemobj++/container/string.hpp>
#include <libpmemobj++/container/vector.hpp>
#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/transaction.hpp>
#undef LOG
#define ASSERT(expr) \
do { \
if (!(expr)) \
std::cout << pmemkv_errormsg() << std::endl; \
assert(expr); \
} while (0)
#define LOG(msg) std::cout << msg << std::endl
using pmemoid_vector = pmem::obj::vector<PMEMoid>;
const uint64_t SIZE = 1024UL * 1024UL * 1024UL;
struct Root {
pmem::obj::persistent_ptr<pmemoid_vector> oids;
pmem::obj::persistent_ptr<pmem::obj::string> str;
};
int main(int argc, char *argv[])
{
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " file\n";
exit(1);
}
const char *path = argv[1];
pmem::obj::pool<Root> pop;
try {
pop = pmem::obj::pool<Root>::create(path, "pmemkv", SIZE, S_IRWXU);
pmem::obj::transaction::run(pop, [&] {
pop.root()->oids = pmem::obj::make_persistent<pmemoid_vector>();
pop.root()->str = pmem::obj::make_persistent<pmem::obj::string>();
pop.root()->oids->emplace_back(OID_NULL);
pop.root()->oids->emplace_back(OID_NULL);
});
LOG("Creating configs");
ASSERT(ret == status::OK);
ret = cfg_2.
put_oid(&(pop.root()->oids->at(1)));
ASSERT(ret == status::OK);
LOG("Starting first cmap engine");
ASSERT(kv_1 != nullptr);
ASSERT(s == status::OK);
*(pop.root()->str) = "some string";
LOG("Starting second cmap engine");
ASSERT(kv_2 != nullptr);
s = kv_2->
open(
"cmap", std::move(cfg_2));
ASSERT(s == status::OK);
LOG("Putting new key into first cmap");
s = kv_1->
put(
"key_1",
"value_1");
ASSERT(s == status::OK);
LOG("Putting new key into second cmap");
s = kv_2->
put(
"key_2",
"value_2");
ASSERT(s == status::OK);
LOG("Reading key back from first cmap");
std::string value;
s = kv_1->
get(
"key_1", &value);
ASSERT(s == status::OK && value == "value_1");
LOG("Reading key back from second cmap");
value.clear();
s = kv_2->
get(
"key_2", &value);
ASSERT(s == status::OK && value == "value_2");
LOG("Defragmenting the first cmap");
ASSERT(s == status::OK);
LOG("Defragmenting the second cmap");
ASSERT(s == status::OK);
LOG("Stopping first cmap engine");
delete kv_1;
LOG("Stopping second cmap engine");
delete kv_2;
} catch (std::exception &e) {
std::cerr << "Exception occured: " << e.what() << std::endl;
}
try {
} catch (const std::logic_error &e) {
std::cerr << "Exception occured: " << e.what() << std::endl;
}
return 0;
}