#hash-map #concurrency

diskmap

基于磁盘的类似 HashMap 的共享并发内存使用

5 个版本

0.2.0 2022年6月7日
0.1.3 2022年6月7日
0.1.2 2022年6月5日
0.1.1 2022年5月12日
0.1.0 2022年5月11日

#2321数据结构

MIT/Apache

14KB
309 代码行

DiskMap


基于磁盘的并发共享 HashMap。

为了允许并发共享访问数据,每个键都由文件系统处理的 RwLock 管理。

用法

简单示例

fn main() {
    use diskmap::DiskMap;

    let d: DiskMap<String, i32> = DiskMap::open_new("/tmp/db").unwrap();

    d.insert("a".to_owned(), 12000).unwrap();
    d.insert("b".to_owned(), 2).unwrap();
    d.insert("c".to_owned(), 3).unwrap();

    d.alter(&"a".to_owned(), |_| 1).unwrap();

    let v = d.get(&"a".to_owned()).unwrap();

    assert_eq!(v, 1);
}

复杂示例

fn main() {
    use diskmap::DiskMap;

    let d: DiskMap<String, i32> = DiskMap::open_new("/tmp/db").unwrap();

    d.insert("a".to_owned(), 1).unwrap();
    d.insert("b".to_owned(), 2).unwrap();
    d.insert("c".to_owned(), 3).unwrap();

    let d1 = d.clone();
    thread::spawn(move || loop {
        let keys = d1.get_keys().unwrap();

        keys.iter().for_each(|e_ref| {
            d1.alter(e_ref, |_| 11).unwrap();
        });
    });

    let d2 = d.clone();
    thread::spawn(move || loop {
        let keys = d2.get_keys().unwrap();

        keys.iter().for_each(|e_ref| {
            d2.alter(e_ref, |_| 11).unwrap();
        });
    });

    // the two threads are accessing the same key and modifying the value without 
    // one colliding with the other
}

依赖项

~0.8–1.6MB
~32K SLoC