Incremental engine for long horizon agents 🌟 Star if you like it!
Rust agentic-data-frameworkaiai-agentschange-data-capturecodebase-intelligencecontext-engineeringdata-engineeringdata-indexingdata-processingetlhelp-wantedindexingknowledge-graphllmlong-horizon-agentpythonragreal-timerustsemantic-search
fix(rust-sdk): SurrealDB relation updates on 3.2, vector index changes, battle tests (#2772)
LJ 🥥🌴28794306c3bd
diff --git a/docs/src/content/docs/connectors/surrealdb.mdx b/docs/src/content/docs/connectors/surrealdb.mdxindex c007f6b1..4500e8b2 100644--- a/docs/src/content/docs/connectors/surrealdb.mdx+++ b/docs/src/content/docs/connectors/surrealdb.mdx@@ -196,7 +196,7 @@ Python writes a record with `UPSERT ... MERGE`, which merges nested objects. Rus<Lang only="rust">-**Server version.** A relation record that changes is replaced with `DELETE` and then `RELATE` in one transaction. SurrealDB 3.2.4 rejects this with "already exists"; use SurrealDB 3.3.5 or later when relation records change.+**Server version.** A relation record whose fields change is replaced in place. A relation record whose endpoints change (one with an explicit `id`) is replaced with `DELETE` and then `RELATE` in one transaction. SurrealDB 3.2 rejects that with "already exists"; use SurrealDB 3.3.5 or later to move such a relation.</Lang>diff --git a/rust/sdk/cocoindex/src/connectors/surrealdb.rs b/rust/sdk/cocoindex/src/connectors/surrealdb.rsindex 28b6df9a..9513c494 100644--- a/rust/sdk/cocoindex/src/connectors/surrealdb.rs+++ b/rust/sdk/cocoindex/src/connectors/surrealdb.rs@@ -445,14 +445,12 @@ fn json_object_keys(info: Option<&JsonValue>, field: &str) -> Vec<String> {/// A SurrealDB table target — a handle to declare records on.////// As a `mount_each!` / `use_mount!` or memoized function argument it-/// fingerprints by its stable key, the graph key and table name; the managed-by-/// flag and runtime provider are not part of the identity.+/// fingerprints by its stable key, the graph key and table name; the runtime+/// provider is not part of the identity.#[derive(Clone, Serialize)]pub struct TableTarget {graph_key: Arc<str>,table_name: Arc<str>,- #[serde(skip)]- managed_by: ManagedBy,/// The table's record provider; also carries its vector-index attachment.#[serde(skip)]records: TargetStateProvider<RecordState>,@@ -522,7 +520,9 @@ impl TableTarget {/// Declare a vector index on `field` as an attachment of this table. The/// index is created/recreated/dropped to match the declared options- /// (`DEFINE INDEX … <METHOD> DIMENSION … DIST … TYPE …`).+ /// (`DEFINE INDEX … <METHOD> DIMENSION … DIST … TYPE …`), on a+ /// user-managed table too (as in Python): declaring it hands it to+ /// CocoIndex.pub fn declare_vector_index(&self,ctx: &Ctx,@@ -545,7 +545,7 @@ impl TableTarget {method: options.method.to_string(),dimension,vector_type: options.vector_type.to_string(),- managed_by: self.managed_by,+ managed_by: ManagedBy::System,};declare_target_state(ctx,@@ -724,7 +724,6 @@ fn table_handle(TableTarget {graph_key: Arc::from(graph.name()),table_name: Arc::from(spec.table_name),- managed_by: spec.managed_by,records,}}@@ -1333,7 +1332,8 @@ enum RecordWrite {/// Fields mode, group removed: unset the fields it owned. A missing record/// is fine.UnsetFields(Vec<String>),- /// Relation records are single-owner: `DELETE` + `RELATE` replaces the edge.+ /// Relation records are single-owner: the edge is replaced whole, in place+ /// when its endpoints are unchanged, else by `DELETE` + `RELATE`.Relate {fields: Map<String, JsonValue>,endpoints: RelationEndpoints,@@ -1647,22 +1647,74 @@ fn table_sink() -> TargetActionSink<TableAction> {// Record handler (child) + sink + vector-index attachment// ---------------------------------------------------------------------------+/// One write to a table: a record write, or a change to one of its vector+/// indexes. Records and indexes share one sink, so that one apply sees both and+/// can order them: an index whose definition changes is removed before the+/// records are written, and defined after them. The engine orders no two+/// sinks, and a changed index cannot be defined over the old records (a new+/// dimension), nor the new records written under the old index.+#[derive(Clone, Debug, Serialize, Deserialize)]+enum TableWrite {+ Record(RecordAction),+ Index(VectorIndexAction),+}++/// The sink of a table's record and vector-index writes (see [`TableWrite`]).+fn table_write_sink(graph_key: String, spec: TableSpec) -> TargetActionSink<TableWrite> {+ TargetActionSink::from_async_fn_with_ctx(+ move |host_ctx, actions: Vec<TargetAction<TableWrite>>| {+ let graph_key = graph_key.clone();+ let spec = spec.clone();+ async move {+ let graph = resolve_graph(&host_ctx, &graph_key)?;+ let mut records = Vec::new();+ let mut indexes = Vec::new();+ for action in actions {+ match action {+ TargetAction::Create(write)+ | TargetAction::Update(write)+ | TargetAction::Delete(write) => match write {+ TableWrite::Record(record) => records.push(record),+ TableWrite::Index(index) => indexes.push(index),+ },+ }+ }+ for index in &indexes {+ remove_vector_index(&graph, index).await?;+ }+ apply_records(&graph, &spec, records).await?;+ for index in &indexes {+ define_vector_index(&graph, index).await?;+ }+ Ok(())+ }+ },+ )+}+struct RecordHandler {- sink: SinkCache<RecordAction>,+ /// Shared with the vector-index attachment handler (see [`TableWrite`]).+ sink: SinkCache<TableWrite>,graph_key: String,spec: TableSpec,}+impl RecordHandler {+ fn write_sink(&self) -> TargetActionSink<TableWrite> {+ self.sink+ .get(|| table_write_sink(self.graph_key.clone(), self.spec.clone()))+ }+}+impl TargetHandler<RecordState> for RecordHandler {type TrackingRecord = RecordTracking;- type Action = RecordAction;+ type Action = TableWrite;fn attachments(&self) -> Result<Vec<(String, ChildTargetDef)>> {Ok(vec![("vector_index".to_string(),ChildTargetDef::new::<VectorIndexSpec, _>(VectorIndexHandler {- sink: SinkCache::default(),- graph_key: self.graph_key.clone(),+ sink: self.write_sink(),}),)])}@@ -1673,7 +1725,7 @@ impl TargetHandler<RecordState> for RecordHandler {desired: Option<RecordState>,prev: Vec<RecordTracking>,prev_may_be_missing: bool,- ) -> Result<Option<TargetReconcileOutput<RecordAction, RecordTracking>>> {+ ) -> Result<Option<TargetReconcileOutput<TableWrite, RecordTracking>>> {let RecordKey { record_id, group } = RecordKey::from_stable_key(&key)?;let in_fields_mode = group.is_some();let (write, tracking) = match desired {@@ -1732,39 +1784,14 @@ impl TargetHandler<RecordState> for RecordHandler {}};Ok(Some(TargetReconcileOutput {- action: TargetAction::Update(RecordAction { record_id, write }),- sink: self.sink.get(|| self.record_sink()),+ action: TargetAction::Update(TableWrite::Record(RecordAction { record_id, write })),+ sink: self.write_sink(),tracking_record: tracking,child_invalidation: None,}))}}-impl RecordHandler {- fn record_sink(&self) -> TargetActionSink<RecordAction> {- let graph_key = self.graph_key.clone();- let spec = self.spec.clone();- TargetActionSink::from_async_fn_with_ctx(- move |host_ctx, actions: Vec<TargetAction<RecordAction>>| {- let graph_key = graph_key.clone();- let spec = spec.clone();- async move {- let graph = resolve_graph(&host_ctx, &graph_key)?;- let actions = actions- .into_iter()- .map(|action| match action {- TargetAction::Create(r)- | TargetAction::Update(r)- | TargetAction::Delete(r) => r,- })- .collect();- apply_records(&graph, &spec, actions).await- }- },- )- }-}-// ---------------------------------------------------------------------------// Vector-index attachment handler + sink// ---------------------------------------------------------------------------@@ -1780,6 +1807,8 @@ pub struct VectorIndexSpec {method: String,dimension: usize,vector_type: String,+ /// Always `System`: a declared index is CocoIndex's, whoever manages the+ /// table. Kept so that tracking records written with it read back.#[serde(default)]managed_by: ManagedBy,}@@ -1793,13 +1822,13 @@ struct VectorIndexAction {}struct VectorIndexHandler {- sink: SinkCache<VectorIndexAction>,- graph_key: String,+ /// The table's record sink (see [`TableWrite`]).+ sink: TargetActionSink<TableWrite>,}impl TargetHandler<VectorIndexSpec> for VectorIndexHandler {type TrackingRecord = VectorIndexSpec;- type Action = VectorIndexAction;+ type Action = TableWrite;fn reconcile(&self,@@ -1807,7 +1836,7 @@ impl TargetHandler<VectorIndexSpec> for VectorIndexHandler {desired: Option<VectorIndexSpec>,prev: Vec<VectorIndexSpec>,prev_may_be_missing: bool,- ) -> Result<Option<TargetReconcileOutput<VectorIndexAction, VectorIndexSpec>>> {+ ) -> Result<Option<TargetReconcileOutput<TableWrite, VectorIndexSpec>>> {let name = match &key {StableKey::Str(s) | StableKey::Symbol(s) => s.to_string(),other => {@@ -1831,80 +1860,53 @@ impl TargetHandler<VectorIndexSpec> for VectorIndexHandler {.or_else(|| prev.first().map(|p| p.table_name.clone())).unwrap_or_default();Ok(Some(TargetReconcileOutput {- action: TargetAction::Update(VectorIndexAction {+ action: TargetAction::Update(TableWrite::Index(VectorIndexAction {name,table_name,spec: desired.clone(),- }),- sink: self.sink.get(|| self.vector_index_sink()),+ })),+ sink: self.sink.clone(),tracking_record: desired,child_invalidation: None,}))}}-impl VectorIndexHandler {- fn vector_index_sink(&self) -> TargetActionSink<VectorIndexAction> {- let graph_key = self.graph_key.clone();- TargetActionSink::from_async_fn_with_ctx(- move |host_ctx, actions: Vec<TargetAction<VectorIndexAction>>| {- let graph_key = graph_key.clone();- async move {- let graph = resolve_graph(&host_ctx, &graph_key)?;- for action in actions {- let action = match action {- TargetAction::Create(a)- | TargetAction::Update(a)- | TargetAction::Delete(a) => a,- };- apply_vector_index(&graph, action).await?;- }- Ok(())- }- },- )- }+/// The first half of applying a [`VectorIndexAction`], before the records of+/// its batch are written: remove the index.+async fn remove_vector_index(graph: &Graph, action: &VectorIndexAction) -> Result<()> {+ validate_ident(&action.name, "vector index name")?;+ validate_ident(&action.table_name, "table name")?;+ run_query(+ graph,+ &format!(+ "REMOVE INDEX IF EXISTS {} ON TABLE {}",+ action.name, action.table_name+ ),+ )+ .await}-async fn apply_vector_index(graph: &Graph, action: VectorIndexAction) -> Result<()> {+/// The second half, after the records of its batch are written: define the+/// index, unless it is removed.+async fn define_vector_index(graph: &Graph, action: &VectorIndexAction) -> Result<()> {+ let Some(spec) = &action.spec else {+ return Ok(());+ };validate_ident(&action.name, "vector index name")?;validate_ident(&action.table_name, "table name")?;- match action.spec {- // User-managed indexes: CocoIndex does not own the DDL.- Some(spec) if spec.managed_by.is_user() => Ok(()),- Some(spec) => {- validate_ident(&spec.field, "vector index field")?;- run_query(- graph,- &format!(- "REMOVE INDEX IF EXISTS {} ON TABLE {}",- action.name, action.table_name- ),- )- .await?;- let stmt = format!(- "DEFINE INDEX {} ON {} FIELDS {} {} DIMENSION {} DIST {} TYPE {}",- action.name,- action.table_name,- spec.field,- spec.method.to_uppercase(),- spec.dimension,- spec.metric.to_uppercase(),- spec.vector_type.to_uppercase(),- );- run_query(graph, &stmt).await- }- None => {- run_query(- graph,- &format!(- "REMOVE INDEX IF EXISTS {} ON TABLE {}",- action.name, action.table_name- ),- )- .await- }- }+ validate_ident(&spec.field, "vector index field")?;+ let stmt = format!(+ "DEFINE INDEX {} ON {} FIELDS {} {} DIMENSION {} DIST {} TYPE {}",+ action.name,+ action.table_name,+ spec.field,+ spec.method.to_uppercase(),+ spec.dimension,+ spec.metric.to_uppercase(),+ spec.vector_type.to_uppercase(),+ );+ run_query(graph, &stmt).await}async fn run_query(graph: &Graph, stmt: &str) -> Result<()> {@@ -2171,19 +2173,29 @@ fn record_chunk(spec: &TableSpec, actions: Vec<RecordAction>) -> Result<RecordChRecordWrite::Relate { fields, endpoints } => {validate_ident(&endpoints.from_table, "relation from table name")?;validate_ident(&endpoints.to_table, "relation to table name")?;- let mut record = record_params(id, fields_params(schema, &fields)?);- record.insert(- "f",- Value::RecordId(endpoints.from_id.to_record_id(&endpoints.from_table)),- );- record.insert(- "t",- Value::RecordId(endpoints.to_id.to_record_id(&endpoints.to_table)),- );+ let from = Value::RecordId(endpoints.from_id.to_record_id(&endpoints.from_table));+ let to = Value::RecordId(endpoints.to_id.to_record_id(&endpoints.to_table));+ // The content carries the endpoints, so that `UPDATE … CONTENT`+ // keeps them.+ let mut content = fields_params(schema, &fields)?;+ content.insert("in", from.clone());+ content.insert("out", to.clone());+ let mut record = record_params(id, content);+ record.insert("f", from);+ record.insert("t", to);+ // An edge that exists between the same endpoints is replaced+ // in place. Otherwise any old edge is deleted and the new one+ // related: SurrealDB 3.2 cannot create a record deleted earlier+ // in the same transaction, so only an edge whose endpoints+ // change takes `DELETE` + `RELATE`.(- "DELETE $r.id RETURN NONE; \- RELATE ($r.f)->($r.id)->($r.t) CONTENT $r.c RETURN NONE;"- .to_string(),+ "IF (SELECT VALUE [in, out] FROM ONLY $r.id) = [$r.f, $r.t] { \+ UPDATE $r.id CONTENT $r.c RETURN NONE; \+ } ELSE { \+ DELETE $r.id RETURN NONE; \+ RELATE ($r.f)->($r.id)->($r.t) CONTENT $r.c RETURN NONE; \+ };"+ .to_string(),record,)}@@ -2648,13 +2660,28 @@ mod tests {assert_eq!(chunk.surql,"BEGIN TRANSACTION;\n\- FOR $r IN $g0 { DELETE $r.id RETURN NONE; \- RELATE ($r.f)->($r.id)->($r.t) CONTENT $r.c RETURN NONE; };\n\+ FOR $r IN $g0 { \+ IF (SELECT VALUE [in, out] FROM ONLY $r.id) = [$r.f, $r.t] { \+ UPDATE $r.id CONTENT $r.c RETURN NONE; \+ } ELSE { \+ DELETE $r.id RETURN NONE; \+ RELATE ($r.f)->($r.id)->($r.t) CONTENT $r.c RETURN NONE; \+ }; };\n\COMMIT TRANSACTION;\n");let [r1, r2] = &bound_records(&chunk, 0)[..] else {panic!("expected two relations");};+ // The content carries the endpoints, which `UPDATE … CONTENT` keeps.+ let Some(Value::Object(content)) = r1.get("c") else {+ panic!("expected the content object");+ };+ assert_eq!(+ content.get("since"),+ Some(&Value::Number(Number::Int(2020)))+ );+ assert_eq!(content.get("in"), r1.get("f"));+ assert_eq!(content.get("out"), r1.get("t"));assert_eq!(r1.get("id"),Some(&Value::RecordId(RecordId::new("knows", "r1")))diff --git a/rust/sdk/cocoindex/tests/surrealdb_scenarios.rs b/rust/sdk/cocoindex/tests/surrealdb_scenarios.rsnew file mode 100644index 00000000..f903ac6d--- /dev/null+++ b/rust/sdk/cocoindex/tests/surrealdb_scenarios.rs@@ -0,0 +1,765 @@+//! Edge-case scenarios for the SurrealDB target: relation updates, record id+//! forms, payload values, vectors, failed batches and recovery, out-of-band+//! changes and chunk boundaries. Each test needs a SurrealDB server at+//! `SURREALDB_URL` (default `127.0.0.1:8787`) and skips without one.+#![cfg(feature = "surrealdb")]++use cocoindex::connectors::surrealdb::{self, ColumnDef, Graph, TableSchema, VectorIndexOptions};+use cocoindex::prelude::*;++cocoindex::context_key!(static GRAPH: Graph, key = "surrealdb_scenarios_graph");++/// A fresh database on the server `SURREALDB_URL` names, plus an app on it.+async fn scenario(tag: &str) -> Option<(Graph, App, tempfile::TempDir)> {+ let url = std::env::var("SURREALDB_URL").unwrap_or_else(|_| "127.0.0.1:8787".to_string());+ let ns = std::env::var("SURREALDB_NS").unwrap_or_else(|_| "cocoindex".to_string());+ let user = std::env::var("SURREALDB_USER").unwrap_or_else(|_| "root".to_string());+ let pass = std::env::var("SURREALDB_PASS").unwrap_or_else(|_| "root".to_string());+ let nanos = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .unwrap()+ .as_nanos();+ let db_name = format!("rust_sdk_surrealdb_{tag}_{}_{nanos}", std::process::id());+ let Ok(graph) = Graph::connect(&url, &ns, &db_name, &user, &pass).await else {+ eprintln!("skipping SurrealDB {tag} test; no local SurrealDB connection");+ return None;+ };+ let dir = tempfile::tempdir().unwrap();+ let app = Environment::builder()+ .db_path(dir.path().join("lmdb"))+ .provide_key(&GRAPH, graph.clone())+ .build()+ .await+ .unwrap()+ .app(&format!("surrealdb_{tag}"))+ .await+ .unwrap();+ Some((graph, app, dir))+}++/// The rows a query returns, as JSON.+async fn query_json(graph: &Graph, query: &str) -> Vec<serde_json::Value> {+ let mut response = graph.client().query(query).await.unwrap().check().unwrap();+ response.take(0).unwrap()+}++/// The server's (major, minor) version.+async fn server_version(graph: &Graph) -> (u64, u64) {+ let version = graph.client().version().await.unwrap();+ (version.major, version.minor)+}++// ---------------------------------------------------------------------------+// Relations+// ---------------------------------------------------------------------------++/// Declare `person` records and `knows` relations with their fields.+async fn run_knows(+ app: &App,+ people: Vec<&'static str>,+ edges: Vec<(&'static str, &'static str, serde_json::Value)>,+) -> Result<()> {+ app.run(move |ctx| async move {+ let person = surrealdb::mount_table_target(&ctx, &GRAPH, "person").await?;+ let knows =+ surrealdb::mount_relation_target(&ctx, &GRAPH, "knows", &person, &person).await?;+ for name in people {+ person.declare_record(&ctx, name, &serde_json::json!({ "name": name }))?;+ }+ for (from, to, fields) in edges {+ knows.declare_relation_record(&ctx, from, to, &fields)?;+ }+ Ok(())+ })+ .await?;+ Ok(())+}++/// The `knows` edges as `[in, out, fields]`, sorted.+async fn knows_edges(graph: &Graph) -> Vec<serde_json::Value> {+ let mut edges = query_json(+ graph,+ "SELECT VALUE [<string>in, <string>out, (SELECT * OMIT id, in, out FROM ONLY $this.id)] \+ FROM knows",+ )+ .await;+ edges.sort_by_key(|edge| edge.to_string());+ edges+}++#[tokio::test]+async fn surrealdb_relation_fields_change_in_place_when_available() {+ let Some((graph, app, _dir)) = scenario("rel_update").await else {+ return;+ };+ let people = || vec!["alice", "bob"];+ run_knows(+ &app,+ people(),+ vec![+ (+ "alice",+ "bob",+ serde_json::json!({ "since": 2020, "note": "x" }),+ ),+ ("alice", "alice", serde_json::json!({ "since": 1990 })),+ ],+ )+ .await+ .unwrap();+ assert_eq!(knows_edges(&graph).await.len(), 2);++ // An edge's fields change and one goes away; the self-loop changes too.+ // This failed with "already exists" on SurrealDB 3.2, which cannot+ // re-create a record deleted in the same transaction.+ run_knows(+ &app,+ people(),+ vec![+ ("alice", "bob", serde_json::json!({ "since": 2021 })),+ ("alice", "alice", serde_json::json!({ "since": 1991 })),+ ],+ )+ .await+ .unwrap();+ assert_eq!(+ knows_edges(&graph).await,+ vec![+ serde_json::json!(["person:alice", "person:alice", { "since": 1991 }]),+ serde_json::json!(["person:alice", "person:bob", { "since": 2021 }]),+ ]+ );+ // Graph traversal follows the edges.+ assert_eq!(+ query_json(+ &graph,+ "SELECT VALUE <-knows<-person.name FROM ONLY person:bob"+ )+ .await,+ vec![serde_json::json!("alice")]+ );++ // An unchanged run leaves the edges alone; dropping them deletes them.+ run_knows(+ &app,+ people(),+ vec![("alice", "bob", serde_json::json!({ "since": 2021 }))],+ )+ .await+ .unwrap();+ assert_eq!(+ knows_edges(&graph).await,+ vec![serde_json::json!(["person:alice", "person:bob", { "since": 2021 }])]+ );+}++/// A relation with an explicit `id` whose endpoints change keeps its id and+/// moves to the new endpoints.+#[tokio::test]+async fn surrealdb_relation_with_explicit_id_moves_endpoints_when_available() {+ let Some((graph, app, _dir)) = scenario("rel_move").await else {+ return;+ };+ let people = || vec!["alice", "bob", "carol"];+ run_knows(+ &app,+ people(),+ vec![("alice", "bob", serde_json::json!({ "id": "e1", "w": 1 }))],+ )+ .await+ .unwrap();+ let result = run_knows(+ &app,+ people(),+ vec![("alice", "carol", serde_json::json!({ "id": "e1", "w": 2 }))],+ )+ .await;+ if server_version(&graph).await < (3, 3) {+ // SurrealDB 3.2 cannot re-create a record deleted in the same+ // transaction, so moving an edge needs 3.3 or later.+ let message = result.unwrap_err().to_string();+ assert!(message.contains("already exists"), "{message}");+ return;+ }+ result.unwrap();+ assert_eq!(+ knows_edges(&graph).await,+ vec![serde_json::json!(["person:alice", "person:carol", { "w": 2 }])]+ );+ assert_eq!(+ query_json(&graph, "SELECT VALUE <string>id FROM knows").await,+ vec![serde_json::json!("knows:e1")]+ );+}++#[tokio::test]+async fn surrealdb_many_relations_per_node_when_available() {+ let Some((graph, app, _dir)) = scenario("rel_fanout").await else {+ return;+ };+ async fn run(app: &App, n: usize, weight: i64) {+ app.run(move |ctx| async move {+ let node = surrealdb::mount_table_target(&ctx, &GRAPH, "node").await?;+ let link = surrealdb::mount_relation_target(&ctx, &GRAPH, "link", &node, &node).await?;+ node.declare_record(&ctx, "hub", &serde_json::json!({}))?;+ for i in 0..n {+ let leaf = format!("leaf{i}");+ node.declare_record(&ctx, leaf.as_str(), &serde_json::json!({}))?;+ link.declare_relation_record(+ &ctx,+ "hub",+ leaf.as_str(),+ &serde_json::json!({ "w": weight }),+ )?;+ }+ Ok(())+ })+ .await+ .unwrap();+ }+ // More than two chunks of relations, then every edge changes, then most go.+ run(&app, 1201, 1).await;+ assert_eq!(graph.count("link").await.unwrap(), 1201);+ run(&app, 1201, 2).await;+ assert_eq!(+ query_json(&graph, "SELECT VALUE count(->link) FROM ONLY node:hub").await,+ vec![serde_json::json!(1201)]+ );+ assert!(+ query_json(&graph, "SELECT VALUE w FROM link WHERE w != 2")+ .await+ .is_empty()+ );+ run(&app, 3, 2).await;+ assert_eq!(graph.count("link").await.unwrap(), 3);+ assert_eq!(graph.count("node").await.unwrap(), 4);+}++/// Deleting a record makes SurrealDB delete its edges, behind the back of an+/// edge that stays declared. The edge is not written again when the endpoint+/// comes back, since its own declaration did not change (a known gap, as in+/// the Python connector); a full reprocess restores it.+#[tokio::test]+async fn surrealdb_relation_endpoint_deleted_and_restored_when_available() {+ let Some((graph, app, _dir)) = scenario("rel_endpoint").await else {+ return;+ };+ let edge = || vec![("alice", "bob", serde_json::json!({ "w": 1 }))];+ let expected = vec![serde_json::json!(["person:alice", "person:bob", { "w": 1 }])];+ run_knows(&app, vec!["alice", "bob"], edge()).await.unwrap();+ assert_eq!(knows_edges(&graph).await, expected);++ // bob's record goes away while the edge stays declared: SurrealDB deletes+ // the edge with it.+ run_knows(&app, vec!["alice"], edge()).await.unwrap();+ assert!(knows_edges(&graph).await.is_empty());++ // bob comes back; the edge's declaration is unchanged, so it stays gone.+ run_knows(&app, vec!["alice", "bob"], edge()).await.unwrap();+ assert!(knows_edges(&graph).await.is_empty());++ app.update_with_options(+ cocoindex::app::UpdateOptions {+ full_reprocess: true,+ ..Default::default()+ },+ |ctx| async move {+ let person = surrealdb::mount_table_target(&ctx, &GRAPH, "person").await?;+ let knows =+ surrealdb::mount_relation_target(&ctx, &GRAPH, "knows", &person, &person).await?;+ for name in ["alice", "bob"] {+ person.declare_record(&ctx, name, &serde_json::json!({ "name": name }))?;+ }+ knows.declare_relation_record(&ctx, "alice", "bob", &serde_json::json!({ "w": 1 }))+ },+ )+ .await+ .unwrap();+ assert_eq!(knows_edges(&graph).await, expected);+}++// ---------------------------------------------------------------------------+// Record ids and values+// ---------------------------------------------------------------------------++const STRING_IDS: &[&str] = &[+ "plain",+ "with space",+ "a/b:c",+ "back`tick",+ "back\\slash",+ "⟨angle⟩",+ "ünïcødé 😀",+ "123",+ "-7",+ "",+ "550e8400-e29b-41d4-a716-446655440000",+ "x; REMOVE TABLE thing; --",+];++#[tokio::test]+async fn surrealdb_record_id_forms_round_trip_when_available() {+ let Some((graph, app, _dir)) = scenario("ids").await else {+ return;+ };+ async fn run(app: &App, declare: bool) {+ app.run(move |ctx| async move {+ let t = surrealdb::mount_table_target(&ctx, &GRAPH, "thing").await?;+ if declare {+ for (i, id) in STRING_IDS.iter().enumerate() {+ t.declare_record(&ctx, *id, &serde_json::json!({ "i": i }))?;+ }+ for n in [0i64, -1, i64::MIN, i64::MAX] {+ t.declare_record(&ctx, n, &serde_json::json!({ "n": n.to_string() }))?;+ }+ t.declare_record(&ctx, u64::MAX, &serde_json::json!({ "n": "u64max" }))?;+ }+ Ok(())+ })+ .await+ .unwrap();+ }+ run(&app, true).await;+ let expected = STRING_IDS.len() + 5;+ assert_eq!(graph.count("thing").await.unwrap(), expected);+ for (i, id) in STRING_IDS.iter().enumerate() {+ assert_eq!(+ graph.record("thing", *id).await.unwrap(),+ Some(serde_json::json!({ "i": i })),+ "id {id:?}"+ );+ }+ // A numeric string and a number are distinct ids.+ for n in [-1i64, i64::MIN, i64::MAX] {+ assert_eq!(+ graph.record("thing", n).await.unwrap(),+ Some(serde_json::json!({ "n": n.to_string() })),+ "id {n}"+ );+ }+ assert_eq!(+ graph.record("thing", u64::MAX).await.unwrap(),+ Some(serde_json::json!({ "n": "u64max" }))+ );+ // An unchanged re-run writes nothing new; then every id is deleted.+ run(&app, true).await;+ assert_eq!(graph.count("thing").await.unwrap(), expected);+ run(&app, false).await;+ assert_eq!(graph.count("thing").await.unwrap(), 0);+}++#[tokio::test]+async fn surrealdb_payload_values_round_trip_when_available() {+ let Some((graph, app, _dir)) = scenario("values").await else {+ return;+ };+ let mut deep = serde_json::json!({ "leaf": true });+ for i in 0..40 {+ deep = serde_json::json!({ format!("k{i}"): deep, "arr": [i, [i], { "x": i }] });+ }+ let big_array: Vec<f64> = (0..100_000).map(|i| f64::from(i) * 0.5).collect();+ let row = serde_json::json!({+ "b": true,+ "i": i64::MIN,+ "u": i64::MAX,+ "f": -1.5e300,+ "s": "quote ' \" ` \\ \n \t end",+ "null_field": null,+ "empty_s": "",+ "empty_a": [],+ "empty_o": {},+ "mixed": [1, "two", 3.5, null, false, { "k": [] }],+ "deep": deep,+ "big_text": "é".repeat(2 * 1024 * 1024),+ "big_array": big_array,+ });+ let declared = row.clone();+ app.run(move |ctx| async move {+ let t = surrealdb::mount_table_target(&ctx, &GRAPH, "blob").await?;+ t.declare_record(&ctx, "r1", &declared)+ })+ .await+ .unwrap();+ let stored = graph.record("blob", "r1").await.unwrap().unwrap();+ let mut expected = row.as_object().unwrap().clone();+ // `null` is NONE: the field is absent.+ expected.remove("null_field");+ for (name, value) in &expected {+ assert_eq!(stored.get(name), Some(value), "field {name}");+ }+ assert_eq!(stored.as_object().unwrap().len(), expected.len());+}++#[tokio::test]+async fn surrealdb_non_finite_floats_are_rejected_at_declare() {+ let dir = tempfile::tempdir().unwrap();+ let app = Environment::builder()+ .db_path(dir.path().join("lmdb"))+ .build()+ .await+ .unwrap()+ .app("surrealdb_non_finite")+ .await+ .unwrap();+ #[derive(serde::Serialize)]+ struct Doc {+ v: Vec<f32>,+ }+ for bad in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {+ let message = app+ .run(move |ctx| async move {+ let t = surrealdb::declare_table_target(&ctx, &GRAPH, "doc")?;+ t.declare_record(&ctx, "d", &Doc { v: vec![1.0, bad] })+ })+ .await+ .unwrap_err()+ .to_string();+ assert!(message.contains("non-finite"), "{message}");+ }+}++// ---------------------------------------------------------------------------+// Vectors, failures and recovery+// ---------------------------------------------------------------------------++/// A schemafull vector column: a wrong-length or empty vector fails the run,+/// and the next run with valid data writes every record, including those of a+/// chunk that committed before the failing one.+#[tokio::test]+async fn surrealdb_failed_chunk_is_written_on_the_next_run_when_available() {+ let Some((graph, app, _dir)) = scenario("midbatch").await else {+ return;+ };+ async fn run(app: &App, bad: Option<(usize, Vec<f32>)>, value: i64) -> Result<()> {+ app.run(move |ctx| async move {+ let schema = TableSchema::new([+ ("v", ColumnDef::new("int")),+ ("embedding", ColumnDef::new("array<float, 3>")),+ ])?;+ let t = surrealdb::mount_table_target_with_schema(&ctx, &GRAPH, "vec", Some(schema))+ .await?;+ for i in 0..800 {+ let embedding = match &bad {+ Some((at, vector)) if *at == i => vector.clone(),+ _ => vec![i as f32, 0.0, 1.0],+ };+ t.declare_record(+ &ctx,+ i as i64,+ &serde_json::json!({ "v": value, "embedding": embedding }),+ )?;+ }+ Ok(())+ })+ .await?;+ Ok(())+ }+ // Record 700 lands in the second chunk of 500.+ for bad in [vec![1.0, 2.0], vec![1.0, 2.0, 3.0, 4.0], vec![]] {+ let message = run(&app, Some((700, bad.clone())), 1)+ .await+ .unwrap_err()+ .to_string();+ assert!(message.contains("embedding"), "{bad:?}: {message}");+ }+ run(&app, None, 1).await.unwrap();+ assert_eq!(graph.count("vec").await.unwrap(), 800);+ assert_eq!(+ graph.record("vec", 700i64).await.unwrap().unwrap()["embedding"],+ serde_json::json!([700.0, 0.0, 1.0])+ );+ // An update that fails in its second chunk, then succeeds.+ run(&app, Some((650, vec![0.0])), 2).await.unwrap_err();+ run(&app, None, 2).await.unwrap();+ assert!(+ query_json(&graph, "SELECT VALUE <string>id FROM vec WHERE v != 2")+ .await+ .is_empty()+ );+}++/// The vector column's dimension, and the index's dimension and element type,+/// change between runs.+#[tokio::test]+async fn surrealdb_vector_dimension_and_type_change_between_runs_when_available() {+ let Some((graph, app, _dir)) = scenario("vecdim").await else {+ return;+ };+ async fn run(app: &App, dim: usize, vector_type: &'static str) {+ app.run(move |ctx| async move {+ let schema =+ TableSchema::new([("embedding", ColumnDef::new(format!("array<float, {dim}>")))])?;+ let t = surrealdb::mount_table_target_with_schema(&ctx, &GRAPH, "emb", Some(schema))+ .await?;+ t.declare_vector_index(+ &ctx,+ "embedding",+ dim,+ VectorIndexOptions {+ vector_type,+ ..Default::default()+ },+ )?;+ for i in 0..5 {+ let v: Vec<f64> = (0..dim).map(|d| (i * 10 + d) as f64 + 0.25).collect();+ t.declare_record(&ctx, i as i64, &serde_json::json!({ "embedding": v }))?;+ }+ Ok(())+ })+ .await+ .unwrap();+ }+ let index_def = || async {+ let info = query_json(&graph, "INFO FOR TABLE emb").await;+ info[0]["indexes"]["idx_emb__embedding"]+ .as_str()+ .unwrap_or_default()+ .to_string()+ };+ run(&app, 3, "f32").await;+ let def = index_def().await;+ assert!(def.contains("DIMENSION 3"), "{def}");+ run(&app, 4, "f32").await;+ let def = index_def().await;+ assert!(def.contains("DIMENSION 4"), "{def}");+ assert_eq!(+ graph.record("emb", 1i64).await.unwrap().unwrap()["embedding"],+ serde_json::json!([10.25, 11.25, 12.25, 13.25])+ );+ run(&app, 4, "f64").await;+ let def = index_def().await;+ assert!(def.contains("F64"), "{def}");+ // The index answers a nearest-neighbor query.+ assert_eq!(+ query_json(+ &graph,+ "SELECT VALUE <string>id FROM emb WHERE embedding <|1,40|> [20.25, 21.25, 22.25, 23.25]"+ )+ .await,+ vec![serde_json::json!("emb:2")]+ );+}++/// A table removed behind CocoIndex's back is defined again on the next run;+/// its unchanged records come back with a full reprocess.+#[tokio::test]+async fn surrealdb_table_removed_out_of_band_is_restored_when_available() {+ let Some((graph, app, _dir)) = scenario("oob").await else {+ return;+ };+ async fn run(app: &App, full_reprocess: bool) {+ app.update_with_options(+ cocoindex::app::UpdateOptions {+ full_reprocess,+ ..Default::default()+ },+ |ctx| async move {+ let schema = TableSchema::new([("name", ColumnDef::new("string"))])?;+ let t =+ surrealdb::mount_table_target_with_schema(&ctx, &GRAPH, "gone", Some(schema))+ .await?;+ t.declare_record(&ctx, "a", &serde_json::json!({ "name": "A" }))?;+ t.declare_record(&ctx, "b", &serde_json::json!({ "name": "B" }))?;+ Ok(())+ },+ )+ .await+ .unwrap();+ }+ run(&app, false).await;+ assert_eq!(graph.count("gone").await.unwrap(), 2);+ query_json(&graph, "REMOVE TABLE gone").await;+ run(&app, false).await;+ assert!(+ graph+ .table_names()+ .await+ .unwrap()+ .contains(&"gone".to_string())+ );+ assert!(+ graph+ .field_names("gone")+ .await+ .unwrap()+ .contains(&"name".to_string())+ );+ run(&app, true).await;+ assert_eq!(graph.count("gone").await.unwrap(), 2);+}++// ---------------------------------------------------------------------------+// Field groups and chunk boundaries+// ---------------------------------------------------------------------------++/// Declare `player:ann` in the root component, and each field group in a+/// component of its own.+async fn run_players(app: &App, groups: Vec<(&'static str, serde_json::Value)>) -> Result<()> {+ app.run(move |ctx| async move {+ let players = surrealdb::mount_table_target(&ctx, &GRAPH, "player").await?;+ players.declare_record(&ctx, "ann", &serde_json::json!({ "name": "Ann" }))?;+ ctx.mount_each(+ groups,+ |(group, _)| group.to_string(),+ move |ctx, (group, fields)| {+ let players = players.clone();+ async move { players.declare_fields(&ctx, "ann", group, &fields) }+ },+ )+ .await?;+ Ok(())+ })+ .await?;+ Ok(())+}++/// Field groups declared in the same run as their base record: the groups'+/// components commit before the record's, so their writes fail (logged) and+/// land on the next run.+#[tokio::test]+async fn surrealdb_field_groups_declared_with_their_record_land_on_the_next_run_when_available() {+ let Some((graph, app, _dir)) = scenario("fields_same_run").await else {+ return;+ };+ let groups = || {+ vec![+ ("a", serde_json::json!({ "a_only": true })),+ ("b", serde_json::json!({ "score": 2 })),+ ]+ };+ run_players(&app, groups()).await.unwrap();+ run_players(&app, groups()).await.unwrap();+ assert_eq!(+ graph.record("player", "ann").await.unwrap(),+ Some(serde_json::json!({ "name": "Ann", "a_only": true, "score": 2 }))+ );+}++/// A vector index declared on a user-managed table is CocoIndex's, as in+/// Python: it is created, and removed when no longer declared. (Rust created+/// none, yet removed it when undeclared.)+#[tokio::test]+async fn surrealdb_vector_index_on_a_user_managed_table_when_available() {+ let Some((graph, app, _dir)) = scenario("user_index").await else {+ return;+ };+ query_json(&graph, "DEFINE TABLE ext SCHEMALESS").await;+ async fn run(app: &App, declare_index: bool) {+ app.run(move |ctx| async move {+ let t = surrealdb::mount_table_target_with_schema_and_options(+ &ctx,+ &GRAPH,+ "ext",+ None,+ cocoindex::statediff::ManagedTargetOptions::user_managed(),+ )+ .await?;+ if declare_index {+ t.declare_vector_index(&ctx, "embedding", 2, VectorIndexOptions::default())?;+ }+ t.declare_record(&ctx, "e1", &serde_json::json!({ "embedding": [1.0, 0.0] }))+ })+ .await+ .unwrap();+ }+ let idx = "idx_ext__embedding".to_string();+ run(&app, true).await;+ assert!(graph.index_names("ext").await.unwrap().contains(&idx));+ run(&app, false).await;+ assert!(!graph.index_names("ext").await.unwrap().contains(&idx));+ run(&app, true).await;+ app.drop_state().await.unwrap();+ // The table is the user's and stays.+ assert!(+ graph+ .table_names()+ .await+ .unwrap()+ .contains(&"ext".to_string())+ );+ // `drop_state` leaves a user-managed table's contents alone, its records+ // and the index alike.+ assert!(graph.index_names("ext").await.unwrap().contains(&idx));+ assert_eq!(graph.count("ext").await.unwrap(), 1);+}++/// Batches at the transaction chunk boundaries (500 actions per chunk).+#[tokio::test]+async fn surrealdb_batches_at_chunk_boundaries_when_available() {+ let Some((graph, app, _dir)) = scenario("chunks").await else {+ return;+ };+ for n in [499usize, 500, 501, 1000, 1001] {+ app.run(move |ctx| async move {+ let t = surrealdb::mount_table_target(&ctx, &GRAPH, "chunk").await?;+ let r = surrealdb::mount_relation_target(&ctx, &GRAPH, "next", &t, &t).await?;+ for i in 0..n {+ t.declare_record(&ctx, i as i64, &serde_json::json!({ "n": n }))?;+ if i > 0 {+ r.declare_relation(&ctx, (i - 1) as i64, i as i64)?;+ }+ }+ Ok(())+ })+ .await+ .unwrap();+ assert_eq!(graph.count("chunk").await.unwrap(), n);+ assert_eq!(graph.count("next").await.unwrap(), n - 1);+ assert!(+ query_json(+ &graph,+ &format!("SELECT VALUE <string>id FROM chunk WHERE n != {n}")+ )+ .await+ .is_empty(),+ "n={n}"+ );+ }+}++/// A large declaration, written twice (soak). Run with+/// `SURREALDB_URL=127.0.0.1:8000 cargo test -p cocoindex --features surrealdb,testing --test surrealdb_scenarios -- --ignored`.+#[tokio::test]+#[ignore = "soak: needs a SurrealDB server and takes tens of seconds"]+async fn surrealdb_very_large_batch_soak() {+ let Some((graph, app, _dir)) = scenario("soak").await else {+ return;+ };+ let n = 50_000usize;+ for round in 0..2i64 {+ let started = std::time::Instant::now();+ app.run(move |ctx| async move {+ let t = surrealdb::mount_table_target(&ctx, &GRAPH, "soak").await?;+ let r = surrealdb::mount_relation_target(&ctx, &GRAPH, "soak_next", &t, &t).await?;+ for i in 0..n {+ t.declare_record(+ &ctx,+ i as i64,+ &serde_json::json!({ "round": round, "text": format!("record {i}") }),+ )?;+ if i > 0 {+ r.declare_relation_record(+ &ctx,+ (i - 1) as i64,+ i as i64,+ &serde_json::json!({ "round": round }),+ )?;+ }+ }+ Ok(())+ })+ .await+ .unwrap();+ eprintln!("round {round}: {:?}", started.elapsed());+ assert_eq!(graph.count("soak").await.unwrap(), n);+ assert_eq!(graph.count("soak_next").await.unwrap(), n - 1);+ assert!(+ query_json(+ &graph,+ &format!("SELECT VALUE <string>id FROM soak_next WHERE round != {round}")+ )+ .await+ .is_empty()+ );+ }+}