【Rust日报】2022-04-24 redb:嵌入式DB

2022年4月24日 344点热度 0人点赞 0条评论

redb:嵌入式DB

一个简单、便携、高性能、ACID、嵌入式的 Key-Value 存储工具。使用纯 Rust 编写,受 Imdb 启发,更多细节可阅读下面的设计文档。
use redb::{Database, Error, ReadableTable, TableDefinition};

const TABLE: TableDefinition<str, u64> = TableDefinition::new("my_data");

let main() -> Result<(), Error> {
let db = unsafe {Database::create("my_db.redb", 1024 * 1024)? };
let write_txn = db.begin_writer()?;
{
let mut talbe = write_txt.open_table(TABLE)?;
table.insert("my_key", &123)?;
}
write_txt.commit()?;

let read_txt = db.begin_read()?;
let table = read_txn.open_table(TABLE)?;
assert_eq!(table.get("my_key")?.unwrap(), 123);

Ok(())
}
Benchmark:
+--------------------+--------+--------+--------+
| | redb | lmdb | sled |
+===============================================+
|
bulk load | 1605ms | 1294ms | 4642ms |
|--------------------+--------+--------+--------|
| individual writes | 516ms | 411ms | 527ms |
|
--------------------+--------+--------+--------|
|
batch writes | 7444ms | 3938ms | 1465ms |
|--------------------+--------+--------+--------|
| large writes | 12s | 11s | 315s |
|
--------------------+--------+--------+--------|
|
random reads | 716ms | 649ms | 1552ms |
|--------------------+--------+--------+--------|
| removals | 1282ms | 1012ms | 1911ms |
+--------------------+--------+--------+--------+
设计文档:https://github.com/cberner/redb/blob/master/docs/design.md
GitHub:https://github.com/cberner/redb

Rust异步框架评测

主要对 async_std,Tokio 和 smol 三个进行比较。结论如下:
  • async_std 和 smol 非常接近标准库,并在某些负载上更优。
  • Tokio 似乎受到 CPU-bound(Rust)异步任务的不利影响。
  • Tokio 在 localhost 引入 8µs、在网络上引入 10 µs 的延迟。
async_std:https://async.rs/
Tokio:https://tokio.rs/
smol:https://github.com/smol-rs/smol
文章链接:https://zenoh.io/blog/2022-04-14-rust-async-eval/

Passerine:小巧简洁可扩展的功能脚本语言

Passerine 植根于 Scheme 和 ML 风格的语言 —— 它是对编程语言所期望的一切的顶峰,包括希望一切尽可能简约、简洁的愿望。Passerine 的核心是具有模式匹配、结构类型、基于纤程的并发和句法扩展的 lambda 演算。
关于纤程可以阅读:https://github.com/Tencent/flare/blob/master/flare/doc/fiber.md 和 https://stackoverflow.com/questions/796217/what-is-the-difference-between-a-thread-and-a-fiber
官网:https://www.passerine.io/
GitHub:https://github.com/vrtbl/passerine

rust-apache-age:AGE连接器

AGE 是 postgres 的开源后端,允许用户在 postgres 上执行与图相关的操作。
use apache_age::{AgeClient, Client, NoTls, AgType};
use serde::{Deserialize, Serialize};

let mut client = Client::connect_age(
"host=localhost user=postgres password=passwd port=8081",
NoTls
).unwrap();

client.create_graph("my_apache_graph");

#[derive(Debug, Serialize, Deserialize, Clone)]
struct Person {
pub name: String,
pub surname: String,
}

match client.query_cypher::<()>(
"my_apache_graph",
"MATCH (n: Person) WHERE n.name = 'Alfred' RETURN {name: n.name, surname: n.surname}",
None,
) {
Ok(rows) => {
let x: AgType<Person> = rows[0].get(0);
// do whatever you need
}
Err(e) => {
// handle error
}
}

client.drop_graph("my_apache_graph");
代码中的 query 是图查询语言 Cypher,比较有名的Neo4j就是用的它,事实上,Cypher 是 Andrés Taylor 在位 Neo4j 工作时发明的。
GitHub:https://github.com/dzordzu/rust-apache-age

ripsecrets:防止密钥提交

一个防止密钥提交到源代码的命令行工具。有以下特点:
  • 关注预提交:从一开始就防止密钥被提交比后面处理要容易的多。
  • 速度极快:比其他工具快 95 倍。
  • 始终本地操作:仅本地执行,永远不会发送数据。
  • 误报率低:基于概率方法,比其他工具更准确。
  • 没有依赖的单个二进制文件。
GitHub:https://github.com/sirwart/ripsecrets

stevedore:Win下更好地使用的Docker

项目旨在在 Windows 上提供丝滑的 Docker 体验。可用作 Docker Desktop、DockerMsftProvider 或 Mirantis Container Runtime 的替代品。支持 Win 和 Linux 容器。
Mac 下可以用:https://github.com/abiosoft/colima
GitHub:https://github.com/slonopotamus/stevedore

pydantic-core:pydantic验证

pydantic 的核心功能。使用示例:
from pydantic_core import SchemaValidator, ValidationError

v = SchemaValidator({
'title': 'MyModel',
'type': 'model',
'fields': {
'name': {
'type': 'str',
},
'age': {
'type': 'int-constrained',
'ge': 18,
},
'is_developer': {
'type': 'bool',
'default': True,
},
},
})
print(v)
"""
SchemaValidator(title="MyModel", validator=ModelValidator ...
"""


r1 = v.validate_python({'name': 'Samuel', 'age': 35})
print(r1)
"""
(
{'name': 'Samuel', 'age': 35, 'is_developer': True}, <- validated data
{'age', 'name'} <- fields set
)
"""


# pydantic-core can also validate JSON directly
r2 = v.validate_json('{"name": "Samuel", "age": 35}')
assert r1 == r2

try:
v.validate_python({'name': 'Samuel', 'age': 11})
except ValidationError as e:
print(e)
"""
1 validation error for MyModel
age
Value must be greater than or equal to 18
[kind=int_greater_than_equal, context={ge: 18}, input_value=11, input_type=int]
"""

GitHub:https://github.com/samuelcolvin/pydantic-core

Rust平台crate

提供有关有效 Rust 平台信息的编程访问。需要 Rust 1.40 或更高。
GitHub:https://github.com/rustsec/rustsec/tree/main/platforms

pg2rs:pg到rust源码

命令行工具,用于从 Postgres 表创建 Rust 源代码实体。生成 enums 和 structs
mod structs;
use structs::{User};
let result = client.query("SELECT * FROM \"Users\"", &[]).await.unwrap();
let users: Vec<User> = result.into_iter().map(|row| User::from(row)).collect();
println!("{:#?}", users);
GitHub:https://github.com/spanasik/pg2rs

资源和教程

  • 新手资源:https://apollolabsblog.hashnode.dev/35-rust-learning-resources-every-beginner-should-know-in-2022
  • Nim,Zig,Rust,C++编译时间评测:https://castillodel.github.io/compile-time-evaluation/
  • 一个项目中引入两个不同版本的 crates:https://reltech.substack.com/p/including-two-versions-of-a-rust?s=r
  • 对 DynamoDB 全文检索:https://jakejscott.com/full-text-search-for-dynamodb-using-lambda-efs-tantivy-and-rust
  • 命令行参数解析工具 clap 使用:https://blog.logrocket.com/command-line-argument-parsing-rust-using-clap/

From 日报小组 长琴

社区学习交流平台订阅:
  • Rustcc 论坛:支持 rss
  • 微信公众号:Rust 语言中文社区

57890【Rust日报】2022-04-24 redb:嵌入式DB

这个人很懒,什么都没留下

文章评论