use core::{
num::NonZeroUsize,
pin::pin,
task::{Context, Poll, Waker},
};
use std::sync::Mutex;
use smol_bytes::Utf8Bytes;
use super::{AsyncFulltextStore, FulltextEngine, FulltextSubset};
use crate::{
AsyncFulltextRead, AsyncFulltextWrite, ColumnReader, ColumnWriter, Connected, Field,
FulltextRecord, FulltextTable, Hits, Identifiable, Scored, SlotName, Transformer,
store::column::testkit::{Col, Fallback, FallbackRead, Refusal},
};
const K: NonZeroUsize = NonZeroUsize::new(10).expect("10 is not zero");
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;
}
}
}
struct Caption {
media: u64,
frame: u32,
text: String,
}
#[derive(Debug, Clone, PartialEq)]
struct CaptionKey {
media: u64,
frame: u32,
}
#[derive(Debug, PartialEq)]
struct CaptionLineage {
media: u64,
}
impl Identifiable for Caption {
type Id = u32;
fn id_ref(&self) -> &u32 {
&self.frame
}
}
impl Transformer for Caption {
type Key = CaptionKey;
type Lineage = CaptionLineage;
type Record = Self;
type Graphql = ();
fn key(&self) -> CaptionKey {
CaptionKey {
media: self.media,
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");
}
fn caption(media: u64, frame: u32, text: &str) -> Caption {
Caption {
media,
frame,
text: text.to_owned(),
}
}
struct Fields(Vec<(&'static str, String)>);
struct Structured(Fields);
impl FulltextRecord for Structured {
type Document = Fields;
fn document(&self) -> &Fields {
&self.0
}
}
#[test]
fn a_fulltext_record_states_its_document() {
let caption = caption(7, 12, "a red car");
assert_eq!(caption.document(), "a red car");
fn document_of<T: FulltextRecord>(record: &T) -> &T::Document {
record.document()
}
assert_eq!(document_of(&caption), "a red car");
}
#[test]
fn the_document_face_carries_no_identity() {
fn width_of<T: FulltextRecord<Document = Fields>>(record: &T) -> usize {
record.document().0.len()
}
let structured = Structured(Fields(vec![
("title", "a red car".to_owned()),
("caption", "parked".to_owned()),
]));
assert_eq!(width_of(&structured), 2);
assert_eq!(structured.document().0[1].1, "parked");
}
#[test]
fn the_unused_face_answers_the_whole_seam() {
assert_eq!(
block_on(AsyncFulltextWrite::upsert(&(), Connected::new(&(), ()), ())),
Ok(())
);
assert_eq!(
block_on(AsyncFulltextRead::search(
&(),
Connected::new(&(), ()),
FulltextSubset::All,
"car",
K,
None
)),
Ok(Vec::new())
);
fn whole_lifecycle<E: FulltextEngine>() -> Result<(), E::Error> {
E::open(E::finalize(E::buffer())).finish()
}
assert_eq!(whole_lifecycle::<()>(), Ok(()));
}
#[test]
fn neither_verb_projects_a_record() {
fn index_anything<D>(db: &D, doc: D::Doc) -> impl Future<Output = Result<(), D::Error>>
where
D: AsyncFulltextWrite<Connection = (), WriteContext<'static> = ()>,
{
db.upsert(Connected::new(&(), ()), doc)
}
fn search<D>(
db: &D,
query: &str,
) -> impl Future<Output = Result<Hits<D::Address, D::Score>, D::Error>>
where
D: AsyncFulltextRead<Connection = (), ReadContext<'static> = ()>,
D::SearchOptions: 'static,
{
db.search(Connected::new(&(), ()), FulltextSubset::All, query, K, None)
}
assert_eq!(block_on(index_anything::<()>(&(), ())), Ok(()));
assert_eq!(block_on(search::<()>(&(), "car")), Ok(Vec::new()));
}
struct MemoryIndex {
lengths: Mutex<Vec<usize>>,
hit: Vec<Col>,
}
impl FulltextEngine for MemoryIndex {
type Options = ();
type SearchOptions = ();
type Error = Refusal;
type Score = f32;
type Address = Vec<Col>;
type Connection = ();
type WriteBuffer = Fallback;
type ReadBuffer = FallbackRead;
fn buffer() -> Self::WriteBuffer {
Fallback::default()
}
fn finalize(buf: Self::WriteBuffer) -> Self::Address {
buf.0
}
fn open(address: Self::Address) -> Self::ReadBuffer {
Fallback(address).read()
}
}
impl AsyncFulltextWrite for MemoryIndex {
type Doc = String;
type WriteContext<'a> = ();
fn upsert(
&self,
_ctx: Connected<'_, Self::Connection, Self::WriteContext<'_>>,
doc: Self::Doc,
) -> impl Future<Output = Result<(), Self::Error>> + Send {
self
.lengths
.lock()
.expect("the toy index is not poisoned")
.push(doc.len());
core::future::ready(Ok(()))
}
}
impl AsyncFulltextRead for MemoryIndex {
type ReadContext<'a> = ();
fn search(
&self,
_ctx: Connected<'_, Self::Connection, Self::ReadContext<'_>>,
_subset: FulltextSubset<'_>,
query: &str,
_k: NonZeroUsize,
_options: ::core::option::Option<&Self::SearchOptions>,
) -> impl Future<Output = Result<Hits<Self::Address, Self::Score>, Self::Error>> + Send {
core::future::ready(Ok(vec![Scored::new(self.hit.clone(), query.len() as f32)]))
}
}
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: u64::read(r)?,
frame: u32::read(r)?,
})
}
}
fn addressed(key: &CaptionKey) -> Vec<Col> {
let mut buf = MemoryIndex::buffer();
key.write(&mut buf);
MemoryIndex::finalize(buf)
}
#[test]
fn a_written_face_takes_the_entry_and_answers_with_addresses() {
let caption = caption(7, 12, "a red car");
let index = MemoryIndex {
lengths: Mutex::new(Vec::new()),
hit: addressed(&caption.key()),
};
assert_eq!(
block_on(index.upsert(Connected::new(&(), ()), caption.document().clone())),
Ok(())
);
assert_eq!(
*index.lengths.lock().expect("the toy index is not poisoned"),
vec!["a red car".len()]
);
let hits = block_on(index.search(Connected::new(&(), ()), FulltextSubset::All, "car", K, None))
.expect("the toy index cannot fail");
let keyed: Hits<CaptionKey, f32> =
crate::retrieve::keyed::<MemoryIndex, CaptionKey>(hits).expect("the address reads back");
assert_eq!(keyed.len(), 1);
assert_eq!(keyed[0].data_ref(), &caption.key());
assert_eq!(keyed[0].score(), 3.0);
assert_eq!(keyed[0].clone().into_data(), caption.key());
}
struct RecordingStore {
minted: Mutex<Vec<Utf8Bytes>>,
}
impl AsyncFulltextStore for RecordingStore {
type Options = ();
type Error = core::convert::Infallible;
type Handle = ();
async fn open(_options: Self::Options) -> Result<Self, Self::Error> {
Ok(Self {
minted: Mutex::new(Vec::new()),
})
}
fn fulltext(&self, slot: impl Into<Utf8Bytes>) -> Self::Handle {
self
.minted
.lock()
.expect("the recording store is not poisoned")
.push(slot.into());
}
}
#[test]
fn a_declared_slot_reaches_the_store_through_the_factory() {
let store = block_on(RecordingStore::open(())).expect("the recording store opens");
store.fulltext(Caption::SLOT.as_ref());
store.fulltext(String::from("transcripts"));
let minted = store
.minted
.lock()
.expect("the recording store is not poisoned");
let minted: Vec<&str> = minted.iter().map(|slot| &**slot).collect();
assert_eq!(minted, vec!["captions", "transcripts"]);
}