cqlite-core 0.11.0

Core engine for CQLite — read Apache Cassandra 5.0 SSTables locally 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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
//! Schema-Aware SSTable Reader
//!
//! This module provides a schema-aware wrapper around the SSTable reader that uses
//! the SchemaParser for all value parsing operations instead of type guessing.
//! It requires a complete schema definition and uses proper comparators for
//! partition and clustering keys.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use crate::{
    error::{Error, Result},
    parser::header::CassandraVersion,
    platform::Platform,
    schema::{
        parser::SchemaParser,
        registry::{ParsingContext, SchemaRegistry},
        CqlType, TableSchema,
    },
    storage::sstable::{
        format_detector::SSTableFormat,
        reader::{SSTableReader, SSTableReaderStats},
    },
    types::{ComparatorType, Value},
    Config, RowKey,
};

/// Schema-aware SSTable reader that uses SchemaParser for all value parsing
#[derive(Debug)]
#[allow(dead_code)]
pub struct SchemaAwareReader {
    /// Path to the SSTable file
    file_path: PathBuf,

    /// Underlying SSTable reader (for low-level file operations)
    reader: SSTableReader,

    /// Schema parser for value parsing
    schema_parser: SchemaParser,

    /// Parsing context with schema and comparators
    context: ParsingContext,

    /// Detected SSTable format
    format: SSTableFormat,

    /// Cassandra version from header
    version: CassandraVersion,

    /// Platform abstraction
    platform: Arc<Platform>,
}

/// Configuration for schema-aware reading
#[derive(Debug, Clone)]
pub struct SchemaAwareReaderConfig {
    /// Whether to validate schema completeness on creation
    pub validate_schema_completeness: bool,

    /// Whether to fail if schema is missing required fields
    pub strict_schema_validation: bool,

    /// Whether to enable format-specific optimizations
    pub enable_format_optimizations: bool,

    /// Whether to cache parsed values
    pub cache_parsed_values: bool,
}

impl Default for SchemaAwareReaderConfig {
    fn default() -> Self {
        Self {
            validate_schema_completeness: true,
            strict_schema_validation: true,
            enable_format_optimizations: true,
            cache_parsed_values: true,
        }
    }
}

/// Statistics for schema-aware reading operations
#[derive(Debug, Clone)]
pub struct SchemaAwareStats {
    /// Base SSTable reader statistics
    pub base_stats: SSTableReaderStats,

    /// Number of values parsed using schema
    pub schema_parsed_values: u64,

    /// Number of partition keys parsed
    pub partition_keys_parsed: u64,

    /// Number of clustering keys parsed
    pub clustering_keys_parsed: u64,

    /// Number of column values parsed
    pub column_values_parsed: u64,

    /// Number of parse errors encountered
    pub parse_errors: u64,

    /// Number of format-specific optimizations used
    pub format_optimizations_used: u64,
}

impl SchemaAwareReader {
    /// Create a new schema-aware reader with complete schema validation
    pub async fn new(
        path: &Path,
        schema: TableSchema,
        schema_registry: Arc<SchemaRegistry>,
        config: &Config,
        platform: Arc<Platform>,
    ) -> Result<Self> {
        Self::new_with_config(
            path,
            schema,
            schema_registry,
            config,
            platform,
            SchemaAwareReaderConfig::default(),
        )
        .await
    }

    /// Create a new schema-aware reader with custom configuration
    pub async fn new_with_config(
        path: &Path,
        schema: TableSchema,
        schema_registry: Arc<SchemaRegistry>,
        config: &Config,
        platform: Arc<Platform>,
        reader_config: SchemaAwareReaderConfig,
    ) -> Result<Self> {
        // Validate schema before proceeding
        if reader_config.validate_schema_completeness {
            Self::validate_schema_completeness(&schema)?;
        }

        // Create parsing context
        let context = Self::create_parsing_context(&schema, &schema_registry)?;

        // Validate context completeness if strict validation is enabled
        if reader_config.strict_schema_validation && !context.is_complete() {
            return Err(Error::Schema(format!(
                "Incomplete parsing context for table {}.{}: missing schema or comparators",
                schema.keyspace, schema.table
            )));
        }

        // Create schema parser
        let schema_parser = SchemaParser::new(context.clone())?;

        // Open underlying SSTable reader
        let reader = SSTableReader::open(path, config, platform.clone()).await?;

        // Detect SSTable format from filename
        let format = Self::detect_format(&reader)?;

        // Get Cassandra version from header
        let version = reader.cassandra_version();

        Ok(Self {
            file_path: path.to_path_buf(),
            reader,
            schema_parser,
            context,
            format,
            version,
            platform,
        })
    }

    /// Validate that the schema contains all required fields for schema-aware parsing
    pub fn validate_schema_completeness(schema: &TableSchema) -> Result<()> {
        // Must have at least one partition key
        if schema.partition_keys.is_empty() {
            return Err(Error::Schema(format!(
                "Schema for table {}.{} must have at least one partition key",
                schema.keyspace, schema.table
            )));
        }

        // All partition keys must have valid types
        for (idx, key) in schema.partition_keys.iter().enumerate() {
            CqlType::parse(&key.data_type).map_err(|e| {
                Error::Schema(format!(
                    "Invalid partition key type '{}' at position {} in {}.{}: {}",
                    key.data_type, idx, schema.keyspace, schema.table, e
                ))
            })?;
        }

        // All clustering keys must have valid types
        for (idx, key) in schema.clustering_keys.iter().enumerate() {
            CqlType::parse(&key.data_type).map_err(|e| {
                Error::Schema(format!(
                    "Invalid clustering key type '{}' at position {} in {}.{}: {}",
                    key.data_type, idx, schema.keyspace, schema.table, e
                ))
            })?;
        }

        // All columns must have valid types
        for column in &schema.columns {
            CqlType::parse(&column.data_type).map_err(|e| {
                Error::Schema(format!(
                    "Invalid column type '{}' for column '{}' in {}.{}: {}",
                    column.data_type, column.name, schema.keyspace, schema.table, e
                ))
            })?;
        }

        // Validate positions are contiguous for partition keys
        let mut positions: Vec<usize> = schema.partition_keys.iter().map(|k| k.position).collect();
        positions.sort();
        for (expected, &actual) in positions.iter().enumerate() {
            if expected != actual {
                return Err(Error::Schema(format!(
                    "Non-contiguous partition key positions in {}.{}: expected {}, found {}",
                    schema.keyspace, schema.table, expected, actual
                )));
            }
        }

        // Validate positions are contiguous for clustering keys (if any)
        if !schema.clustering_keys.is_empty() {
            let mut positions: Vec<usize> =
                schema.clustering_keys.iter().map(|k| k.position).collect();
            positions.sort();
            for (expected, &actual) in positions.iter().enumerate() {
                if expected != actual {
                    return Err(Error::Schema(format!(
                        "Non-contiguous clustering key positions in {}.{}: expected {}, found {}",
                        schema.keyspace, schema.table, expected, actual
                    )));
                }
            }
        }

        Ok(())
    }

    /// Create parsing context from schema and registry
    pub fn create_parsing_context(
        schema: &TableSchema,
        _registry: &SchemaRegistry,
    ) -> Result<ParsingContext> {
        // Get partition key comparators
        let partition_comparators = schema.get_partition_key_comparators()?;

        // Get clustering key comparators
        let clustering_comparators = schema.get_clustering_key_comparators()?;

        // Get all column comparators
        let column_comparators = schema.get_all_comparators()?;

        Ok(ParsingContext {
            schema: schema.clone(),
            partition_comparators,
            clustering_comparators,
            column_comparators,
        })
    }

    /// Detect SSTable format from reader using filename and format detector
    fn detect_format(reader: &SSTableReader) -> Result<SSTableFormat> {
        use super::format_detector::FormatDetector;

        let format_version = reader.format_version()?;
        let detector = FormatDetector::new();
        detector.detect_from_version(&format_version)
    }

    /// Get a value by partition and clustering keys with schema-driven parsing
    pub async fn get(
        &self,
        partition_key: &[Value],
        clustering_key: Option<&[Value]>,
    ) -> Result<Option<HashMap<String, Value>>> {
        // Validate partition key against schema
        self.validate_partition_key(partition_key)?;

        // Validate clustering key if provided
        if let Some(ck) = clustering_key {
            self.validate_clustering_key(ck)?;
        }

        // Create RowKey from partition and clustering keys
        let row_key = self.create_row_key(partition_key, clustering_key)?;

        // Get raw value from underlying reader
        let table_id = self.get_table_id();
        if let Some(raw_value) = self.reader.get(&table_id, &row_key).await? {
            // Parse the raw value using schema
            let parsed_row = self.parse_row_value(&raw_value)?;
            Ok(Some(parsed_row))
        } else {
            Ok(None)
        }
    }

    /// Scan a range of rows with schema-driven parsing
    pub async fn scan(
        &self,
        start_partition: Option<&[Value]>,
        end_partition: Option<&[Value]>,
        start_clustering: Option<&[Value]>,
        end_clustering: Option<&[Value]>,
        limit: Option<usize>,
    ) -> Result<Vec<(Vec<Value>, Vec<Value>, HashMap<String, Value>)>> {
        // Validate start partition key if provided
        if let Some(pk) = start_partition {
            self.validate_partition_key(pk)?;
        }

        // Validate end partition key if provided
        if let Some(pk) = end_partition {
            self.validate_partition_key(pk)?;
        }

        // Validate start clustering key if provided
        if let Some(ck) = start_clustering {
            self.validate_clustering_key(ck)?;
        }

        // Validate end clustering key if provided
        if let Some(ck) = end_clustering {
            self.validate_clustering_key(ck)?;
        }

        // Create start and end row keys
        let start_key = if let Some(pk) = start_partition {
            Some(self.create_row_key(pk, start_clustering)?)
        } else {
            None
        };

        let end_key = if let Some(pk) = end_partition {
            Some(self.create_row_key(pk, end_clustering)?)
        } else {
            None
        };

        // Scan using underlying reader, passing schema for accurate parsing
        let table_id = self.get_table_id();
        let raw_results = self
            .reader
            .scan(
                &table_id,
                start_key.as_ref(),
                end_key.as_ref(),
                limit,
                Some(&self.context.schema),
            )
            .await?;

        // Parse results using schema
        let mut parsed_results = Vec::new();
        for (row_key, raw_value) in raw_results {
            let (partition_values, clustering_values) = self.parse_row_key(&row_key)?;
            let column_values = self.parse_row_value(&raw_value)?;
            parsed_results.push((partition_values, clustering_values, column_values));
        }

        Ok(parsed_results)
    }

    /// Parse a raw row key into partition and clustering key values
    pub fn parse_row_key(&self, row_key: &RowKey) -> Result<(Vec<Value>, Vec<Value>)> {
        let key_bytes = row_key.as_bytes();

        // Parse partition key portion
        let partition_values = self.schema_parser.parse_partition_key(key_bytes)?;

        // Calculate partition key length to find clustering key start
        let partition_length = self.calculate_partition_key_length(&partition_values)?;

        // Parse clustering key portion (if present)
        let clustering_values = if key_bytes.len() > partition_length {
            self.schema_parser
                .parse_clustering_keys(&key_bytes[partition_length..])?
        } else {
            Vec::new()
        };

        Ok((partition_values, clustering_values))
    }

    /// Parse a raw row value into column values
    pub fn parse_row_value(&self, raw_value: &Value) -> Result<HashMap<String, Value>> {
        let value_bytes = raw_value
            .as_bytes()
            .ok_or_else(|| Error::Schema("Row value is not binary data".to_string()))?;

        let mut column_values = HashMap::new();
        let mut offset = 0;

        // Parse each column according to schema
        for column in &self.context.schema.columns {
            // Skip key columns as they're parsed separately
            if self.context.schema.is_partition_key(&column.name)
                || self.context.schema.is_clustering_key(&column.name)
            {
                continue;
            }

            if offset >= value_bytes.len() {
                break; // No more data
            }

            // Parse column value using schema parser with exact consumed-byte tracking
            let (column_value, consumed) = self
                .schema_parser
                .parse_column_value(&column.name, &value_bytes[offset..])?;

            offset += consumed;
            column_values.insert(column.name.clone(), column_value);
        }

        Ok(column_values)
    }

    /// Get reader statistics including schema-aware metrics
    pub async fn stats(&self) -> Result<SchemaAwareStats> {
        let base_stats = self.reader.stats().await?.clone();

        Ok(SchemaAwareStats {
            base_stats,
            schema_parsed_values: 0, // Would be tracked in real implementation
            partition_keys_parsed: 0,
            clustering_keys_parsed: 0,
            column_values_parsed: 0,
            parse_errors: 0,
            format_optimizations_used: 0,
        })
    }

    /// Get the table name for this reader
    pub fn table_name(&self) -> String {
        format!(
            "{}.{}",
            self.context.schema.keyspace, self.context.schema.table
        )
    }

    /// Get the schema used by this reader
    pub fn schema(&self) -> &TableSchema {
        &self.context.schema
    }

    /// Get the parsing context
    pub fn context(&self) -> &ParsingContext {
        &self.context
    }

    /// Check if format-specific optimizations are available
    pub fn has_format_optimizations(&self) -> bool {
        matches!(self.format, SSTableFormat::V5x(_))
    }

    /// Get Cassandra version detected from the SSTable
    pub fn cassandra_version(&self) -> CassandraVersion {
        self.version
    }

    /// Get the file path being read
    pub fn file_path(&self) -> &Path {
        &self.file_path
    }

    // Helper methods

    fn validate_partition_key(&self, key: &[Value]) -> Result<()> {
        if key.len() != self.context.partition_comparators.len() {
            return Err(Error::Schema(format!(
                "Partition key length mismatch: expected {}, got {}",
                self.context.partition_comparators.len(),
                key.len()
            )));
        }
        Ok(())
    }

    fn validate_clustering_key(&self, key: &[Value]) -> Result<()> {
        if key.len() > self.context.clustering_comparators.len() {
            return Err(Error::Schema(format!(
                "Clustering key too long: expected max {}, got {}",
                self.context.clustering_comparators.len(),
                key.len()
            )));
        }
        Ok(())
    }

    fn create_row_key(
        &self,
        partition_key: &[Value],
        clustering_key: Option<&[Value]>,
    ) -> Result<RowKey> {
        // Serialize partition key
        let mut key_bytes = Vec::new();
        for (value, comparator) in partition_key
            .iter()
            .zip(&self.context.partition_comparators)
        {
            let serialized = self.serialize_value_with_comparator(value, comparator)?;
            key_bytes.extend_from_slice(&serialized);
        }

        // Serialize clustering key if provided
        if let Some(ck) = clustering_key {
            for (value, comparator) in ck.iter().zip(&self.context.clustering_comparators) {
                let serialized = self.serialize_value_with_comparator(value, comparator)?;
                key_bytes.extend_from_slice(&serialized);
            }
        }

        Ok(RowKey::new(key_bytes))
    }

    fn serialize_value_with_comparator(
        &self,
        value: &Value,
        _comparator: &ComparatorType,
    ) -> Result<Vec<u8>> {
        // This would use the proper Cassandra serialization format for each type
        // For now, return a simplified implementation
        Ok(value.as_bytes().unwrap_or(&[]).to_vec())
    }

    fn calculate_partition_key_length(&self, partition_values: &[Value]) -> Result<usize> {
        // Calculate the byte length of the serialized partition key
        let mut total_length = 0;
        for (value, comparator) in partition_values
            .iter()
            .zip(&self.context.partition_comparators)
        {
            let serialized = self.serialize_value_with_comparator(value, comparator)?;
            total_length += serialized.len();
        }
        Ok(total_length)
    }

    fn get_table_id(&self) -> crate::types::TableId {
        // Create a table ID from the schema
        crate::types::TableId::from(format!(
            "{}.{}",
            self.context.schema.keyspace, self.context.schema.table
        ))
    }
}

/// Error handling specific to schema-aware reading
#[derive(Debug, thiserror::Error)]
pub enum SchemaAwareReaderError {
    #[error("Schema validation failed: {0}")]
    SchemaValidation(String),

    #[error("Parsing context incomplete: {0}")]
    IncompleteContext(String),

    #[error("Key validation failed: {0}")]
    KeyValidation(String),

    #[error("Value parsing failed for column '{column}': {reason}")]
    ValueParsing { column: String, reason: String },

    #[error("Format-specific error: {0}")]
    FormatSpecific(String),

    #[error("Incompatible Cassandra version: expected {expected:?}, found {found:?}")]
    VersionMismatch {
        expected: CassandraVersion,
        found: CassandraVersion,
    },
}

impl From<SchemaAwareReaderError> for Error {
    fn from(err: SchemaAwareReaderError) -> Self {
        Error::Schema(err.to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schema::{ClusteringColumn, ClusteringOrder, Column, KeyColumn};

    fn create_test_schema() -> TableSchema {
        TableSchema {
            keyspace: "test_ks".to_string(),
            table: "test_table".to_string(),
            partition_keys: vec![KeyColumn {
                name: "id".to_string(),
                data_type: "uuid".to_string(),
                position: 0,
            }],
            clustering_keys: vec![ClusteringColumn {
                name: "timestamp".to_string(),
                data_type: "timestamp".to_string(),
                position: 0,
                order: ClusteringOrder::Asc,
            }],
            columns: vec![
                Column {
                    name: "id".to_string(),
                    data_type: "uuid".to_string(),
                    nullable: false,
                    default: None,
                    is_static: false,
                },
                Column {
                    name: "timestamp".to_string(),
                    data_type: "timestamp".to_string(),
                    nullable: false,
                    default: None,
                    is_static: false,
                },
                Column {
                    name: "data".to_string(),
                    data_type: "text".to_string(),
                    nullable: true,
                    default: None,
                    is_static: false,
                },
            ],
            comments: HashMap::new(),
        }
    }

    #[test]
    fn test_schema_validation() {
        let schema = create_test_schema();
        assert!(SchemaAwareReader::validate_schema_completeness(&schema).is_ok());
    }

    #[test]
    fn test_invalid_schema_validation() {
        let mut schema = create_test_schema();
        schema.partition_keys.clear(); // Remove partition keys

        assert!(SchemaAwareReader::validate_schema_completeness(&schema).is_err());
    }

    #[test]
    fn test_non_contiguous_positions() {
        let mut schema = create_test_schema();
        schema.partition_keys.push(KeyColumn {
            name: "other".to_string(),
            data_type: "text".to_string(),
            position: 2, // Non-contiguous position
        });

        assert!(SchemaAwareReader::validate_schema_completeness(&schema).is_err());
    }
}