#library

semilattice-database

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

14 releases (4 breaking)

new 0.79.1 Sep 18, 2023
0.78.2 Sep 7, 2023
0.73.0 Jul 29, 2023
0.55.0 Mar 25, 2023
0.31.0 Nov 23, 2022

#839 in Database interfaces

Download history 287/week @ 2023-05-28 99/week @ 2023-06-04 189/week @ 2023-06-11 183/week @ 2023-06-18 71/week @ 2023-06-25 251/week @ 2023-07-02 205/week @ 2023-07-09 168/week @ 2023-07-16 168/week @ 2023-07-23 28/week @ 2023-07-30 180/week @ 2023-08-06 166/week @ 2023-08-13 106/week @ 2023-08-20 353/week @ 2023-08-27 193/week @ 2023-09-03 393/week @ 2023-09-10

1,046 downloads per month
Used in 4 crates (2 directly)

MIT/Apache

37KB
947 lines

semilattice-database

Example

use semilattice_database::*;

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();
}
let mut database = Database::new(dir, None);

let collection_person_id=database.collection_id_or_create("person");
let collection_history_id=database.collection_id_or_create("history");

if let Some(collection_person) = database.collection_mut(collection_person_id) {
    if let Ok(row) = collection_person.update(&Operation::New {
        activity: Activity::Active,
        term_begin: Term::Default,
        term_end: Term::Default,
        fields: vec![
            KeyValue::new("name", "Joe"),
            KeyValue::new("birthday", "1972-08-02"),
        ],
    }) {
        let depend = CollectionRow::new(collection_person_id, row);
        let mut pends = vec![];
        if let Some(collection_history) = database.collection_mut(collection_history_id) {
            let history_row = collection_history.update(&Operation::New {
                activity: Activity::Active,
                term_begin: Term::Default,
                term_end: Term::Default,
                fields: vec![
                    KeyValue::new("date", "1972-08-02"),
                    KeyValue::new("event", "Birth"),
                ],
            });
            pends.push((
                "history".to_owned(),
                CollectionRow::new(collection_history_id, history_row),
            ));

            let history_row = collection_history.update(&Operation::New {
                activity: Activity::Active,
                term_begin: Term::Default,
                term_end: Term::Default,
                fields: vec![
                    KeyValue::new("date", "1999-12-31"),
                    KeyValue::new("event", "Mariage"),
                ],
            });
            pends.push((
                "history".to_owned(),
                CollectionRow::new(collection_history_id, history_row),
            ));
        }
        database.register_relations(&depend, pends).unwrap();
    }
}
if let (Some(person), Some(history)) = (
    database.collection(collection_person_id),
    database.collection(collection_history_id),
) {
    let search = database.search(person);
    if let Ok(result) = search.result(&database) {
        for row in result {
            println!(
                "{},{}",
                std::str::from_utf8(person.field_bytes(row, "name")).unwrap(),
                std::str::from_utf8(person.field_bytes(row, "birthday")).unwrap()
            );
            let search = database
                .search(history)
                .search(Condition::Depend(
                    Some("history".to_owned()),
                    CollectionRow::new(collection_person_id, row),
                ));
            for h in database.result(search, &vec![]).unwrap() {
                println!(
                    " {} : {}",
                    std::str::from_utf8(history.field_bytes(h, "date")).unwrap(),
                    std::str::from_utf8(history.field_bytes(h, "event")).unwrap()
                );
            }
        }
    }
}

Dependencies

~2–3MB
~59K SLoC