pjtatlow/jammdb

panicked at 'attempt to subtract with overflow'

0xAAE opened this issue · 1 comments

0xAAE commented

The following code panics with
thread 'main' panicked at 'attempt to subtract with overflow', .../jammdb-0.5.0/src/page.rs:107:25:

const TEST_DB: &str = "tmp.db";
fn main() {
    let _ = std::fs::remove_file(TEST_DB);
    {
        let db = jammdb::DB::open(TEST_DB).unwrap();
        {
            let tx = db.tx(true).unwrap();
            let root = tx.get_or_create_bucket("ROOT").unwrap();
            tx.commit().unwrap();
        }
        {
            let tx = db.tx(true).unwrap();
            let root = tx.get_or_create_bucket("ROOT").unwrap();
            let child = root.get_or_create_bucket("CHILD").unwrap();
            tx.commit().unwrap(); // panic! is here, page.rs:107: self.overflow = num_pages - 1;  and num_pages is 0
        }
    }
    let _ = std::fs::remove_file(TEST_DB);
}

If I do all stuff in the single transaction everything is OK

fn main() {
    let _ = std::fs::remove_file(TEST_DB);
    {
        let db = jammdb::DB::open(TEST_DB).unwrap();
        {
            let tx = db.tx(true).unwrap();
            let root = tx.get_or_create_bucket("ROOT").unwrap();
            // remove tx.commit() + db.tx()
            let root = tx.get_or_create_bucket("ROOT").unwrap();
            let child = root.get_or_create_bucket("CHILD").unwrap();
            tx.commit().unwrap();
        }
    }
    let _ = std::fs::remove_file(TEST_DB);
}

Unfortunately, the problem prevents me from using nested buckets at all

Hey thanks for finding this, I should have some time later today to take a closer look, I'll let you know what I find!