6 个版本

0.0.6 2024年3月6日
0.0.5 2024年2月29日

#2884数据库接口

Download history 71/week @ 2024-03-09 7/week @ 2024-03-16 34/week @ 2024-03-30 7/week @ 2024-04-06

293 每月下载量
kvcli 中使用

MIT/Apache

74KB
1.5K SLoC

kv-rs 是一个键值对到只追加日志文件的库,并在内存中保留键到文件位置的映射。所有活动键必须适合内存。删除会在日志文件中写入一个墓碑值。为了删除旧垃圾,可以通过写入仅包含活动数据的新的日志文件来压缩日志,跳过替换的值和墓碑。[作者 fengyang]

入门

use std::path::PathBuf;
use kv_rs::error::Error;
use kv_rs::storage::engine::Engine;
use kv_rs::storage::log_cask::LogCask;

fn main() {
    println!("Hello, kv CLI!");

    run().unwrap();

    println!("Bye~");
}

fn run() -> Result<(), Error> {
    let storage_path = PathBuf::new().join("D:/workspace/kv/storage/kvdb");
    // let storage_path = tempdir::TempDir::new("demo")?.path().join("kvdb");

    let mut engine = LogCask::new(storage_path)?;
    engine.set(b"b", vec![0x01])?;
    engine.set(b"b", vec![0x02])?;

    engine.set(b"e", vec![0x05])?;
    engine.delete(b"e")?;

    engine.set(b"c", vec![0x00])?;
    engine.delete(b"c")?;
    engine.set(b"c", vec![0x03])?;

    engine.set(b"", vec![])?;

    engine.set(b"a", vec![0x01])?;

    engine.delete(b"f")?;

    engine.delete(b"d")?;
    engine.set(b"d", vec![0x04])?;

    // Make sure the scan yields the expected results.
    assert_eq!(
            vec![
                (b"".to_vec(), vec![]),
                (b"a".to_vec(), vec![0x01]),
                (b"b".to_vec(), vec![0x02]),
                (b"c".to_vec(), vec![0x03]),
                (b"d".to_vec(), vec![0x04]),
            ],
            engine.scan(..).collect::<Result<Vec<_>,Error>> ()?,
        );

    let rs = engine.flush();

    Ok(())
}

依赖项

~9–20MB
~290K SLoC