diff options
Diffstat (limited to 'base/src/database.rs')
-rw-r--r-- | base/src/database.rs | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/base/src/database.rs b/base/src/database.rs index f1ae595..2fabf1c 100644 --- a/base/src/database.rs +++ b/base/src/database.rs @@ -3,6 +3,7 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2024 metamuffin <metamuffin.org> */ +use anyhow::Context; use bincode::{Decode, Encode}; use jellycommon::{ user::{NodeUserData, User}, @@ -10,13 +11,15 @@ use jellycommon::{ }; use log::info; use serde::{Deserialize, Serialize}; -use std::{borrow::Borrow, fs::create_dir_all, ops::Deref, path::Path}; +use std::{borrow::Borrow, fs::create_dir_all, ops::Deref, path::Path, sync::RwLock}; use tantivy::{ - schema::{Field, Schema, FAST, STORED, TEXT}, - Index, IndexReader, ReloadPolicy, + directory::MmapDirectory, + schema::{Field, Schema, FAST, INDEXED, STORED, TEXT}, + DateOptions, Index, IndexReader, IndexWriter, ReloadPolicy, }; pub use redb::*; +pub use tantivy; pub const T_USER: TableDefinition<&str, Ser<User>> = TableDefinition::new("user"); pub const T_USER_NODE: TableDefinition<(&str, &str), Ser<NodeUserData>> = @@ -30,16 +33,19 @@ pub const T_NODE_IMPORT: TableDefinition<&str, Ser<Vec<(Vec<usize>, Node)>>> = pub struct DataAcid { pub inner: redb::Database, - pub ft_node: NodeFulltextIndex, + pub node_index: NodeFulltextIndex, } impl DataAcid { pub fn open(path: &Path) -> Result<Self, anyhow::Error> { info!("database"); + create_dir_all(path)?; let db = redb::Database::create(path.join("data"))?; - let ft_node = NodeFulltextIndex::new(path)?; - let r = Self { inner: db, ft_node }; + let r = Self { + inner: db, + node_index: ft_node, + }; { // this creates all tables such that read operations on them do not fail. @@ -68,10 +74,13 @@ impl Deref for DataAcid { pub struct NodeFulltextIndex { 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 index: Index, + pub f_index: Field, } impl NodeFulltextIndex { fn new(path: &Path) -> anyhow::Result<Self> { @@ -79,17 +88,31 @@ impl NodeFulltextIndex { 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 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_fts_index"))?; - let index = Index::create_in_dir(path.join("node_fts_index"), schema.clone())?; + 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()?; + .try_into() + .context("creating reader")?; + let writer = index.writer(30_000_000).context("creating writer")?; Ok(Self { index, + writer: writer.into(), reader, schema, + f_index, + releasedate, id, description, title, |