aboutsummaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
Diffstat (limited to 'base')
-rw-r--r--base/Cargo.toml2
-rw-r--r--base/src/database.rs36
-rw-r--r--base/src/lib.rs1
3 files changed, 39 insertions, 0 deletions
diff --git a/base/Cargo.toml b/base/Cargo.toml
index 45c3843..0dede64 100644
--- a/base/Cargo.toml
+++ b/base/Cargo.toml
@@ -13,3 +13,5 @@ base64 = "0.21.4"
tokio = { workspace = true }
anyhow = "1.0.75"
bincode = "2.0.0-rc.3"
+sled = "0.34.7"
+typed-sled = "0.2.3"
diff --git a/base/src/database.rs b/base/src/database.rs
new file mode 100644
index 0000000..b8ba28e
--- /dev/null
+++ b/base/src/database.rs
@@ -0,0 +1,36 @@
+/*
+ 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::path::Path;
+use typed_sled::Tree;
+
+pub use sled;
+
+pub struct Database {
+ pub db: sled::Db,
+
+ pub user: Tree<String, User>,
+ 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"),
+ db,
+ });
+ info!("ready");
+ r
+ }
+}
diff --git a/base/src/lib.rs b/base/src/lib.rs
index 132fd45..5d96b1a 100644
--- a/base/src/lib.rs
+++ b/base/src/lib.rs
@@ -7,6 +7,7 @@
pub mod cache;
pub mod permission;
pub mod temp;
+pub mod database;
use jellycommon::{config::GlobalConfig, AssetLocation};
use std::{fs::File, path::PathBuf, sync::LazyLock};