aboutsummaryrefslogtreecommitdiff
path: root/database/src/lib.rs
blob: 828761ecc158bffa42d51247c13d577277d7c091 (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
40
41
42
43
44
45
46
47
48
/*
    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) 2025 metamuffin <metamuffin.org>
*/

use anyhow::{Result, bail};

use crate::{
    backends::{KV, memory::Memory, redb::Redb, rocksdb::Rocksdb},
    indices::Index,
};
use std::{path::Path, sync::Arc};

pub mod backends;
pub mod indices;

pub struct Database {
    storage: Arc<dyn KV>,
}

impl Database {
    pub fn new(driver: &str, path: &Path) -> Result<Self> {
        Ok(Self {
            storage: match driver {
                "rocksdb" => Arc::new(Rocksdb::new(path)?),
                "redb" => Arc::new(Redb::new(path)?),
                "memory" => Arc::new(Memory::new()),
                _ => bail!("unknown db driver"),
            },
        })
    }
}

pub struct Table<T> {
    id: u32,
    indices: Vec<Box<dyn Index<T>>>,
    db: Arc<dyn KV>,
}
impl<T> Table<T> {
    pub fn new(db: &Database, id: u32) -> Self {
        Self {
            id,
            indices: Vec::new(),
            db: db.storage.clone(),
        }
    }
}