61 个版本 (34 个破坏性更新)
0.110.0 | 2024年2月13日 |
---|---|
0.108.0 | 2024年2月6日 |
0.98.0 | 2023年12月26日 |
0.93.0 | 2023年11月29日 |
0.31.0 |
|
#716 在 数据库接口
每月86 次下载
在 8 个Crates中(2 个直接) 使用
38KB
928 行
semilattice-database
示例
use std::sync::Arc;
use semilattice_database::*;
use versatile_data::FieldName;
let dir = "./sl-test/";
if std::path::Path::new(dir).exists() {
std::fs::remove_dir_all(dir).unwrap();
std::fs::create_dir_all(dir).unwrap();
} else {
std::fs::create_dir_all(dir).unwrap();
}
futures::executor::block_on(async {
let mut database = Database::new(dir.into(), None, 10);
let collection_person_id = database.collection_id_or_create("person");
let collection_history_id = database.collection_id_or_create("history");
let collection_person = database.collection_mut(collection_person_id).unwrap();
let id_name = FieldName::new("name".into());
let id_birthday = FieldName::new("birthday".into());
let row = collection_person
.insert(
Activity::Active,
Term::Default,
Term::Default,
[
(id_name.clone(), "Joe".into()),
(id_birthday.clone(), "1972-08-02".into()),
]
.into(),
)
.await;
let depend = CollectionRow::new(collection_person_id, row);
let mut pends = vec![];
let collection_history = database.collection_mut(collection_history_id).unwrap();
let id_date = FieldName::new("date".into());
let id_event = FieldName::new("event".into());
let history_row = collection_history
.insert(
Activity::Active,
Term::Default,
Term::Default,
[
(id_date.clone(), "1972-08-02".into()),
(id_event.clone(), "Birth".into()),
]
.into(),
)
.await;
pends.push((
Arc::new("history".to_owned()),
CollectionRow::new(collection_history_id, history_row),
));
let history_row = collection_history
.insert(
Activity::Active,
Term::Default,
Term::Default,
[
(id_date.clone(), "1999-12-31".into()),
(id_event.clone(), "Mariage".into()),
]
.into(),
)
.await;
pends.push((
Arc::new("history".to_owned()),
CollectionRow::new(collection_history_id, history_row),
));
database.register_relations(&depend, pends).await;
if let (Some(person), Some(history)) = (
database.collection(collection_person_id),
database.collection(collection_history_id),
) {
let result = database
.search(collection_person_id)
.result(&database)
.await;
for row in result.rows().into_iter() {
println!(
"{},{}",
std::str::from_utf8(person.field_bytes(*row, &id_name)).unwrap(),
std::str::from_utf8(person.field_bytes(*row, &id_birthday)).unwrap()
);
for h in database
.search(collection_history_id)
.search(Condition::Depend(
Some(Arc::new("history".into())),
CollectionRow::new(collection_person_id, *row),
))
.result(&database)
.await
.rows()
.into_iter()
{
println!(
" {} : {}",
std::str::from_utf8(history.field_bytes(*h, &id_date)).unwrap(),
std::str::from_utf8(history.field_bytes(*h, &id_event)).unwrap()
);
}
}
}
});
依赖
~4–10MB
~91K SLoC