Comments (3)
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.
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.
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)
- Allow copying of lmdb::dbi instances HOT 2
- Feature Request: remove dependency on pthreads? HOT 4
- How can I save '0' ? HOT 2
- Symbol mdb_env_create() doesn't resolve HOT 1
- No updating value when key exists HOT 1
- Example uses the same dbi instance with a different transaction HOT 2
- Could you tag a new release? HOT 1
- Cursor double free on write tx HOT 1
- I forked this repo for C++17
- mdb_del : Invalid argument for simple deletion HOT 9
- Cursor.get returns key and value concatenated into key variable HOT 10
- Double-free when txn.commit() throws
- Merge into lmdb HOT 5
- Help needed cursor_put and MDB_MULTIPLE HOT 5
- problem in lmdb++.h line 1654 HOT 1
- Delete MacPorts Portfile HOT 1
- Querying for different types in key/value pair HOT 1
- thread-local storage is not supported for clang7 HOT 1
- Enhance error messages in exceptions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lmdbxx.