| 1 | /* This is free and unencumbered software released into the public domain. */ |
| 2 | |
| 3 | #include <cstdio> |
| 4 | #include <cstdlib> |
| 5 | #include <lmdb++.h> |
| 6 | |
| 7 | int main() { |
| 8 | /* Create and open the LMDB environment: */ |
| 9 | auto env = lmdb::env::create(); |
| 10 | env.set_mapsize(1UL * 1024UL * 1024UL * 1024UL); /* 1 GiB */ |
| 11 | env.open("./example.mdb", 0, 0664); |
| 12 | |
| 13 | /* Insert some key/value pairs in a write transaction: */ |
| 14 | auto wtxn = lmdb::txn::begin(env); |
| 15 | auto dbi = lmdb::dbi::open(wtxn, nullptr); |
| 16 | dbi.put(wtxn, "username", "jhacker"); |
| 17 | dbi.put(wtxn, "email", "[email protected]"); |
| 18 | dbi.put(wtxn, "fullname", "J. Random Hacker"); |
| 19 | wtxn.commit(); |
| 20 | |
| 21 | /* Fetch key/value pairs in a read-only transaction: */ |
| 22 | auto rtxn = lmdb::txn::begin(env, nullptr, MDB_RDONLY); |
| 23 | auto cursor = lmdb::cursor::open(rtxn, dbi); |
| 24 | std::string key, value; |
| 25 | while (cursor.get(key, value, MDB_NEXT)) { |
| 26 | std::printf("key: '%s', value: '%s'\n", key.c_str(), value.c_str()); |
| 27 | } |
| 28 | cursor.close(); |
| 29 | rtxn.abort(); |
| 30 | |
| 31 | /* The enviroment is closed automatically. */ |
| 32 | |
| 33 | return EXIT_SUCCESS; |
| 34 | } |