Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <lmdb.h>
- #include <iostream>
- #include <cstring>
- using namespace std;
- class mdb_exception : public exception {
- const char *msg;
- public:
- inline explicit mdb_exception(int rc) {
- msg = mdb_strerror(rc);
- }
- inline const char *what() const noexcept {
- return msg;
- }
- };
- inline int C(int rc) {
- if (rc != 0 && rc != MDB_NOTFOUND) {
- throw mdb_exception(rc);
- }
- return rc;
- }
- inline void print(const MDB_val &key, const MDB_val &data) {
- cout << "---> key: " << string(static_cast<char*>(key.mv_data), key.mv_size / sizeof(char)) << endl;
- cout << "---> data: " << string(static_cast<char*>(data.mv_data), data.mv_size / sizeof(char)) << endl;
- }
- int main() {
- MDB_val key, data;
- MDB_env *env;
- MDB_dbi dbi;
- MDB_txn *txn;
- MDB_cursor *cursor;
- // test data
- unsigned testN = 6;
- const char* testKeys[] = { "a", "b", "c", "e", "f", "g" };
- const char* testData[] = { "helló_1", "wőúéáűrld_2", "42_3", "hello_4", "world_5", "42_6" };
- // set up env
- C(mdb_env_create(&env));
- C(mdb_env_set_maxdbs(env, 10));
- C(mdb_env_open(env, "./testdata", 0, 0644));
- // ------ open a clean dbi + fill up with test data
- // begin a txn
- C(mdb_txn_begin(env, nullptr, 0, &txn));
- // ensure there is an empty dbi
- C(mdb_dbi_open(txn, "my_test_data", MDB_CREATE, &dbi));
- C(mdb_drop(txn, dbi, 1));
- C(mdb_dbi_open(txn, "my_test_data", MDB_CREATE, &dbi));
- // fill up with some data
- for (unsigned i = 0; i < testN; i++) {
- key.mv_data = const_cast<char*>(testKeys[i]);
- key.mv_size = sizeof(char) * strlen(testKeys[i]);
- data.mv_data = const_cast<char*>(testData[i]);
- data.mv_size = sizeof(char) * strlen(testData[i]);
- mdb_put(txn, dbi, &key, &data, 0);
- }
- // commit the txn
- C(mdb_txn_commit(txn));
- // ------ use a cursor
- // begin a txn + cursor
- C(mdb_txn_begin(env, nullptr, 0, &txn));
- C(mdb_cursor_open(txn, dbi, &cursor));
- // mess with the stuff
- cout << "first (expected a)" << endl;
- C(mdb_cursor_get(cursor, &key, &data, MDB_FIRST));
- C(mdb_cursor_get(cursor, &key, &data, MDB_GET_CURRENT));
- print(key, data);
- cout << "next (expected b)" << endl;
- C(mdb_cursor_get(cursor, &key, &data, MDB_NEXT));
- C(mdb_cursor_get(cursor, &key, &data, MDB_GET_CURRENT));
- print(key, data);
- cout << "next (expected c)" << endl;
- C(mdb_cursor_get(cursor, &key, &data, MDB_NEXT));
- C(mdb_cursor_get(cursor, &key, &data, MDB_GET_CURRENT));
- print(key, data);
- cout << "next (expected e)" << endl;
- C(mdb_cursor_get(cursor, &key, &data, MDB_NEXT));
- C(mdb_cursor_get(cursor, &key, &data, MDB_GET_CURRENT));
- print(key, data);
- cout << "prev (expected c)" << endl;
- C(mdb_cursor_get(cursor, &key, &data, MDB_PREV));
- C(mdb_cursor_get(cursor, &key, &data, MDB_GET_CURRENT));
- print(key, data);
- cout << "last (expected g)" << endl;
- C(mdb_cursor_get(cursor, &key, &data, MDB_LAST));
- C(mdb_cursor_get(cursor, &key, &data, MDB_GET_CURRENT));
- print(key, data);
- // oops
- mdb_dbi_close(env, dbi);
- cout << "go to key b (expected b)" << endl;
- key.mv_data = (void*) "b";
- key.mv_size = sizeof(char);
- C(mdb_cursor_get(cursor, &key, &data, MDB_SET));
- C(mdb_cursor_get(cursor, &key, &data, MDB_GET_CURRENT));
- print(key, data);
- cout << "go to key d (expected e)" << endl;
- key.mv_data = (void*) "d";
- key.mv_size = sizeof(char);
- C(mdb_cursor_get(cursor, &key, &data, MDB_SET_RANGE));
- C(mdb_cursor_get(cursor, &key, &data, MDB_GET_CURRENT));
- print(key, data);
- cout << "del (expected f)" << endl;
- C(mdb_cursor_del(cursor, 0));
- C(mdb_cursor_get(cursor, &key, &data, MDB_GET_CURRENT));
- print(key, data);
- // now go through all keys
- cout << "---------------------------------" << endl;
- C(mdb_cursor_get(cursor, &key, &data, MDB_FIRST));
- do {
- print(key, data);
- } while (C(mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0);
- // close cursor + commit the txn
- mdb_cursor_close(cursor);
- C(mdb_txn_commit(txn));
- // close dbi & env
- mdb_dbi_close(env, dbi);
- mdb_env_close(env);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement