heliosdb-nano 3.23.2

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//! Query cancellation support
//!
//! Provides cooperative query cancellation using tokens that can be
//! checked at various points during query execution.

use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, RwLock};
use std::time::Duration;
use chrono::{DateTime, Utc};

use crate::{Result, Error};

// =============================================================================
// CancellationToken - Thread-safe cancellation signal
// =============================================================================

/// A cooperative cancellation token
///
/// Executors should periodically check `is_cancelled()` and return early
/// if the query has been cancelled.
#[derive(Debug, Clone)]
pub struct CancellationToken {
    /// Whether cancellation has been requested
    cancelled: Arc<AtomicBool>,
    /// Reason for cancellation (if any)
    reason: Arc<RwLock<Option<String>>>,
}

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

impl CancellationToken {
    /// Create a new cancellation token
    pub fn new() -> Self {
        Self {
            cancelled: Arc::new(AtomicBool::new(false)),
            reason: Arc::new(RwLock::new(None)),
        }
    }

    /// Check if cancellation has been requested
    #[inline]
    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(Ordering::Relaxed)
    }

    /// Request cancellation
    pub fn cancel(&self) {
        self.cancelled.store(true, Ordering::SeqCst);
    }

    /// Request cancellation with a reason
    pub fn cancel_with_reason(&self, reason: impl Into<String>) {
        if let Ok(mut r) = self.reason.write() {
            *r = Some(reason.into());
        }
        self.cancelled.store(true, Ordering::SeqCst);
    }

    /// Get the cancellation reason (if any)
    pub fn cancellation_reason(&self) -> Option<String> {
        self.reason.read().ok().and_then(|r| r.clone())
    }

    /// Check if cancelled and return an error if so
    pub fn check(&self) -> Result<()> {
        if self.is_cancelled() {
            let reason = self.cancellation_reason()
                .unwrap_or_else(|| "Query cancelled".to_string());
            Err(Error::QueryCancelled(reason))
        } else {
            Ok(())
        }
    }

    /// Create a child token that is cancelled when either parent or child is cancelled
    pub fn child(&self) -> CancellationToken {
        // Child shares the same cancelled flag but can have its own reason
        Self {
            cancelled: self.cancelled.clone(),
            reason: Arc::new(RwLock::new(None)),
        }
    }
}

// =============================================================================
// RunningQuery - Metadata for a running query
// =============================================================================

/// State of a running query
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QueryState {
    /// Query is being parsed/planned
    Planning,
    /// Query is executing
    Executing,
    /// Query is cancelled but still cleaning up
    Cancelling,
    /// Query completed successfully
    Completed,
    /// Query failed with an error
    Failed,
    /// Query was cancelled
    Cancelled,
}

impl std::fmt::Display for QueryState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            QueryState::Planning => write!(f, "planning"),
            QueryState::Executing => write!(f, "executing"),
            QueryState::Cancelling => write!(f, "cancelling"),
            QueryState::Completed => write!(f, "completed"),
            QueryState::Failed => write!(f, "failed"),
            QueryState::Cancelled => write!(f, "cancelled"),
        }
    }
}

/// Information about a running query
#[derive(Debug, Clone)]
pub struct RunningQuery {
    /// Unique query ID
    pub query_id: u64,
    /// SQL text (possibly truncated)
    pub sql: String,
    /// Session/user who submitted the query
    pub session_id: Option<u64>,
    /// User name
    pub user_name: String,
    /// Database name
    pub database: String,
    /// Query state
    pub state: QueryState,
    /// When the query started
    pub started_at: DateTime<Utc>,
    /// Elapsed time
    pub elapsed: Duration,
    /// Rows processed so far
    pub rows_processed: u64,
    /// Whether the query can be cancelled
    pub cancellable: bool,
    /// Cancellation token (for internal use)
    #[doc(hidden)]
    pub cancel_token: CancellationToken,
}

impl RunningQuery {
    /// Update elapsed time
    pub fn update_elapsed(&mut self) {
        let now = Utc::now();
        self.elapsed = (now - self.started_at)
            .to_std()
            .unwrap_or(Duration::ZERO);
    }
}

// =============================================================================
// QueryRegistry - Track all running queries
// =============================================================================

/// Registry of all running queries in the system
///
/// Provides thread-safe tracking and cancellation of queries.
#[derive(Debug)]
pub struct QueryRegistry {
    /// Running queries indexed by query_id
    queries: RwLock<HashMap<u64, RunningQuery>>,
    /// Next query ID
    next_id: AtomicU64,
    /// Maximum number of queries to track (prevents memory exhaustion)
    max_tracked: usize,
    /// Default query timeout
    default_timeout: Option<Duration>,
}

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

impl QueryRegistry {
    /// Create a new query registry
    pub fn new() -> Self {
        Self {
            queries: RwLock::new(HashMap::new()),
            next_id: AtomicU64::new(1),
            max_tracked: 10000,
            default_timeout: None,
        }
    }

    /// Create a query registry with a default timeout
    pub fn with_timeout(timeout: Duration) -> Self {
        Self {
            queries: RwLock::new(HashMap::new()),
            next_id: AtomicU64::new(1),
            max_tracked: 10000,
            default_timeout: Some(timeout),
        }
    }

    /// Set the maximum number of tracked queries
    pub fn set_max_tracked(&mut self, max: usize) {
        self.max_tracked = max;
    }

    /// Register a new query and get a cancellation token
    pub fn register_query(
        &self,
        sql: &str,
        user_name: &str,
        database: &str,
        session_id: Option<u64>,
    ) -> (u64, CancellationToken) {
        let query_id = self.next_id.fetch_add(1, Ordering::SeqCst);
        let cancel_token = CancellationToken::new();

        // Truncate SQL for storage (keep first 1000 chars)
        let truncated_sql = if sql.len() > 1000 {
            format!("{}...", &sql[..1000])
        } else {
            sql.to_string()
        };

        let query = RunningQuery {
            query_id,
            sql: truncated_sql,
            session_id,
            user_name: user_name.to_string(),
            database: database.to_string(),
            state: QueryState::Planning,
            started_at: Utc::now(),
            elapsed: Duration::ZERO,
            rows_processed: 0,
            cancellable: true,
            cancel_token: cancel_token.clone(),
        };

        if let Ok(mut queries) = self.queries.write() {
            // Clean up old completed queries if we're at capacity
            if queries.len() >= self.max_tracked {
                let completed: Vec<u64> = queries
                    .iter()
                    .filter(|(_, q)| matches!(
                        q.state,
                        QueryState::Completed | QueryState::Failed | QueryState::Cancelled
                    ))
                    .map(|(id, _)| *id)
                    .collect();

                for id in completed.into_iter().take(queries.len() / 4) {
                    queries.remove(&id);
                }
            }
            queries.insert(query_id, query);
        }

        (query_id, cancel_token)
    }

    /// Update query state
    pub fn update_state(&self, query_id: u64, state: QueryState) {
        if let Ok(mut queries) = self.queries.write() {
            if let Some(query) = queries.get_mut(&query_id) {
                query.state = state;
                query.update_elapsed();
            }
        }
    }

    /// Update rows processed
    pub fn update_rows_processed(&self, query_id: u64, rows: u64) {
        if let Ok(mut queries) = self.queries.write() {
            if let Some(query) = queries.get_mut(&query_id) {
                query.rows_processed = rows;
            }
        }
    }

    /// Mark query as completed
    pub fn complete_query(&self, query_id: u64) {
        self.update_state(query_id, QueryState::Completed);
    }

    /// Mark query as failed
    pub fn fail_query(&self, query_id: u64) {
        self.update_state(query_id, QueryState::Failed);
    }

    /// Unregister a query (removes it from tracking)
    pub fn unregister_query(&self, query_id: u64) {
        if let Ok(mut queries) = self.queries.write() {
            queries.remove(&query_id);
        }
    }

    /// Cancel a specific query by ID
    pub fn cancel_query(&self, query_id: u64) -> Result<bool> {
        if let Ok(mut queries) = self.queries.write() {
            if let Some(query) = queries.get_mut(&query_id) {
                if !query.cancellable {
                    return Err(Error::Generic(format!(
                        "Query {} cannot be cancelled",
                        query_id
                    )));
                }

                query.cancel_token.cancel_with_reason("Cancelled by user request");
                query.state = QueryState::Cancelling;
                return Ok(true);
            }
        }
        Ok(false) // Query not found
    }

    /// Cancel a query with a specific reason
    pub fn cancel_query_with_reason(&self, query_id: u64, reason: &str) -> Result<bool> {
        if let Ok(mut queries) = self.queries.write() {
            if let Some(query) = queries.get_mut(&query_id) {
                if !query.cancellable {
                    return Err(Error::Generic(format!(
                        "Query {} cannot be cancelled",
                        query_id
                    )));
                }

                query.cancel_token.cancel_with_reason(reason);
                query.state = QueryState::Cancelling;
                return Ok(true);
            }
        }
        Ok(false)
    }

    /// Cancel all queries for a specific session
    pub fn cancel_session_queries(&self, session_id: u64) -> usize {
        let mut cancelled = 0;
        if let Ok(mut queries) = self.queries.write() {
            for query in queries.values_mut() {
                if query.session_id == Some(session_id) && query.cancellable {
                    if matches!(query.state, QueryState::Planning | QueryState::Executing) {
                        query.cancel_token.cancel_with_reason("Session terminated");
                        query.state = QueryState::Cancelling;
                        cancelled += 1;
                    }
                }
            }
        }
        cancelled
    }

    /// Cancel all queries that have exceeded their timeout
    pub fn cancel_timed_out_queries(&self, timeout: Duration) -> usize {
        let mut cancelled = 0;
        if let Ok(mut queries) = self.queries.write() {
            for query in queries.values_mut() {
                if query.cancellable
                    && matches!(query.state, QueryState::Planning | QueryState::Executing)
                {
                    query.update_elapsed();
                    if query.elapsed > timeout {
                        query.cancel_token.cancel_with_reason(format!(
                            "Query timeout exceeded ({:.1}s)",
                            timeout.as_secs_f64()
                        ));
                        query.state = QueryState::Cancelling;
                        cancelled += 1;
                    }
                }
            }
        }
        cancelled
    }

    /// Get information about a specific query
    pub fn get_query(&self, query_id: u64) -> Option<RunningQuery> {
        self.queries.read().ok()?.get(&query_id).cloned()
    }

    /// List all running queries
    pub fn list_running_queries(&self) -> Vec<RunningQuery> {
        if let Ok(queries) = self.queries.read() {
            queries
                .values()
                .filter(|q| matches!(q.state, QueryState::Planning | QueryState::Executing | QueryState::Cancelling))
                .cloned()
                .collect()
        } else {
            Vec::new()
        }
    }

    /// List all queries (including completed)
    pub fn list_all_queries(&self) -> Vec<RunningQuery> {
        if let Ok(queries) = self.queries.read() {
            queries.values().cloned().collect()
        } else {
            Vec::new()
        }
    }

    /// Get count of running queries
    pub fn running_count(&self) -> usize {
        if let Ok(queries) = self.queries.read() {
            queries
                .values()
                .filter(|q| matches!(q.state, QueryState::Planning | QueryState::Executing))
                .count()
        } else {
            0
        }
    }

    /// Get count of running queries for a specific user
    pub fn user_running_count(&self, user_name: &str) -> usize {
        if let Ok(queries) = self.queries.read() {
            queries
                .values()
                .filter(|q| {
                    q.user_name == user_name
                        && matches!(q.state, QueryState::Planning | QueryState::Executing)
                })
                .count()
        } else {
            0
        }
    }

    /// Clean up old completed queries
    pub fn cleanup_completed(&self, max_age: Duration) {
        if let Ok(mut queries) = self.queries.write() {
            let cutoff = Utc::now() - chrono::Duration::from_std(max_age).unwrap_or(chrono::Duration::hours(1));
            queries.retain(|_, q| {
                // Keep running queries
                if matches!(q.state, QueryState::Planning | QueryState::Executing | QueryState::Cancelling) {
                    return true;
                }
                // Keep recent completed queries
                q.started_at > cutoff
            });
        }
    }

    /// Get default timeout
    pub fn default_timeout(&self) -> Option<Duration> {
        self.default_timeout
    }
}

// =============================================================================
// QueryGuard - RAII guard for automatic query cleanup
// =============================================================================

/// RAII guard that automatically unregisters a query when dropped
pub struct QueryGuard<'a> {
    registry: &'a QueryRegistry,
    query_id: u64,
    auto_cleanup: bool,
}

impl<'a> QueryGuard<'a> {
    /// Create a new query guard
    pub fn new(registry: &'a QueryRegistry, query_id: u64) -> Self {
        Self {
            registry,
            query_id,
            auto_cleanup: true,
        }
    }

    /// Get the query ID
    pub fn query_id(&self) -> u64 {
        self.query_id
    }

    /// Disable automatic cleanup on drop
    pub fn disable_cleanup(&mut self) {
        self.auto_cleanup = false;
    }

    /// Mark the query as completed
    pub fn complete(mut self) {
        self.registry.complete_query(self.query_id);
        self.auto_cleanup = false;
    }

    /// Mark the query as failed
    pub fn fail(mut self) {
        self.registry.fail_query(self.query_id);
        self.auto_cleanup = false;
    }
}

impl Drop for QueryGuard<'_> {
    fn drop(&mut self) {
        if self.auto_cleanup {
            // If we're dropping without explicit completion, assume failure
            self.registry.fail_query(self.query_id);
        }
    }
}

// =============================================================================
// Timeout background task
// =============================================================================

/// Start a background task that cancels queries exceeding their timeout
pub fn start_timeout_checker(registry: Arc<QueryRegistry>, check_interval: Duration, timeout: Duration) {
    tokio::spawn(async move {
        let mut interval = tokio::time::interval(check_interval);
        loop {
            interval.tick().await;
            let cancelled = registry.cancel_timed_out_queries(timeout);
            if cancelled > 0 {
                tracing::info!("Cancelled {} timed out queries", cancelled);
            }
        }
    });
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;
    use std::time::Duration;

    #[test]
    fn test_cancellation_token() {
        let token = CancellationToken::new();
        assert!(!token.is_cancelled());

        token.cancel();
        assert!(token.is_cancelled());
    }

    #[test]
    fn test_cancellation_token_with_reason() {
        let token = CancellationToken::new();
        token.cancel_with_reason("Timeout exceeded");

        assert!(token.is_cancelled());
        assert_eq!(token.cancellation_reason(), Some("Timeout exceeded".to_string()));
    }

    #[test]
    fn test_cancellation_token_check() {
        let token = CancellationToken::new();
        assert!(token.check().is_ok());

        token.cancel();
        assert!(token.check().is_err());
    }

    #[test]
    fn test_child_token() {
        let parent = CancellationToken::new();
        let child = parent.child();

        assert!(!child.is_cancelled());

        parent.cancel();
        assert!(child.is_cancelled());
    }

    #[test]
    fn test_query_registry() {
        let registry = QueryRegistry::new();

        let (id1, token1) = registry.register_query("SELECT 1", "alice", "test", Some(1));
        let (id2, _token2) = registry.register_query("SELECT 2", "bob", "test", Some(2));

        assert_eq!(registry.running_count(), 2);

        // Cancel first query
        assert!(registry.cancel_query(id1).unwrap());
        assert!(token1.is_cancelled());

        // Complete second query
        registry.complete_query(id2);
        assert_eq!(registry.running_count(), 0);
    }

    #[test]
    fn test_cancel_session_queries() {
        let registry = QueryRegistry::new();

        registry.register_query("SELECT 1", "alice", "test", Some(1));
        registry.register_query("SELECT 2", "alice", "test", Some(1));
        registry.register_query("SELECT 3", "bob", "test", Some(2));

        let cancelled = registry.cancel_session_queries(1);
        assert_eq!(cancelled, 2);
    }

    #[test]
    fn test_list_running_queries() {
        let registry = QueryRegistry::new();

        let (id1, _) = registry.register_query("SELECT 1", "alice", "test", None);
        registry.register_query("SELECT 2", "bob", "test", None);

        registry.complete_query(id1);

        let running = registry.list_running_queries();
        assert_eq!(running.len(), 1);
        assert_eq!(running[0].user_name, "bob");
    }

    #[test]
    fn test_query_guard() {
        let registry = QueryRegistry::new();
        let (id, _) = registry.register_query("SELECT 1", "alice", "test", None);

        {
            let guard = QueryGuard::new(&registry, id);
            // Guard dropped here, query should be marked as failed
            drop(guard);
        }

        let query = registry.get_query(id).unwrap();
        assert_eq!(query.state, QueryState::Failed);
    }

    #[test]
    fn test_query_guard_complete() {
        let registry = QueryRegistry::new();
        let (id, _) = registry.register_query("SELECT 1", "alice", "test", None);

        let guard = QueryGuard::new(&registry, id);
        guard.complete();

        let query = registry.get_query(id).unwrap();
        assert_eq!(query.state, QueryState::Completed);
    }
}