Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get an specific value with lmdb++ #1

Open
hiteboar opened this issue May 17, 2015 · 3 comments
Open

How to get an specific value with lmdb++ #1

hiteboar opened this issue May 17, 2015 · 3 comments
Assignees

Comments

@hiteboar
Copy link

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:

int main() {
  auto env = lmdb::env::create();
  env.open("./example.mdb", 0, 0664);

  auto wtxn = lmdb::txn::begin(env);
  auto dbi = lmdb::dbi::open(wtxn, nullptr);
  dbi.put(wtxn, "username", "jhacker");
  dbi.put(wtxn, "email", "jhacker@example.org");
  dbi.put(wtxn, "fullname", "J. Random Hacker");
  wtxn.commit();

  auto rtxn = lmdb::txn::begin(env);

  std::string value;
  dbi.get(rtxn, "username", value);
  std::printf("value: '%s'\n",value.c_str());

  rtxn.abort();

  return EXIT_SUCCESS;
}

But it returns me:
Segmentation fault (core dumped)

How can I do this?

@artob artob self-assigned this May 17, 2015
@artob
Copy link
Member

artob commented May 18, 2015

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.

@hyichao
Copy link

hyichao commented Aug 2, 2017

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

@hoytech
Copy link

hoytech commented Feb 4, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants