Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

fix: zvec writes over 1024 documents, deletes of missing documents, f32 overflow (#2771)

LJ πŸ₯₯🌴b3f87ab42ca5
diff --git a/python/cocoindex/connectors/zvec/_target.py b/python/cocoindex/connectors/zvec/_target.py
index b5c191b1..8eb97294 100644
--- a/python/cocoindex/connectors/zvec/_target.py
+++ b/python/cocoindex/connectors/zvec/_target.py
@@ -605,15 +605,27 @@ class _DocAction(NamedTuple):
value: _DocValue | None # None means delete
-def _check_status(result: Any) -> None:
- """Raise if any zvec Status in the result is not OK."""
+# The most documents one zvec write takes: zvec refuses a larger upsert or delete
+# ("Too many docs: N exceeds max write batch size of 1024").
+_MAX_WRITE_BATCH = 1024
+
+
+def _check_status(result: Any, *, missing_ok: bool = False) -> None:
+ """Raise if any zvec Status in the result is not OK.
+
+ With ``missing_ok``, a NOT_FOUND status is OK: a delete of a document that is
+ not there (its write failed in an earlier run, or it was removed behind
+ CocoIndex's back) has nothing to do.
+ """
statuses = result if isinstance(result, list) else [result]
for status in statuses:
- if not status.ok():
- raise RuntimeError(
- f"zvec operation failed: code={status.code()} "
- f"message={status.message()}"
- )
+ if status.ok():
+ continue
+ if missing_ok and status.code() == _zvec.StatusCode.NOT_FOUND:
+ continue
+ raise RuntimeError(
+ f"zvec operation failed: code={status.code()} message={status.message()}"
+ )
def _build_doc(value: _DocValue) -> Any:
@@ -653,10 +665,13 @@ class _DocHandler(coco.TargetHandler[_DocValue, bytes]):
else:
upserts.append(_build_doc(action.value))
- if upserts:
- _check_status(col.upsert(upserts))
- if deletes:
- _check_status(col.delete(ids=deletes))
+ for start in range(0, len(upserts), _MAX_WRITE_BATCH):
+ _check_status(col.upsert(upserts[start : start + _MAX_WRITE_BATCH]))
+ for start in range(0, len(deletes), _MAX_WRITE_BATCH):
+ _check_status(
+ col.delete(ids=deletes[start : start + _MAX_WRITE_BATCH]),
+ missing_ok=True,
+ )
if upserts or deletes:
col.optimize()
diff --git a/python/tests/connectors/test_zvec_target.py b/python/tests/connectors/test_zvec_target.py
index b4ebc586..0393cac6 100644
--- a/python/tests/connectors/test_zvec_target.py
+++ b/python/tests/connectors/test_zvec_target.py
@@ -369,6 +369,43 @@ def test_drop_collection(conn: Any) -> None:
assert not conn.collection_path("test_drop").exists()
+def test_writes_more_documents_than_one_zvec_batch(conn: Any) -> None:
+ """A run writes and deletes more documents than one zvec write takes (1024)."""
+ _reset(SimpleDoc, "test_big_batch")
+ app = _make_app(conn, "test_writes_more_documents_than_one_zvec_batch")
+
+ _rows.extend(
+ SimpleDoc(id=str(i), title="t", year=i, embedding=_EMB) for i in range(2500)
+ )
+ app.update_blocking()
+ col = conn.open_existing("test_big_batch")
+ assert col.stats.doc_count == 2500
+
+ _rows[:] = _rows[:10]
+ app.update_blocking()
+ assert col.stats.doc_count == 10
+
+
+def test_undeclaring_a_document_removed_out_of_band(conn: Any) -> None:
+ """A document already gone from the collection is deleted without error."""
+ _reset(SimpleDoc, "test_gone")
+ app = _make_app(conn, "test_undeclaring_a_document_removed_out_of_band")
+
+ _rows.extend(
+ [
+ SimpleDoc(id="1", title="A", year=1, embedding=_EMB),
+ SimpleDoc(id="2", title="B", year=2, embedding=_EMB),
+ ]
+ )
+ app.update_blocking()
+ conn.open_existing("test_gone").delete(ids=["2"])
+
+ _rows[:] = _rows[:1]
+ app.update_blocking()
+ assert fetch_doc(conn, "test_gone", "1") is not None
+ assert fetch_doc(conn, "test_gone", "2") is None
+
+
def test_no_op_when_unchanged(conn: Any) -> None:
_reset(SimpleDoc, "test_noop")
app = _make_app(conn, "test_no_op_when_unchanged")
diff --git a/rust/sdk/cocoindex/src/connectors/zvec.rs b/rust/sdk/cocoindex/src/connectors/zvec.rs
index 4af42294..743220dc 100644
--- a/rust/sdk/cocoindex/src/connectors/zvec.rs
+++ b/rust/sdk/cocoindex/src/connectors/zvec.rs
@@ -851,6 +851,12 @@ fn json_array<T>(value: &JsonValue, convert: impl Fn(&JsonValue) -> Option<T>) -
value.as_array()?.iter().map(convert).collect()
}
+/// `value` as an `f32`, or `None` when it is beyond the `f32` range: the cast
+/// would store an infinity.
+fn f32_value(value: f64) -> Option<f32> {
+ Some(value as f32).filter(|v| v.is_finite())
+}
+
/// A declared document: its id and its non-null fields.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct DocState {
@@ -925,7 +931,7 @@ fn scalar_value(
.and_then(|v| u32::try_from(v).ok())
.map(FieldValue::Uint32),
ScalarType::Uint64 => value.as_u64().map(FieldValue::Uint64),
- ScalarType::Float => value.as_f64().map(|v| FieldValue::Float(v as f32)),
+ ScalarType::Float => value.as_f64().and_then(f32_value).map(FieldValue::Float),
ScalarType::Double => value.as_f64().map(FieldValue::Double),
ScalarType::String => value.as_str().map(|s| FieldValue::String(s.to_string())),
ScalarType::ArrayBool => json_array(value, JsonValue::as_bool).map(FieldValue::ArrayBool),
@@ -942,7 +948,7 @@ fn scalar_value(
json_array(value, JsonValue::as_u64).map(FieldValue::ArrayUint64)
}
ScalarType::ArrayFloat => {
- json_array(value, |v| v.as_f64().map(|v| v as f32)).map(FieldValue::ArrayFloat)
+ json_array(value, |v| v.as_f64().and_then(f32_value)).map(FieldValue::ArrayFloat)
}
ScalarType::ArrayDouble => {
json_array(value, JsonValue::as_f64).map(FieldValue::ArrayDouble)
@@ -963,16 +969,17 @@ fn field_value(col: &Column, value: &JsonValue) -> Result<FieldValue> {
.map(|s| FieldValue::String(s.to_string()))
.ok_or_else(|| value_mismatch(col, "a string", value)),
ColumnKind::Dense { dimension, .. } => {
- // A non-finite element serializes as null, so it fails here.
+ // A non-finite element serializes as null, so it fails here, and
+ // so does an element beyond the `f32` range.
let vector = value
.as_array()
.and_then(|items| {
items
.iter()
- .map(|x| x.as_f64().map(|x| x as f32))
+ .map(|x| x.as_f64().and_then(f32_value))
.collect::<Option<Vec<f32>>>()
})
- .ok_or_else(|| value_mismatch(col, "a vector of finite numbers", value))?;
+ .ok_or_else(|| value_mismatch(col, "a vector of finite 32-bit floats", value))?;
if vector.len() != *dimension as usize {
return Err(Error::engine(format!(
"zvec vector column {:?} expects dimension {dimension}, got {}",
@@ -1063,23 +1070,34 @@ fn build_doc(state: &DocState) -> Result<Doc> {
Ok(doc)
}
-/// Fail if any document of a write was rejected.
-fn check_write(operation: &str, result: &zvec_rust::WriteResult) -> Result<()> {
+/// Fail if any document of a write was rejected. A delete of a document that
+/// is not there succeeds: the document may never have been written (its write
+/// failed in an earlier run) or may have been removed behind CocoIndex's back.
+fn check_write(operation: WriteOperation, result: &zvec_rust::WriteResult) -> Result<()> {
if result.error_count == 0 {
return Ok(());
}
- let first = result
- .results
- .iter()
- .find(|r| !r.is_success())
- .map(|r| format!(": code={:?} message={}", r.code, r.message))
- .unwrap_or_default();
+ let mut failures = result.results.iter().filter(|r| {
+ !r.is_success()
+ && !(operation == WriteOperation::Delete && r.code == zvec_rust::ErrorCode::NotFound)
+ });
+ let Some(first) = failures.next() else {
+ return Ok(());
+ };
Err(Error::engine(format!(
- "zvec {operation} failed for {} document(s){first}",
- result.error_count
+ "zvec {operation:?} failed for {} document(s): code={:?} message={}",
+ 1 + failures.count(),
+ first.code,
+ first.message
)))
}
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+enum WriteOperation {
+ Upsert,
+ Delete,
+}
+
#[derive(Clone, Debug, Serialize, Deserialize)]
struct DocAction {
doc_id: String,
@@ -1121,6 +1139,10 @@ impl DocHandler {
}
}
+/// The most documents one zvec write takes: zvec refuses a larger upsert or
+/// delete ("Too many docs: N exceeds max write batch size of 1024").
+const MAX_WRITE_BATCH: usize = 1024;
+
fn apply_docs(
conn: &ManagedConnection,
collection_name: &str,
@@ -1138,19 +1160,19 @@ fn apply_docs(
}
}
- if !upserts.is_empty() {
- let docs: Vec<&Doc> = upserts.iter().collect();
+ for chunk in upserts.chunks(MAX_WRITE_BATCH) {
+ let docs: Vec<&Doc> = chunk.iter().collect();
let result = collection
.upsert(&docs)
.map_err(|e| zvec_err(format_args!("upsert into {collection_name:?}"), e))?;
- check_write("upsert", &result)?;
+ check_write(WriteOperation::Upsert, &result)?;
}
- if !deletes.is_empty() {
- let ids: Vec<&str> = deletes.iter().map(String::as_str).collect();
+ for chunk in deletes.chunks(MAX_WRITE_BATCH) {
+ let ids: Vec<&str> = chunk.iter().map(String::as_str).collect();
let result = collection
.delete(&ids)
.map_err(|e| zvec_err(format_args!("delete from {collection_name:?}"), e))?;
- check_write("delete", &result)?;
+ check_write(WriteOperation::Delete, &result)?;
}
collection
.optimize()
@@ -1401,10 +1423,16 @@ fn collection_sink() -> TargetActionSink<CollectionAction> {
// User-facing API
// ---------------------------------------------------------------------------
-/// A target for writing documents to a zvec collection.
-#[derive(Clone)]
+/// A target for writing documents to a zvec collection. It serializes to the
+/// connection's `ContextKey` name and the collection name, so it can be an
+/// argument of a memoized function or of a mounted component.
+#[derive(Clone, Serialize)]
pub struct CollectionTarget {
+ db_key: Arc<str>,
+ collection_name: Arc<str>,
+ #[serde(skip)]
schema: CollectionSchema,
+ #[serde(skip)]
docs: TargetStateProvider<DocState>,
}
@@ -1495,10 +1523,17 @@ pub fn declare_collection_target_with_options(
schema: CollectionSchema,
options: ManagedTargetOptions,
) -> Result<CollectionTarget> {
- let target_state = collection_target_with_options(ctx, db, collection_name, schema, options)?;
+ let collection_name: Arc<str> = Arc::from(collection_name.into());
+ let target_state =
+ collection_target_with_options(ctx, db, collection_name.to_string(), schema, options)?;
let schema = target_state.value().schema.clone();
let docs = declare_target_state_with_child::<CollectionSpec, DocState>(ctx, target_state)?;
- Ok(CollectionTarget { schema, docs })
+ Ok(CollectionTarget {
+ db_key: Arc::from(db.name()),
+ collection_name,
+ schema,
+ docs,
+ })
}
/// Mount a zvec collection target and return a ready-to-use
@@ -1528,10 +1563,17 @@ pub async fn mount_collection_target_with_options(
schema: CollectionSchema,
options: ManagedTargetOptions,
) -> Result<CollectionTarget> {
- let target_state = collection_target_with_options(ctx, db, collection_name, schema, options)?;
+ let collection_name: Arc<str> = Arc::from(collection_name.into());
+ let target_state =
+ collection_target_with_options(ctx, db, collection_name.to_string(), schema, options)?;
let schema = target_state.value().schema.clone();
let docs = mount_target::<CollectionSpec, DocState>(ctx, target_state).await?;
- Ok(CollectionTarget { schema, docs })
+ Ok(CollectionTarget {
+ db_key: Arc::from(db.name()),
+ collection_name,
+ schema,
+ docs,
+ })
}
#[cfg(test)]
@@ -1626,5 +1668,24 @@ mod tests {
// A NaN element reaches the connector as null.
let err = field_value(&vector, &serde_json::json!([1.0, null, 2.0])).unwrap_err();
assert!(err.to_string().contains("finite"), "{err}");
+ // A finite `f64` beyond the `f32` range is an error, not an infinity.
+ let err = field_value(&vector, &serde_json::json!([1.0, 1e39, 2.0])).unwrap_err();
+ assert!(err.to_string().contains("finite 32-bit floats"), "{err}");
+ for data_type in [ScalarType::Float, ScalarType::ArrayFloat] {
+ let float = column(
+ "f",
+ ColumnKind::Scalar {
+ data_type,
+ encoding: ValueEncoding::Native,
+ indexed: true,
+ },
+ );
+ let value = match data_type {
+ ScalarType::Float => serde_json::json!(-1e39),
+ _ => serde_json::json!([0.5, -1e39]),
+ };
+ let err = field_value(&float, &value).unwrap_err();
+ assert!(err.to_string().contains("\"f\""), "{data_type:?}: {err}");
+ }
}
}
diff --git a/rust/sdk/cocoindex/tests/zvec_battle.rs b/rust/sdk/cocoindex/tests/zvec_battle.rs
new file mode 100644
index 00000000..511dd7ae
--- /dev/null
+++ b/rust/sdk/cocoindex/tests/zvec_battle.rs
@@ -0,0 +1,411 @@
+//! Edge and failure cases of the zvec target: document id forms, vector values
+//! that do not fit their column, schema changes between runs, batches around
+//! and far beyond the sizes a run writes at once, and a run that fails part
+//! way followed by one that succeeds.
+//!
+//! Hermetic (zvec is embedded; collections live in a temp directory):
+//!
+//! cargo test -p cocoindex --features zvec,testing --test zvec_battle
+#![cfg(feature = "zvec")]
+
+use std::sync::Arc;
+
+use cocoindex::connectors::zvec::{
+ self, CollectionSchema, ConnectOptions, ManagedConnection, ZvecType,
+};
+use cocoindex::{App, Environment, Result, SchemaFields};
+use serde::Serialize;
+use serde_json::{Value, json};
+
+cocoindex::context_key!(
+ static DB: ManagedConnection, key = "zvec_battle"
+);
+
+const COL: &str = "docs";
+
+struct Fixture {
+ _dir: tempfile::TempDir,
+ conn: ManagedConnection,
+ app: App,
+}
+
+async fn fixture(name: &str) -> Fixture {
+ let dir = tempfile::tempdir().unwrap();
+ let conn = zvec::connect(dir.path().join("zvec"), ConnectOptions::default()).unwrap();
+ let app = Environment::builder()
+ .db_path(dir.path().join(".cocoindex_db"))
+ .provide_key(&DB, conn.clone())
+ .build()
+ .await
+ .unwrap()
+ .app(name)
+ .await
+ .unwrap();
+ Fixture {
+ _dir: dir,
+ conn,
+ app,
+ }
+}
+
+impl Fixture {
+ async fn run<R: Serialize + Send + Sync + 'static>(
+ &self,
+ schema: &CollectionSchema,
+ rows: Vec<R>,
+ ) -> Result<()> {
+ let schema = schema.clone();
+ let rows = Arc::new(rows);
+ self.app
+ .run(move |ctx| {
+ let schema = schema.clone();
+ let rows = rows.clone();
+ async move {
+ let target = zvec::mount_collection_target(&ctx, &DB, COL, schema).await?;
+ for row in rows.iter() {
+ target.declare_row(&ctx, row)?;
+ }
+ Ok(())
+ }
+ })
+ .await
+ .map(|_| ())
+ }
+
+ async fn doc_count(&self) -> u64 {
+ let handle = self.conn.open_existing(COL).await.unwrap();
+ handle.stats().unwrap().doc_count
+ }
+
+ async fn title(&self, id: &str) -> Option<String> {
+ let handle = self.conn.open_existing(COL).await.unwrap();
+ let doc = handle.fetch(&[id]).unwrap().into_iter().next()?;
+ doc.get_string("title").unwrap()
+ }
+}
+
+#[derive(Clone, Serialize, SchemaFields)]
+struct StrDoc {
+ id: String,
+ title: String,
+ #[coco(vector = 3)]
+ embedding: Vec<f32>,
+}
+
+fn str_doc(id: &str, title: &str) -> StrDoc {
+ StrDoc {
+ id: id.to_string(),
+ title: title.to_string(),
+ embedding: vec![0.1, 0.2, 0.3],
+ }
+}
+
+#[derive(Clone, Serialize, SchemaFields)]
+struct IntDoc {
+ id: i64,
+ title: String,
+ #[coco(vector = 3)]
+ embedding: Vec<f32>,
+}
+
+/// String document ids with quotes, backslashes, whitespace, unicode and a
+/// long id are inserted, updated and deleted.
+#[tokio::test]
+async fn string_ids_with_special_characters_are_updated_and_deleted() -> Result<()> {
+ let f = fixture("ZvecBattleStringIds").await;
+ let schema = CollectionSchema::from_row::<StrDoc>(["id"])?;
+ let long = "k".repeat(512);
+ let mut ids: Vec<&str> = vec![
+ "plain",
+ "it's",
+ "back\\slash",
+ "double\"quote",
+ "unicode-ΠΊΠ»ΡŽΡ‡-πŸ”‘",
+ " spaced ",
+ "slash/and:colon",
+ "x' OR '1'='1",
+ &long,
+ ];
+ let docs =
+ |ids: &[&str], title: &str| ids.iter().map(|id| str_doc(id, title)).collect::<Vec<_>>();
+
+ // zvec refuses an empty id and one with a control character. The run
+ // fails naming the id, and the next run without it succeeds.
+ for bad in ["", "new\nline", "tab\tchar"] {
+ let mut with_bad = ids.clone();
+ with_bad.push(bad);
+ let err = f.run(&schema, docs(&with_bad, "v0")).await.unwrap_err();
+ assert!(err.to_string().contains("zvec upsert"), "{bad:?}: {err}");
+ }
+ ids.sort();
+ let docs = |title: &str| docs(&ids, title);
+ f.run(&schema, docs("v1")).await?;
+ assert_eq!(f.doc_count().await, ids.len() as u64);
+ f.run(&schema, docs("v2")).await?;
+ assert_eq!(f.doc_count().await, ids.len() as u64);
+ for id in &ids {
+ assert_eq!(f.title(id).await.as_deref(), Some("v2"), "{id:?}");
+ }
+ f.run(&schema, vec![str_doc("plain", "v2")]).await?;
+ assert_eq!(f.doc_count().await, 1);
+ assert_eq!(f.title("plain").await.as_deref(), Some("v2"));
+ Ok(())
+}
+
+/// Integer document ids at the ends of the i64 range are stored under their
+/// decimal text and deleted.
+#[tokio::test]
+async fn extreme_integer_ids_are_updated_and_deleted() -> Result<()> {
+ let f = fixture("ZvecBattleIntIds").await;
+ let schema = CollectionSchema::from_row::<IntDoc>(["id"])?;
+ let ids = [i64::MIN, -1, 0, 1, i64::MAX];
+ let docs = |title: &str| {
+ ids.iter()
+ .map(|id| IntDoc {
+ id: *id,
+ title: title.to_string(),
+ embedding: vec![0.1, 0.2, 0.3],
+ })
+ .collect::<Vec<_>>()
+ };
+ f.run(&schema, docs("v1")).await?;
+ f.run(&schema, docs("v2")).await?;
+ assert_eq!(f.doc_count().await, ids.len() as u64);
+ assert_eq!(f.title(&i64::MIN.to_string()).await.as_deref(), Some("v2"));
+ f.run(&schema, docs("v2")[2..3].to_vec()).await?;
+ assert_eq!(f.doc_count().await, 1);
+ assert_eq!(f.title("0").await.as_deref(), Some("v2"));
+ Ok(())
+}
+
+/// A vector of the wrong length (or empty) fails the run; the next run, with
+/// the vector fixed, writes every document.
+#[tokio::test]
+async fn wrong_length_vector_fails_then_the_fixed_run_succeeds() -> Result<()> {
+ let f = fixture("ZvecBattleWrongLength").await;
+ let schema = CollectionSchema::from_row::<StrDoc>(["id"])?;
+ let mut docs: Vec<StrDoc> = (0..20).map(|i| str_doc(&i.to_string(), "t")).collect();
+ for bad in [vec![1.0, 2.0], vec![]] {
+ docs[7].embedding = bad.clone();
+ let err = f.run(&schema, docs.clone()).await.unwrap_err();
+ assert!(
+ err.to_string()
+ .contains(&format!("expects dimension 3, got {}", bad.len())),
+ "{err}"
+ );
+ }
+ docs[7].embedding = vec![1.0, 2.0, 3.0];
+ f.run(&schema, docs).await?;
+ assert_eq!(f.doc_count().await, 20);
+ Ok(())
+}
+
+/// NaN and infinite elements are rejected when the row is declared, and so is
+/// a finite `f64` beyond the `f32` range, in a vector and in a float field.
+#[tokio::test]
+async fn non_finite_and_overflowing_values_are_rejected() -> Result<()> {
+ let f = fixture("ZvecBattleNonFinite").await;
+ let schema = CollectionSchema::from_row::<StrDoc>(["id"])?;
+ for bad in [f32::NAN, f32::INFINITY] {
+ let mut doc = str_doc("a", "t");
+ doc.embedding[0] = bad;
+ let err = f.run(&schema, vec![doc]).await.unwrap_err();
+ assert!(err.to_string().contains("non-finite"), "{bad}: {err}");
+ }
+
+ let schema = CollectionSchema::from_row::<StrDoc>(["id"])?.with_column_type(
+ "title",
+ ZvecType {
+ data_type: zvec::ScalarType::Float,
+ indexed: false,
+ },
+ )?;
+ for (field, value) in [
+ ("embedding", json!([1e39, 0.0, 0.0])),
+ ("title", json!(1e39)),
+ ] {
+ let mut row = json!({"id": "a", "title": 1.0, "embedding": [0.1, 0.2, 0.3]});
+ row[field] = value.clone();
+ let err = f.run(&schema, vec![row]).await.unwrap_err();
+ assert!(
+ err.to_string()
+ .contains(&format!("zvec column {field:?} expects")),
+ "{field}={value}: {err}"
+ );
+ }
+ Ok(())
+}
+
+/// A float16 vector field is refused when the schema is derived.
+#[test]
+fn float16_vector_field_is_refused() {
+ #[derive(Serialize, SchemaFields)]
+ struct HalfDoc {
+ id: String,
+ #[coco(vector = 3, half)]
+ embedding: Vec<f32>,
+ }
+ let err = CollectionSchema::from_row::<HalfDoc>(["id"]).unwrap_err();
+ assert!(err.to_string().contains("float16"), "{err}");
+}
+
+/// The vector dimension changes between runs: the collection is rebuilt and
+/// holds the documents of the new run.
+#[tokio::test]
+async fn vector_dimension_change_rebuilds_the_collection() -> Result<()> {
+ let f = fixture("ZvecBattleDimChange").await;
+ for dim in [3usize, 8, 3] {
+ let schema =
+ CollectionSchema::from_row::<StrDoc>(["id"])?.with_vector_dim("embedding", dim)?;
+ let docs: Vec<Value> = (0..5)
+ .map(|i| json!({"id": i.to_string(), "title": format!("d{dim}"), "embedding": vec![0.5_f32; dim]}))
+ .collect();
+ f.run(&schema, docs).await?;
+ assert_eq!(f.doc_count().await, 5, "dim {dim}");
+ assert_eq!(f.title("0").await, Some(format!("d{dim}")), "dim {dim}");
+ }
+ Ok(())
+}
+
+/// Document counts around the sizes a sink writes at once, inserted then all
+/// deleted, in one app so each count reconciles against the previous one.
+#[tokio::test]
+async fn batches_around_the_chunk_boundaries() -> Result<()> {
+ let f = fixture("ZvecBattleBatches").await;
+ let schema = CollectionSchema::from_row::<StrDoc>(["id"])?;
+ for n in [499, 500, 501, 999, 1000, 1001, 2500] {
+ let title = format!("v{n}");
+ let docs: Vec<StrDoc> = (0..n).map(|i| str_doc(&i.to_string(), &title)).collect();
+ f.run(&schema, docs).await?;
+ assert_eq!(f.doc_count().await, n as u64, "{n} docs");
+ assert_eq!(f.title(&(n - 1).to_string()).await, Some(title), "{n} docs");
+ }
+ f.run(&schema, Vec::<StrDoc>::new()).await?;
+ assert_eq!(f.doc_count().await, 0, "every document deleted");
+ Ok(())
+}
+
+/// A large run: 100 000 documents inserted, then half deleted. Slow, so
+/// ignored by default:
+///
+/// cargo test -p cocoindex --features zvec,testing --test zvec_battle \
+/// -- --ignored large_run
+#[tokio::test]
+#[ignore = "soak: 100k documents"]
+async fn large_run_inserts_and_deletes() -> Result<()> {
+ let f = fixture("ZvecBattleLarge").await;
+ let schema = CollectionSchema::from_row::<StrDoc>(["id"])?;
+ const N: usize = 100_000;
+ let docs: Vec<StrDoc> = (0..N).map(|i| str_doc(&i.to_string(), "v1")).collect();
+ let start = std::time::Instant::now();
+ f.run(&schema, docs.clone()).await?;
+ eprintln!("insert {N}: {:?}", start.elapsed());
+ assert_eq!(f.doc_count().await, N as u64);
+ let start = std::time::Instant::now();
+ f.run(&schema, docs[..N / 2].to_vec()).await?;
+ eprintln!("delete {}: {:?}", N / 2, start.elapsed());
+ assert_eq!(f.doc_count().await, (N / 2) as u64);
+ Ok(())
+}
+
+/// Nested JSON, a multi-megabyte string and bytes round-trip.
+#[tokio::test]
+async fn nested_json_and_huge_values_round_trip() -> Result<()> {
+ #[derive(Clone, Serialize, SchemaFields)]
+ struct RichDoc {
+ id: String,
+ title: String,
+ meta: serde_json::Value,
+ blob: Vec<u8>,
+ #[coco(vector = 3)]
+ embedding: Vec<f32>,
+ }
+ let f = fixture("ZvecBattleRich").await;
+ let schema = CollectionSchema::from_row::<RichDoc>(["id"])?;
+ let nested = json!({"a": [1, {"b": null, "c": "ΓΌ"}], "d": {"e": [true, 1.5]}});
+ let huge = "x".repeat(4 << 20);
+ f.run(
+ &schema,
+ vec![RichDoc {
+ id: "r".to_string(),
+ title: huge.clone(),
+ meta: nested.clone(),
+ blob: vec![0, 255, 7],
+ embedding: vec![0.1, 0.2, 0.3],
+ }],
+ )
+ .await?;
+ let handle = f.conn.open_existing(COL).await.unwrap();
+ let doc = handle.fetch(&["r"]).unwrap().into_iter().next().unwrap();
+ assert_eq!(doc.get_string("title").unwrap().unwrap().len(), huge.len());
+ let meta: Value = serde_json::from_str(&doc.get_string("meta").unwrap().unwrap()).unwrap();
+ assert_eq!(meta, nested);
+ assert_eq!(doc.get_string("blob").unwrap().as_deref(), Some("AP8H"));
+ Ok(())
+}
+
+/// A document removed behind CocoIndex's back is deleted without error when a
+/// later run no longer declares it.
+#[tokio::test]
+async fn undeclaring_a_document_removed_out_of_band_succeeds() -> Result<()> {
+ let f = fixture("ZvecBattleOutOfBandDelete").await;
+ let schema = CollectionSchema::from_row::<StrDoc>(["id"])?;
+ f.run(&schema, vec![str_doc("a", "t"), str_doc("b", "t")])
+ .await?;
+ {
+ let handle = f.conn.open_existing(COL).await.unwrap();
+ handle.delete(&["b"]).unwrap();
+ }
+ f.run(&schema, vec![str_doc("a", "t")]).await?;
+ assert_eq!(f.doc_count().await, 1);
+ Ok(())
+}
+
+static TARGET_ARG_CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
+
+#[cocoindex::function]
+async fn declare_doc_in_component(
+ ctx: &cocoindex::Ctx,
+ id: String,
+ target: zvec::CollectionTarget,
+) -> Result<()> {
+ TARGET_ARG_CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
+ target.declare_row(ctx, &str_doc(&id, "t"))
+}
+
+/// A collection handle as a mount argument: it serializes to its connection
+/// and collection names, so an unchanged child is skipped and a child given
+/// another collection runs again.
+#[tokio::test]
+async fn collection_target_is_a_mount_argument_keyed_by_its_collection() -> Result<()> {
+ let f = fixture("ZvecBattleTargetArg").await;
+ let schema = CollectionSchema::from_row::<StrDoc>(["id"])?;
+ let run = |name: &'static str| {
+ let schema = schema.clone();
+ let app = f.app.clone();
+ async move {
+ app.run(move |ctx| async move {
+ let target = zvec::mount_collection_target(&ctx, &DB, name, schema).await?;
+ assert_eq!(
+ serde_json::to_value(&target).unwrap(),
+ json!({"db_key": "zvec_battle", "collection_name": name})
+ );
+ cocoindex::use_mount_each!(vec![("1".to_string(), "1".to_string())], |id| {
+ declare_doc_in_component(ctx, id, target.clone())
+ })
+ .await?;
+ Ok(())
+ })
+ .await
+ }
+ };
+ let calls = || TARGET_ARG_CALLS.load(std::sync::atomic::Ordering::SeqCst);
+ run(COL).await?;
+ assert_eq!(calls(), 1);
+ assert_eq!(f.doc_count().await, 1);
+ run(COL).await?;
+ assert_eq!(calls(), 1, "same collection: the child is skipped");
+ run("other_docs").await?;
+ assert_eq!(calls(), 2, "another collection: the child runs");
+ Ok(())
+}