2 releases

0.1.1 May 16, 2023
0.1.0 May 16, 2023

#2611 in Database interfaces

Download history 17/week @ 2024-01-08 13/week @ 2024-01-15 17/week @ 2024-01-22 9/week @ 2024-01-29 21/week @ 2024-02-05 14/week @ 2024-02-12 39/week @ 2024-02-19 72/week @ 2024-02-26 38/week @ 2024-03-04 55/week @ 2024-03-11 70/week @ 2024-03-18 74/week @ 2024-03-25 90/week @ 2024-04-01 47/week @ 2024-04-08 45/week @ 2024-04-15

262 downloads per month

MIT/Apache

25KB
477 lines

WeeDB   crates-io-batch docs-badge rust-version-badge

A thin wrapper around RocksDB.

Example

use weedb::{rocksdb, Caches, ColumnFamily, Migrations};

// Describe column family
struct MyTable;

impl ColumnFamily for MyTable {
    // Column family name
    const NAME: &'static str = "my_table";

    // Modify general options
    fn options(opts: &mut rocksdb::Options, caches: &Caches) {
        opts.set_write_buffer_size(128 * 1024 * 1024);

        let mut block_factory = rocksdb::BlockBasedOptions::default();
        block_factory.set_block_cache(&caches.block_cache);
        block_factory.set_data_block_index_type(rocksdb::DataBlockIndexType::BinaryAndHash);

        opts.set_block_based_table_factory(&block_factory);

        opts.set_optimize_filters_for_hits(true);
    }

    // Modify read options
    fn read_options(opts: &mut rocksdb::ReadOptions) {
        opts.set_verify_checksums(false);
    }

    // Modify write options
    fn write_options(opts: &mut rocksdb::WriteOptions) {
        // ...
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Prepare caches
    let caches = Caches::with_capacity(10 << 23);

    // Prepare db
    let db = WeeDb::builder("./", caches)
        .options(|opts, _| {
            // Example configuration:

            opts.set_level_compaction_dynamic_level_bytes(true);

            // Compression opts
            opts.set_zstd_max_train_bytes(32 * 1024 * 1024);
            opts.set_compression_type(rocksdb::DBCompressionType::Zstd);

            // Logging
            opts.set_log_level(rocksdb::LogLevel::Error);
            opts.set_keep_log_file_num(2);
            opts.set_recycle_log_file_num(2);

            // Cfs
            opts.create_if_missing(true);
            opts.create_missing_column_families(true);
        })
        .with_table::<MyTable>() // register column families
        .build()?;

    // Prepare and apply migration
    let mut migrations = Migrations::with_target_version([0, 1, 0]);
    migrations.register([0, 0, 0], [0, 1, 0], |db| {
        // do some migration stuff
        Ok(())
    })?;

    db.apply(migrations)?;

    // Table usage example
    let my_table = db.instantiate_table::<MyTable>();
    my_table.insert(b"asd", b"123")?;
    assert!(my_table.get(b"asd")?.is_some());

    Ok(())
}

Contributing

We welcome contributions to the project! If you notice any issues or errors, feel free to open an issue or submit a pull request.

License

Licensed under either of

at your option.

Dependencies

~28MB
~560K SLoC