GithubHelp home page GithubHelp logo

Comments (3)

hyichao avatar hyichao commented on September 21, 2024 1

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

from lmdbxx.

hoytech avatar hoytech commented on September 21, 2024 1

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

from lmdbxx.

artob avatar artob commented on September 21, 2024

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.

from lmdbxx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.