Open hiteboar opened 9 years ago
The convenience template methods for lmdb::dbi#get()
seem a bit too permissive at the moment, as in your example you are passing in a C string (const char*
) to the key parameter and a C++ string (std::string
) to the value parameter; whereas both key and value must actually correspond in type for these method overloads. The segmentation fault probably arises due to this.
Here follows an example of the "low-level" way to perform key/value lookups; everything else is just a convenience method on top of this fundamental lmdb::dbi_get()
interface:
lmdb::val key{"username"};
lmdb::val value;
bool found = lmdb::dbi_get(rtxn, dbi, key, value);
std::printf("found: %c, size: %zu, data: '%s'\n",
found ? 'y' : 'n', value.size(), value.data());
I will do some work to improve and better document the various convenience methods for this, but in the meantime the above method will definitely work. I'll update this ticket when there is a better (i.e., more convenient) example available.
hi bendiken,
its exciting to found this repo as i need to use lmdb in my work recently. but it seems like the codes are not well supported to std::string. I do try the following
auto env = lmdb::env::create();
env.set_mapsize(1UL * 1024UL * 1024UL * 1024UL); /* 1 GiB */
env.open(lmdbname.c_str(), 0, 0664);
/* Insert some key/value pairs in a write transaction: */
auto wtxn = lmdb::txn::begin(env);
auto dbi = lmdb::dbi::open(wtxn, nullptr);
for(int i=0;i<10;i++){
string key = "key"+to_string(i);
string val = "value"+to_string(i);
dbi.put(wtxn, key.c_str(),val.c_str());
}
wtxn.commit();
/* get value of certain key */
auto gtxn = lmdb::txn::begin(env);
auto gdbi = lmdb::dbi::open(gtxn, nullptr);
lmdb::val target_key("key5");
lmdb::val target_val;
bool found = lmdb::dbi_get(gtxn, gdbi, target_key, target_val);
std::printf("found: %c, size: %zu, data: '%s'\n",
found ? 'y' : 'n', target_val.size(), target_val.data());
the output data size is correct, but the data is not. It looks like this,
found: y, size: 6, data: 'value5'
that some unknown character show up in the data end. I suppose there is some bug on conversion of char array and string?
any idea about this? thanks
I have removed the convenience template functions in my fork of this project. Instead you use std::string_view, which should solve the issues in this thread.
Here are some more details on my fork's approach: https://github.com/hoytech/lmdbxx#string_view
I have already tried to use lmdb++ but I failed. What I want to do is simple, it is like the example.cc but I want to get an specific value of a key of the data base instead of go over all the values that have been saved on it with cursors.
For example:
But it returns me: Segmentation fault (core dumped)
How can I do this?