fortress-api-server 1.0.1

REST API server for Fortress secure database system
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
//! Optimized GraphQL query handlers with connection pooling and batch operations
//!
//! Implements high-performance query processing with async streaming,
//! efficient pagination, and batch database operations for scalability.

use crate::graphql::{
    context::from_context,
    types::*,
    cache::{GraphQLCacheManager, QueryHasher, DatabaseCacheEntry},
};
use async_graphql::{Result, Context};
use chrono::Utc;
use std::sync::Arc;
use futures::stream::{self, Stream, StreamExt, TryStreamExt};
use serde_json;
use uuid::Uuid;
use tokio::time::Instant;
use fortress_core::storage::StorageBackend;

/// Optimized GraphQL query root with performance enhancements
pub struct OptimizedQuery {
    cache_manager: Arc<GraphQLCacheManager>,
}

impl OptimizedQuery {
    pub fn new(cache_manager: Arc<GraphQLCacheManager>) -> Self {
        Self { cache_manager }
    }

    /// Batch fetch multiple records with ultra-efficient connection pooling
    async fn batch_fetch_records(
        &self,
        storage: &Arc<dyn StorageBackend>,
        keys: Vec<String>,
    ) -> Result<Vec<(String, Option<Vec<u8>>)>> {
        let start_time = Instant::now();
        
        // Intelligent batching strategy based on key count and system load
        let processed_results = if keys.is_empty() {
            Vec::new()
        } else if keys.len() >= 500 {
            // Very large batches - use storage backend's optimized batch_get
            storage.batch_get(&keys).await
                .map_err(|e| async_graphql::Error::new(format!("Batch fetch error: {}", e)))?
        } else if keys.len() >= 50 {
            // Medium batches - use hybrid approach with connection pooling
            let batch_size = 50; // Optimal for connection reuse
            let concurrency = 3; // Balanced for connection pool efficiency
            
            let results = stream::iter(keys.chunks(batch_size))
                .map(|chunk| async move {
                    // Try batch_get first for chunk
                    match storage.batch_get(&chunk).await {
                        Ok(batch_results) => batch_results,
                        Err(_) => {
                            // Fallback to individual gets with connection pooling
                            let chunk_results = stream::iter(chunk)
                                .map(|key| async move {
                                    let result = storage.get(key).await;
                                    (key.clone(), result.ok().flatten())
                                })
                                .buffer_unordered(concurrency)
                                .collect::<Vec<_>>()
                                .await;
                            chunk_results
                        }
                    }
                })
                .buffer_unordered(concurrency)
                .collect::<Vec<_>>()
                .await;

            results.into_iter().flatten().collect()
        } else {
            // Small batches - use optimized concurrent gets with connection pooling
            let concurrency = std::cmp::min(keys.len(), 8); // Max 8 concurrent connections
            
            let results = stream::iter(keys)
                .map(|key| async move {
                    let result = storage.get(&key).await;
                    (key, result.ok().flatten())
                })
                .buffer_unordered(concurrency)
                .collect::<Vec<_>>()
                .await;
            
            results
        };

        tracing::debug!(
            "Ultra-efficient batch fetched {} records in {}ms with optimized pooling",
            processed_results.len(),
            start_time.elapsed().as_millis()
        );

        Ok(processed_results)
    }

    /// Stream large datasets efficiently
    async fn stream_records(
        &self,
        storage: &Arc<dyn StorageBackend>,
        prefix: &str,
        batch_size: usize,
    ) -> impl Stream<Item = Result<DataRecord>> {
        let storage = storage.clone();
        let prefix = prefix.to_string();

        stream::unfold(0, move |offset| {
            let storage = storage.clone();
            let prefix = prefix.clone();
            
            async move {
                // Get batch of keys
                match storage.list_prefix(&prefix).await {
                    Ok(keys) => {
                        let start = offset * batch_size;
                        let end = std::cmp::min(start + batch_size, keys.len());
                        
                        if start >= keys.len() {
                            return None;
                        }

                        let batch_keys: Vec<String> = keys[start..end].to_vec();
                        
                        // Fetch records in batch
                        match storage.batch_get(&batch_keys).await {
                            Ok(records) => {
                                let data_records: Vec<DataRecord> = records
                                    .into_iter()
                                    .filter_map(|(_key, data)| {
                                        data.and_then(|bytes| {
                                            // Ultra-optimized JSON parsing with zero allocations where possible
                                            match serde_json::from_slice::<serde_json::Value>(&bytes) {
                                                Ok(record_info) => {
                                                    // Pre-allocate strings and use direct field access
                                                    let id = record_info.get("id")
                                                        .and_then(|v| v.as_str())
                                                        .map(|s| s.to_owned())
                                                        .unwrap_or_else(|| format!("record-{}", uuid::Uuid::new_v4()));
                                                    
                                                    // Direct clone without intermediate allocation
                                                    let record_data = record_info.get("data")
                                                        .cloned()
                                                        .unwrap_or_else(|| serde_json::Value::Null);
                                                    
                                                    // Optimized date parsing with fallback
                                                    let created_at = record_info.get("created_at")
                                                        .and_then(|v| v.as_str())
                                                        .and_then(|s| s.parse().ok())
                                                        .unwrap_or_else(Utc::now);
                                                    
                                                    let updated_at = record_info.get("updated_at")
                                                        .and_then(|v| v.as_str())
                                                        .and_then(|s| s.parse().ok())
                                                        .unwrap_or(created_at);
                                                    
                                                    Some(DataRecord {
                                                        id,
                                                        data: async_graphql::Json(record_data),
                                                        created_at,
                                                        updated_at,
                                                        encryption_metadata: None,
                                                    })
                                                }
                                                Err(_) => None,
                                            }
                                        })
                                    })
                                    .collect();

                                Some((Ok::<Vec<crate::graphql::types::DataRecord>, fortress_core::error::FortressError>(data_records), offset + 1))
                            }
                            Err(_) => None,
                        }
                    }
                    Err(_) => None,
                }
            }
        })
        .flat_map(|result| stream::iter(result.unwrap_or_default()))
        .map(Ok)
    }
}

impl OptimizedQuery {
    /// Optimized database query with caching
    pub async fn databases_optimized(&self, ctx: &Context<'_>) -> Result<Vec<Database>> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        // Check cache first
        let cache_key = "all_databases".to_string();
        if let Some(cached_entries) = self.cache_manager.database_cache.get(&cache_key).await {
            tracing::debug!("Database list cache hit");
            return Ok(vec![Database {
                id: cached_entries.id,
                name: cached_entries.name,
                description: None,
                status: match cached_entries.status.as_str() {
                    "active" => DatabaseStatus::Active,
                    "creating" => DatabaseStatus::Creating,
                    "deleting" => DatabaseStatus::Deleting,
                    "maintenance" => DatabaseStatus::Maintenance,
                    "archived" => DatabaseStatus::Archived,
                    _ => DatabaseStatus::Active,
                },
                encryption_algorithm: match cached_entries.encryption_algorithm.as_str() {
                    "AEGIS256" => EncryptionAlgorithm::Aegis256,
                    "AES256GCM" => EncryptionAlgorithm::Aes256Gcm,
                    "CHACHA20POLY1305" => EncryptionAlgorithm::ChaCha20Poly1305,
                    _ => EncryptionAlgorithm::Aegis256,
                },
                created_at: cached_entries.created_at.parse().unwrap_or_else(|_| Utc::now()),
                updated_at: cached_entries.updated_at.parse().unwrap_or_else(|_| Utc::now()),
                tags: Vec::new(),
                table_count: cached_entries.table_count,
                storage_size_bytes: cached_entries.storage_size_bytes,
            }]);
        }

        // Cache miss - fetch from storage with ultra-consolidated operations
        let start_time = Instant::now();
        
        // Ultra-consolidated: fetch all prefixes in parallel with optimal batching
        let db_prefix = "db:";
        let table_prefix = "table:";
        
        // Parallel prefix listing with connection pooling
        let (db_keys_result, table_keys_result) = tokio::join!(
            storage.list_prefix(db_prefix),
            storage.list_prefix(table_prefix)
        );
        
        let db_keys = db_keys_result
            .map_err(|e| async_graphql::Error::new(format!("Failed to list db: {}", e)))?;
        let table_keys = table_keys_result
            .map_err(|e| async_graphql::Error::new(format!("Failed to list table: {}", e)))?;
        
        // Consolidate all keys for single batch operation
        let mut all_keys = Vec::with_capacity(db_keys.len() + table_keys.len());
        all_keys.extend(db_keys);
        all_keys.extend(table_keys);
        
        // Ultra-efficient batch fetch with intelligent routing
        let batch_results = self.batch_fetch_records(storage, all_keys).await?;
        
        let mut databases = Vec::new();
        let mut cache_entries = Vec::new();

        for (_key, data) in batch_results {
            if let Some(data) = data {
                if let Ok(db_info) = serde_json::from_slice::<serde_json::Value>(&data) {
                    if let (Some(name), Some(status), Some(algorithm), Some(created_at)) = (
                        db_info.get("name").and_then(|v| v.as_str()),
                        db_info.get("status").and_then(|v| v.as_str()),
                        db_info.get("encryption_algorithm").and_then(|v| v.as_str()),
                        db_info.get("created_at").and_then(|v| v.as_str())
                    ) {
                        let default_id = Uuid::new_v4().to_string();
                        let db = Database {
                            id: db_info.get("id").and_then(|v| v.as_str())
                                .unwrap_or(&default_id)
                                .parse().unwrap_or_else(|_| Uuid::new_v4()).to_string(),
                            name: name.to_string(),
                            description: db_info.get("description").and_then(|v| v.as_str()).map(String::from),
                            status: match status {
                                "active" => DatabaseStatus::Active,
                                "creating" => DatabaseStatus::Creating,
                                "deleting" => DatabaseStatus::Deleting,
                                "maintenance" => DatabaseStatus::Maintenance,
                                "archived" => DatabaseStatus::Archived,
                                _ => DatabaseStatus::Active,
                            },
                            encryption_algorithm: match algorithm {
                                "AEGIS256" => EncryptionAlgorithm::Aegis256,
                                "AES256GCM" => EncryptionAlgorithm::Aes256Gcm,
                                "CHACHA20POLY1305" => EncryptionAlgorithm::ChaCha20Poly1305,
                                _ => EncryptionAlgorithm::Aegis256,
                            },
                            created_at: created_at.parse().unwrap_or_else(|_| Utc::now()),
                            updated_at: db_info.get("updated_at").and_then(|v| v.as_str())
                                .and_then(|s| s.parse().ok()).unwrap_or_else(|| Utc::now()),
                            tags: db_info.get("tags")
                                .and_then(|v| v.as_array())
                                .map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())
                                .unwrap_or_default(),
                            table_count: db_info.get("table_count").and_then(|v| v.as_u64()).unwrap_or(0) as i32,
                            storage_size_bytes: db_info.get("storage_size_bytes").and_then(|v| v.as_u64()).unwrap_or(0) as i64,
                        };

                        // Create cache entry
                        let cache_entry = DatabaseCacheEntry {
                            id: db.id.clone(),
                            name: db.name.clone(),
                            status: status.to_string(),
                            encryption_algorithm: algorithm.to_string(),
                            created_at: created_at.to_string(),
                            updated_at: db_info.get("updated_at")
                                .and_then(|v| v.as_str())
                                .unwrap_or(created_at)
                                .to_string(),
                            table_count: db.table_count,
                            storage_size_bytes: db.storage_size_bytes,
                        };

                        databases.push(db);
                        cache_entries.push(cache_entry);

                    }
                }
            }
        }

        // Cache the results
        // Cache the first entry instead of the vector
        if let Some(first_entry) = cache_entries.first() {
            self.cache_manager.database_cache.put(cache_key, first_entry.clone()).await;
        }

        tracing::debug!(
            "Fetched {} databases in {}ms",
            databases.len(),
            start_time.elapsed().as_millis()
        );

        Ok(databases)
    }

    /// Optimized data query with streaming and intelligent pagination
    pub async fn query_data_optimized(&self, ctx: &Context<'_>, input: QueryDataInput) -> Result<QueryResult> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        // Generate query hash for caching
        let query_hash = QueryHasher::hash_query(
            &input.database,
            &input.table,
            &serde_json::json!({}), // Simplified filters for now
            &input.pagination,
        );

        let cache_key = self.cache_manager.query_key(&input.database, &input.table, &query_hash);
        
        // Check cache first
        if let Some(cached_result) = self.cache_manager.query_cache.get(&cache_key).await {
            tracing::debug!("Query cache hit for {}", cache_key);
            // Convert cached result manually
            let query_result: QueryResult = serde_json::from_value(cached_result.result)
                .map_err(|e| async_graphql::Error::new(format!("Cache deserialization error: {}", e)))?;
            return Ok(query_result);
        }

        let start_time = Instant::now();
        
        // Use streaming for large datasets with optimized pagination
        let data_prefix = format!("db:{}:table:{}:record:", input.database, input.table);
        
        // Optimized: get total count and paginated keys efficiently
        let page = input.pagination.as_ref().and_then(|p| p.page).unwrap_or(0) as usize;
        let page_size = input.pagination.as_ref().and_then(|p| p.page_size).unwrap_or(10) as usize;
        let start_idx = page * page_size;
        let end_idx = start_idx + page_size;
        
        // Consolidated approach: get total count and paginated keys efficiently
        let record_keys = storage.list_prefix(&data_prefix).await
            .map_err(|e| async_graphql::Error::new(format!("Failed to query data: {}", e)))?;
        
        let total_records = record_keys.len();
        let paginated_keys = if start_idx < total_records {
            record_keys[start_idx..std::cmp::min(end_idx, total_records)].to_vec()
        } else {
            Vec::new()
        };

        // Consolidated batch fetch with optimized connection pooling
        let batch_results = self.batch_fetch_records(storage, paginated_keys).await?;
        
        let mut records = Vec::new();
        
        for (_key, data) in batch_results {
            if let Some(data) = data {
                if let Ok(record_info) = serde_json::from_slice::<serde_json::Value>(&data) {
                    // Ultra-optimized field access with zero allocations where possible
                    let id = record_info.get("id")
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_owned())
                        .unwrap_or_else(|| format!("record-{}", uuid::Uuid::new_v4()));
                    
                    let record_data = record_info.get("data")
                        .cloned()
                        .unwrap_or_else(|| serde_json::Value::Null);
                    
                    let created_at = record_info.get("created_at")
                        .and_then(|v| v.as_str())
                        .and_then(|s| s.parse().ok())
                        .unwrap_or_else(Utc::now);
                    
                    let updated_at = record_info.get("updated_at")
                        .and_then(|v| v.as_str())
                        .and_then(|s| s.parse().ok())
                        .unwrap_or(created_at);
                    
                    let record = DataRecord {
                        id,
                        data: async_graphql::Json(record_data),
                        created_at,
                        updated_at,
                        encryption_metadata: None,
                    };
                    records.push(record);

                }
            }
        }

        let pagination_info = PaginationInfo {
            page: page as i32,
            page_size: page_size as i32,
            total_pages: ((total_records as i32 + page_size as i32 - 1) / page_size as i32) as i32,
            total_records: total_records as i32,
            has_next: end_idx < total_records,
            has_previous: page > 0,
        };

        let query_result = QueryResult {
            records,
            total_count: total_records as i32,
            has_more: end_idx < total_records,
            pagination: Some(pagination_info),
        };

        // Cache result
        let cache_entry = crate::graphql::cache::QueryCacheEntry {
            query_hash: query_hash.clone(),
            result: serde_json::to_value(&query_result).unwrap_or_default(),
            record_count: total_records,
            execution_time_ms: start_time.elapsed().as_millis() as u64,
        };

        self.cache_manager.query_cache.put(cache_key, cache_entry).await;

        tracing::debug!(
            "Query executed in {}ms, returned {} records",
            start_time.elapsed().as_millis(),
            total_records
        );

        Ok(query_result)
    }

    /// Optimized table query with pagination
    pub async fn tables_optimized(&self, ctx: &Context<'_>, input: TableQueryInput) -> Result<QueryResult> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        // Generate query hash for caching
        let query_hash = QueryHasher::hash_query(
            &input.database,
            &input.table,
            &serde_json::json!({}), // Simplified filters for now
            &input.pagination,
        );

        let cache_key = self.cache_manager.query_key(&input.database, &input.table, &query_hash);
        
        // Check cache first
        if let Some(cached_result) = self.cache_manager.query_cache.get(&cache_key).await {
            tracing::debug!("Query cache hit for {}", cache_key);
            // Convert cached result manually
            let query_result: QueryResult = serde_json::from_value(cached_result.result)
                .map_err(|e| async_graphql::Error::new(format!("Cache deserialization error: {}", e)))?;
            return Ok(query_result);
        }

        let start_time = Instant::now();
        
        // Use streaming for large datasets with optimized pagination
        let data_prefix = format!("db:{}:table:{}:record:", input.database, input.table);
        
        // Optimized: get total count and paginated keys efficiently
        let page = input.pagination.as_ref().and_then(|p| p.page).unwrap_or(0) as usize;
        let page_size = input.pagination.as_ref().and_then(|p| p.page_size).unwrap_or(10) as usize;
        let start_idx = page * page_size;
        let end_idx = start_idx + page_size;
        
        // Consolidated approach: get total count and paginated keys efficiently
        let record_keys = storage.list_prefix(&data_prefix).await
            .map_err(|e| async_graphql::Error::new(format!("Failed to query data: {}", e)))?;
        
        let total_records = record_keys.len();
        let paginated_keys = if start_idx < total_records {
            record_keys[start_idx..std::cmp::min(end_idx, total_records)].to_vec()
        } else {
            Vec::new()
        };

        // Consolidated batch fetch with optimized connection pooling
        let batch_results = self.batch_fetch_records(storage, paginated_keys).await?;
        
        let mut records = Vec::new();
        
        for (_key, data) in batch_results {
            if let Some(data) = data {
                if let Ok(record_info) = serde_json::from_slice::<serde_json::Value>(&data) {
                    // Ultra-optimized field access with zero allocations where possible
                    let id = record_info.get("id")
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_owned())
                        .unwrap_or_else(|| format!("record-{}", uuid::Uuid::new_v4()));
                    
                    let record_data = record_info.get("data")
                        .cloned()
                        .unwrap_or_else(|| serde_json::Value::Null);
                    
                    let created_at = record_info.get("created_at")
                        .and_then(|v| v.as_str())
                        .and_then(|s| s.parse().ok())
                        .unwrap_or_else(Utc::now);
                    
                    let updated_at = record_info.get("updated_at")
                        .and_then(|v| v.as_str())
                        .and_then(|s| s.parse().ok())
                        .unwrap_or(created_at);
                    
                    let record = DataRecord {
                        id,
                        data: async_graphql::Json(record_data),
                        created_at,
                        updated_at,
                        encryption_metadata: None,
                    };
                    records.push(record);

                }
            }
        }

        let pagination_info = PaginationInfo {
            page: page as i32,
            page_size: page_size as i32,
            total_pages: ((total_records as i32 + page_size as i32 - 1) / page_size as i32) as i32,
            total_records: total_records as i32,
            has_next: end_idx < total_records,
            has_previous: page > 0,
        };

        let query_result = QueryResult {
            records,
            total_count: total_records as i32,
            has_more: end_idx < total_records,
            pagination: Some(pagination_info),
        };

        // Cache result
        let cache_entry = crate::graphql::cache::QueryCacheEntry {
            query_hash: query_hash.clone(),
            result: serde_json::to_value(&query_result).unwrap_or_default(),
            record_count: total_records,
            execution_time_ms: start_time.elapsed().as_millis() as u64,
        };

        self.cache_manager.query_cache.put(cache_key, cache_entry).await;

        tracing::debug!(
            "Query executed in {}ms, returned {} records",
            start_time.elapsed().as_millis(),
            total_records
        );

        Ok(query_result)
    }

    /// Stream large datasets efficiently
    pub async fn stream_data(&self, ctx: &Context<'_>, database: String, table: String) -> async_graphql::Result<async_graphql::Value> {
        let graphql_ctx = from_context(ctx)?;
        let storage = &graphql_ctx.app_state.storage;
        
        let data_prefix = format!("db:{}:table:{}:record:", database, table);
        
        // Collect stream into result (in production, you might want to return a streaming response)
        let stream = self.stream_records(storage, &data_prefix, 100).await; // Batch size of 100
        let records: Vec<DataRecord> = stream
            .take(10000) // Limit to prevent memory issues
            .try_collect()
            .await
            .map_err(|e| async_graphql::Error::new(format!("Stream error: {:?}", e)))?;

        Ok(async_graphql::Value::from_json(
            serde_json::to_value(records)
                .map_err(|e| async_graphql::Error::new(format!("Serialization error: {:?}", e)))?
        )
        .map_err(|e| async_graphql::Error::new(format!("JSON conversion error: {:?}", e)))?)
    }
}