aboutsummaryrefslogtreecommitdiff
path: root/base/src/database.rs
blob: c3ca5d4bbc38eeb939891b61fc17d74d1d0ce493 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
    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::{anyhow, bail, Context, Result};
use bincode::{config::standard, Decode, Encode};
use jellycommon::{
    user::{NodeUserData, User},
    Node, NodeID,
};
use log::info;
use redb::{Durability, ReadableTable, StorageError, TableDefinition};
use std::{
    fs::create_dir_all,
    hash::{DefaultHasher, Hasher},
    path::{Path, PathBuf},
    str::FromStr,
    sync::{Arc, RwLock},
    time::SystemTime,
};
use tantivy::{
    collector::{Count, TopDocs},
    directory::MmapDirectory,
    doc,
    query::QueryParser,
    schema::{Field, Schema, Value, FAST, INDEXED, STORED, STRING, TEXT},
    DateOptions, DateTime, Index, IndexReader, IndexWriter, ReloadPolicy, TantivyDocument,
};

const T_USER: TableDefinition<&str, Ser<User>> = TableDefinition::new("user");
const T_USER_NODE: TableDefinition<(&str, [u8; 32]), Ser<NodeUserData>> =
    TableDefinition::new("user_node");
const T_INVITE: TableDefinition<&str, ()> = TableDefinition::new("invite");
const T_NODE: TableDefinition<[u8; 32], Ser<Node>> = TableDefinition::new("node");
const T_NODE_CHILDREN: TableDefinition<([u8; 32], [u8; 32]), ()> =
    TableDefinition::new("node_children");
const T_TAG_NODE: TableDefinition<(&str, [u8; 32]), ()> = TableDefinition::new("tag_node");
const T_NODE_EXTERNAL_ID: TableDefinition<(&str, &str), [u8; 32]> =
    TableDefinition::new("node_external_id");
const T_IMPORT_FILE_MTIME: TableDefinition<&[u8], u64> = TableDefinition::new("import_file_mtime");
const T_NODE_MTIME: TableDefinition<[u8; 32], u64> = TableDefinition::new("node_mtime");
const T_NODE_MEDIA_PATHS: TableDefinition<([u8; 32], &str), ()> =
    TableDefinition::new("node_media_paths");

#[derive(Clone)]
pub struct Database {
    inner: Arc<redb::Database>,
    text_search: Arc<NodeTextSearchIndex>,
}

impl Database {
    pub fn open(path: &Path) -> Result<Self, anyhow::Error> {
        create_dir_all(path).context("creating database directory")?;
        info!("opening kv store...");
        let db = redb::Database::create(path.join("data")).context("opening kv store")?;
        info!("opening node index...");
        let ft_node = NodeTextSearchIndex::new(path).context("in node index")?;
        let r = Self {
            inner: db.into(),
            text_search: ft_node.into(),
        };

        {
            // this creates all tables such that read operations on them do not fail.
            let txn = r.inner.begin_write()?;
            txn.open_table(T_INVITE)?;
            txn.open_table(T_USER)?;
            txn.open_table(T_USER_NODE)?;
            txn.open_table(T_NODE)?;
            txn.open_table(T_NODE_MTIME)?;
            txn.open_table(T_NODE_CHILDREN)?;
            txn.open_table(T_NODE_EXTERNAL_ID)?;
            txn.open_table(T_NODE_MEDIA_PATHS)?;
            txn.open_table(T_IMPORT_FILE_MTIME)?;
            txn.commit()?;
        }

        info!("ready");
        Ok(r)
    }

    pub fn get_node_slug(&self, slug: &str) -> Result<Option<Arc<Node>>> {
        self.get_node(NodeID::from_slug(slug))
    }

    pub fn get_node(&self, id: NodeID) -> Result<Option<Arc<Node>>> {
        let txn = self.inner.begin_read()?;
        let t_node = txn.open_table(T_NODE)?;
        if let Some(node) = t_node.get(id.0)? {
            Ok(Some(node.value().0.into()))
        } else {
            Ok(None)
        }
    }
    pub fn get_node_external_id(&self, platform: &str, eid: &str) -> Result<Option<NodeID>> {
        let txn = self.inner.begin_read()?;
        let t_node_external_id = txn.open_table(T_NODE_EXTERNAL_ID)?;
        if let Some(id) = t_node_external_id.get((platform, eid))? {
            Ok(Some(NodeID(id.value())))
        } else {
            Ok(None)
        }
    }
    pub fn get_node_children(&self, id: NodeID) -> Result<Vec<NodeID>> {
        let txn = self.inner.begin_read()?;
        let t_node_children = txn.open_table(T_NODE_CHILDREN)?;
        Ok(t_node_children
            .range((id.0, NodeID::MIN.0)..(id.0, NodeID::MAX.0))?
            .map(|r| r.map(|r| NodeID(r.0.value().1)))
            .collect::<Result<Vec<_>, StorageError>>()?)
    }
    pub fn get_tag_nodes(&self, tag: &str) -> Result<Vec<NodeID>> {
        let txn = self.inner.begin_read()?;
        let t_tag_node = txn.open_table(T_TAG_NODE)?;
        Ok(t_tag_node
            .range((tag, NodeID::MIN.0)..(tag, NodeID::MAX.0))?
            .map(|r| r.map(|r| NodeID(r.0.value().1)))
            .collect::<Result<Vec<_>, StorageError>>()?)
    }
    pub fn get_nodes_modified_since(&self, since: u64) -> Result<Vec<NodeID>> {
        let txn = self.inner.begin_read()?;
        let t_node_mtime = txn.open_table(T_NODE_MTIME)?;
        Ok(t_node_mtime
            .iter()?
            .flat_map(|r| r.map(|r| (NodeID(r.0.value()), r.1.value())))
            .filter(|(_, mtime)| *mtime >= since)
            .map(|(id, _)| id)
            .collect())
    }

    pub fn clear_nodes(&self) -> Result<()> {
        let mut txn = self.inner.begin_write()?;
        let mut t_node = txn.open_table(T_NODE)?;
        let mut t_node_mtime = txn.open_table(T_NODE_MTIME)?;
        let mut t_node_children = txn.open_table(T_NODE_CHILDREN)?;
        let mut t_node_external_id = txn.open_table(T_NODE_EXTERNAL_ID)?;
        let mut t_import_file_mtime = txn.open_table(T_IMPORT_FILE_MTIME)?;
        let mut t_node_media_paths = txn.open_table(T_NODE_MEDIA_PATHS)?;
        t_node.retain(|_, _| false)?;
        t_node_mtime.retain(|_, _| false)?;
        t_node_children.retain(|_, _| false)?;
        t_node_external_id.retain(|_, _| false)?;
        t_import_file_mtime.retain(|_, _| false)?;
        t_node_media_paths.retain(|_, _| false)?;
        drop((
            t_node,
            t_node_mtime,
            t_node_children,
            t_node_external_id,
            t_import_file_mtime,
            t_node_media_paths,
        ));
        txn.set_durability(Durability::Eventual);
        txn.commit()?;
        Ok(())
    }

    pub fn get_node_udata(&self, id: NodeID, username: &str) -> Result<Option<NodeUserData>> {
        let txn = self.inner.begin_read()?;
        let t_node = txn.open_table(T_USER_NODE)?;
        if let Some(node) = t_node.get((username, id.0))? {
            Ok(Some(node.value().0))
        } else {
            Ok(None)
        }
    }

    pub fn update_node_init(
        &self,
        id: NodeID,
        update: impl FnOnce(&mut Node) -> Result<()>,
    ) -> Result<()> {
        let time = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let mut txn = self.inner.begin_write()?;
        let mut t_node = txn.open_table(T_NODE)?;
        let mut t_node_mtime = txn.open_table(T_NODE_MTIME)?;
        let mut t_node_children = txn.open_table(T_NODE_CHILDREN)?;
        let mut t_node_external_id = txn.open_table(T_NODE_EXTERNAL_ID)?;
        let mut t_tag_node = txn.open_table(T_TAG_NODE)?;
        let mut node = t_node.get(id.0)?.map(|v| v.value().0).unwrap_or_default();

        let mut dh_before = HashWriter(DefaultHasher::new());
        bincode::encode_into_writer(&node, &mut dh_before, standard()).unwrap();
        update(&mut node)?;
        let mut dh_after = HashWriter(DefaultHasher::new());
        bincode::encode_into_writer(&node, &mut dh_after, standard()).unwrap();

        if dh_before.0.finish() == dh_after.0.finish() {
            return Ok(());
        }

        for parent in &node.parents {
            t_node_children.insert((parent.0, id.0), ())?;
        }
        for (pl, eid) in &node.external_ids {
            t_node_external_id.insert((pl.as_str(), eid.as_str()), id.0)?;
        }
        for tag in &node.tags {
            t_tag_node.insert((tag.as_str(), id.0), ())?;
        }
        t_node.insert(&id.0, Ser(node))?;
        t_node_mtime.insert(&id.0, time)?;
        drop((
            t_node,
            t_node_mtime,
            t_node_children,
            t_node_external_id,
            t_tag_node,
        ));
        txn.set_durability(Durability::Eventual);
        txn.commit()?;
        Ok(())
    }
    pub fn get_node_media_paths(&self, id: NodeID) -> Result<Vec<PathBuf>> {
        let txn = self.inner.begin_read()?;
        let table = txn.open_table(T_NODE_MEDIA_PATHS)?;
        let mut paths = Vec::new();
        // TODO fix this
        for p in table.range((id.0, "\0")..(id.0, "\x7f"))? {
            paths.push(PathBuf::from_str(p?.0.value().1)?);
        }
        Ok(paths)
    }
    pub fn insert_node_media_path(&self, id: NodeID, path: &Path) -> Result<()> {
        let txn = self.inner.begin_write()?;
        let mut table = txn.open_table(T_NODE_MEDIA_PATHS)?;
        table.insert((id.0, path.to_str().unwrap()), ())?;
        drop(table);
        txn.commit()?;
        Ok(())
    }

    pub fn update_node_udata(
        &self,
        node: NodeID,
        username: &str,
        update: impl FnOnce(&mut NodeUserData) -> Result<()>,
    ) -> Result<()> {
        let txn = self.inner.begin_write()?;
        let mut user_nodes = txn.open_table(T_USER_NODE)?;

        let mut udata = user_nodes
            .get((username, node.0))?
            .map(|x| x.value().0)
            .unwrap_or_default();

        update(&mut udata)?;

        user_nodes.insert((username, node.0), Ser(udata))?;
        drop(user_nodes);
        txn.commit()?;
        Ok(())
    }

    pub fn get_user(&self, username: &str) -> Result<Option<User>> {
        let txn = self.inner.begin_read()?;
        let t_user = txn.open_table(T_USER)?;
        if let Some(user) = t_user.get(username)? {
            Ok(Some(user.value().0))
        } else {
            Ok(None)
        }
    }
    pub fn update_user(
        &self,
        username: &str,
        update: impl FnOnce(&mut User) -> Result<()>,
    ) -> Result<()> {
        let txn = self.inner.begin_write()?;
        let mut users = txn.open_table(T_USER)?;
        let mut user = users
            .get(username)?
            .ok_or(anyhow!("user does not exist"))?
            .value()
            .0;
        update(&mut user)?;
        users.insert(&username, Ser(user))?;
        drop(users);
        txn.commit()?;

        Ok(())
    }
    pub fn delete_user(&self, username: &str) -> Result<bool> {
        let txn = self.inner.begin_write()?;
        let mut table = txn.open_table(T_USER)?;
        let r = table.remove(username)?.is_some();
        drop(table);
        txn.commit()?;
        Ok(r)
    }
    pub fn list_users(&self) -> Result<Vec<User>> {
        let txn = self.inner.begin_read()?;
        let table = txn.open_table(T_USER)?;
        let i = table
            .iter()?
            .map(|a| {
                let (_, y) = a.unwrap(); // TODO
                y.value().0
            })
            .collect::<Vec<_>>();
        drop(table);
        Ok(i)
    }
    pub fn list_invites(&self) -> Result<Vec<String>> {
        let txn = self.inner.begin_read()?;
        let table = txn.open_table(T_INVITE)?;
        let i = table
            .iter()?
            .map(|a| {
                let (x, _) = a.unwrap();
                x.value().to_owned()
            })
            .collect::<Vec<_>>();
        drop(table);
        Ok(i)
    }
    pub fn create_invite(&self, inv: &str) -> Result<()> {
        let txn = self.inner.begin_write()?;
        let mut table = txn.open_table(T_INVITE)?;
        table.insert(inv, ())?;
        drop(table);
        txn.commit()?;
        Ok(())
    }
    pub fn delete_invite(&self, inv: &str) -> Result<bool> {
        let txn = self.inner.begin_write()?;
        let mut table = txn.open_table(T_INVITE)?;
        let r = table.remove(inv)?.is_some();
        drop(table);
        txn.commit()?;
        Ok(r)
    }
    pub fn register_user(&self, invite: &str, username: &str, user: User) -> Result<()> {
        let txn = self.inner.begin_write()?;
        let mut invites = txn.open_table(T_INVITE)?;
        let mut users = txn.open_table(T_USER)?;

        if invites.remove(invite)?.is_none() {
            bail!("invitation invalid");
        }
        let prev_user = users.insert(username, Ser(user))?.map(|x| x.value().0);
        if prev_user.is_some() {
            bail!("username taken");
        }

        drop(users);
        drop(invites);
        txn.commit()?;
        Ok(())
    }
    pub fn list_nodes_with_udata(&self, username: &str) -> Result<Vec<(Arc<Node>, NodeUserData)>> {
        let txn = self.inner.begin_read()?;
        let nodes = txn.open_table(T_NODE)?;
        let node_users = txn.open_table(T_USER_NODE)?;
        let i = nodes
            .iter()?
            .map(|a| {
                let (x, y) = a.unwrap();
                let (x, y) = (x.value().to_owned(), Arc::new(y.value().0));
                let z = node_users
                    .get(&(username, x))
                    .unwrap()
                    .map(|z| z.value().0)
                    .unwrap_or_default();
                (y, z)
            })
            .collect::<Vec<_>>();
        drop(nodes);
        Ok(i)
    }
    pub fn search(&self, query: &str, limit: usize, offset: usize) -> Result<(usize, Vec<NodeID>)> {
        let query = QueryParser::for_index(
            &self.text_search.index,
            vec![self.text_search.title, self.text_search.description],
        )
        .parse_query(query)
        .context("parsing query")?;

        let searcher = self.text_search.reader.searcher();
        let sres = searcher.search(&query, &TopDocs::with_limit(limit).and_offset(offset))?;
        let scount = searcher.search(&query, &Count)?;

        let mut results = Vec::new();
        for (_, daddr) in sres {
            let doc: TantivyDocument = searcher.doc(daddr)?;
            let id = doc
                .get_first(self.text_search.id)
                .unwrap()
                .as_bytes()
                .unwrap();
            let id = NodeID(id.try_into().unwrap());
            results.push(id);
        }
        Ok((scount, results))
    }

    pub fn search_create_index(&self) -> Result<()> {
        let mut w = self.text_search.writer.write().unwrap();
        w.delete_all_documents()?;

        let txn = self.inner.begin_read()?;
        let nodes = txn.open_table(T_NODE)?;
        for node in nodes.iter()? {
            let (x, y) = node?;
            let (id, node) = (x.value().to_owned(), y.value().0);

            w.add_document(doc!(
                self.text_search.id => id.to_vec(),
                self.text_search.title => node.title.unwrap_or_default(),
                self.text_search.description => node.description.unwrap_or_default(),
                self.text_search.releasedate => DateTime::from_timestamp_millis(node.release_date.unwrap_or_default()),
                self.text_search.f_index => node.index.unwrap_or_default() as u64,
            ))?;
        }

        w.commit()?;
        Ok(())
    }

    pub fn create_admin_user(&self, username: &str, password_hash: Vec<u8>) -> Result<()> {
        let txn = self.inner.begin_write().unwrap();
        let mut users = txn.open_table(T_USER).unwrap();

        let admin = users.get(username).unwrap().map(|x| x.value().0);
        users
            .insert(
                username,
                Ser(User {
                    admin: true,
                    name: username.to_owned(),
                    password: password_hash,
                    ..admin.unwrap_or_else(|| User {
                        display_name: "Admin".to_string(),
                        ..Default::default()
                    })
                }),
            )
            .unwrap();

        drop(users);
        txn.commit().unwrap();
        Ok(())
    }
    pub fn get_import_file_mtime(&self, path: &Path) -> Result<Option<u64>> {
        let bytes = path.as_os_str().as_encoded_bytes();
        let txn = self.inner.begin_read()?;
        let table = txn.open_table(T_IMPORT_FILE_MTIME)?;
        if let Some(v) = table.get(bytes)? {
            Ok(Some(v.value()))
        } else {
            Ok(None)
        }
    }
    pub fn set_import_file_mtime(&self, path: &Path, mtime: u64) -> Result<()> {
        let bytes = path.as_os_str().as_encoded_bytes();
        let txn = self.inner.begin_write()?;
        let mut table = txn.open_table(T_IMPORT_FILE_MTIME)?;
        table.insert(bytes, mtime)?;
        drop(table);
        txn.commit()?;
        Ok(())
    }
}

pub struct NodeTextSearchIndex {
    pub schema: Schema,
    pub reader: IndexReader,
    pub writer: RwLock<IndexWriter>,
    pub index: Index,
    pub id: Field,
    pub title: Field,
    pub releasedate: Field,
    pub description: Field,
    pub parent: Field,
    pub f_index: Field,
}
impl NodeTextSearchIndex {
    fn new(path: &Path) -> anyhow::Result<Self> {
        let mut schema = Schema::builder();
        let id = schema.add_text_field("id", TEXT | STORED | FAST);
        let title = schema.add_text_field("title", TEXT);
        let description = schema.add_text_field("description", TEXT);
        let parent = schema.add_text_field("parent", STRING | FAST);
        let f_index = schema.add_u64_field("index", FAST);
        let releasedate = schema.add_date_field(
            "releasedate",
            DateOptions::from(INDEXED)
                .set_fast()
                .set_precision(tantivy::DateTimePrecision::Seconds),
        );
        let schema = schema.build();
        create_dir_all(path.join("node_index"))?;
        let directory =
            MmapDirectory::open(path.join("node_index")).context("opening index directory")?;
        let index = Index::open_or_create(directory, schema.clone()).context("creating index")?;
        let reader = index
            .reader_builder()
            .reload_policy(ReloadPolicy::OnCommitWithDelay)
            .try_into()
            .context("creating reader")?;
        let writer = index.writer(30_000_000).context("creating writer")?;
        Ok(Self {
            index,
            writer: writer.into(),
            reader,
            schema,
            parent,
            f_index,
            releasedate,
            id,
            description,
            title,
        })
    }
}

pub struct HashWriter(DefaultHasher);
impl bincode::enc::write::Writer for HashWriter {
    fn write(&mut self, bytes: &[u8]) -> std::result::Result<(), bincode::error::EncodeError> {
        self.0.write(bytes);
        Ok(())
    }
}

#[derive(Debug)]
#[cfg(not(feature = "db_json"))]
pub struct Ser<T>(pub T);
#[cfg(not(feature = "db_json"))]
impl<T: Encode + Decode + std::fmt::Debug> redb::Value for Ser<T> {
    type SelfType<'a>
        = Ser<T>
    where
        Self: 'a;
    type AsBytes<'a>
        = Vec<u8>
    where
        Self: 'a;

    fn fixed_width() -> Option<usize> {
        None
    }

    fn from_bytes<'a>(data: &'a [u8]) -> Self::SelfType<'a>
    where
        Self: 'a,
    {
        Ser(bincode::decode_from_slice(data, bincode::config::legacy())
            .unwrap()
            .0)
    }

    fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a>
    where
        Self: 'a,
        Self: 'b,
    {
        bincode::encode_to_vec(&value.0, bincode::config::legacy()).unwrap()
    }

    fn type_name() -> redb::TypeName {
        redb::TypeName::new("bincode")
    }
}

#[derive(Debug)]
#[cfg(feature = "db_json")]
pub struct Ser<T>(pub T);
#[cfg(feature = "db_json")]
impl<T: Serialize + for<'a> Deserialize<'a> + std::fmt::Debug> redb::Value for Ser<T> {
    type SelfType<'a>
        = Ser<T>
    where
        Self: 'a;
    type AsBytes<'a>
        = Vec<u8>
    where
        Self: 'a;

    fn fixed_width() -> Option<usize> {
        None
    }

    fn from_bytes<'a>(data: &'a [u8]) -> Self::SelfType<'a>
    where
        Self: 'a,
    {
        Ser(serde_json::from_slice(data).unwrap())
    }

    fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a>
    where
        Self: 'a,
        Self: 'b,
    {
        serde_json::to_vec(&value.0).unwrap()
    }

    fn type_name() -> redb::TypeName {
        redb::TypeName::new("json")
    }
}