use core::{
convert::Infallible,
future::{Future, ready},
num::NonZeroUsize,
};
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
use super::{AsyncServes, FulltextAsk, Responding, Serve, Serves, SlotLane, VectorAsk};
use crate::{
AsyncFulltextRead, AsyncFulltextWrite, AsyncVectorRead, AsyncVectorWrite, ColumnReader,
ColumnWriter, Connected, Elected, Embedding, Field, FulltextEngine, FulltextSubset, Hits, Scored,
SlotHandle, VectorColumn, VectorEngine,
drive::{Answers, AsyncAnswers, QueryArm, door, lane::block_on},
store::column::testkit::{Col, Fallback, FallbackRead, Refusal},
};
const K: NonZeroUsize = NonZeroUsize::new(10).expect("10 is not zero");
struct Transcript;
struct Shelf(Vec<(&'static str, u64)>);
impl FulltextEngine for Shelf {
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 Shelf {
type Doc = (&'static str, u64);
type WriteContext<'a> = ();
fn upsert(
&self,
_ctx: Connected<'_, (), Self::WriteContext<'_>>,
_doc: Self::Doc,
) -> impl Future<Output = Result<(), Self::Error>> + Send {
ready(Ok(()))
}
}
impl AsyncFulltextRead for Shelf {
type ReadContext<'a> = ();
fn search(
&self,
_ctx: Connected<'_, (), 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 {
let hits = self
.0
.iter()
.filter(|(text, _)| text.contains(query))
.map(|(_, key)| Scored::new(shelved(*key), 1.0))
.collect();
ready(Ok(hits))
}
}
fn shelved(key: u64) -> Vec<Col> {
let mut buf = Shelf::buffer();
Held(key).write(&mut buf);
Shelf::finalize(buf)
}
struct ByText;
struct ByImage;
impl Elected<Transcript> for ByText {
const COLUMN: VectorColumn = TEXT;
}
impl Elected<Transcript> for ByImage {
const COLUMN: VectorColumn = IMAGE;
}
const TEXT: VectorColumn = VectorColumn::new("text_embedding");
const IMAGE: VectorColumn = VectorColumn::new("image_embedding");
struct Coordinates(Vec<(VectorColumn, Embedding<f32, 2>, u64)>);
struct Held(u64);
impl Field for Held {
fn write(&self, w: &mut impl ColumnWriter) {
self.0.write(w);
}
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
u64::read(r).map(Self)
}
}
fn addressed(key: u64) -> Vec<Col> {
let mut buf = Coordinates::buffer();
Held(key).write(&mut buf);
Coordinates::finalize(buf)
}
impl VectorEngine for Coordinates {
type Options = ();
type SearchOptions = ();
type Error = Refusal;
type Score = f32;
type Element = 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 AsyncVectorWrite for Coordinates {
type Doc = (Embedding<f32, 2>, u64);
type WriteContext<'a> = ();
fn upsert(
&self,
_ctx: Connected<'_, (), Self::WriteContext<'_>>,
_doc: Self::Doc,
) -> impl Future<Output = Result<(), Self::Error>> + Send {
ready(Ok(()))
}
}
impl AsyncVectorRead for Coordinates {
type ReadContext<'a> = ();
fn knn(
&self,
_ctx: Connected<'_, (), Self::ReadContext<'_>>,
column: VectorColumn,
query: &[Self::Element],
_k: NonZeroUsize,
_options: ::core::option::Option<&Self::SearchOptions>,
) -> impl Future<Output = Result<Hits<Self::Address, Self::Score>, Self::Error>> + Send {
let mut hits: Vec<Scored<Vec<Col>, f32>> = self
.0
.iter()
.filter(|(held, _, _)| *held == column)
.map(|(_, held, key)| {
let distance: f32 = held
.coordinates()
.iter()
.zip(query)
.map(|(a, b)| (a - b) * (a - b))
.sum();
Scored::new(addressed(*key), -distance)
})
.collect();
hits.sort_by(|a, b| b.score().total_cmp(&a.score()));
ready(Ok(hits))
}
fn get(
&self,
_ctx: Connected<'_, (), Self::ReadContext<'_>>,
column: VectorColumn,
address: &Self::Address,
) -> impl Future<Output = Result<Vec<Vec<Self::Element>>, Self::Error>> + Send {
ready(Ok(
self
.0
.iter()
.filter(|(held, _, key)| *held == column && *address == addressed(*key))
.map(|(_, held, _)| held.coordinates().to_vec())
.collect(),
))
}
}
struct Counting<S> {
inner: S,
served: Arc<AtomicUsize>,
}
impl<S> Counting<S> {
const fn new(inner: S, served: Arc<AtomicUsize>) -> Self {
Self { inner, served }
}
}
impl<S: Serve> Serve for Counting<S> {
type Options = S::Options;
}
impl<S, Q, A> Serves<Q, A> for Counting<S>
where
S: Serves<Q, A>,
{
type Error = S::Error;
fn serve(&mut self, q: Q, options: Option<&Self::Options>) -> Result<A, Self::Error> {
self.served.fetch_add(1, Ordering::Relaxed);
self.inner.serve(q, options)
}
}
impl<S, Q, A> AsyncServes<Q, A> for Counting<S>
where
S: AsyncServes<Q, A>,
{
type Error = S::Error;
fn serve(
&mut self,
q: Q,
options: Option<&Self::Options>,
) -> impl Future<Output = Result<A, Self::Error>> + Send {
self.served.fetch_add(1, Ordering::Relaxed);
self.inner.serve(q, options)
}
}
struct Words;
impl Answers<String> for Words {
type Output = usize;
type Error = Infallible;
fn on_invoke(&mut self, input: String) -> Result<usize, Self::Error> {
Ok(input.split_whitespace().count())
}
}
struct AwaitedWords;
impl AsyncAnswers<String> for AwaitedWords {
type Output = usize;
type Error = Infallible;
async fn on_invoke(&mut self, input: String) -> Result<usize, Self::Error> {
Ok(input.split_whitespace().count())
}
}
fn shelf() -> SlotLane<Shelf, Transcript> {
SlotLane::new(SlotHandle::mint(Shelf(vec![
("a red sunset over the bay", 1),
("a blue car", 2),
("sunset boulevard", 3),
])))
}
fn table() -> Coordinates {
let at = |column: VectorColumn, x: f32, key: u64| {
(
column,
Embedding::new(Arc::from(vec![x, 0.0])).expect("the fixture declares the width"),
key,
)
};
Coordinates(vec![
at(TEXT, 1.0, 1),
at(TEXT, 9.0, 9),
at(IMAGE, 9.0, 1),
at(IMAGE, 1.0, 9),
])
}
fn by_text() -> SlotLane<Coordinates, Transcript, ByText> {
SlotLane::new(SlotHandle::mint(table()))
}
fn by_image() -> SlotLane<Coordinates, Transcript, ByImage> {
SlotLane::new(SlotHandle::mint(table()))
}
#[test]
fn the_shipped_lane_serves_the_branded_slot_through_the_engines_own_verb() {
let mut lane = shelf();
let hits = block_on(lane.serve(
FulltextAsk::new(Connected::new(&(), ()), FulltextSubset::All, "sunset", K),
None,
))
.expect("this engine cannot refuse");
assert_eq!(
hits.iter().map(Scored::data_ref).collect::<Vec<_>>(),
vec![&shelved(1), &shelved(3)]
);
assert_eq!(lane.slot().engine().0.len(), 3);
}
#[test]
fn one_lane_serves_both_engine_kinds_and_what_separates_them_is_the_question() {
let mut lane = by_image();
let hits = block_on(lane.serve(
VectorAsk::new(Connected::new(&(), ()), &[8.6, 0.0], K),
None,
))
.expect("this engine cannot refuse");
assert_eq!(hits.first().expect("a nearest").data_ref(), &addressed(1));
}
#[test]
fn a_door_typed_for_an_election_answers_from_that_elections_column() {
fn text_door(lane: &mut SlotLane<Coordinates, Transcript, ByText>) -> Vec<u64> {
nearest(lane)
}
fn image_door(lane: &mut SlotLane<Coordinates, Transcript, ByImage>) -> Vec<u64> {
nearest(lane)
}
fn nearest<C: Elected<Transcript>>(lane: &mut SlotLane<Coordinates, Transcript, C>) -> Vec<u64> {
block_on(lane.serve(
VectorAsk::new(Connected::new(&(), ()), &[8.6, 0.0], K),
None,
))
.expect("this engine cannot refuse")
.into_iter()
.map(|hit| {
let mut phase = Coordinates::open(hit.into_data());
let Held(key) = Held::read(&mut phase).expect("the toy sealed this key");
phase.finish().expect("and nothing beyond it");
key
})
.collect()
}
let by_text = text_door(&mut by_text());
let by_image = image_door(&mut by_image());
assert_eq!(by_text, vec![9, 1], "the text column's neighbours");
assert_eq!(by_image, vec![1, 9], "the image column's neighbours");
assert_ne!(
by_text, by_image,
"two elections, two columns, two answers — over ONE slot",
);
}
#[test]
fn a_wrapping_lane_composes_over_the_shipped_one_without_naming_its_engine() {
let served = Arc::new(AtomicUsize::new(0));
let mut lane = Counting::new(shelf(), Arc::clone(&served));
let hits = block_on(lane.serve(
FulltextAsk::new(Connected::new(&(), ()), FulltextSubset::All, "blue", K),
None,
))
.expect("this engine cannot refuse");
assert_eq!(hits.len(), 1);
assert_eq!(served.load(Ordering::Relaxed), 1);
}
#[test]
fn a_responder_reaches_the_seam_through_the_bridge_without_naming_it() {
let mut words = Responding::new(Words);
let count = words
.serve("a red car".to_owned(), None)
.expect("infallible");
assert_eq!(count, 3);
assert_eq!(
core::mem::size_of::<Responding<Words>>(),
core::mem::size_of::<Words>()
);
}
#[test]
fn a_wrapping_lane_counts_a_live_round_trip_the_arm_cannot_see() {
let served = Arc::new(AtomicUsize::new(0));
let (front, invokes) = door::<String, usize>(4);
let mut arm = QueryArm::serving(
"words",
Counting::new(Responding::new(Words), Arc::clone(&served)),
invokes,
);
let awaiting = front.invoke("a red car".to_owned()).expect("listening");
drop(front);
let report = arm.run();
assert_eq!(awaiting.take().expect("answered"), 3);
assert_eq!(report.invokes(), 1);
assert_eq!(report.passed(), 1);
assert_eq!(served.load(Ordering::Relaxed), 1);
}
#[test]
fn the_async_engine_serves_through_the_same_wrapper() {
let served = Arc::new(AtomicUsize::new(0));
let (front, invokes) = door::<String, usize>(4);
let mut arm = QueryArm::serving(
"words",
Counting::new(Responding::new(AwaitedWords), Arc::clone(&served)),
invokes,
);
let awaiting = front.invoke("a red car".to_owned()).expect("listening");
drop(front);
let report = tokio::runtime::Builder::new_current_thread()
.build()
.expect("a current-thread runtime")
.block_on(arm.run_async());
assert_eq!(awaiting.take().expect("answered"), 3);
assert_eq!(report.passed(), 1);
assert_eq!(served.load(Ordering::Relaxed), 1);
}
struct Clip(());
struct ClipTower;
impl Answers<String> for ClipTower {
type Output = Embedding<f32, 4, Clip>;
type Error = Infallible;
fn on_invoke(&mut self, input: String) -> Result<Self::Output, Self::Error> {
let weight = input.len() as f32;
Ok(
Embedding::new(Arc::from([weight, 0.0, 0.0, 0.0].as_slice()))
.expect("four coordinates of four"),
)
}
}
#[test]
fn a_door_serves_the_arm_behind_it() {
let (mut front, invokes) = door::<String, Embedding<f32, 4, Clip>>(4);
let mut arm = QueryArm::new("clip", ClipTower, invokes);
let asking = std::thread::spawn(move || {
block_on(async move { AsyncServes::serve(&mut front, "a cat".to_owned(), None).await })
});
let served = arm.run::<Embedding<f32, 4, Clip>>();
let answer = asking.join().expect("the asking thread finishes");
assert_eq!(served.invokes(), 1);
assert_eq!(
answer.expect("the door answered").coordinates(),
&[5.0, 0.0, 0.0, 0.0]
);
}
#[test]
fn a_door_with_no_arm_refuses() {
let (mut front, invokes) = door::<String, Embedding<f32, 4, Clip>>(4);
drop(invokes);
let answer =
block_on(async move { AsyncServes::serve(&mut front, "a cat".to_owned(), None).await });
assert!(answer.is_err());
}