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

contains_key returns true even if key does not exist. #1441

Open
vsabavat opened this issue Mar 15, 2023 · 3 comments
Open

contains_key returns true even if key does not exist. #1441

vsabavat opened this issue Mar 15, 2023 · 3 comments
Labels

Comments

@vsabavat
Copy link

I tried to prototype with sled db and the contains_key API always returns true even though the key does not exist.

use random_string::generate;
use sled;

static ENCODE_CHARSET: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

struct SledCli {
    db: sled::Db,
}

impl SledCli {
    fn new(db: sled::Db) -> Self {
        SledCli { db }
    }
    fn contains_key(&self, key: &str) -> bool {
        match self.db.contains_key(key) {
           Err(e) => false,
           Ok(v) => true,
        }
    }
    fn put(&self, key: &str, value: &str) -> bool {
        match self.db.insert(key, value) {
            Ok(_) => true,
            Err(e) => {
                println!("Failed to insert {} err {:?}", key, e);
                false
            }
        }
    }
    fn get(&self, key: &str) -> Option<String> {
        match self.db.get(key) {
            Err(e) => {
                println!("Cannot find the tiny_url {} err {:?}", key, e);
                None
            }
            Ok(v) => {
                if let Some(url) = v {
                    match String::from_utf8(url.to_vec()) {
                        Ok(value) => Some(value),
                        Err(e) => {
                            println!("Unable to decode utf8 string {:?}", e);
                            None
                        }
                    }
                } else {
                    None
                }
            }
        }
    }
}
fn main() {
    println!("Hello, world!");
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_sled() {
        let db = sled::open("test_db").unwrap();
        let sled_cli = SledCli::new(db);
        for _ in 0..10 {
            let url = generate(200, ENCODE_CHARSET);
            let tiny_url = generate(6, ENCODE_CHARSET);
            sled_cli.put(&tiny_url, &url);
            assert!(sled_cli.contains_key(&tiny_url));
            let found = sled_cli.get(&tiny_url).unwrap();
            assert_eq!(url, found);
            let tiny_url = generate(6, ENCODE_CHARSET);
            assert!(!sled_cli.contains_key(&tiny_url), false);
        }
    }
}
@vsabavat vsabavat added the bug label Mar 15, 2023
@Tsai002
Copy link

Tsai002 commented Mar 16, 2023

db.contains_key(key)

fn contains_key(&self, key: &str) -> bool {
        match self.db.contains_key(key) {
           Err(e) => false,
           Ok(v) => v,
        }
    }

@vsabavat
Copy link
Author

Sorry, you can change that to match the actual API, it will still return true for both cases. whether key exists or does not exist. I tried to simplify code that I copied here and I forgot to change it for match. I am using .get() to be able to determine if key exists or not.

@Tsai002
Copy link

Tsai002 commented Mar 17, 2023

fn contains_key(&self, key: &str) -> bool {
        match self.db.contains_key(key) {
           Err(e) => false,
           Ok(v) => v, // attention here 
        }
    }

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

No branches or pull requests

2 participants