use super::{Dedup, Elected, ElectionLabel, IndexGraphql, IndexModel, Projected, Upstream};
use crate::{
engine::{Engine, FieldEngines},
model::{Column, GraphqlKind, NodeKind, Projection},
prefix::EmissionPrefix,
};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn column(name: &str, ty: &str) -> Column {
Column {
name: ident(name),
source: ident(name),
ty: syn::parse_str(ty).expect("a type"),
project: Projection::Borrowed,
}
}
fn projected(field: &str, ty: &str) -> Projected {
Projected {
field: ident(field),
from: ident(field),
ty: syn::parse_str(ty).expect("a type"),
}
}
fn stored(field: &str, engines: &[&str], fanout: bool) -> FieldEngines {
FieldEngines {
field: ident(field),
engines: engines
.iter()
.map(|word| Engine::bare(ident(word)))
.collect(),
fanout,
}
}
fn document(engines: Vec<FieldEngines>) -> IndexModel {
IndexModel {
prefix: EmissionPrefix::default(),
numbers: crate::index::Numbers::default(),
field_numbers: Vec::new(),
stores: Vec::new(),
extension: None,
data: false,
keys: false,
subscribe: false,
graphql: Some(IndexGraphql::of(GraphqlKind::AsyncGraphql)),
rename: None,
slot: None,
index: ident("SearchDocument"),
kind: NodeKind::Fulltext,
vis: syn::Visibility::Public(syn::token::Pub::default()),
krate: syn::parse_quote!(::ingraph),
source: Upstream {
node: syn::parse_quote!(asr::Segment),
projections: vec![projected("title", "String"), projected("body", "String")],
},
dedup: Dedup::Hash,
embeddings: Vec::new(),
labelled: Vec::new(),
searchable: vec![ident("title"), ident("body")],
engines,
fields: vec![column("title", "String"), column("body", "String")],
}
}
fn frames(engines: Vec<FieldEngines>) -> IndexModel {
let ty: syn::Type = syn::parse_str("Embedding<f32, 4>").expect("a type");
IndexModel {
prefix: EmissionPrefix::default(),
numbers: crate::index::Numbers::default(),
field_numbers: Vec::new(),
stores: Vec::new(),
extension: None,
data: false,
keys: false,
subscribe: false,
graphql: Some(IndexGraphql::of(GraphqlKind::AsyncGraphql)),
rename: None,
slot: None,
index: ident("Keyframes"),
kind: NodeKind::Vector,
vis: syn::Visibility::Public(syn::token::Pub::default()),
krate: syn::parse_quote!(::ingraph),
source: Upstream {
node: syn::parse_quote!(asr::Segment),
projections: vec![projected("image", "Embedding<f32, 4>")],
},
dedup: Dedup::Hash,
embeddings: vec![Elected::new(
ident("image"),
ElectionLabel::Embedding,
ty.clone(),
None,
)],
labelled: Vec::new(),
searchable: Vec::new(),
engines,
fields: vec![column("image", "Embedding<f32, 4>")],
}
}
fn emission(model: &IndexModel) -> syn::File {
use quote::ToTokens as _;
let file: syn::File = syn::parse2(model.emit()).expect("the emission is a parsable file");
let tier: syn::File =
syn::parse2(model.tier().emit()).expect("the fusion tree is a parsable file");
let citizen: syn::File =
syn::parse2(model.citizen()).expect("the wire citizenship is a parsable file");
let fusion: std::collections::HashSet<String> = tier
.items
.iter()
.chain(citizen.items.iter())
.map(|item| item.to_token_stream().to_string())
.collect();
syn::File {
items: file
.items
.into_iter()
.filter(|item| !fusion.contains(&item.to_token_stream().to_string()))
.collect(),
..file
}
}
fn structs(model: &IndexModel) -> Vec<String> {
emission(model)
.items
.into_iter()
.filter_map(|item| match item {
syn::Item::Struct(declared) => Some(declared.ident.to_string()),
_ => None,
})
.collect()
}
fn doors(model: &IndexModel) -> Vec<String> {
emission(model)
.items
.into_iter()
.filter_map(|item| match item {
syn::Item::Impl(declared) if declared.trait_.is_none() => Some(declared),
_ => None,
})
.flat_map(|declared| {
declared.items.into_iter().filter_map(|inner| match inner {
syn::ImplItem::Fn(method) => Some(method.sig.ident.to_string()),
_ => None,
})
})
.collect()
}
fn tokens(model: &IndexModel) -> String {
model.emit().to_string()
}
#[test]
fn every_declared_slot_becomes_a_marker_type() {
let model = document(vec![
stored("title", &["tantivy", "hnsw"], false),
stored("body", &["tantivy"], false),
]);
let declared = structs(&model);
assert!(
declared.contains(&"SearchDocumentTitleTantivy".to_owned()),
"{declared:?}"
);
assert!(
declared.contains(&"SearchDocumentTitleHnsw".to_owned()),
"{declared:?}"
);
assert!(
declared.contains(&"SearchDocumentBodyTantivy".to_owned()),
"{declared:?}"
);
assert_eq!(declared.len(), 3, "and nothing else: {declared:?}");
}
#[test]
fn two_fields_sharing_an_engine_get_two_markers() {
let model = document(vec![
stored("title", &["tantivy"], false),
stored("body", &["tantivy"], false),
]);
assert_eq!(
structs(&model),
["SearchDocumentTitleTantivy", "SearchDocumentBodyTantivy"]
);
}
#[test]
fn a_declaration_that_named_no_engine_mints_no_marker() {
let model = document(vec![
stored("title", &[], false),
stored("body", &[], false),
]);
assert!(structs(&model).is_empty());
}
#[test]
fn a_slot_marker_carries_its_durable_name() {
let emitted = tokens(&document(vec![stored("title", &["tantivy"], false)]));
assert!(
emitted.contains("\"search_document__title__tantivy\""),
"the minted name is in the expansion: {emitted}"
);
}
#[test]
fn a_slot_marker_names_its_field_and_its_engine() {
let emitted = tokens(&document(vec![stored("title", &["hnsw"], false)]));
assert!(emitted.contains("const FIELD"), "{emitted}");
assert!(emitted.contains("const ENGINE"), "{emitted}");
assert!(emitted.contains("\"title\""), "{emitted}");
assert!(emitted.contains("\"hnsw\""), "{emitted}");
}
#[test]
fn a_portable_field_gets_no_fan_door() {
let model = document(vec![stored("title", &["tantivy", "hnsw"], false)]);
assert!(
doors(&model).is_empty(),
"portability is written through the one slot a deployment opened: {:?}",
doors(&model)
);
}
#[test]
fn a_fanned_field_gets_one_door_named_after_it() {
let model = document(vec![
stored("title", &["tantivy", "hnsw"], true),
stored("body", &["tantivy", "hnsw"], false),
]);
assert_eq!(doors(&model), ["fan_title"]);
}
#[test]
fn a_fan_door_takes_one_engine_parameter_per_leg() {
let model = document(vec![stored("title", &["tantivy", "hnsw"], true)]);
let emitted = tokens(&model);
assert!(emitted.contains("__INGRAPH_ENGINE_0__"), "{emitted}");
assert!(emitted.contains("__INGRAPH_ENGINE_1__"), "{emitted}");
assert!(
!emitted.contains("__INGRAPH_ENGINE_2__"),
"and no more than there are legs: {emitted}"
);
}
#[test]
fn each_leg_is_branded_with_its_own_slot() {
let emitted = tokens(&document(vec![stored("title", &["tantivy", "hnsw"], true)]));
assert!(emitted.contains("SearchDocumentTitleTantivy"), "{emitted}");
assert!(emitted.contains("SearchDocumentTitleHnsw"), "{emitted}");
}
#[test]
fn the_fan_names_its_kind_s_write_verb() {
let full_text = tokens(&document(vec![stored("title", &["tantivy", "hnsw"], true)]));
assert!(full_text.contains("upsert_document"), "{full_text}");
assert!(!full_text.contains("upsert_vector"), "{full_text}");
let coordinates = tokens(&frames(vec![stored("image", &["lance", "hnsw"], true)]));
assert!(coordinates.contains("upsert_vector"), "{coordinates}");
assert!(!coordinates.contains("upsert_document"), "{coordinates}");
}
#[test]
fn a_three_legged_fan_nests_its_refusal() {
let emitted = tokens(&document(vec![stored(
"title",
&["tantivy", "hnsw", "sonic"],
true,
)]));
assert_eq!(
emitted.matches("Refused").count(),
7,
"the nesting is written once per position: {emitted}"
);
}
#[test]
fn an_artifact_family_emits_no_slot_and_no_fan() {
let mut artifacts = document(Vec::new());
artifacts.kind = NodeKind::Blob;
artifacts.searchable = Vec::new();
assert!(structs(&artifacts).is_empty());
assert!(doors(&artifacts).is_empty());
}
#[test]
fn a_slot_marker_rides_no_engine_gate() {
let emitted = tokens(&document(vec![stored("title", &["tantivy", "hnsw"], true)]));
assert!(
!emitted.contains("if_tantivy"),
"nothing here names a concrete engine, so nothing here is gated: {emitted}"
);
}