aboutsummaryrefslogtreecommitdiff
path: root/database/src/lib.rs
blob: 2ecf3c68f1169c972035f425738a6c8d426421b4 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
    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) 2026 metamuffin <metamuffin.org>
*/
pub mod kv;
pub mod query_ser;
#[cfg(test)]
pub mod test_shared;

use anyhow::Result;
use jellyobject::{ObjectBuffer, Path, Tag};

pub type RowNum = u64;
pub type RowIter = Box<dyn Iterator<Item = Result<(RowNum, Vec<u8>)>>>;

pub trait Database: Send + Sync {
    fn transaction(&self, f: &mut dyn FnMut(&mut dyn Transaction) -> Result<()>) -> Result<()>;
}

pub trait Transaction {
    fn insert(&mut self, entry: ObjectBuffer) -> Result<RowNum>;
    fn remove(&mut self, row: RowNum) -> Result<()>;
    fn update(&mut self, row: RowNum, entry: ObjectBuffer) -> Result<()>;
    fn get(&self, row: RowNum) -> Result<Option<ObjectBuffer>>;
    fn query<'a>(
        &'a mut self,
        query: Query,
    ) -> Result<Box<dyn Iterator<Item = Result<(RowNum, Vec<u8>)>> + 'a>>;
    fn query_single(&mut self, query: Query) -> Result<Option<RowNum>>;
    fn count(&mut self, query: Query) -> Result<u64>;
    fn debug_info(&self) -> Result<String>;
}

#[derive(Default, Clone)]
pub struct Query {
    pub filter: Filter,
    pub sort: Sort,
}

#[derive(Default, Clone)]
pub enum Sort {
    #[default]
    None,
    Value(ValueSort),
    TextSearch(Path, String),
}

#[derive(Clone)]
pub struct ValueSort {
    pub order: SortOrder,
    pub path: Path,
    pub multi: MultiBehaviour,
    pub offset: Option<Vec<u8>>,
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum MultiBehaviour {
    First,
    ForEach,
    Max,
    Min,
    Count,
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum SortOrder {
    Ascending,
    Descending,
}

#[derive(Debug, Clone, Default)]
pub enum Filter {
    #[default]
    True,
    All(Vec<Filter>),
    Any(Vec<Filter>),
    Match(Path, Value),
    Has(Path),
}

#[derive(Debug, Clone)]
pub enum Value {
    Tag(Tag),
    U32(u32),
    U64(u64),
    I64(i64),
    String(String),
    Binary(Vec<u8>),
}

impl From<&str> for Value {
    fn from(value: &str) -> Self {
        Self::String(value.to_owned())
    }
}
impl From<String> for Value {
    fn from(value: String) -> Self {
        Self::String(value)
    }
}
impl From<u32> for Value {
    fn from(value: u32) -> Self {
        Self::U32(value)
    }
}
impl From<u64> for Value {
    fn from(value: u64) -> Self {
        Self::U64(value)
    }
}
impl From<i64> for Value {
    fn from(value: i64) -> Self {
        Self::I64(value)
    }
}
impl From<Tag> for Value {
    fn from(value: Tag) -> Self {
        Self::Tag(value)
    }
}