use core::{
future::Future,
num::NonZeroUsize,
pin::pin,
task::{Context, Poll, Waker},
};
use super::{
AsyncFulltextRead, AsyncFulltextWrite, ColumnReader, Connected, FulltextEngine, FulltextRead,
FulltextStore, FulltextSubset, FulltextWrite, Hits, TantivyAddress, TantivyDoc, TantivyFulltext,
TantivyStore,
};
fn block_on<F: Future>(future: F) -> F::Output {
let mut future = pin!(future);
let mut cx = Context::from_waker(Waker::noop());
loop {
if let Poll::Ready(value) = future.as_mut().poll(&mut cx) {
return value;
}
}
}
const HEAP: usize = TantivyStore::MIN_WRITER_HEAP_BYTES;
const ROOMY: NonZeroUsize = NonZeroUsize::new(10).expect("10 is not zero");
const SLOT: &str = "face_captions";
const SOURCE: &str = "captions";
fn open_store() -> (tempfile::TempDir, TantivyStore) {
let dir = tempfile::tempdir().expect("a temporary directory");
let store = TantivyStore::open(dir.path()).expect("the store opens");
(dir, store)
}
fn entry(id: i64, body: &str) -> TantivyDoc {
TantivyDoc::new(SOURCE)
.with_identity(&id)
.with_address(&id)
.with_body(body)
}
fn commit_through_the_blocking_face(
store: &TantivyStore,
face: &TantivyFulltext,
entries: Vec<TantivyDoc>,
) {
{
let mut writer = store.writer(SLOT, HEAP).expect("the slot's writer opens");
for doc in entries {
FulltextWrite::upsert(face, Connected::new(store, &mut writer), doc)
.expect("the document queues");
}
writer.commit().expect("the batch commits");
}
store.reload(SLOT).expect("the slot's reader reloads");
}
fn live_documents(store: &TantivyStore) -> u64 {
store
.slot_index(SLOT, false)
.expect("the slot opens")
.reader
.searcher()
.num_docs()
}
fn addressed(hits: &Hits<TantivyAddress, f32>) -> Vec<i64> {
hits
.iter()
.map(|hit| {
let mut read = TantivyFulltext::open(hit.data_ref().clone());
let id = read.i64().expect("the stored address decodes");
read.finish().expect("the key took every column");
id
})
.collect()
}
#[test]
fn a_document_written_through_the_blocking_face_is_found_by_it() {
let (_dir, store) = open_store();
let face = store.fulltext(SLOT);
commit_through_the_blocking_face(
&store,
&face,
vec![
entry(1, "the quick brown fox"),
entry(2, "a heron over still water"),
],
);
let hits = FulltextRead::search(
&face,
Connected::new(&store, ()),
FulltextSubset::All,
"fox",
ROOMY,
None,
)
.expect("the search runs");
assert_eq!(addressed(&hits), vec![1]);
assert!(hits[0].score() > 0.0, "{hits:?}");
}
#[test]
fn the_blocking_write_face_replaces_rather_than_appends() {
let (_dir, store) = open_store();
let face = store.fulltext(SLOT);
commit_through_the_blocking_face(&store, &face, vec![entry(1, "a red car")]);
commit_through_the_blocking_face(&store, &face, vec![entry(1, "a blue car")]);
assert_eq!(live_documents(&store), 1);
let hits = FulltextRead::search(
&face,
Connected::new(&store, ()),
FulltextSubset::All,
"blue",
ROOMY,
None,
)
.expect("the search runs");
assert_eq!(addressed(&hits), vec![1]);
}
#[test]
fn both_read_faces_answer_one_question_alike() {
let (_dir, store) = open_store();
let face = store.fulltext(SLOT);
commit_through_the_blocking_face(
&store,
&face,
vec![
entry(1, "a red car on a red road"),
entry(2, "a red car"),
entry(3, "a heron over still water"),
],
);
let blocking = FulltextRead::search(
&face,
Connected::new(&store, ()),
FulltextSubset::All,
"red car",
ROOMY,
None,
)
.expect("the blocking search runs");
let asynchronous = block_on(AsyncFulltextRead::search(
&face,
Connected::new(&store, ()),
FulltextSubset::All,
"red car",
ROOMY,
None,
))
.expect("the async search runs");
let mut matched = addressed(&blocking);
matched.sort_unstable();
assert_eq!(matched, vec![1, 2]);
assert_eq!(blocking, asynchronous);
}
#[test]
fn both_write_faces_queue_one_entry_alike() {
let (_dir, store) = open_store();
let face = store.fulltext(SLOT);
commit_through_the_blocking_face(&store, &face, vec![entry(7, "a red car")]);
{
let mut writer = store.writer(SLOT, HEAP).expect("the slot's writer opens");
block_on(AsyncFulltextWrite::upsert(
&face,
Connected::new(&store, &mut writer),
entry(7, "a blue car"),
))
.expect("the document queues");
writer.commit().expect("the batch commits");
}
store.reload(SLOT).expect("the slot's reader reloads");
assert_eq!(live_documents(&store), 1);
let hits = FulltextRead::search(
&face,
Connected::new(&store, ()),
FulltextSubset::All,
"blue",
ROOMY,
None,
)
.expect("the search runs");
assert_eq!(addressed(&hits), vec![7]);
let stale = FulltextRead::search(
&face,
Connected::new(&store, ()),
FulltextSubset::All,
"red",
ROOMY,
None,
)
.expect("the search runs");
assert!(stale.is_empty(), "{stale:?}");
}