nopaldb 0.4.34

High-performance graph database with ACID transactions, MVCC time-travel, and Arrow analytics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// - COMMIT: Operational space (execution)
use crate::error::{NopalError, Result};

// src/query/sketch_manager.rs
//
// Sketch Manager - Manages conceptual operations (SKETCH/COMMIT)
//
// Philosophy:
// Sketches separate "thinking" from "executing". They allow users to:
// 1. Define complex operations conceptually
// 2. Preview their impact before execution
// 3. Iterate safely without modifying the graph
// 4. Commit only when ready
//
// This implements the "Bow-Tie Cognitive Model":
// - SKETCH: Conceptual space (no persistence)
// - PREVIEW: Reasoning space (analysis)
use std::collections::HashMap;
use std::time::SystemTime;
use crate::query::nql::parser::ast::{Statement, DeleteStmt, UpdateStmt, AddStmt, Query};
use crate::query::nql::executor::{Executor, UpdateResult};
use crate::query::nql::executor::result::QueryResult;
use crate::Transaction;

// ═══════════════════════════════════════════════════════════════
// SKETCH MANAGER
// ═══════════════════════════════════════════════════════════════

/// Manages sketches (conceptual operations)
///
/// Sketches are named operations that can be:
/// - Previewed (dry-run to see impact)
/// - Committed (executed on the graph)
/// - Discarded (removed without execution)
///
/// Example workflow:
/// ```nql
/// -- Define sketch
/// sketch cleanup =
///   delete (u:User)
///   where u.last_login < timestamp("2020-01-01")
///
/// -- Preview impact
/// find count(*) from cleanup
///
/// -- Execute
/// commit cleanup
/// ```
pub struct SketchManager {
    /// Active sketches (name -> sketch)
    sketches: HashMap<String, Sketch>,
}

/// A sketch - conceptual operation
///
/// Sketches can be:
/// - Query (read-only, safe to execute)
/// - Delete (needs preview)
/// - Update (needs preview)
/// - Add (safe to execute)
#[derive(Debug, Clone)]
pub struct Sketch {
    /// Sketch name
    pub name: String,

    /// The statement to execute
    pub statement: Statement,

    /// When the sketch was created
    pub created_at: SystemTime,

    /// Optional description
    pub description: Option<String>,

    /// Cached preview result (for efficiency)
    preview_cache: Option<SketchPreview>,
}

/// Preview result for a sketch
///
/// Different operations have different preview outputs:
/// - Query: Shows the result (read-only)
/// - Delete: Shows count of nodes/edges that would be deleted
/// - Update: Shows count of nodes/edges that would be updated + sample
/// - Add: Shows count of nodes/edges that would be added
#[derive(Debug, Clone)]
pub struct SketchPreview {
    /// Preview type
    pub preview_type: PreviewType,

    /// When preview was generated
    pub generated_at: SystemTime,
}

#[derive(Debug, Clone)]
pub enum PreviewType {
    /// Query result (read-only)
    QueryResult(QueryResult),

    /// Delete preview
    DeletePreview {
        nodes_affected: usize,
        edges_affected: usize,
        sample_nodes: Vec<String>, // Sample node IDs
    },

    /// Update preview
    UpdatePreview {
        nodes_affected: usize,
        edges_affected: usize,
        sample_changes: Vec<UpdateSample>,
    },

    /// Add preview
    AddPreview {
        nodes_to_add: usize,
        edges_to_add: usize,
    },
    Other(String),
}

#[derive(Debug, Clone)]
pub struct UpdateSample {
    pub node_id: String,
    pub property: String,
    pub old_value: String,
    pub new_value: String,
}

// ═══════════════════════════════════════════════════════════════
// IMPLEMENTATION
// ═══════════════════════════════════════════════════════════════

impl SketchManager {
    /// Create a new sketch manager
    pub fn new() -> Self {
        Self {
            sketches: HashMap::new(),
        }
    }

    /// Define a new sketch
    ///
    /// If a sketch with the same name exists, it will be replaced.
    ///
    /// # Arguments
    /// * `name` - Unique name for the sketch
    /// * `statement` - The statement to execute when committed
    ///
    /// # Returns
    /// Ok(()) if sketch was defined successfully
    /// Err if sketch is invalid
    pub fn define(
        &mut self,
        name: String,
        statement: Statement,
        description: Option<String>,
    ) -> Result<()> {
        // Validate sketch
        self.validate_sketch(&statement)?;

        // Add sketch
        let sketch = Sketch {
            name: name.clone(),
            statement,
            created_at: SystemTime::now(),
            description,
            preview_cache: None,
        };

        // Store sketch
        self.sketches.insert(name, sketch);

        Ok(())
    }

    /// Preview a sketch (dry-run)
    ///
    /// Shows what would happen if the sketch were committed,
    /// without actually modifying the graph.
    ///
    /// # Arguments
    /// * `name` - Name of the sketch to preview
    /// * `executor` - Query executor (for executing queries)
    ///
    /// # Returns
    /// Preview result showing impact of the sketch
    pub async fn preview(
        &mut self,
        name: &str,
        executor: &mut Executor<'_>,
    ) -> Result<SketchPreview> {
        // Check cache first (immutable borrow)
        if let Some(sketch) = self.sketches.get(name)
            && let Some(cached) = &sketch.preview_cache {
                return Ok(cached.clone());
        }

        // Clone statement to avoid borrow conflicts
        let statement = self.sketches.get(name)
            .ok_or_else(|| NopalError::SketchNotFound(name.to_string()))?
            .statement
            .clone();

        // Generate preview based on statement type
        let preview = match &statement {
            Statement::Query(query) => {
                self.preview_query(query, executor).await?
            }
            Statement::Delete(delete) => {
                self.preview_delete(delete, executor).await?
            }
            Statement::Update(update) => {
                self.preview_update(update, executor).await?
            }
            Statement::Add(add) => {
                self.preview_add(add)?
            }
            Statement::CreateIndex(_) => {
                SketchPreview{
                    preview_type: PreviewType::Other("CREATE INDEX operation".to_string()),
                    generated_at: SystemTime::now(),
                }
            }
            Statement::DropIndex(_) => {
                SketchPreview {
                    preview_type: PreviewType::Other("DROP INDEX operation".to_string()),
                    generated_at: SystemTime::now(),
                }
            }
            Statement::Explain(_) => {
                SketchPreview {
                    preview_type: PreviewType::Other("EXPLAIN query plan".to_string()),
                    generated_at: SystemTime::now(),
                }
            }
            Statement::Profile(_) => {
                SketchPreview {
                    preview_type: PreviewType::Other("PROFILE query execution".to_string()),
                    generated_at: SystemTime::now(),
                }
            }
            Statement::Sketch(_) => {
                return Err(NopalError::InvalidSketch(
                    "Cannot sketch a sketch".to_string()
                ));
            }
            Statement::Commit(_) => {
                return Err(NopalError::InvalidSketch(
                    "Cannot sketch a commit".to_string()
                ));
            }
        };

        // Cache preview (mutable borrow after match)
        if let Some(sketch) = self.sketches.get_mut(name) {
            sketch.preview_cache = Some(preview.clone());
        }

        Ok(preview)
    }

    /// Commit a sketch (execute it)
    ///
    /// Executes the sketch and modifies the graph.
    /// The sketch is removed from the manager after commit.
    ///
    /// # Arguments
    /// * `name` - Name of the sketch to commit
    /// * `executor` - Query executor
    /// * `tx` - Transaction to execute within
    ///
    /// # Returns
    /// Result of the execution
    pub async fn commit(
        &mut self,
        name: &str,
        executor: &mut Executor<'_>,
        _tx: &mut Transaction,
    ) -> Result<CommitResult> {
        let sketch = self.sketches.remove(name)
            .ok_or_else(|| NopalError::SketchNotFound(name.to_string()))?;

        // Execute based on statement type
        let result = match sketch.statement {
            Statement::Query(query) => {
                let result = executor.execute(query.clone()).await?;
                CommitResult::Query(result)
            }
            Statement::Delete(_delete) => {
                // TODO: Implement execute_delete in executor
                return Err(NopalError::query_error("DELETE not yet implemented in executor"));
            }
            Statement::Update(_update) => {
                // TODO: Implement execute_update in executor
                return Err(NopalError::query_error("UPDATE not yet implemented in executor"));
            }
            Statement::CreateIndex(_) => {
                return Err(NopalError::custom("CreateIndex not supported in sketches yet"));
            }
            Statement::DropIndex(_) => {
                return Err(NopalError::custom("DropIndex not supported in sketches yet"));
            }
            Statement::Explain(_) => {
                return Err(NopalError::custom("Explain not supported in sketches yet"));
            }
            Statement::Profile(_) => {
                return Err(NopalError::custom("Profile not supported in sketches yet"));
            }
            Statement::Add(_add) => {
                // TODO: Implement execute_add in executor
                return Err(NopalError::query_error("ADD not yet implemented in executor"));
            }
            Statement::Sketch(_) | Statement::Commit(_) => {
                return Err(NopalError::InvalidCommit(
                    "Cannot commit a sketch or commit statement".to_string()
                ));
            }
        };

        Ok(result)
    }

    /// Discard a sketch without executing
    pub fn discard(&mut self, name: &str) -> Result<()> {
        self.sketches.remove(name)
            .ok_or_else(|| NopalError::SketchNotFound(name.to_string()))?;
        Ok(())
    }

    /// List all active sketches
    pub fn list(&self) -> Vec<&Sketch> {
        self.sketches.values().collect()
    }

    /// Get a sketch by name
    pub fn get(&self, name: &str) -> Option<&Sketch> {
        self.sketches.get(name)
    }

    /// Clear all sketches
    pub fn clear(&mut self) {
        self.sketches.clear();
    }
}

// ═══════════════════════════════════════════════════════════════
// PREVIEW IMPLEMENTATIONS
// ═══════════════════════════════════════════════════════════════

impl SketchManager {
    /// Preview a query (just execute it read-only)
    async fn preview_query(
        &self,
        query: &Query,
        executor: &mut Executor<'_>,
    ) -> Result<SketchPreview> {
        let result = executor.execute(query.clone()).await?;

        Ok(SketchPreview {
            preview_type: PreviewType::QueryResult(result),
            generated_at: SystemTime::now(),
        })
    }

    /// Preview a delete operation
    async fn preview_delete(
        &self,
        delete: &DeleteStmt,
        executor: &mut Executor<'_>,
    ) -> Result<SketchPreview> {
        // Match pattern
        let matches = executor.match_pattern(&delete.pattern).await?;

        // Apply WHERE filter
        let to_delete = if let Some(where_clause) = &delete.filter {
            executor.filter_matches(matches, &where_clause.condition)?
        } else {
            matches
        };

        // Apply LIMIT
        let to_delete: Vec<_> = if let Some(limit_clause) = &delete.limit {
            to_delete.into_iter().take(limit_clause.limit).collect()
        } else {
            to_delete
        };

        // Count and sample
        let (nodes_affected, edges_affected) = executor.count_elements(&to_delete);
        let sample_nodes = executor.sample_node_ids(&to_delete, 10);

        Ok(SketchPreview {
            preview_type: PreviewType::DeletePreview {
                nodes_affected,
                edges_affected,
                sample_nodes,
            },
            generated_at: SystemTime::now(),
        })
    }

    /// Preview an update operation
    async fn preview_update(
        &self,
        update: &UpdateStmt,
        executor: &mut Executor<'_>,
    ) -> Result<SketchPreview> {
        // Match pattern
        let matches = executor.match_pattern(&update.pattern).await?;

        // Apply WHERE filter
        let to_update = if let Some(where_clause) = &update.filter {
            executor.filter_matches(matches, &where_clause.condition)?
        } else {
            matches
        };

        // Apply LIMIT
        let to_update: Vec<_> = if let Some(limit_clause) = &update.limit {
            to_update.into_iter().take(limit_clause.limit).collect()
        } else {
            to_update
        };

        // Count and sample
        let (nodes_affected, edges_affected) = executor.count_elements(&to_update);
        let sample_changes = executor.sample_updates(
            &to_update,
            &update.assignments,
            10
        );

        Ok(SketchPreview {
            preview_type: PreviewType::UpdatePreview {
                nodes_affected,
                edges_affected,
                sample_changes,
            },
            generated_at: SystemTime::now(),
        })
    }

    /// Preview a add operation
    fn preview_add(&self, add: &AddStmt) -> Result<SketchPreview> {
        // Count nodes and edges in pattern
        let mut nodes_to_add = 0;
        let mut edges_to_add = 0;

        for element in &add.pattern.elements {
            match element {
                crate::query::nql::parser::ast::PatternElement::Node(_) => {
                    nodes_to_add += 1;
                }
                crate::query::nql::parser::ast::PatternElement::Relationship(_) => {
                    edges_to_add += 1;
                }
            }
        }

        Ok(SketchPreview {
            preview_type: PreviewType::AddPreview {
                nodes_to_add,
                edges_to_add,
            },
            generated_at: SystemTime::now(),
        })
    }
}

// ═══════════════════════════════════════════════════════════════
// VALIDATION
// ═══════════════════════════════════════════════════════════════

impl SketchManager {
    /// Validate a sketch before defining it
    fn validate_sketch(&self, statement: &Statement) -> Result<()> {
        match statement {
            Statement::Query(_) => Ok(()),
            Statement::Delete(_) => Ok(()),
            Statement::Update(_) => Ok(()),
            Statement::CreateIndex(_) => Ok(()),
            Statement::DropIndex(_) => Ok(()),
            Statement::Explain(_) => Ok(()),
            Statement::Profile(_) => Ok(()),
            Statement::Add(_) => Ok(()),
            Statement::Sketch(_) => {
                Err(NopalError::InvalidSketch(
                    "Cannot sketch a sketch (no nested sketches)".to_string()
                ))
            }
            Statement::Commit(_) => {
                Err(NopalError::InvalidSketch(
                    "Cannot sketch a commit".to_string()
                ))
            }
        }
    }
}

// ═══════════════════════════════════════════════════════════════
// COMMIT RESULTS
// ═══════════════════════════════════════════════════════════════

/// Result of committing a sketch
#[derive(Debug)]
pub enum CommitResult {
    Query(QueryResult),
    Delete(DeleteResult),
    Update(UpdateResult),
    Add(AddResult),
}

#[derive(Debug)]
pub struct DeleteResult {
    pub nodes_deleted: usize,
    pub edges_deleted: usize,
}

#[derive(Debug)]
pub struct AddResult {
    pub nodes_added: usize,
    pub edges_added: usize,
}

// ═══════════════════════════════════════════════════════════════
// DISPLAY IMPLEMENTATIONS
// ═══════════════════════════════════════════════════════════════

impl std::fmt::Display for SketchPreview {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.preview_type {
            PreviewType::QueryResult(result) => {
                write!(f, "Query result: {} rows", result.rows.len())
            }
            PreviewType::DeletePreview { nodes_affected, edges_affected, .. } => {
                write!(
                    f,
                    "Will delete: {} nodes, {} edges",
                    nodes_affected, edges_affected
                )
            }
            PreviewType::UpdatePreview { nodes_affected, edges_affected, .. } => {
                write!(
                    f,
                    "Will update: {} nodes, {} edges",
                    nodes_affected, edges_affected
                )
            }
            PreviewType::AddPreview { nodes_to_add, edges_to_add } => {
                write!(
                    f,
                    "Will add: {} nodes, {} edges",
                    nodes_to_add, edges_to_add
                )
            }
            //TODO: revisar esta parte
            PreviewType::Other(desc) => {
                write!(f, "Preview: {}", desc)
            }
        }
    }
}

impl Default for SketchManager {
    fn default() -> Self {
        Self::new()
    }
}

// ═══════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sketch_manager_basics() {
        let manager = SketchManager::new();

        // Initially empty
        assert_eq!(manager.list().len(), 0);

        // TODO: Add more tests when we have full Statement constructors
    }

    #[test]
    fn test_sketch_validation() {
        let _manager = SketchManager::new();

        // Nested sketches should be invalid
        // TODO: Test when we have Statement constructors
    }
}