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

Cursor::iter_start() panics on empty database #27

Open
marshallpierce opened this issue Mar 31, 2018 · 2 comments
Open

Cursor::iter_start() panics on empty database #27

marshallpierce opened this issue Mar 31, 2018 · 2 comments

Comments

@marshallpierce
Copy link
Contributor

It seems like it should instead return an iterator that yields no results.

Unfortunately, it doesn't work to simply ignore the result of get() in iter_start() and return the Iter anyway: the subsequent mdb_cursor_get() call in Iter yields error code 22.

One way to do this would be to add a bool field to Iter to record whether or not it should try to look up more records, and set the field to true if the first get() doesn't find a key. Could also set the field when the first error is returned to spare further lookups, though that's likely not a bottleneck for anybody.

Anyway, if this sounds good I'm happy to submit a PR for it.

@marshallpierce
Copy link
Contributor Author

I went ahead and tossed together a PR on the off chance the implementation approach above sounds good to you.

ncloudioj added a commit to ncloudioj/lmdb-rs that referenced this issue Mar 13, 2019
use newly published lmdb-rkv-sys crate with upgraded LMDB version 0.9.23
@jgarzik
Copy link

jgarzik commented Nov 2, 2019

Hitting this issue also. Test case demonstrating problem:

use lmdb::{Cursor, Transaction};
use std::path::Path;

fn main() {
    let cfg_builder = lmdb::Environment::new();
    let path = Path::new("./test.db");
    let env = cfg_builder.open(path).unwrap();
    let db = env.create_db(None, lmdb::DatabaseFlags::empty()).unwrap();
    let txn = env.begin_ro_txn().unwrap();
    let cursor_res = txn.open_ro_cursor(db);
    if cursor_res.is_err() { panic!("open-ro-cursor"); }
    let mut cursor = cursor_res.unwrap();

    println!("calling cursor.iter_start");
    let mut it = cursor.iter_start();
    loop {
        match it.next() {
            None => break,
            Some(tupl) => {
                println!("key = {}", String::from_utf8_lossy(tupl.0));
            }
        }
    }
}

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

No branches or pull requests

2 participants