lmdb-safe/examples/view.cc

49 lines
1.2 KiB
C++
Raw Normal View History

#include "../lmdb-safe.hh"
#include <c++utilities/application/global.h>
2018-12-28 16:33:20 +01:00
#include <iostream>
using namespace std;
2022-01-18 22:08:36 +01:00
using namespace LMDBSafe;
2018-12-28 16:33:20 +01:00
2022-01-30 21:32:08 +01:00
void countDB(MDBEnv &env, MDBROTransaction &txn, const std::string &dbname)
2018-12-28 16:33:20 +01:00
{
CPP_UTILITIES_UNUSED(env)
2022-01-30 21:32:08 +01:00
auto db = txn->openDB(dbname, 0);
auto cursor = txn->getCursor(db);
std::uint32_t count = 0;
2022-01-30 21:32:08 +01:00
MDBOutVal key, val;
while (!cursor.get(key, val, count ? MDB_NEXT : MDB_FIRST)) {
cout << key.get<std::string>();
2022-01-30 21:32:08 +01:00
if (key.d_mdbval.mv_size == 4)
cout << " " << key.get<std::uint32_t>();
2022-01-30 21:32:08 +01:00
cout << ": " << val.get<std::string>();
cout << "\n";
++count;
}
cout << count << endl;
2018-12-28 16:33:20 +01:00
}
2022-01-30 21:32:08 +01:00
int main(int argc, char **argv)
2018-12-28 16:33:20 +01:00
{
if (argc < 2) {
cout << "No database file specified.\n";
return 0;
}
MDBEnv env(argv[1], MDB_RDONLY | MDB_NOSUBDIR, 0600);
2022-01-30 21:32:08 +01:00
auto main = env.openDB("", 0);
auto txn = env.getROTransaction();
2018-12-28 16:33:20 +01:00
2022-01-30 21:32:08 +01:00
auto cursor = txn->getCursor(main);
2018-12-28 16:33:20 +01:00
2022-01-30 21:32:08 +01:00
MDBOutVal key, val;
if (cursor.get(key, val, MDB_FIRST)) {
cout << "Database is empty" << endl;
}
do {
cout << key.get<string>() << endl;
countDB(env, txn, key.get<string>());
} while (!cursor.get(key, val, MDB_NEXT));
2018-12-28 16:33:20 +01:00
}