|
| 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");
config cfg;
status s = cfg.put_path(argv[1]);
s = cfg.put_size(SIZE);
s = cfg.put_create_if_missing(true);
LOG("Opening pmemkv database with 'cmap' engine");
s = kv.open("cmap", std::move(cfg));
LOG("Putting new key");
s = kv.put("key1", "value1");
size_t cnt;
s = kv.count_all(cnt);
LOG("Reading key back");
std::string value;
s = kv.get("key1", &value);
LOG("Iterating existing keys");
s = kv.put("key2", "value2");
s = kv.put("key3", "value3");
LOG(" visited: " << k.data());
return 0;
});
LOG("Defragmenting the database");
s = kv.defrag(0, 100);
LOG("Removing existing key");
s = kv.remove("key1");
s = kv.exists("key1");
std::cout << s << std::endl;
std::ostringstream oss;
oss << s;
assert(oss.str() == "NOT_FOUND (2)");
LOG("Closing database");
return 0;
}
db() noexcept
Default constructor with uninitialized database.
Definition: libpmemkv.hpp:1775
Main C++ pmemkv public header.
status
Status returned by most of pmemkv functions.
Definition: libpmemkv.hpp:84
@ NOT_FOUND
record (or config item) not found
obj::string_view string_view
Partial string_view implementation, defined in pmem::obj namespace in libpmemobj-cpp library (see: ht...
Definition: libpmemkv.hpp:47
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]);
LOG("Opening pmemkv database with 'cmap' engine");
ASSERT(kv != nullptr);
s = kv->open("cmap", std::move(cfg));
LOG("Putting new key");
s = kv->put("key1", "value1");
size_t cnt;
s = kv->count_all(cnt);
LOG("Reading key back");
std::string value;
s = kv->get("key1", &value);
LOG("Iterating existing keys");
s = kv->put("key2", "value2");
s = kv->put("key3", "value3");
LOG(" visited: " << k.data());
return 0;
});
LOG("Removing existing key");
s = kv->remove("key1");
s = kv->exists("key1");
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");
config cfg_1;
config cfg_2;
status ret = cfg_1.put_oid(&(pop.root()->oids->at(0)));
ret = cfg_2.put_oid(&(pop.root()->oids->at(1)));
LOG("Starting first cmap engine");
ASSERT(kv_1 != nullptr);
status s = kv_1->open(
"cmap", std::move(cfg_1));
*(pop.root()->str) = "some string";
LOG("Starting second cmap engine");
ASSERT(kv_2 != nullptr);
s = kv_2->open("cmap", std::move(cfg_2));
LOG("Putting new key into first cmap");
s = kv_1->put("key_1", "value_1");
LOG("Putting new key into second cmap");
s = kv_2->put("key_2", "value_2");
LOG("Reading key back from first cmap");
std::string value;
s = kv_1->get("key_1", &value);
LOG("Reading key back from second cmap");
value.clear();
s = kv_2->get("key_2", &value);
LOG("Defragmenting the first cmap");
s = kv_1->defrag(0, 100);
LOG("Defragmenting the second cmap");
s = kv_2->defrag(0, 100);
LOG("Stopping first cmap engine");
delete kv_1;
LOG("Stopping second cmap engine");
delete kv_2;
} catch (std::exception &e) {
std::cerr << "Exception occurred: " << e.what() << std::endl;
}
try {
pop.close();
} catch (const std::logic_error &e) {
std::cerr << "Exception occurred: " << e.what() << std::endl;
}
return 0;
}