#actor #actor-framework #lmdb #rest #actix-actor #storage #json

ledb-actix

LEDB Actor for Actix actor framework and storage REST-interface

6 个版本 (3 个破坏性更新)

0.4.0 2020年6月11日
0.3.0 2019年9月6日
0.2.1 2018年10月27日
0.1.1 2018年10月1日
0.1.0 2018年9月28日

#1180 in 数据库接口

MIT 许可证

260KB
6K SLoC

LEDB 的 Actor 和 REST 接口

License: MIT Travis-CI Build Status Appveyor Build status Crates.io Package Docs.rs API Documentation

LEDB 是实现简单但高效、轻量但强大的文档存储的尝试。

缩写 LEDB 可视为轻量级嵌入式数据库 (Lightweight Embedded DB),也可视为低端数据库 (Low End DB),也可视为锂离子引擎数据库 (Litium Engine DB),也可视为 LitE DB 等。

REST 接口

LEDB HTTP 接口 0.1.0

存储 API

获取数据库信息

GET /info

获取数据库统计信息

GET /stats

集合 API

获取集合列表

GET /collection

创建新的空集合

POST /collection?name=_$collection_name_

删除包含所有文档的集合

DELETE /collection/$collection_name

索引 API

获取集合的索引

GET /collection/$collection_name/index

为集合创建新的索引

POST /collection/$collection_name/index?path=_$field_name_&kind=_$index_kind_&key=_$key_type_

删除集合的索引

DELETE /collection/$collection_name/document/$index_name

文档 API

使用查询查找文档

GET /collection/$collection_name/document?filter=_$query_&order=_$ordering_&offset=_$skip_&length=_$take_

GET /collection/$collection_name?filter=_$query_&order=_$ordering_&offset=_$skip_&length=_$take_

使用查询修改文档

PUT /collection/$collection_name/document?filter=_$query_&modify=_$modifications_

PATCH /collection/$collection_name?filter=_$query_&modify=_$modifications_

使用查询删除文档

DELETE /collection/$collection_name/document?filter=_$query_

PUT /collection/$collection_name?filter=_$query_

插入新文档

POST /collection/$collection_name/document

POST /collection/$collection_name

通过 ID 获取文档

GET /collection/$collection_name/document/$document_id

GET /collection/$collection_name/$document_id

替换文档

PUT /collection/$collection_name/document/$document_id

PUT /collection/$collection_name/$document_id

删除文档

DELETE /collection/$collection_name/document/$document_id

DELETE /collection/$collection_name/$document_id

支持的索引类型

  • uni -- 唯一键
  • dup -- 重复键

支持的关键类型

  • int -- 64位有符号整数
  • float -- 64位浮点数
  • bool -- 布尔值
  • string -- UTF-8 字符串
  • binary -- 二进制数据

Actor

使用示例

use std::env;

use serde::{Deserialize, Serialize};
use serde_json::json;

use ledb_actix::{query, Document, Options, Primary, Storage, StorageAddrExt};
use log::info;
use serde_json::from_value;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Document)]
struct BlogPost {
    #[document(primary)]
    pub id: Option<Primary>,
    pub title: String,
    pub tags: Vec<String>,
    pub content: String,
}

#[actix_rt::main]
async fn main() {
    env::set_var("RUST_LOG", "info");
    pretty_env_logger::init();

    let _ = std::fs::remove_dir_all("example_db");

    let addr = Storage::new("example_db", Options::default())
        .unwrap()
        .start(1);

    let id = addr
        .send_query(query!(
            insert into blog {
                "title": "Absurd",
                "tags": ["absurd", "psychology"],
                "content": "Still nothing..."
            }
        ))
        .await
        .unwrap();

    info!("Inserted document id: {}", id);
    assert_eq!(id, 1);

    let id = addr.send_query(query!(
        insert into blog {
            "title": "Lorem ipsum",
            "tags": ["lorem", "ipsum"],
            "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        }
    )).await.unwrap();

    info!("Inserted document id: {}", id);
    assert_eq!(id, 2);

    addr.send_query(query!(
        index for blog tags string
    ))
    .await
    .unwrap();

    info!("Indexing is ok");

    let mut docs = addr
        .send_query(query!(
            find BlogPost in blog
            where tags == "psychology"
                order asc
        ))
        .await
        .unwrap();

    info!("Number of found documents: {}", docs.size_hint().0);

    assert_eq!(docs.size_hint(), (1, Some(1)));

    let doc = docs.next().unwrap().unwrap();

    info!("Found document: {:?}", doc);

    let doc_data: BlogPost = from_value(json!({
        "id": 1,
        "title": "Absurd",
        "tags": ["absurd", "psychology"],
        "content": "Still nothing..."
    }))
    .unwrap();

    assert_eq!(&doc, &doc_data);
    assert!(docs.next().is_none());
}

依赖项

~11-17MB
~305K SLoC