use crate::Resources;
use std::path::PathBuf;
use smol_bytes::Utf8Bytes;
use super::{TantivyFulltext, TantivyFulltextError, TantivySlotMaker, TantivyStore};
use crate::{
AsyncResource, Describe, FulltextMount, FulltextStore, Habitat, Shared, SlotColumn, SlotName,
SlotSchema, TypeVisitor,
};
struct Ft {
store: TantivyStore,
}
impl AsyncResource for Ft {
type Options = PathBuf;
type Error = TantivyFulltextError;
async fn build(_ctx: &Resources, options: Self::Options) -> Result<Self, Self::Error> {
<TantivyStore as FulltextStore>::open(options).map(|store| Self { store })
}
}
impl Habitat for Ft {
type Blueprint = &'static [SlotSchema];
}
impl FulltextMount for Ft {
type Handle = TantivyFulltext;
fn corpus(&self, slot: impl Into<Utf8Bytes>) -> Self::Handle {
<TantivyStore as FulltextStore>::fulltext(&self.store, slot)
}
fn connection(&self) -> &TantivyStore {
&self.store
}
}
struct Media;
impl Describe<Ft> for Media {
const BLUEPRINT: &'static [SlotSchema] = &[
SlotSchema::new(
SlotName::new("media__title__lexical"),
&[SlotColumn::text("title")],
),
SlotSchema::new(
SlotName::new("media__body__lexical"),
&[SlotColumn::text("body")],
)
.keeping(),
];
}
struct Caption;
impl Describe<Ft> for Caption {
const BLUEPRINT: &'static [SlotSchema] = &[SlotSchema::new(
SlotName::new("caption__text__lexical"),
&[SlotColumn::text("text")],
)];
}
async fn mounted(root: PathBuf) -> Shared<Ft> {
Shared::<Ft>::build(&Resources::new(), root)
.await
.expect("the mount opens")
}
#[tokio::test]
async fn the_walk_creates_every_corpus_its_blueprints_name() {
let home = tempfile::tempdir().expect("a temporary directory");
let ft = mounted(home.path().to_path_buf()).await;
let mut maker = TantivySlotMaker::new(ft);
maker.next_type::<Media>().expect("the corpora are made");
maker.next_type::<Caption>().expect("the corpus is made");
assert_eq!(
maker.made(),
[
"media__title__lexical",
"media__body__lexical",
"caption__text__lexical"
],
"the walk creates in blueprint order, declaration by declaration",
);
for slot in maker.made() {
assert!(
home.path().join(slot.as_str()).is_dir(),
"`{slot}` has no directory, so nothing was created",
);
}
}
#[tokio::test]
async fn a_kept_corpus_is_created_keeping_its_text() {
let home = tempfile::tempdir().expect("a temporary directory");
let ft = mounted(home.path().to_path_buf()).await;
let store: &TantivyStore = FulltextMount::connection(ft.get());
let mut maker = TantivySlotMaker::new(ft.clone());
maker.next_type::<Media>().expect("the corpora are made");
store
.writer_with(
"media__body__lexical",
TantivyStore::MIN_WRITER_HEAP_BYTES,
true,
)
.expect("the kept corpus reopens at the posture the walk created it under");
store
.writer_with(
"media__title__lexical",
TantivyStore::MIN_WRITER_HEAP_BYTES,
false,
)
.expect("the indexed corpus reopens at its own posture");
}
#[tokio::test]
async fn a_second_walk_over_a_live_root_refuses_nothing() {
let home = tempfile::tempdir().expect("a temporary directory");
{
let ft = mounted(home.path().to_path_buf()).await;
let mut first = TantivySlotMaker::new(ft);
first.next_type::<Media>().expect("the corpora are made");
}
let ft = mounted(home.path().to_path_buf()).await;
let mut second = TantivySlotMaker::new(ft);
second
.next_type::<Media>()
.expect("a second boot over a live root opens what is already there");
assert_eq!(second.made().len(), 2);
}
#[tokio::test]
async fn the_knob_table_is_the_options_seats_projection() {
let home = tempfile::tempdir().expect("a temporary directory");
let ft = mounted(home.path().to_path_buf()).await;
let knobs: <TantivyFulltext as crate::FulltextEngine>::Options =
crate::store::tantivy::LaneAnalyzers::new();
let mut maker = TantivySlotMaker::new(ft).with_knobs("media__title__lexical", knobs);
maker.next_type::<Media>().expect("the corpora are made");
assert_eq!(maker.made().len(), 2, "both corpora are still made");
}