#key-value-database #graph-database #library #directed-graph #graph-node #versatile

semilattice-database

Data is connected by a directed graph, and each node has an arbitrarily defined key-value.A database is not a tree

61 releases (34 breaking)

0.110.0 Feb 13, 2024
0.108.0 Feb 6, 2024
0.98.0 Dec 26, 2023
0.93.0 Nov 29, 2023
0.31.0 Nov 23, 2022

#481 in Database interfaces

Download history 65/week @ 2023-12-11 247/week @ 2023-12-18 214/week @ 2023-12-25 129/week @ 2024-01-01 34/week @ 2024-01-08 21/week @ 2024-01-15 71/week @ 2024-01-22 99/week @ 2024-01-29 53/week @ 2024-02-05 47/week @ 2024-02-12 16/week @ 2024-02-19 108/week @ 2024-02-26 1833/week @ 2024-03-11 8/week @ 2024-03-25

1,843 downloads per month
Used in 8 crates (2 directly)

MIT/Apache

38KB
928 lines

semilattice-database

Example

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()
                );
            }
        }
    }
});

Dependencies

~4–12MB
~92K SLoC