block-db 0.2.0

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

use crate::tests::{test_db::TestDB, util::random_bytes};

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

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

    let bytes_one = random_bytes(chunk_size);
    let bytes_two = random_bytes(max_file_size);
    let bytes_three = random_bytes(max_file_size * 2);
    let bytes_four = random_bytes(chunk_size * 3);
    let bytes_five = random_bytes(chunk_size * 4);
    let bytes_six = random_bytes(max_file_size * 3);
    let bytes_seven = random_bytes(max_file_size / 2);
    let bytes_eight = random_bytes(max_file_size + 100);
    let bytes_nine = random_bytes(max_file_size);
    let bytes_ten = random_bytes(max_file_size * 5);

    let block_key_one = test_db.write(&bytes_one, true).await.unwrap();

    test_db.free(&block_key_one, &bytes_one).await.unwrap();
    test_db.compact(None, vec![&bytes_one]).await.unwrap();
    test_db.compact::<Vec<u8>>(None, vec![]).await.unwrap();

    let block_key_two = test_db.write(&bytes_two, false).await.unwrap();

    test_db.free(&block_key_two, &bytes_two).await.unwrap();
    test_db.compact(None, vec![&bytes_two]).await.unwrap();

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

    let (block_key_four, block_key_five, block_key_six) = {
        let mut new_block_keys = test_db
            .batch(
                vec![&bytes_four, &bytes_five, &bytes_six],
                vec![(&block_key_three, &bytes_three)],
                1,
            )
            .await
            .unwrap();

        (
            new_block_keys.remove(0),
            new_block_keys.remove(0),
            new_block_keys.remove(0),
        )
    };

    test_db.compact(None, vec![&bytes_three]).await.unwrap();
    test_db.compact::<Vec<u8>>(None, vec![]).await.unwrap();
    test_db.compact::<Vec<u8>>(None, vec![]).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
        .read(&block_key_four, Some(&bytes_four))
        .await
        .unwrap();

    test_db
        .read(&block_key_five, Some(&bytes_five))
        .await
        .unwrap();

    test_db
        .read(&block_key_six, Some(&bytes_six))
        .await
        .unwrap();

    let block_key_seven = test_db.write(&bytes_seven, false).await.unwrap();
    let block_key_eight = test_db.write(&bytes_eight, false).await.unwrap();
    let block_key_nine = test_db.write(&bytes_nine, true).await.unwrap();
    let block_key_ten = test_db.write(&bytes_ten, true).await.unwrap();

    test_db
        .batch(
            vec![],
            vec![
                (&block_key_seven, &bytes_seven),
                (&block_key_eight, &bytes_eight),
            ],
            0,
        )
        .await
        .unwrap();

    test_db
        .compact(None, vec![&bytes_seven, &bytes_eight])
        .await
        .unwrap();
    test_db.compact::<Vec<u8>>(None, vec![]).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
        .read(&block_key_four, Some(&bytes_four))
        .await
        .unwrap();

    test_db
        .read(&block_key_five, Some(&bytes_five))
        .await
        .unwrap();

    test_db
        .read(&block_key_six, Some(&bytes_six))
        .await
        .unwrap();

    test_db.read(&block_key_seven, None).await.unwrap();
    test_db.read(&block_key_eight, None).await.unwrap();

    test_db
        .read(&block_key_nine, Some(&bytes_nine))
        .await
        .unwrap();

    test_db
        .read(&block_key_ten, Some(&bytes_ten))
        .await
        .unwrap();

    test_db
        .batch(
            vec![],
            vec![
                (&block_key_five, &bytes_five),
                (&block_key_six, &bytes_six),
                (&block_key_nine, &bytes_nine),
                (&block_key_ten, &bytes_ten),
            ],
            0,
        )
        .await
        .unwrap();

    test_db
        .compact(None, vec![&bytes_five, &bytes_six, &bytes_nine, &bytes_ten])
        .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
        .read(&block_key_four, Some(&bytes_four))
        .await
        .unwrap();

    test_db.read(&block_key_five, None).await.unwrap();
    test_db.read(&block_key_six, None).await.unwrap();
    test_db.read(&block_key_seven, None).await.unwrap();
    test_db.read(&block_key_eight, None).await.unwrap();
    test_db.read(&block_key_nine, None).await.unwrap();
    test_db.read(&block_key_ten, None).await.unwrap();

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