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

[bug] prefix iterator behavioral inconsistencies #816

Closed
fansehep opened this issue Sep 3, 2023 · 4 comments
Closed

[bug] prefix iterator behavioral inconsistencies #816

fansehep opened this issue Sep 3, 2023 · 4 comments

Comments

@fansehep
Copy link

fansehep commented Sep 3, 2023

I test this c++ code:

  std::string key = "your_key";
  std::string value = "dasd";

  int n = 10;
  while (n--) {
    db->Put(rocksdb::WriteOptions(), key, value);
    key += "ad";
  }

  db->Put(rocksdb::WriteOptions(), "1ewadada", "daklda");

  auto prefix_iter = db->NewIterator(rocksdb::ReadOptions());
  prefix_iter->Seek("your_key");

  for (; prefix_iter->Valid(); prefix_iter->Next()) {
    fmt::print("key: {}, value: {}\n", prefix_iter->key().ToString(), prefix_iter->value().ToString());
  }

c++ output:

key: your_key, value: dasd
key: your_keyad, value: dasd
key: your_keyadad, value: dasd
key: your_keyadadad, value: dasd
key: your_keyadadadad, value: dasd
key: your_keyadadadadad, value: dasd
key: your_keyadadadadadad, value: dasd
key: your_keyadadadadadadad, value: dasd
key: your_keyadadadadadadadad, value: dasd
key: your_keyadadadadadadadadad, value: dasd

in rust code:

     db.put("anckjank", "123123dasd").unwrap();
    db.put("1234", "asdasdas").unwrap();
    db.put("12345", "123912031").unwrap();
    db.put("94203u423894", "12312djidjaisd").unwrap();
    db.put("1", "djaklsdalsd").unwrap();
    db.put("000xx-dasd", "123123").unwrap();
    db.put("000xx-dadasdasd", "djaiosdfhjnaiklfjdika").unwrap();

    let prefix_iter = db.prefix_iterator("123");
 
     for it in prefix_iter {
        match it {
            Ok(res) => println!(
                "{} {}",
                String::from_utf8(res.0.to_vec()).unwrap(),
                String::from_utf8(res.1.to_vec()).unwrap()
            ),
            Err(e) => println!("err: {}", e),
        }
    }

rust output:

123 123
1234 asdasdas
12345 123912031
94203u423894 12312djidjaisd
anckjank 123123dasd

we can see the rust output is not equal c++ output. Can someone help me?

@kayabaNerve
Copy link

@fansehep Reading the documentation on RocksDBs prefix iteration, the iterator returns Valid()=false when the prefix is no longer present. This Rust library didn't check Valid() properly prior to #829. Updating your lib to a version with that commit may correct the above.

@fansehep
Copy link
Author

@fansehep Reading the documentation on RocksDBs prefix iteration, the iterator returns Valid()=false when the prefix is no longer present. This Rust library didn't check Valid() properly prior to #829. Updating your lib to a version with that commit may correct the above.

Really thinks your help. I try the latest rocksdb-rs version. It has been fix it. 😄

@emfax
Copy link

emfax commented Mar 14, 2024

I'm finding similar issues with prefix_iterator on the current master branch. I assume that prefix_iterator would iterate over the keys that start with a certain prefix, but it iterates over keys with the prefix as well as every other key that comes after. Am I missing something?

@zaidoon1
Copy link
Contributor

@emfax you are not missing anything, this is the default behaviour, see: https://github.com/facebook/rocksdb/wiki/Prefix-Seek#transition-to-the-new-usage

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

4 participants