21个版本 (5个重大更新)
0.6.0 | 2024年3月18日 |
---|---|
0.5.1 | 2024年3月9日 |
0.4.3 | 2024年3月9日 |
0.3.0 | 2024年3月9日 |
0.1.11 | 2024年3月5日 |
54 在 数据库实现 中
每月下载量 1,210
29KB
(不包括注释)SLoC 约96K
Airomem-rs
在crates.io发布
(Toy) Rust的持久化库,受prevayler和airomem启发。
它遵循Prevalent系统设计模式,其中业务对象保存在内存中,并且事务被记录以便系统恢复。
假设
- 所有数据都保存在内存中。
- 默认情况下,每个事务都保存到追加日志文件,并且立即进行同步。因此,单个写操作可能较慢,但它们应该能够从故障中恢复(例如,电源故障或软件崩溃)。
但是,您可以手动同步。有关更多信息,请参阅JournalFlushPolicy
。 - 我不保证持久性,它适用于玩具项目或非关键数据(例如缓存)。
特性
- - 将执行的事务保存到追加文件
- - 如果日志文件过大,则拆分日志文件 - 恢复数据时,一次性从磁盘加载所有日志以最大化吞吐量(并简化过程)
- - 快照以实现快速恢复
- - 稳定的API
资源
- Jaroslaw Ratajski - DROP DATABASE - 星际故事
- https://postgresql.ac.cn/docs/9.4/wal-reliability.html
- https://github.com/killertux/prevayler-rs
示例
type UserId = usize;
type SessionsStore = JsonStore<Sessions, SessionsTx>;
#[derive(Serialize, Deserialize, Default)]
pub struct Sessions {
tokens: HashMap<String, UserId>,
operations: usize,
}
MergeTx!(pub SessionsTx<Sessions> = CreateSession | DeleteSession);
#[derive(Serialize, Deserialize)]
pub struct CreateSession {
token: String,
user_id: UserId,
}
impl Tx<Sessions> for CreateSession {
fn execute(self, data: &mut Sessions) {
data.operations += 1;
data.tokens.insert(self.token, self.user_id);
}
}
#[derive(Serialize, Deserialize)]
pub struct DeleteSession {
token: String,
}
impl Tx<Sessions, Option<UserId>> for DeleteSession {
fn execute(self, data: &mut Sessions) -> Option<UserId> {
data.operations += 1;
data.tokens.remove(&self.token)
}
}
#[tokio::test]
async fn test_mem_commit() {
let dir = tempdir().unwrap();
let mut store: SessionsStore =
Store::open(JsonSerializer, StoreOptions::default(), dir.into_path())
.await
.unwrap();
let example_token = "access_token".to_string();
let example_uid = 1;
store
.commit(CreateSession {
token: example_token.clone(),
user_id: example_uid,
})
.await
.unwrap();
let mut expected_tokens = HashMap::new();
expected_tokens.insert(example_token.clone(), example_uid);
assert_eq!(store.query().tokens, expected_tokens);
let deleted_uid = store
.commit(DeleteSession {
token: example_token,
})
.await
.unwrap();
assert_eq!(deleted_uid, Some(example_uid));
}
依赖项
~3.5–5.5MB
~96K SLoC