#json #bson #嵌入式数据库 #数据库 #FFI #JSON 格式

程序+库 ejdb2

为 EJDB2 提供绑定,一个单文件嵌入式 NoSQL 数据库

6 个版本

使用旧版 Rust 2015

0.0.6 2021 年 10 月 12 日
0.0.5 2021 年 10 月 12 日

数据库实现 中排名第 141

MIT 许可证

1.5MB
38K SLoC

C 28K SLoC // 0.1% comments Java 4K SLoC // 0.2% comments Dart 1.5K SLoC // 0.1% comments JavaScript 1K SLoC // 0.4% comments Rust 1K SLoC // 0.0% comments Forge Config 748 SLoC // 0.7% comments Swift 500 SLoC Shell 363 SLoC // 0.0% comments Batch 359 SLoC TypeScript 207 SLoC // 0.8% comments Prolog 14 SLoC Objective-C 10 SLoC // 0.2% comments Xcode Config 3 SLoC

ejdb2 的 rust 绑定

EJDB2 是一个单文件嵌入式文档数据库,用 C 实现。本项目为其提供了 Rust 绑定(版本 2.61)。

安装

[dependencies]
ejdb2-sys = "2.61.0"
ejdb2 = "0.0.5"
serde_json = "1.0"

示例

extern crate serde_json;
extern crate ejdb2;
extern crate ejdb2_sys;


use ejdb2::ejdbquery::{EJDBQuery, SetPlaceholder};
use ejdb2::ejdb::EJDB;
use serde_json::json;

fn main() {
    EJDB::init().unwrap();
    let mut db = EJDB::new();

    db.open("test.db").unwrap();

    let data = json!({
        "serde_key" : 32,
        "test_val" : [1,2,3],
        "nested" : { "test" : "str"}
    });

    let id = db.put_new("test", &data).unwrap();

    let meta:serde_json::Value = db.info().unwrap();

    println!("db meta {}", meta);

    let result:String = db.get("test", id).unwrap();

    println!("get {}, {}",1, result);

    let mut query: EJDBQuery = EJDBQuery::new("test", "/* | limit :limit skip :skip ");
    query.init().unwrap();

    query.set_placeholder("limit", 0, 3 as i64).unwrap();
    query.set_placeholder("skip", 0, 3 as i64).unwrap();

    let mut result = Vec::<(i64, serde_json::Value)>::new();

    db.exec::<serde_json::Value>(&query, &mut result).unwrap();
    println!("after exec {}", result.len());

    for (id, r) in result {
        println!("id {}, value {}", id, r);
    }
}

内部 EJDB 使用 JBL 作为 json 的二进制格式。这个 Rust 绑定支持 serde_json 与 JBL 之间的转换。如果你使用的不是 serde_json,你可以提供自己的格式转换器。

实现

pub trait EJDBSerializable<T> {
    fn from_jbl(jbl: ejdb2_sys::JBL) -> Result<T, ejdb2_sys::iwrc> ;

    fn to_jbl(&self) -> Result<ejdb2_sys::JBL, ejdb2_sys::iwrc>;
}

此绑定还支持字符串与 JBL 之间的转换。你也可以将你的格式序列化为 json 字符串并转换为 JBL。但这会慢一些。

依赖项

~0.4–2.9MB
~57K SLoC