sled icon indicating copy to clipboard operation
sled copied to clipboard

Fix for tree_names method in DB

Open tessob opened this issue 2 years ago • 0 comments

During the implementation of my service, I encountered a problem related to deserialization of custom types, which could represent tree names. If you will try on code bellow, it will produce Io(Kind(UnexpectedEof)) error because vector of names contains values serialized from str [__sled__default] and from String [AAA, BBB, CCC] both.

#[test]
fn bincode_db_names_test() {
    let db = sled::Config::default().temporary(true).open().unwrap();
    db.open_tree(bincode::serialize(&String::from("AAA")).unwrap()).unwrap();
    db.open_tree(bincode::serialize(&String::from("BBB")).unwrap()).unwrap();
    db.open_tree(bincode::serialize(&String::from("CCC")).unwrap()).unwrap();

    let names: Vec<String> = db
        .tree_names()
        .iter()
        .filter_map(|name| match bincode::deserialize::<String>(name) {
            Ok(value) => Some(value),
            Err(e) => {
                println!("Sled name={name:?} caused error: {e:?}");
                None
            }
        })
        .collect();

    println!("{:?}", names);
}

So, basically, we have a situation where the API for creating a Tree is more generic than the API that lists created trees. The main question here is - should Sled's specific/system data be a part of public API or not. I think - not.

tessob avatar May 27 '22 09:05 tessob