diff options
author | metamuffin <metamuffin@disroot.org> | 2024-04-15 12:55:19 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-04-15 12:55:19 +0200 |
commit | c5bb4949eb0959ec3a3c2fa010d2d7549347e587 (patch) | |
tree | 3085882ba5cee5d3578617afc481364beb67c07b /base/src/database.rs | |
parent | 0acf32113051addd1aba4a6f823b9c918d839e04 (diff) | |
download | jellything-c5bb4949eb0959ec3a3c2fa010d2d7549347e587.tar jellything-c5bb4949eb0959ec3a3c2fa010d2d7549347e587.tar.bz2 jellything-c5bb4949eb0959ec3a3c2fa010d2d7549347e587.tar.zst |
fulltext search pt.1
Diffstat (limited to 'base/src/database.rs')
-rw-r--r-- | base/src/database.rs | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/base/src/database.rs b/base/src/database.rs index ca9360f..f1ae595 100644 --- a/base/src/database.rs +++ b/base/src/database.rs @@ -10,7 +10,11 @@ use jellycommon::{ }; use log::info; use serde::{Deserialize, Serialize}; -use std::{borrow::Borrow, ops::Deref, path::Path}; +use std::{borrow::Borrow, fs::create_dir_all, ops::Deref, path::Path}; +use tantivy::{ + schema::{Field, Schema, FAST, STORED, TEXT}, + Index, IndexReader, ReloadPolicy, +}; pub use redb::*; @@ -26,13 +30,16 @@ pub const T_NODE_IMPORT: TableDefinition<&str, Ser<Vec<(Vec<usize>, Node)>>> = pub struct DataAcid { pub inner: redb::Database, + pub ft_node: NodeFulltextIndex, } impl DataAcid { pub fn open(path: &Path) -> Result<Self, anyhow::Error> { info!("database"); - let db = redb::Database::create(path)?; - let r = Self { inner: db }; + let db = redb::Database::create(path.join("data"))?; + + let ft_node = NodeFulltextIndex::new(path)?; + let r = Self { inner: db, ft_node }; { // this creates all tables such that read operations on them do not fail. @@ -58,6 +65,38 @@ impl Deref for DataAcid { } } +pub struct NodeFulltextIndex { + pub schema: Schema, + pub reader: IndexReader, + pub id: Field, + pub title: Field, + pub description: Field, + pub index: Index, +} +impl NodeFulltextIndex { + 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 schema = schema.build(); + create_dir_all(path.join("node_fts_index"))?; + let index = Index::create_in_dir(path.join("node_fts_index"), schema.clone())?; + let reader = index + .reader_builder() + .reload_policy(ReloadPolicy::OnCommitWithDelay) + .try_into()?; + Ok(Self { + index, + reader, + schema, + id, + description, + title, + }) + } +} + pub trait TableExt<Key, KeyRef, Value> { fn get(self, db: &DataAcid, key: KeyRef) -> anyhow::Result<Option<Value>>; fn insert(self, db: &DataAcid, key: KeyRef, value: Value) -> anyhow::Result<()>; |