aboutsummaryrefslogtreecommitdiff
path: root/database/src/kv/index_key.rs
blob: ec8f1832541b17bbce9ace4839e82e2791d94b7e (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use std::fmt::Display;

/*
    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>
*/
use crate::{
    MultiBehaviour, Sort,
    kv::{
        T_INDICES,
        binning::{Binning, BinningComponent},
    },
};
use jellyobject::{Path, Tag};

#[derive(Debug, PartialEq, Eq)]
pub struct IndexKey(pub Binning, pub SortKey);

#[derive(Debug, Hash, PartialEq, Eq)]
pub enum SortKey {
    None,
    Count,
    Random,
    Value(Path, MultiBehaviour),
    Text(Path),
}

impl Sort {
    pub fn key(&self) -> SortKey {
        match self {
            Sort::None => SortKey::None,
            Sort::Random(_) => SortKey::Random,
            Sort::Value(vs) => SortKey::Value(vs.path.clone(), vs.multi),
            Sort::TextSearch(p, _) => SortKey::Text(p.to_owned()),
        }
    }
}

impl IndexKey {
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut out = Vec::new();
        out.extend(T_INDICES.to_be_bytes());
        self.0.write(&mut out);
        self.1.write(&mut out);
        out
    }
    pub fn from_bytes(mut b: &[u8]) -> Self {
        b = &b[4..]; // remove subtree prefix
        let binning = Binning::read(&mut b);
        let sort = SortKey::read(&mut b);
        assert!(b.is_empty(), "index key deser left-over bytes");
        Self(binning, sort)
    }
}
impl Binning {
    fn read(b: &mut &[u8]) -> Self {
        let len = b[0];
        *b = &b[1..];
        let mut o = Vec::new();
        for _ in 0..len {
            let ty = b[0];
            *b = &b[1..];
            o.push(match ty {
                0 => BinningComponent::Has(read_path(b)),
                1 => BinningComponent::Match(read_path(b)),
                _ => unreachable!(),
            });
        }
        Self(o)
    }
    fn write(&self, out: &mut Vec<u8>) {
        assert!(out.len() < 256);
        out.push(self.0.len() as u8);
        for b in &self.0 {
            match b {
                BinningComponent::Has(path) => {
                    out.push(0);
                    write_path(path, out);
                }
                BinningComponent::Match(path) => {
                    out.push(1);
                    write_path(path, out);
                }
            }
        }
    }
}
impl SortKey {
    fn read(b: &mut &[u8]) -> Self {
        let ty = b[0];
        *b = &b[1..];
        match ty {
            0 => SortKey::None,
            4 => SortKey::Random,
            1 => SortKey::Count,
            2 => SortKey::Value(read_path(b), {
                let mb = b[0];
                *b = &b[1..];
                match mb {
                    0 => MultiBehaviour::First,
                    1 => MultiBehaviour::ForEach,
                    2 => MultiBehaviour::Max,
                    3 => MultiBehaviour::Min,
                    4 => MultiBehaviour::Count,
                    _ => unreachable!(),
                }
            }),
            3 => SortKey::Text(read_path(b)),
            _ => unreachable!(),
        }
    }
    fn write(&self, out: &mut Vec<u8>) {
        match self {
            SortKey::None => out.push(0),
            SortKey::Random => out.push(4),
            SortKey::Count => out.push(1),
            SortKey::Value(path, multi_behaviour) => {
                out.push(2);
                write_path(path, out);
                out.push(match multi_behaviour {
                    MultiBehaviour::First => 0,
                    MultiBehaviour::ForEach => 1,
                    MultiBehaviour::Max => 2,
                    MultiBehaviour::Min => 3,
                    MultiBehaviour::Count => 4,
                });
            }
            SortKey::Text(path) => {
                out.push(3);
                write_path(path, out);
            }
        }
    }
}

impl Display for IndexKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} | {}", self.0, self.1)
    }
}
impl Display for Binning {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for (i, b) in self.0.iter().enumerate() {
            if i > 0 {
                write!(f, " ")?;
            }
            write!(f, "{b}")?;
        }
        Ok(())
    }
}
impl Display for BinningComponent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BinningComponent::Has(path) => write!(f, "H({path})"),
            BinningComponent::Match(path) => write!(f, "M({path})"),
        }
    }
}
impl Display for SortKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SortKey::None => write!(f, "none"),
            SortKey::Random => write!(f, "random"),
            SortKey::Count => write!(f, "count"),
            SortKey::Value(path, multi) => write!(f, "value({path}, {multi})"),
            SortKey::Text(path) => write!(f, "text({path})"),
        }
    }
}
impl Display for MultiBehaviour {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            MultiBehaviour::First => "first",
            MultiBehaviour::ForEach => "for_each",
            MultiBehaviour::Max => "max",
            MultiBehaviour::Min => "min",
            MultiBehaviour::Count => "count",
        })
    }
}

fn write_path(path: &Path, out: &mut Vec<u8>) {
    assert!(path.0.len() < 256);
    out.push(path.0.len() as u8);
    for c in &path.0 {
        out.extend(c.0.to_be_bytes());
    }
}
fn read_path(b: &mut &[u8]) -> Path {
    let len = b[0];
    *b = &b[1..];
    let mut o = Vec::new();
    for _ in 0..len {
        o.push(Tag(u32::from_be_bytes([b[0], b[1], b[2], b[3]])));
        *b = &b[4..];
    }
    Path(o)
}

#[cfg(test)]
mod test {
    use jellyobject::{Path, Tag};

    use crate::kv::{
        binning::{Binning, BinningComponent},
        index_key::{IndexKey, SortKey},
    };

    #[test]
    fn serialize() {
        let t = |k: IndexKey| {
            let b = k.to_bytes();
            let k2 = IndexKey::from_bytes(&b);
            assert_eq!(k, k2);
        };

        t(IndexKey(
            Binning(vec![BinningComponent::Match(Path(vec![Tag(1001)]))]),
            SortKey::None,
        ));
        t(IndexKey(
            Binning(vec![BinningComponent::Has(Path(vec![Tag(123), Tag(456)]))]),
            SortKey::Text(Path(vec![Tag(789)])),
        ));
    }
}