aboutsummaryrefslogtreecommitdiff
path: root/base/src/database.rs
blob: ba10527b3f29e8dca2e514e907e128f0b1317566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2023 metamuffin <metamuffin.org>
*/
use anyhow::Context;
use jellycommon::{user::User, Node};
use log::info;
use std::{collections::HashMap, path::Path};
use typed_sled::Tree;

pub use sled;
pub use typed_sled;

pub struct Database {
    pub db: sled::Db,

    pub user: Tree<String, User>,
    pub user_progess: Tree<String, HashMap<String, f64>>,
    pub invite: Tree<String, ()>,
    pub node: Tree<String, Node>,
}

impl Database {
    pub fn open(path: &Path) -> Result<Self, anyhow::Error> {
        info!("opening database… (might take up to O(n) time)");
        let db = sled::open(path).context("opening database")?;
        info!("creating trees");
        let r = Ok(Self {
            user: Tree::open(&db, "user"),
            invite: Tree::open(&db, "invite"),
            node: Tree::open(&db, "node"),
            user_progess: Tree::open(&db, "user_progesss"),
            db,
        });
        info!("ready");
        r
    }
}