#leveldb #bindings #iterator #github #git #db

rs-leveldb

为 Rust 提供的 leveldb 几乎完整的绑定

6 个版本

0.1.5 2020 年 12 月 22 日
0.1.4 2020 年 12 月 21 日

#1492数据库接口

Download history 233/week @ 2024-03-13 130/week @ 2024-03-20 57/week @ 2024-03-27 82/week @ 2024-04-03 19/week @ 2024-04-10 28/week @ 2024-04-17 30/week @ 2024-04-24 17/week @ 2024-05-01 80/week @ 2024-05-08 200/week @ 2024-05-15 235/week @ 2024-05-22 276/week @ 2024-05-29 135/week @ 2024-06-05 180/week @ 2024-06-12 230/week @ 2024-06-19 127/week @ 2024-06-26

每月 713 次下载

MIT 许可证

47KB
1K SLoC

Rust leveldb 绑定

为 Rust 提供的 leveldb 几乎完整的绑定。

分支与修改

此仓库从 https://github.com/skade/leveldb.git 分支。

先决条件

snappyleveldb 需要安装。在 Ubuntu 上,我建议

sudo apt-get install libleveldb-dev libsnappy-dev

用法

如果您的项目使用 Cargo,请在您的 Cargo.toml 中添加以下行

[dependencies]

rs-leveldb = "0.1.5"

开发

确保您已安装所有先决条件。运行

$ cargo build

进行构建和

$ cargo test

运行测试套件。

示例

use leveldb::options::{Options, WriteOptions, ReadOptions};
use leveldb::db::Database;
use leveldb::error::Error;
use leveldb::util::FromU8;
use leveldb::iterator::Iterable;
use std::path::Path;

fn main() -> Result<(), Error> {
    let path = Path::new("temp_ldb");
    let mut options = Options::new();
    options.create_if_missing = true;

    let database = Database::open(&path, &options)?;

    let write_ops = WriteOptions::new();
    let read_ops = ReadOptions::new();

    // key of &[u8] type, it's the world 'name';
    let key = &[110, 97, 109, 101][..];
    database.put(&write_ops, &key, &b"tom"[..])?;

    let value = database.get(&read_ops, &key)?;

    assert!(value.is_some());
    assert_eq!(Vec::from(&b"tom"[..]), value.unwrap());

    // key of &str type
    let key = "age";
    database.put(&write_ops, &key, &b"5"[..])?;

    let value = database.get(&read_ops, &key)?;
    assert!(value.is_some());
    assert_eq!(Vec::from(&b"5"[..]), value.unwrap());

    // key of String type
    let key = "from".to_string();
    database.put(&write_ops, &key, &b"mars"[..])?;

    let value = database.get(&read_ops, &key)?;
    assert!(value.is_some());
    assert_eq!(Vec::from(&b"mars"[..]), value.unwrap());

    // key of integer type
    database.put(&write_ops, &1000, &10000i32.to_be_bytes()[..])?;

    let value = database.get(&read_ops, &1000)?;
    assert!(value.is_some());
    assert_eq!(10000, i32::from_u8(value.unwrap().as_slice()));

    // use put_u8 and get_u8
    let key = &b"temp"[..];
    database.put_u8(&write_ops, key, &b"temp"[..])?;

    let value = database.get_u8(&read_ops, key)?;
    assert!(value.is_some());
    assert_eq!(Vec::from(&b"temp"[..]), value.unwrap());

    // delete use key of integer, &str, String
    database.delete(&write_ops, &1000)?;
    let value = database.get(&read_ops, &1000)?;
    assert!(value.is_none());

    // delete use key of type &[u8]
    database.delete_u8(&write_ops, &b"temp"[..])?;
    let value = database.get(&read_ops, &&b"key"[..])?;
    assert!(value.is_none());

    // iterator
    let iter = database.iter(&read_ops);

    let mut key_and_values = vec![("name", "tom"), ("age", "5"), ("from", "mars")];
    key_and_values.sort();

    for entry in  iter.enumerate() {
        let (i, (key, value)) = entry;
        let key_str = String::from_utf8_lossy(key.as_slice());
        let value_str = String::from_utf8_lossy(value.as_slice());

        let (expected_key, expected_value) = key_and_values.get(i).unwrap();

        assert_eq!(*expected_key, &key_str.to_string());
        assert_eq!(*expected_value, &value_str.to_string());
    }


    Ok(())
}

打开的问题

  • 缺少过滤策略
  • 不支持任意起始和结束点的迭代器

许可证

MIT,见 LICENSE

依赖关系

~1.5MB
~26K SLoC