skade / leveldb

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Iterators don't stop iterating when specified "to" key is reached.

palkeo opened this issue · comments

If I instantiate a leveldb iterator like so:

            leveldb
                .iter(ReadOptions::new())
                .from(start_key)
                .to(stop_key)

I would expect the iteration to start at (or after) start_key, and stop at (or before) stop_key.
Here iteration do start after start_key, but never stops before the end of the DB is reached.

Can you provide a reduced test case for this?

Here you go, this could be added to tests/iterator.rs, and is a failing test I would expect to pass:

#[test]
fn test_key_iterator_iterate_from_to() {
  let tmp = tmpdir("key_iter");
  let database = &mut open_database(tmp.path(), true);
  db_put_simple(database, 1, &[1]);
  db_put_simple(database, 2, &[2]);
  db_put_simple(database, 3, &[3]);
  db_put_simple(database, 4, &[4]);
  db_put_simple(database, 5, &[5]);

  let iterable: &mut Iterable<i32> = database;

  let read_opts = ReadOptions::new();
  let mut iter = iterable.keys_iter(read_opts).from(&2).to(&4);
  let vec: Vec<i32> = iter.collect();
  assert_eq!(vec, vec![2, 3, 4]);
}

We just ran into this today.

It seems like there is no check against the end key here https://docs.rs/leveldb/0.8.6/src/leveldb/database/iterator.rs.html#137-150 ?