block-db 0.2.0

Local, multi-threaded, durable byte DB.
Documentation
// Authors: Robert Lopez

use crate::{
    data_file::wal::DataFileLog,
    tests::{test_db::TestDB, util::random_bytes},
    uncorrupt::UncorruptAction,
};

#[tokio::test]
async fn uncorrupt_smoke() {
    let max_file_size = 10_000;

    let mut test_db = TestDB::open("uncorrupt_smoke", Some(100), Some(max_file_size))
        .await
        .unwrap();

    let bytes_one = random_bytes(max_file_size);
    let bytes_two = random_bytes(max_file_size);
    let bytes_three = random_bytes(max_file_size);

    let block_key_one = test_db.block_db.write(&bytes_one).await.unwrap();
    let block_key_two = test_db.block_db.write(&bytes_two).await.unwrap();

    test_db
        .block_db
        .data_files
        .read()
        .await
        .get(&block_key_one.data_file_id)
        .unwrap()
        .write()
        .await
        .wal
        .log(&DataFileLog::Clear)
        .await
        .unwrap();

    test_db
        .block_db
        .uncorrupt(UncorruptAction::Clear {
            data_file_id: block_key_one.data_file_id.clone(),
        })
        .await
        .unwrap();

    test_db.read(&block_key_one, None).await.unwrap();
    test_db
        .read(&block_key_two, Some(&bytes_two))
        .await
        .unwrap();

    test_db
        .block_db
        .uncorrupt(UncorruptAction::Recover)
        .await
        .unwrap();

    test_db.read(&block_key_one, None).await.unwrap();
    test_db
        .read(&block_key_two, Some(&bytes_two))
        .await
        .unwrap();

    let block_key_three = test_db.block_db.write(&bytes_three).await.unwrap();

    test_db
        .block_db
        .uncorrupt(UncorruptAction::BadBatchUnwind {
            data_file_ids_to_delete: vec![block_key_two.data_file_id.to_string()],
            data_file_inverse_log_map: vec![(
                block_key_three.data_file_id.to_string(),
                vec![DataFileLog::Free(block_key_three.data_block_id.to_string())],
            )]
            .into_iter()
            .collect(),
        })
        .await
        .unwrap();

    test_db.read(&block_key_one, None).await.unwrap();
    test_db.read(&block_key_two, None).await.unwrap();
    test_db.read(&block_key_three, None).await.unwrap();

    test_db.cleanup().await.unwrap();
}