11个版本

0.3.3 2024年6月6日
0.3.2 2024年5月28日
0.2.5 2024年5月28日
0.1.1 2023年5月16日

#818 in 数据库接口

Download history 107/week @ 2024-04-21 35/week @ 2024-04-28 129/week @ 2024-05-05 517/week @ 2024-05-12 187/week @ 2024-05-19 243/week @ 2024-05-26 219/week @ 2024-06-02 28/week @ 2024-06-09 20/week @ 2024-06-16 17/week @ 2024-06-23 20/week @ 2024-06-30 6/week @ 2024-07-07 3/week @ 2024-07-14 122/week @ 2024-07-28 21/week @ 2024-08-04

每月147次下载

MIT/Apache

47KB
993 代码行

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

基于RocksDB的轻量级包装。

示例

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

// Describe tables group via macros.
//
// A group is parametrized with a table creation context.
weedb::tables! {
    pub struct MyTables<Caches> {
        pub my_table: MyTable,
        // ..and some other tables as fields...
    }
}

// Describe a column family.
struct MyTable;

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

    // 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) {
        // ...
    }
}

// Implement cf options setup for some specific context.
impl ColumnFamilyOptions<Caches> for MyTable {
    // Modify general options
    fn options(opts: &mut rocksdb::Options, caches: &mut Caches) {
        opts.set_write_buffer_size(128 * 1024 * 1024);

        // Use cache from the context
        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);
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let tempdir = tempfile::tempdir()?;

    // Prepare caches
    let caches = Caches::with_capacity(10 << 23);

    // Prepare db
    let db = WeeDb::<MyTables>::builder(&tempdir, caches)
        .with_name("test")
        .with_metrics_enabled(true)
        .with_options(|opts, _ctx| {
            // 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);
        })
        .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.tables().my_table;
    my_table.insert(b"asd", b"123")?;
    assert!(my_table.get(b"asd")?.is_some());

    Ok(())
}

如何生成Grafana仪表板

cd scripts
python3 -m venv venv
# activate venv according to your shell (source venv/bin/activate)
pip install -r requirements.txt
python rocksdb_metrics.py dashboard.json

贡献

我们欢迎对项目的贡献!如果您注意到任何问题或错误,请随时打开一个问题或提交一个拉取请求。

许可证

许可协议为以下之一

您可以选择。

依赖关系

~29MB
~573K SLoC