|
| 1 | +//! Experimental LanceDB parallel commands for CRUD validation |
| 2 | +//! |
| 3 | +//! This module implements parallel CRUD operations running both Turso and LanceDB |
| 4 | +//! simultaneously for comparison and validation. These commands are experimental |
| 5 | +//! and enabled only via the EXPERIMENTAL_USE_LANCEDB environment variable. |
| 6 | +
|
| 7 | +use crate::datastore::lance::LanceDataStore; |
| 8 | +use nodespace_core::{Node, NodeService}; |
| 9 | +use serde::{Deserialize, Serialize}; |
| 10 | +use std::sync::Arc; |
| 11 | +use std::time::Instant; |
| 12 | +use tauri::State; |
| 13 | +use tokio::sync::RwLock; |
| 14 | + |
| 15 | +/// Comparison result for side-by-side validation |
| 16 | +#[derive(Debug, Serialize)] |
| 17 | +#[serde(rename_all = "camelCase")] |
| 18 | +pub struct ComparisonResult { |
| 19 | + /// Time taken by Turso operation in milliseconds |
| 20 | + pub turso_time_ms: f64, |
| 21 | + /// Time taken by LanceDB operation in milliseconds |
| 22 | + pub lance_time_ms: f64, |
| 23 | + /// Whether results match between backends |
| 24 | + pub results_match: bool, |
| 25 | + /// Result from Turso (serialized as JSON) |
| 26 | + pub turso_result: serde_json::Value, |
| 27 | + /// Result from LanceDB (serialized as JSON) |
| 28 | + pub lance_result: serde_json::Value, |
| 29 | + /// List of differences found (if any) |
| 30 | + pub differences: Vec<String>, |
| 31 | +} |
| 32 | + |
| 33 | +/// Structured error for experimental commands |
| 34 | +#[derive(Debug, Serialize)] |
| 35 | +pub struct ExperimentalError { |
| 36 | + pub message: String, |
| 37 | + pub code: String, |
| 38 | +} |
| 39 | + |
| 40 | +impl From<nodespace_core::NodeServiceError> for ExperimentalError { |
| 41 | + fn from(err: nodespace_core::NodeServiceError) -> Self { |
| 42 | + ExperimentalError { |
| 43 | + message: err.to_string(), |
| 44 | + code: "NODE_SERVICE_ERROR".to_string(), |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl From<crate::datastore::lance::LanceDBError> for ExperimentalError { |
| 50 | + fn from(err: crate::datastore::lance::LanceDBError) -> Self { |
| 51 | + ExperimentalError { |
| 52 | + message: err.to_string(), |
| 53 | + code: "LANCEDB_ERROR".to_string(), |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Experimental state wrapper holding optional LanceDB instance |
| 59 | +pub struct ExperimentalState { |
| 60 | + pub lance_db: Option<Arc<RwLock<LanceDataStore>>>, |
| 61 | +} |
| 62 | + |
| 63 | +/// Create a node using LanceDB (experimental) |
| 64 | +/// |
| 65 | +/// This bypasses Turso entirely and uses only LanceDB for storage. |
| 66 | +/// Requires EXPERIMENTAL_USE_LANCEDB=true environment variable. |
| 67 | +#[tauri::command] |
| 68 | +pub async fn create_node_lance_experimental( |
| 69 | + experimental_state: State<'_, ExperimentalState>, |
| 70 | + node: Node, |
| 71 | +) -> Result<String, ExperimentalError> { |
| 72 | + if let Some(lance_db) = &experimental_state.lance_db { |
| 73 | + let db = lance_db.read().await; |
| 74 | + let id = db |
| 75 | + .create_node(node) |
| 76 | + .await |
| 77 | + .map_err(ExperimentalError::from)?; |
| 78 | + Ok(id) |
| 79 | + } else { |
| 80 | + Err(ExperimentalError { |
| 81 | + message: "LanceDB not enabled. Set EXPERIMENTAL_USE_LANCEDB=true".to_string(), |
| 82 | + code: "LANCEDB_NOT_ENABLED".to_string(), |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// Read a node using LanceDB (experimental) |
| 88 | +#[tauri::command] |
| 89 | +pub async fn read_node_lance_experimental( |
| 90 | + experimental_state: State<'_, ExperimentalState>, |
| 91 | + id: String, |
| 92 | +) -> Result<Option<Node>, ExperimentalError> { |
| 93 | + if let Some(lance_db) = &experimental_state.lance_db { |
| 94 | + let db = lance_db.read().await; |
| 95 | + let node = db.read_node(&id).await.map_err(ExperimentalError::from)?; |
| 96 | + Ok(node) |
| 97 | + } else { |
| 98 | + Err(ExperimentalError { |
| 99 | + message: "LanceDB not enabled".to_string(), |
| 100 | + code: "LANCEDB_NOT_ENABLED".to_string(), |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/// Update a node using LanceDB (experimental) |
| 106 | +#[tauri::command] |
| 107 | +pub async fn update_node_lance_experimental( |
| 108 | + experimental_state: State<'_, ExperimentalState>, |
| 109 | + node: Node, |
| 110 | +) -> Result<(), ExperimentalError> { |
| 111 | + if let Some(lance_db) = &experimental_state.lance_db { |
| 112 | + let db = lance_db.read().await; |
| 113 | + db.update_node(node) |
| 114 | + .await |
| 115 | + .map_err(ExperimentalError::from)?; |
| 116 | + Ok(()) |
| 117 | + } else { |
| 118 | + Err(ExperimentalError { |
| 119 | + message: "LanceDB not enabled".to_string(), |
| 120 | + code: "LANCEDB_NOT_ENABLED".to_string(), |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/// Delete a node using LanceDB (experimental) |
| 126 | +#[tauri::command] |
| 127 | +pub async fn delete_node_lance_experimental( |
| 128 | + experimental_state: State<'_, ExperimentalState>, |
| 129 | + id: String, |
| 130 | +) -> Result<(), ExperimentalError> { |
| 131 | + if let Some(lance_db) = &experimental_state.lance_db { |
| 132 | + let db = lance_db.read().await; |
| 133 | + db.delete_node(&id).await.map_err(ExperimentalError::from)?; |
| 134 | + Ok(()) |
| 135 | + } else { |
| 136 | + Err(ExperimentalError { |
| 137 | + message: "LanceDB not enabled".to_string(), |
| 138 | + code: "LANCEDB_NOT_ENABLED".to_string(), |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/// Operation types for comparison |
| 144 | +#[derive(Debug, Deserialize)] |
| 145 | +#[serde(rename_all = "lowercase")] |
| 146 | +pub enum Operation { |
| 147 | + Create, |
| 148 | + Read, |
| 149 | + Update, |
| 150 | + Delete, |
| 151 | +} |
| 152 | + |
| 153 | +/// Compare backends by running the same operation on both |
| 154 | +/// |
| 155 | +/// This is the primary validation command - it runs the same CRUD operation |
| 156 | +/// on both Turso and LanceDB, measures performance, and compares results. |
| 157 | +#[tauri::command] |
| 158 | +pub async fn compare_backends( |
| 159 | + node_service: State<'_, NodeService>, |
| 160 | + experimental_state: State<'_, ExperimentalState>, |
| 161 | + operation: Operation, |
| 162 | + node_data: Option<Node>, |
| 163 | + node_id: Option<String>, |
| 164 | +) -> Result<ComparisonResult, ExperimentalError> { |
| 165 | + if experimental_state.lance_db.is_none() { |
| 166 | + return Err(ExperimentalError { |
| 167 | + message: "LanceDB not enabled for comparison".to_string(), |
| 168 | + code: "LANCEDB_NOT_ENABLED".to_string(), |
| 169 | + }); |
| 170 | + } |
| 171 | + |
| 172 | + let lance_db = experimental_state.lance_db.as_ref().unwrap(); |
| 173 | + |
| 174 | + // Execute on Turso |
| 175 | + let turso_start = Instant::now(); |
| 176 | + let turso_result = match operation { |
| 177 | + Operation::Create => { |
| 178 | + let node = node_data |
| 179 | + .as_ref() |
| 180 | + .ok_or_else(|| ExperimentalError { |
| 181 | + message: "Node data required for create".to_string(), |
| 182 | + code: "MISSING_NODE_DATA".to_string(), |
| 183 | + })? |
| 184 | + .clone(); |
| 185 | + let id = node_service |
| 186 | + .create_node(node) |
| 187 | + .await |
| 188 | + .map_err(ExperimentalError::from)?; |
| 189 | + serde_json::json!({ "id": id }) |
| 190 | + } |
| 191 | + Operation::Read => { |
| 192 | + let id = node_id.as_ref().ok_or_else(|| ExperimentalError { |
| 193 | + message: "Node ID required for read".to_string(), |
| 194 | + code: "MISSING_NODE_ID".to_string(), |
| 195 | + })?; |
| 196 | + let node = node_service |
| 197 | + .get_node(id) |
| 198 | + .await |
| 199 | + .map_err(ExperimentalError::from)?; |
| 200 | + serde_json::to_value(&node).unwrap_or(serde_json::Value::Null) |
| 201 | + } |
| 202 | + Operation::Update => { |
| 203 | + let node = node_data |
| 204 | + .as_ref() |
| 205 | + .ok_or_else(|| ExperimentalError { |
| 206 | + message: "Node data required for update".to_string(), |
| 207 | + code: "MISSING_NODE_DATA".to_string(), |
| 208 | + })? |
| 209 | + .clone(); |
| 210 | + // Convert Node to NodeUpdate |
| 211 | + // NodeUpdate uses Option<Option<T>> for nullable fields |
| 212 | + let update = nodespace_core::NodeUpdate { |
| 213 | + node_type: Some(node.node_type.clone()), |
| 214 | + content: Some(node.content.clone()), |
| 215 | + parent_id: Some(node.parent_id.clone()), |
| 216 | + container_node_id: Some(node.container_node_id.clone()), |
| 217 | + before_sibling_id: Some(node.before_sibling_id.clone()), |
| 218 | + properties: Some(node.properties.clone()), |
| 219 | + embedding_vector: Some(node.embedding_vector.clone()), |
| 220 | + }; |
| 221 | + node_service |
| 222 | + .update_node(&node.id, update) |
| 223 | + .await |
| 224 | + .map_err(ExperimentalError::from)?; |
| 225 | + serde_json::json!({ "updated": true }) |
| 226 | + } |
| 227 | + Operation::Delete => { |
| 228 | + let id = node_id.as_ref().ok_or_else(|| ExperimentalError { |
| 229 | + message: "Node ID required for delete".to_string(), |
| 230 | + code: "MISSING_NODE_ID".to_string(), |
| 231 | + })?; |
| 232 | + node_service |
| 233 | + .delete_node(id) |
| 234 | + .await |
| 235 | + .map_err(ExperimentalError::from)?; |
| 236 | + serde_json::json!({ "deleted": true }) |
| 237 | + } |
| 238 | + }; |
| 239 | + let turso_duration = turso_start.elapsed(); |
| 240 | + |
| 241 | + // Execute on LanceDB |
| 242 | + let lance_start = Instant::now(); |
| 243 | + let lance_result = { |
| 244 | + let db = lance_db.read().await; |
| 245 | + match operation { |
| 246 | + Operation::Create => { |
| 247 | + let node = node_data.unwrap(); |
| 248 | + let id = db |
| 249 | + .create_node(node) |
| 250 | + .await |
| 251 | + .map_err(ExperimentalError::from)?; |
| 252 | + serde_json::json!({ "id": id }) |
| 253 | + } |
| 254 | + Operation::Read => { |
| 255 | + let id = node_id.as_ref().unwrap(); |
| 256 | + let node = db.read_node(id).await.map_err(ExperimentalError::from)?; |
| 257 | + serde_json::to_value(&node).unwrap_or(serde_json::Value::Null) |
| 258 | + } |
| 259 | + Operation::Update => { |
| 260 | + let node = node_data.unwrap(); |
| 261 | + db.update_node(node) |
| 262 | + .await |
| 263 | + .map_err(ExperimentalError::from)?; |
| 264 | + serde_json::json!({ "updated": true }) |
| 265 | + } |
| 266 | + Operation::Delete => { |
| 267 | + let id = node_id.as_ref().unwrap(); |
| 268 | + db.delete_node(id).await.map_err(ExperimentalError::from)?; |
| 269 | + serde_json::json!({ "deleted": true }) |
| 270 | + } |
| 271 | + } |
| 272 | + }; |
| 273 | + let lance_duration = lance_start.elapsed(); |
| 274 | + |
| 275 | + // Compare results |
| 276 | + let results_match = turso_result == lance_result; |
| 277 | + let differences = if !results_match { |
| 278 | + vec![format!( |
| 279 | + "Results differ: Turso={}, Lance={}", |
| 280 | + turso_result, lance_result |
| 281 | + )] |
| 282 | + } else { |
| 283 | + vec![] |
| 284 | + }; |
| 285 | + |
| 286 | + Ok(ComparisonResult { |
| 287 | + turso_time_ms: turso_duration.as_secs_f64() * 1000.0, |
| 288 | + lance_time_ms: lance_duration.as_secs_f64() * 1000.0, |
| 289 | + results_match, |
| 290 | + turso_result, |
| 291 | + lance_result, |
| 292 | + differences, |
| 293 | + }) |
| 294 | +} |
0 commit comments