use core::{
num::NonZeroUsize,
pin::pin,
task::{Context, Poll, Waker},
};
use tantivy::{Term, collector::Count, query::TermQuery, schema::IndexRecordOption};
use super::{
AsyncFulltextRead, AsyncFulltextWrite, ColumnReader, ColumnWriter, Connected, Field,
FulltextEngine, FulltextStore, FulltextSubset, Hits, KeyDecodeError, Scored, TantivyDoc,
TantivyFulltext, TantivyFulltextError, TantivyStore,
};
use crate::{FulltextRecord, FulltextTable, Identifiable, SlotName, Transformer, retrieve::keyed};
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;
mod ceiling;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Frame(i64);
impl Field for Frame {
fn write(&self, w: &mut impl ColumnWriter) {
w.i64(self.0);
}
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
r.i64().map(Self)
}
}
struct Caption {
media: String,
frame: Frame,
text: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct CaptionKey {
media: String,
frame: Frame,
}
impl Field for CaptionKey {
fn write(&self, w: &mut impl ColumnWriter) {
self.media.write(w);
self.frame.write(w);
}
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
Ok(Self {
media: String::read(r)?,
frame: Frame::read(r)?,
})
}
}
impl Identifiable for Caption {
type Id = Frame;
fn id_ref(&self) -> &Frame {
&self.frame
}
}
impl Transformer for Caption {
type Key = CaptionKey;
type Lineage = ();
type Record = Self;
type Graphql = ();
fn key(&self) -> CaptionKey {
CaptionKey {
media: self.media.clone(),
frame: self.frame,
}
}
}
impl FulltextRecord for Caption {
type Document = String;
fn document(&self) -> &String {
&self.text
}
}
impl FulltextTable for Caption {
const SLOT: &'static SlotName = SlotName::new("captions");
}
struct Alias {
media: String,
frame: Frame,
text: String,
}
impl Identifiable for Alias {
type Id = Frame;
fn id_ref(&self) -> &Frame {
&self.frame
}
}
impl Transformer for Alias {
type Key = CaptionKey;
type Lineage = ();
type Record = Self;
type Graphql = ();
fn key(&self) -> CaptionKey {
CaptionKey {
media: self.media.clone(),
frame: self.frame,
}
}
}
impl FulltextRecord for Alias {
type Document = String;
fn document(&self) -> &String {
&self.text
}
}
impl FulltextTable for Alias {
const SLOT: &'static SlotName = SlotName::new("aliases");
}
fn caption(media: &str, frame: i64, text: &str) -> Caption {
Caption {
media: media.to_owned(),
frame: Frame(frame),
text: text.to_owned(),
}
}
fn alias(media: &str, frame: i64, text: &str) -> Alias {
Alias {
media: media.to_owned(),
frame: Frame(frame),
text: text.to_owned(),
}
}
fn caption_key(media: &str, frame: i64) -> CaptionKey {
CaptionKey {
media: media.to_owned(),
frame: Frame(frame),
}
}
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<R>(record: &R) -> TantivyDoc
where
R: FulltextTable + FulltextRecord<Document: AsRef<str>>,
R::Id: Field,
R::Key: Field,
{
TantivyDoc::new(R::SLOT.as_ref())
.with_identity(record.id_ref())
.with_address(&record.key())
.with_body(record.document().as_ref())
}
fn commit<R>(store: &TantivyStore, face: &TantivyFulltext, records: &[R])
where
R: FulltextTable + FulltextRecord<Document: AsRef<str>>,
R::Id: Field,
R::Key: Field,
{
{
let mut writer = store
.writer(face.slot(), HEAP)
.expect("the slot's writer opens");
for record in records {
block_on(face.upsert(Connected::new(store, &mut writer), entry(record)))
.expect("the document queues");
}
writer.commit().expect("the batch commits");
}
store
.reload(face.slot())
.expect("the slot's reader reloads");
}
fn search(
store: &TantivyStore,
face: &TantivyFulltext,
query: &str,
) -> Result<Hits<CaptionKey, f32>, TantivyFulltextError> {
let hits = block_on(face.search(
Connected::new(store, ()),
FulltextSubset::All,
query,
ROOMY,
None,
))?;
keyed::<TantivyFulltext, CaptionKey>(hits)
}
const ROOMY: NonZeroUsize = NonZeroUsize::new(10).expect("10 is not zero");
fn live_documents(store: &TantivyStore, slot: &str) -> u64 {
store
.slot_index(slot, false)
.expect("the slot opens")
.reader
.searcher()
.num_docs()
}
#[test]
fn a_document_indexed_comes_back_as_its_key() {
let (_dir, store) = open_store();
let captions = store.fulltext(Caption::SLOT.as_ref());
commit(
&store,
&captions,
&[
caption("m1", 1, "the quick brown fox"),
caption("m2", 2, "a heron over still water"),
],
);
let hits = search(&store, &captions, "fox").expect("the search runs");
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].data_ref(), &caption_key("m1", 1));
assert!(hits[0].score() > 0.0, "{hits:?}");
let hits = search(&store, &captions, "heron").expect("the search runs");
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].data_ref().media, "m2");
assert_eq!(hits[0].data_ref().frame, Frame(2));
}
#[test]
fn a_query_matching_nothing_is_an_empty_result() {
let (_dir, store) = open_store();
let captions = store.fulltext(Caption::SLOT.as_ref());
commit(
&store,
&captions,
&[caption("m1", 1, "the quick brown fox")],
);
assert_eq!(
search(&store, &captions, "heron").expect("the search runs"),
[]
);
}
#[test]
fn a_keyed_replace_leaves_exactly_one_document() {
let (_dir, store) = open_store();
let captions = store.fulltext(Caption::SLOT.as_ref());
commit(&store, &captions, &[caption("m1", 1, "alpha")]);
assert_eq!(live_documents(&store, captions.slot()), 1);
commit(&store, &captions, &[caption("m1", 1, "beta")]);
assert_eq!(
live_documents(&store, captions.slot()),
1,
"a replace must not leave the old document beside the new one"
);
assert_eq!(
search(&store, &captions, "alpha").expect("the search runs"),
[]
);
let hits = search(&store, &captions, "beta").expect("the search runs");
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].data_ref(), &caption_key("m1", 1));
}
#[test]
fn distinct_ids_are_distinct_entries() {
let (_dir, store) = open_store();
let captions = store.fulltext(Caption::SLOT.as_ref());
commit(
&store,
&captions,
&[caption("m1", 1, "alpha"), caption("m1", 2, "alpha")],
);
assert_eq!(live_documents(&store, captions.slot()), 2);
let mut hits = search(&store, &captions, "alpha").expect("the search runs");
hits.sort_by_key(|hit| hit.data_ref().frame.0);
assert_eq!(
hits
.iter()
.map(|hit| hit.data_ref().frame.0)
.collect::<Vec<_>>(),
[1, 2]
);
}
#[test]
fn one_writer_carries_a_whole_batch() {
let (_dir, store) = open_store();
let captions = store.fulltext(Caption::SLOT.as_ref());
let mut writer = store
.writer(captions.slot(), HEAP)
.expect("the slot's writer opens");
for record in [caption("m1", 1, "alpha"), caption("m2", 2, "beta")] {
block_on(captions.upsert(Connected::new(&store, &mut writer), entry(&record)))
.expect("the document queues");
}
writer.commit().expect("the batch commits");
drop(writer);
store
.reload(captions.slot())
.expect("the slot's reader reloads");
assert_eq!(live_documents(&store, captions.slot()), 2);
}
#[test]
fn a_search_sees_committed_and_reloaded_writes_only() {
let (_dir, store) = open_store();
let captions = store.fulltext(Caption::SLOT.as_ref());
let mut writer = store
.writer(captions.slot(), HEAP)
.expect("the slot's writer opens");
block_on(captions.upsert(
Connected::new(&store, &mut writer),
entry(&caption("m1", 1, "alpha")),
))
.expect("the document queues");
assert_eq!(
search(&store, &captions, "alpha").expect("the search runs"),
[],
"a queued document is not a visible one"
);
writer.commit().expect("the batch commits");
assert_eq!(
search(&store, &captions, "alpha").expect("the search runs"),
[],
"a committed document is not visible until the reader is reloaded"
);
drop(writer);
store
.reload(captions.slot())
.expect("the slot's reader reloads");
assert_eq!(
search(&store, &captions, "alpha")
.expect("the search runs")
.len(),
1
);
}
#[test]
fn dropping_a_writer_frees_the_slot() {
let (_dir, store) = open_store();
let captions = store.fulltext(Caption::SLOT.as_ref());
drop(
store
.writer(captions.slot(), HEAP)
.expect("the slot's writer opens"),
);
drop(
store
.writer(captions.slot(), HEAP)
.expect("the slot's writer opens a second time"),
);
}
#[test]
fn a_rollback_discards_the_batch_and_keeps_the_writer() {
let (_dir, store) = open_store();
let captions = store.fulltext(Caption::SLOT.as_ref());
let mut writer = store
.writer(captions.slot(), HEAP)
.expect("the slot's writer opens");
block_on(captions.upsert(
Connected::new(&store, &mut writer),
entry(&caption("m1", 1, "alpha")),
))
.expect("the document queues");
writer.rollback().expect("the batch rolls back");
block_on(captions.upsert(
Connected::new(&store, &mut writer),
entry(&caption("m2", 2, "beta")),
))
.expect("the writer is still usable");
writer.commit().expect("the batch commits");
drop(writer);
store
.reload(captions.slot())
.expect("the slot's reader reloads");
assert_eq!(live_documents(&store, captions.slot()), 1);
assert_eq!(
search(&store, &captions, "alpha").expect("the search runs"),
[]
);
assert_eq!(
search(&store, &captions, "beta")
.expect("the search runs")
.len(),
1
);
}
#[test]
fn a_reopened_store_reads_the_previous_one_s_writes() {
let dir = tempfile::tempdir().expect("a temporary directory");
{
let store = TantivyStore::open(dir.path()).expect("the store opens");
let captions = store.fulltext(Caption::SLOT.as_ref());
commit(&store, &captions, &[caption("m1", 1, "alpha")]);
}
let store = TantivyStore::open(dir.path()).expect("the store reopens");
let captions = store.fulltext(Caption::SLOT.as_ref());
assert_eq!(
search(&store, &captions, "alpha")
.expect("the search runs")
.len(),
1
);
}
fn documents_with_source(store: &TantivyStore, slot: &str, source: &str) -> usize {
let opened = store.slot_index(slot, false).expect("the slot opens");
let query = TermQuery::new(
Term::from_field_text(opened.fields.source, source),
IndexRecordOption::Basic,
);
opened
.reader
.searcher()
.search(&query, &Count)
.expect("the term query runs")
}
fn tokens(spec: &[(&str, usize)]) -> String {
spec
.iter()
.flat_map(|&(token, count)| core::iter::repeat_n(token, count))
.collect::<Vec<_>>()
.join(" ")
}
fn ranking(hits: &[Scored<CaptionKey, f32>]) -> Vec<&str> {
hits
.iter()
.map(|hit| hit.data_ref().media.as_str())
.collect()
}
#[test]
fn a_sibling_slot_cannot_move_this_slot_s_bm25_ranking() {
let (_dir, store) = open_store();
let captions = store.fulltext(Caption::SLOT.as_ref());
commit(
&store,
&captions,
&[
caption("short", 1, "fox alpha beta gamma"),
caption("long", 2, &tokens(&[("fox", 3), ("delta", 37)])),
],
);
let before = search(&store, &captions, "fox").expect("the search runs");
assert_eq!(
ranking(&before),
["short", "long"],
"the short document outranks the long one: {before:?}"
);
let transcripts = store.fulltext("transcripts");
commit(
&store,
&transcripts,
&[caption("big", 3, &tokens(&[("chatter", 200)]))],
);
store
.reload(captions.slot())
.expect("the slot's reader reloads");
let after = search(&store, &captions, "fox").expect("the search runs");
assert_eq!(
ranking(&after),
["short", "long"],
"a sibling slot must not reorder this slot: {after:?}"
);
for (before, after) in before.iter().zip(&after) {
assert!(
(before.score() - after.score()).abs() < 1e-6,
"a sibling slot must not move this slot's scores: {before:?} vs {after:?}"
);
}
}
#[test]
fn the_source_word_is_a_native_term() {
let (_dir, store) = open_store();
let elsewhere = store.fulltext("elsewhere");
commit(&store, &elsewhere, &[caption("m1", 1, "alpha")]);
assert_eq!(documents_with_source(&store, "elsewhere", "captions"), 1);
assert_eq!(documents_with_source(&store, "elsewhere", "elsewhere"), 0);
assert_eq!(documents_with_source(&store, "elsewhere", "aliases"), 0);
}
#[test]
fn two_sources_sharing_a_slot_do_not_delete_each_other() {
let (_dir, store) = open_store();
let shared = store.fulltext("shared");
commit(&store, &shared, &[caption("m1", 1, "alpha")]);
commit(&store, &shared, &[alias("m1", 1, "beta")]);
assert_eq!(
live_documents(&store, "shared"),
2,
"an identity framed over (source, id) must not collide across sources"
);
assert_eq!(documents_with_source(&store, "shared", "captions"), 1);
assert_eq!(documents_with_source(&store, "shared", "aliases"), 1);
commit(&store, &shared, &[caption("m1", 1, "gamma")]);
assert_eq!(live_documents(&store, "shared"), 2);
assert_eq!(
search(&store, &shared, "alpha").expect("the search runs"),
[]
);
assert_eq!(
search(&store, &shared, "beta")
.expect("the search runs")
.len(),
1
);
}
fn sealed(columns: impl FnOnce(&mut <TantivyFulltext as FulltextEngine>::WriteBuffer)) -> Vec<u8> {
let mut buf = TantivyFulltext::buffer();
columns(&mut buf);
TantivyFulltext::finalize(buf).into_bytes()
}
fn opened(bytes: Vec<u8>) -> <TantivyFulltext as FulltextEngine>::ReadBuffer {
TantivyFulltext::open(bytes.into())
}
#[test]
fn the_address_is_injective_for_the_concatenation_colliding_pair() {
let ab_c = sealed(|w| {
w.text("ab");
w.text("c");
});
let a_bc = sealed(|w| {
w.text("a");
w.text("bc");
});
assert_ne!(ab_c, a_bc);
let mut read = opened(ab_c);
assert_eq!(read.text().expect("the first column"), "ab");
assert_eq!(read.text().expect("the second column"), "c");
read.finish().expect("the key took every column");
}
#[test]
fn every_typed_column_round_trips() {
let uuid = uuid::Uuid::from_bytes([7; 16]);
let mut read = opened(sealed(|w| {
w.bytes(&[1, 2, 3]);
w.text("a red car");
w.i64(-42);
w.uuid(uuid);
}));
assert_eq!(read.bytes().expect("the raw column"), [1, 2, 3]);
assert_eq!(read.text().expect("the text column"), "a red car");
assert_eq!(read.i64().expect("the integer column"), -42);
assert_eq!(read.uuid().expect("the uuid column"), uuid);
read.finish().expect("the key took every column");
}
#[test]
fn the_read_names_a_malformed_address() {
assert!(matches!(
opened(vec![0, 0, 0, 8, 1, 2]).bytes(),
Err(TantivyFulltextError::Key(KeyDecodeError::Truncated))
));
assert!(matches!(
opened(vec![0, 0]).bytes(),
Err(TantivyFulltextError::Key(KeyDecodeError::Truncated))
));
let invalid = sealed(|w| w.bytes(&[0xff, 0xfe]));
assert!(matches!(
opened(invalid.clone()).text(),
Err(TantivyFulltextError::Key(KeyDecodeError::NotText))
));
assert!(matches!(
opened(invalid.clone()).i64(),
Err(TantivyFulltextError::Key(KeyDecodeError::NotI64))
));
assert!(matches!(
opened(invalid).uuid(),
Err(TantivyFulltextError::Key(KeyDecodeError::NotUuid))
));
let mut read = opened(sealed(|w| {
w.i64(1);
w.i64(2);
}));
assert_eq!(read.i64().expect("the first column"), 1);
assert!(matches!(
read.finish(),
Err(TantivyFulltextError::Key(KeyDecodeError::Trailing))
));
}
#[test]
fn a_query_naming_an_unsearchable_field_is_refused() {
let (_dir, store) = open_store();
let captions = store.fulltext(Caption::SLOT.as_ref());
commit(
&store,
&captions,
&[caption("m1", 1, "the quick brown fox")],
);
for (query, field) in [
("key:AAAA", "key"),
("body:fox AND key:AAAA", "key"),
("(body:fox OR identity:AAAA)", "identity"),
("key:AAAA^2", "key"),
("key:[a TO b]", "key"),
("key:IN [a b]", "key"),
("key:*", "key"),
("source:captions", "source"),
] {
match search(&store, &captions, query) {
Err(TantivyFulltextError::UnsearchableField { field: named }) => {
assert_eq!(named, field, "{query}")
}
other => panic!("{query} must be refused, got {other:?}"),
}
}
assert_eq!(search(&store, &captions, "fox").expect("bare").len(), 1);
assert_eq!(
search(&store, &captions, "body:fox").expect("named").len(),
1
);
}
#[test]
fn a_handle_refuses_another_slot_s_writer() {
let (_dir, store) = open_store();
let captions = store.fulltext(Caption::SLOT.as_ref());
let transcripts = store.fulltext("transcripts");
let mut writer = store
.writer(transcripts.slot(), HEAP)
.expect("the slot's writer opens");
let refused = block_on(captions.upsert(
Connected::new(&store, &mut writer),
entry(&caption("m1", 1, "alpha")),
))
.expect_err("a mispaired writer is refused");
match &refused {
TantivyFulltextError::SlotMismatch(mismatch) => {
assert_eq!(mismatch.handle(), "captions");
assert_eq!(mismatch.writer(), "transcripts");
}
other => panic!("expected a slot mismatch, got {other:?}"),
}
writer.commit().expect("the empty batch commits");
drop(writer);
store
.reload(transcripts.slot())
.expect("the slot's reader reloads");
assert_eq!(
live_documents(&store, transcripts.slot()),
0,
"a refused pairing must queue nothing"
);
}
#[test]
fn a_slot_that_is_not_an_identifier_creates_nothing() {
let dir = tempfile::tempdir().expect("a temporary directory");
let root = dir.path().join("root");
let store = TantivyStore::open(&root).expect("the store opens");
for slot in ["../escape", "with space", "", "a/b", "9leading"] {
assert!(
matches!(
store.writer(slot, HEAP),
Err(TantivyFulltextError::InvalidSlot(_))
),
"{slot:?} must not mint a writer"
);
assert!(
matches!(
store.reload(slot),
Err(TantivyFulltextError::InvalidSlot(_))
),
"{slot:?} must not reload"
);
assert!(
matches!(
search(&store, &store.fulltext(slot), "fox"),
Err(TantivyFulltextError::InvalidSlot(_))
),
"{slot:?} must not search"
);
}
assert!(!dir.path().join("escape").exists());
assert_eq!(
std::fs::read_dir(&root)
.expect("the root is readable")
.count(),
0,
"a refused slot must create no directory"
);
}
#[test]
fn the_limit_is_a_ceiling_on_hits() {
let (_dir, store) = open_store();
let captions = store.fulltext(Caption::SLOT.as_ref());
commit(
&store,
&captions,
&[
caption("m1", 1, "fox fox fox fox"),
caption("m2", 2, "fox alpha beta gamma"),
],
);
let one = keyed::<TantivyFulltext, CaptionKey>(
block_on(captions.search(
Connected::new(&store, ()),
FulltextSubset::All,
"fox",
NonZeroUsize::new(1).expect("1 is not zero"),
None,
))
.expect("the search runs"),
)
.expect("the addresses read back");
assert_eq!(one.len(), 1);
assert_eq!(ranking(&one), ["m1"]);
assert_eq!(
search(&store, &captions, "fox")
.expect("the default limit")
.len(),
2
);
}
mod vocabulary {
extern crate alloc;
use super::{
ColumnReader, Field, FulltextEngine, KeyDecodeError, TantivyFulltext, TantivyFulltextError,
};
fn round_trip<K>(value: K) -> Vec<u8>
where
K: Field + PartialEq + core::fmt::Debug,
{
let mut buf = TantivyFulltext::buffer();
value.write(&mut buf);
let bytes = TantivyFulltext::finalize(buf).into_bytes();
let mut read = TantivyFulltext::open(bytes.clone().into());
assert_eq!(K::read(&mut read).expect("the address reads back"), value);
read
.finish()
.expect("and one member reads exactly one column");
bytes
}
#[test]
fn every_integer_width_round_trips_at_its_own_width() {
for value in [i8::MIN, -1, 0, 1, i8::MAX] {
round_trip(value);
}
round_trip(i128::MIN);
round_trip(u128::MAX);
round_trip(0_u8);
round_trip(u64::MAX);
assert_eq!(round_trip(7_u32).len(), 4 + 4);
assert_eq!(round_trip(7_u64).len(), 4 + 8);
}
#[test]
fn a_nonzero_writes_the_address_its_integer_writes() {
let bare = round_trip(7_u64);
let wrapped = round_trip(core::num::NonZeroU64::new(7).expect("7 is not zero"));
assert_eq!(bare, wrapped);
}
#[test]
fn a_zero_is_not_a_nonzero_and_the_address_says_so() {
let mut buf = TantivyFulltext::buffer();
0_u64.write(&mut buf);
let mut read = TantivyFulltext::open(TantivyFulltext::finalize(buf));
assert!(matches!(
<core::num::NonZeroU64 as Field>::read(&mut read),
Err(TantivyFulltextError::Key(KeyDecodeError::Domain)),
));
}
#[test]
fn an_address_of_another_width_is_not_this_value() {
let mut read = TantivyFulltext::open(round_trip(7_u64).into());
assert!(
matches!(
<u32 as Field>::read(&mut read),
Err(TantivyFulltextError::Key(KeyDecodeError::Width)),
),
"eight bytes are not a `u32`",
);
}
#[test]
fn a_width_outside_an_untagged_enums_set_is_the_engines_own_arm() {
let mut read = TantivyFulltext::open(round_trip(7_u64).into());
assert!(matches!(
<core::net::IpAddr as Field>::read(&mut read),
Err(TantivyFulltextError::Key(KeyDecodeError::Width)),
));
}
#[test]
fn two_value_sequences_never_render_one_address() {
let split = |a: &str, b: &str| {
let mut buf = TantivyFulltext::buffer();
a.to_owned().write(&mut buf);
b.to_owned().write(&mut buf);
TantivyFulltext::finalize(buf).into_bytes()
};
assert_ne!(split("ab", "c"), split("a", "bc"));
}
#[test]
fn the_uuid_member_rides_its_sixteen_bytes() {
assert_eq!(round_trip(uuid::Uuid::from_u128(7)).len(), 4 + 16);
}
#[test]
fn the_network_family_rides_its_octets() {
let v4 = core::net::Ipv4Addr::new(10, 0, 0, 1);
let v6 = core::net::Ipv6Addr::LOCALHOST;
assert_eq!(round_trip(v4).len(), 4 + 4);
assert_eq!(round_trip(v6).len(), 4 + 16);
assert_eq!(round_trip(core::net::IpAddr::V4(v4)).len(), 4 + 4);
assert_eq!(round_trip(core::net::IpAddr::V6(v6)).len(), 4 + 16);
let socket_v4 = core::net::SocketAddrV4::new(v4, 8080);
let socket_v6 = core::net::SocketAddrV6::new(v6, 8080, 3, 5);
assert_eq!(round_trip(socket_v4).len(), 4 + 6);
assert_eq!(round_trip(socket_v6).len(), 4 + 26);
assert_eq!(
round_trip(core::net::SocketAddr::V4(socket_v4)).len(),
4 + 6
);
assert_eq!(
round_trip(core::net::SocketAddr::V6(socket_v6)).len(),
4 + 26
);
assert_ne!(
round_trip(core::net::SocketAddrV6::new(v6, 8080, 3, 5)),
round_trip(core::net::SocketAddrV6::new(v6, 8080, 3, 6)),
"the scope id is identity, not decoration",
);
}
#[test]
fn the_generic_rows_are_transparent_to_what_they_hold() {
struct Scene;
let bare = round_trip(7_u64);
assert_eq!(round_trip(alloc::sync::Arc::new(7_u64)), bare);
assert_eq!(round_trip(alloc::boxed::Box::new(7_u64)), bare);
assert_eq!(round_trip([1_u8, 2, 3, 4]).len(), 4 + 4);
assert_eq!(
round_trip(crate::TypedId::<u64, Scene>::new(7)),
bare,
"storage sees only `I`",
);
}
}
mod layering {
use super::{
Caption, Connected, HEAP, TantivyFulltext, TantivyFulltextError, TantivyStore, block_on,
caption, entry, open_store, search,
};
use crate::{AsyncLands, Commit, FulltextStore, FulltextTable, Lands, SlotHandle, SlotWriter};
struct Captions<'a> {
connection: &'a TantivyStore,
slot: SlotHandle<TantivyFulltext, Caption>,
writer: SlotWriter,
}
impl Captions<'_> {
fn landed(&mut self, items: &[Caption]) -> Commit<TantivyFulltextError> {
let queued = items.iter().try_for_each(|record| {
block_on(self.slot.upsert_document(
Connected::new(self.connection, &mut self.writer),
entry(record),
))
});
let landed = queued
.and_then(|()| self.writer.commit().map(drop))
.inspect_err(|_| {
let _ = self.writer.rollback();
});
match landed {
Ok(()) => Commit::all(),
Err(refusal) => Commit::none(refusal),
}
}
}
impl AsyncLands<Caption> for Captions<'_> {
type Error = TantivyFulltextError;
fn land(&mut self, items: &[Caption]) -> impl Future<Output = Commit<Self::Error>> + Send {
core::future::ready(self.landed(items))
}
}
impl Lands<Caption> for Captions<'_> {
type Error = TantivyFulltextError;
fn land(&mut self, items: &[Caption]) -> Commit<Self::Error> {
self.landed(items)
}
}
#[test]
fn the_socket_writes_through_the_engine() {
let (_dir, store) = open_store();
let engine = FulltextStore::fulltext(&store, Caption::SLOT.as_ref());
let writer = store
.writer(engine.slot(), HEAP)
.expect("the slot's writer opens");
let batch = [
caption("m1", 1, "the quick brown fox"),
caption("m2", 2, "a slow fox"),
];
let commit = {
let mut lane = Captions {
connection: &store,
slot: SlotHandle::mint(engine.clone()),
writer,
};
block_on(AsyncLands::land(&mut lane, &batch))
};
assert!(commit.committed(0) && commit.committed(1));
assert!(commit.refusal().is_none());
store.reload(engine.slot()).expect("the reader reloads");
assert_eq!(
search(&store, &engine, "fox")
.expect("the search runs")
.len(),
2,
"every item the socket reported went through the engine",
);
}
#[test]
fn the_blocking_socket_writes_through_the_same_engine() {
let (_dir, store) = open_store();
let engine = FulltextStore::fulltext(&store, Caption::SLOT.as_ref());
let writer = store
.writer(engine.slot(), HEAP)
.expect("the slot's writer opens");
let batch = [caption("m1", 1, "the quick brown fox")];
let commit = {
let mut lane = Captions {
connection: &store,
slot: SlotHandle::mint(engine.clone()),
writer,
};
Lands::land(&mut lane, &batch)
};
assert!(commit.committed(0));
assert!(commit.refusal().is_none());
store.reload(engine.slot()).expect("the reader reloads");
assert_eq!(
search(&store, &engine, "fox")
.expect("the search runs")
.len(),
1,
"the sync socket reached the engine too",
);
}
#[test]
fn a_refused_engine_write_is_a_refused_socket_call() {
let (_dir, store) = open_store();
let engine = FulltextStore::fulltext(&store, Caption::SLOT.as_ref());
let writer = store
.writer("elsewhere", HEAP)
.expect("the other slot's writer opens");
let mut lane = Captions {
connection: &store,
slot: SlotHandle::mint(engine),
writer,
};
let commit = block_on(AsyncLands::land(
&mut lane,
&[caption("m1", 1, "the quick brown fox")],
));
assert!(!commit.committed(0));
assert!(matches!(
commit.refusal(),
Some(TantivyFulltextError::SlotMismatch(_))
));
}
}