cqlite-cli 0.11.0

Command-line interface for CQLite — read Apache Cassandra 5.0 SSTables without a cluster
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
// REPL Session Management
//
// Manages the state and context of a REPL session, including database connections,
// current keyspace, configuration, and session persistence.

use super::{ReplError, ReplResult};
use crate::config::Config;
use anyhow::Result;
use cqlite_core::{Database, QueryResult};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Current state of the REPL session
#[derive(Debug, Clone, PartialEq)]
pub enum SessionState {
    /// Session is initializing
    Initializing,
    /// Session is ready for commands
    Ready,
    /// Session is executing a command
    Executing,
    /// Session is in error state
    Error(String),
    /// Session is shutting down
    Shutdown,
}

/// REPL session context and state
pub struct ReplSession {
    /// Database connection
    database: Arc<Database>,
    /// Session configuration
    config: Config,
    /// Database file path
    db_path: PathBuf,
    /// Current session state
    state: SessionState,
    /// Current keyspace (if any)
    current_keyspace: Option<String>,
    /// Data directory for SSTable files
    data_dir: Option<PathBuf>,
    /// Session variables
    variables: HashMap<String, String>,
    /// Connection metadata
    connection_info: ConnectionInfo,
    /// Performance metrics
    metrics: SessionMetrics,
    /// Schema registry for coverage reporting
    schema_registry:
        Option<Arc<tokio::sync::RwLock<cqlite_core::schema::registry::SchemaRegistry>>>,
}

/// Connection information
#[derive(Debug, Clone)]
pub struct ConnectionInfo {
    /// Database version
    pub version: String,
    /// Connection timestamp
    pub connected_at: std::time::SystemTime,
    /// Last activity timestamp
    pub last_activity: std::time::SystemTime,
    /// Total queries executed
    pub queries_executed: u64,
    /// Total errors encountered
    pub errors_count: u64,
}

/// Session performance metrics
#[derive(Debug, Clone, Default)]
pub struct SessionMetrics {
    /// Total execution time (microseconds)
    pub total_execution_time_us: u64,
    /// Query count by type
    pub query_counts: HashMap<String, u64>,
    /// Average query time (microseconds)
    pub avg_query_time_us: f64,
    /// Memory usage (bytes)
    pub memory_usage_bytes: u64,
    /// Cache statistics
    pub cache_hits: u64,
    pub cache_misses: u64,
}

impl ReplSession {
    /// Create a new REPL session
    pub fn new(db_path: &Path, config: Config, database: Database) -> ReplResult<Self> {
        let connection_info = ConnectionInfo {
            version: env!("CARGO_PKG_VERSION").to_string(),
            connected_at: std::time::SystemTime::now(),
            last_activity: std::time::SystemTime::now(),
            queries_executed: 0,
            errors_count: 0,
        };

        Ok(Self {
            database: Arc::new(database),
            config,
            db_path: db_path.to_path_buf(),
            state: SessionState::Initializing,
            current_keyspace: None,
            data_dir: None,
            variables: HashMap::new(),
            connection_info,
            metrics: SessionMetrics::default(),
            schema_registry: None,
        })
    }

    /// Initialize the session
    pub async fn initialize(&mut self) -> ReplResult<()> {
        self.state = SessionState::Ready;
        self.connection_info.last_activity = std::time::SystemTime::now();

        // Try to load default keyspace from config
        if let Some(default_keyspace) = self.config.default_keyspace.clone() {
            if !default_keyspace.is_empty() {
                let _ = self.use_keyspace(&default_keyspace).await;
            }
        }

        // Set data directory if configured
        if let Some(ref data_dir) = self.config.data_directory {
            if !data_dir.as_os_str().is_empty() {
                self.data_dir = Some(data_dir.clone());
            }
        }

        Ok(())
    }

    /// Get current session state
    pub fn state(&self) -> &SessionState {
        &self.state
    }

    /// Get current keyspace
    pub fn current_keyspace(&self) -> Option<&String> {
        self.current_keyspace.as_ref()
    }

    /// Get database path
    pub fn db_path(&self) -> &Path {
        &self.db_path
    }

    /// Get reference to the database (for status metrics collection)
    pub fn database(&self) -> Option<&Database> {
        Some(&self.database)
    }

    /// Get data directory
    pub fn data_dir(&self) -> Option<&Path> {
        self.data_dir.as_deref()
    }

    /// Set data directory
    pub fn set_data_dir(&mut self, path: Option<PathBuf>) {
        self.data_dir = path;
    }

    /// Switch to a keyspace
    pub async fn use_keyspace(&mut self, keyspace: &str) -> ReplResult<()> {
        self.state = SessionState::Executing;
        self.connection_info.last_activity = std::time::SystemTime::now();

        // Validate keyspace exists by querying system tables
        let query = format!(
            "SELECT keyspace_name FROM system.keyspaces WHERE keyspace_name = '{}'",
            keyspace
        );

        match self.database.execute(&query).await {
            Ok(result) => {
                if result.rows.is_empty() {
                    self.state = SessionState::Ready;
                    return Err(ReplError::Session(format!(
                        "Keyspace '{}' not found",
                        keyspace
                    )));
                }

                self.current_keyspace = Some(keyspace.to_string());
                self.state = SessionState::Ready;
                Ok(())
            }
            Err(e) => {
                // If system query fails, allow setting anyway (might be valid in data directory)
                self.current_keyspace = Some(keyspace.to_string());
                self.state = SessionState::Ready;
                self.connection_info.errors_count += 1;

                // Log warning but don't fail
                log::warn!("Could not verify keyspace '{}': {}", keyspace, e);
                Ok(())
            }
        }
    }

    /// Execute a query
    pub async fn execute_query(&mut self, query: &str) -> ReplResult<QueryResult> {
        self.state = SessionState::Executing;
        self.connection_info.last_activity = std::time::SystemTime::now();

        let start_time = std::time::Instant::now();

        match self.database.execute(query).await {
            Ok(result) => {
                let elapsed = start_time.elapsed();
                self.update_metrics(query, elapsed, true);
                self.connection_info.queries_executed += 1;
                self.state = SessionState::Ready;
                Ok(result)
            }
            Err(e) => {
                let elapsed = start_time.elapsed();
                self.update_metrics(query, elapsed, false);
                self.connection_info.errors_count += 1;
                self.state = SessionState::Ready;
                Err(ReplError::Database(e.into()))
            }
        }
    }

    /// List available tables
    pub async fn list_tables(&mut self) -> ReplResult<Vec<String>> {
        self.state = SessionState::Executing;

        let query = if let Some(ref keyspace) = self.current_keyspace {
            format!(
                "SELECT table_name FROM system.tables WHERE keyspace_name = '{}'",
                keyspace
            )
        } else {
            "SELECT keyspace_name, table_name FROM system.tables WHERE keyspace_name != 'system'"
                .to_string()
        };

        match self.database.execute(&query).await {
            Ok(result) => {
                self.state = SessionState::Ready;
                let mut tables = Vec::new();

                for row in &result.rows {
                    if let Some(ref _keyspace) = self.current_keyspace {
                        // Just table names for current keyspace
                        if let Some(table_name) = row.get("table_name") {
                            tables.push(table_name.to_string());
                        }
                    } else {
                        // Qualified names for all keyspaces
                        if let (Some(keyspace_name), Some(table_name)) =
                            (row.get("keyspace_name"), row.get("table_name"))
                        {
                            tables.push(format!("{}.{}", keyspace_name, table_name));
                        }
                    }
                }

                Ok(tables)
            }
            Err(e) => {
                self.state = SessionState::Ready;

                // Fallback to data directory scanning if system query fails
                if let Some(ref data_dir) = self.data_dir {
                    match self.scan_data_directory_tables(data_dir).await {
                        Ok(tables) => Ok(tables),
                        Err(_) => Err(ReplError::Database(e.into())),
                    }
                } else {
                    Err(ReplError::Database(e.into()))
                }
            }
        }
    }

    /// List available keyspaces
    pub async fn list_keyspaces(&mut self) -> ReplResult<Vec<String>> {
        self.state = SessionState::Executing;

        let query = "SELECT keyspace_name FROM system.keyspaces";

        match self.database.execute(query).await {
            Ok(result) => {
                self.state = SessionState::Ready;
                let mut keyspaces = Vec::new();

                for row in &result.rows {
                    if let Some(keyspace_name) = row.get("keyspace_name") {
                        keyspaces.push(keyspace_name.to_string());
                    }
                }

                Ok(keyspaces)
            }
            Err(e) => {
                self.state = SessionState::Ready;

                // Fallback to data directory scanning
                if let Some(ref data_dir) = self.data_dir {
                    match self.scan_data_directory_keyspaces(data_dir).await {
                        Ok(keyspaces) => Ok(keyspaces),
                        Err(_) => Err(ReplError::Database(e.into())),
                    }
                } else {
                    Err(ReplError::Database(e.into()))
                }
            }
        }
    }

    /// Describe an object (table, keyspace, etc.)
    pub async fn describe_object(&mut self, object_name: &str) -> ReplResult<String> {
        self.state = SessionState::Executing;

        // Parse object name (could be keyspace.table or just table)
        let (keyspace, table) = if object_name.contains('.') {
            let parts: Vec<&str> = object_name.split('.').collect();
            if parts.len() == 2 {
                (Some(parts[0]), parts[1])
            } else {
                (self.current_keyspace.as_deref(), object_name)
            }
        } else {
            (self.current_keyspace.as_deref(), object_name)
        };

        if let Some(ks) = keyspace {
            match self.describe_table(ks, table).await {
                Ok(description) => {
                    self.state = SessionState::Ready;
                    Ok(description)
                }
                Err(e) => {
                    self.state = SessionState::Ready;
                    Err(e)
                }
            }
        } else {
            self.state = SessionState::Ready;
            Err(ReplError::Session(
                "No keyspace specified and no current keyspace set".to_string(),
            ))
        }
    }

    /// Describe a specific table
    async fn describe_table(&self, keyspace: &str, table: &str) -> ReplResult<String> {
        let query = format!(
            "SELECT column_name, type, kind FROM system.columns WHERE keyspace_name = '{}' AND table_name = '{}' ORDER BY position",
            keyspace, table
        );

        match self.database.execute(&query).await {
            Ok(result) => {
                if result.rows.is_empty() {
                    return Err(ReplError::Session(format!(
                        "Table '{}.{}' not found",
                        keyspace, table
                    )));
                }

                let mut description = String::new();
                description.push_str(&format!("Table: {}.{}\n", keyspace, table));
                description.push_str("Columns:\n");

                for row in &result.rows {
                    if let (Some(col_name), Some(col_type), Some(col_kind)) =
                        (row.get("column_name"), row.get("type"), row.get("kind"))
                    {
                        let kind_desc = match col_kind.to_string().as_str() {
                            "partition_key" => " (PARTITION KEY)",
                            "clustering" => " (CLUSTERING KEY)",
                            "regular" => "",
                            _ => "",
                        };
                        description
                            .push_str(&format!("  {} {}{}\n", col_name, col_type, kind_desc));
                    }
                }

                Ok(description)
            }
            Err(e) => Err(ReplError::Database(e.into())),
        }
    }

    /// Get session variable
    pub fn get_variable(&self, name: &str) -> Option<&String> {
        self.variables.get(name)
    }

    /// Set session variable
    pub fn set_variable(&mut self, name: String, value: String) {
        self.variables.insert(name, value);
    }

    /// Get connection information
    pub fn connection_info(&self) -> &ConnectionInfo {
        &self.connection_info
    }

    /// Get session metrics
    pub fn metrics(&self) -> &SessionMetrics {
        &self.metrics
    }

    /// Update session metrics
    fn update_metrics(&mut self, query: &str, elapsed: std::time::Duration, success: bool) {
        let elapsed_us = elapsed.as_micros() as u64;
        self.metrics.total_execution_time_us += elapsed_us;

        // Update average
        let total_queries = self.connection_info.queries_executed + if success { 1 } else { 0 };
        if total_queries > 0 {
            self.metrics.avg_query_time_us =
                self.metrics.total_execution_time_us as f64 / total_queries as f64;
        }

        // Categorize query type
        let query_type = self.categorize_query(query);
        *self.metrics.query_counts.entry(query_type).or_insert(0) += 1;
    }

    /// Categorize query for metrics
    fn categorize_query(&self, query: &str) -> String {
        let upper = query.to_uppercase();
        let trimmed = upper.trim();

        if trimmed.starts_with("SELECT") {
            "SELECT".to_string()
        } else if trimmed.starts_with("INSERT") {
            "INSERT".to_string()
        } else if trimmed.starts_with("UPDATE") {
            "UPDATE".to_string()
        } else if trimmed.starts_with("DELETE") {
            "DELETE".to_string()
        } else if trimmed.starts_with("CREATE") {
            "CREATE".to_string()
        } else if trimmed.starts_with("ALTER") {
            "ALTER".to_string()
        } else if trimmed.starts_with("DROP") {
            "DROP".to_string()
        } else if trimmed.starts_with("DESCRIBE") {
            "DESCRIBE".to_string()
        } else {
            "OTHER".to_string()
        }
    }

    /// Scan data directory for tables (fallback when system queries fail)
    async fn scan_data_directory_tables(&self, data_dir: &Path) -> Result<Vec<String>> {
        use std::fs;

        let mut tables = Vec::new();

        if let Some(ref keyspace) = self.current_keyspace {
            // Scan specific keyspace directory
            let keyspace_dir = data_dir.join(keyspace);
            if keyspace_dir.exists() {
                for entry in fs::read_dir(&keyspace_dir)? {
                    let entry = entry?;
                    if entry.path().is_dir() {
                        if let Some(dir_name) = entry.file_name().to_str() {
                            if let Some(table_name) = self.extract_table_name(dir_name) {
                                tables.push(table_name);
                            }
                        }
                    }
                }
            }
        } else {
            // Scan all keyspace directories
            for entry in fs::read_dir(data_dir)? {
                let entry = entry?;
                if entry.path().is_dir() {
                    if let Some(keyspace_name) = entry.file_name().to_str() {
                        if keyspace_name.starts_with('.') || keyspace_name == "system" {
                            continue;
                        }

                        let keyspace_dir = entry.path();
                        for table_entry in fs::read_dir(&keyspace_dir)? {
                            let table_entry = table_entry?;
                            if table_entry.path().is_dir() {
                                if let Some(dir_name) = table_entry.file_name().to_str() {
                                    if let Some(table_name) = self.extract_table_name(dir_name) {
                                        tables.push(format!("{}.{}", keyspace_name, table_name));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(tables)
    }

    /// Scan data directory for keyspaces
    async fn scan_data_directory_keyspaces(&self, data_dir: &Path) -> Result<Vec<String>> {
        use std::fs;

        let mut keyspaces = Vec::new();

        for entry in fs::read_dir(data_dir)? {
            let entry = entry?;
            if entry.path().is_dir() {
                if let Some(name) = entry.file_name().to_str() {
                    if !name.starts_with('.') && name != "system" {
                        keyspaces.push(name.to_string());
                    }
                }
            }
        }

        keyspaces.sort();
        Ok(keyspaces)
    }

    /// Extract table name from SSTable directory name
    fn extract_table_name(&self, dir_name: &str) -> Option<String> {
        // Expected format: tablename-uuid
        if let Some(dash_pos) = dir_name.find('-') {
            let table_part = &dir_name[..dash_pos];
            if !table_part.is_empty() && table_part.chars().all(|c| c.is_alphanumeric() || c == '_')
            {
                return Some(table_part.to_string());
            }
        }
        None
    }

    /// Shutdown the session
    pub async fn shutdown(&mut self) -> ReplResult<()> {
        self.state = SessionState::Shutdown;

        // Perform cleanup tasks
        self.save_session_state().await?;

        Ok(())
    }

    /// Save session state for persistence
    async fn save_session_state(&self) -> ReplResult<()> {
        // This would save session state to a file or database
        // For now, just log some statistics
        log::info!(
            "Session ending. Queries executed: {}, Errors: {}",
            self.connection_info.queries_executed,
            self.connection_info.errors_count
        );
        Ok(())
    }

    /// Export session metrics as a report
    pub fn export_metrics(&self) -> String {
        let mut report = String::new();

        report.push_str("=== CQLite Session Report ===\n");
        report.push_str(&format!("Database: {}\n", self.db_path.display()));
        report.push_str(&format!(
            "Session Duration: {:?}\n",
            self.connection_info
                .last_activity
                .duration_since(self.connection_info.connected_at)
                .unwrap_or_default()
        ));
        report.push_str(&format!(
            "Queries Executed: {}\n",
            self.connection_info.queries_executed
        ));
        report.push_str(&format!("Errors: {}\n", self.connection_info.errors_count));
        report.push_str(&format!(
            "Average Query Time: {:.2}ms\n",
            self.metrics.avg_query_time_us / 1000.0
        ));

        if !self.metrics.query_counts.is_empty() {
            report.push_str("\nQuery Types:\n");
            for (query_type, count) in &self.metrics.query_counts {
                report.push_str(&format!("  {}: {}\n", query_type, count));
            }
        }

        if let Some(ref keyspace) = self.current_keyspace {
            report.push_str(&format!("Current Keyspace: {}\n", keyspace));
        }

        report
    }

    /// Replace the Database instance with a new one
    ///
    /// Used when rebuilding the database after ingestion changes.
    /// The old Database will be dropped when the Arc refcount reaches zero.
    pub fn replace_database(&mut self, new_database: Database) -> ReplResult<()> {
        self.database = Arc::new(new_database);
        Ok(())
    }

    /// Get reference to the session configuration
    pub fn config(&self) -> &Config {
        &self.config
    }

    /// Get reference to SchemaRegistry for coverage reporting
    pub fn schema_registry(
        &self,
    ) -> Option<Arc<tokio::sync::RwLock<cqlite_core::schema::registry::SchemaRegistry>>> {
        self.schema_registry.clone()
    }

    /// Set the schema registry
    pub fn set_schema_registry(
        &mut self,
        registry: Option<Arc<tokio::sync::RwLock<cqlite_core::schema::registry::SchemaRegistry>>>,
    ) {
        self.schema_registry = registry;
    }
}