graphlite 0.0.1

GraphLite - A lightweight ISO GQL Graph Database
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
//! Storage Manager - Orchestrates multiple storage tiers
//!
//! This module provides a unified interface for managing graph storage across
//! multiple tiers: local cache, persistent disk storage, and external memory stores.
//!
//! Architecture:
//! - Cache: Local in-memory cache for hot data (via MultiGraphManager)
//! - Persistent Store: Optional disk-based storage (RocksDB/Sled via DataAdapter)
//! - Memory Store: Optional external memory store (Redis/Valkey via DataAdapter)
//!
//! At least one of persistent_store or memory_store must be configured.

use crate::catalog::manager::CatalogManager;
use crate::storage::data_adapter::DataAdapter;
use crate::storage::indexes::IndexManager;
use crate::storage::multi_graph::MultiGraphManager;
use crate::storage::persistent::{create_storage_driver, StorageDriver, StorageTree};
use crate::storage::StorageType;
use crate::storage::{GraphCache, StorageError};
use log::{debug, error, info};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::Arc;

/// Storage method configuration
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum StorageMethod {
    /// Disk-based storage only (RocksDB/Sled)
    DiskOnly,
    /// Memory-based storage only (Redis/Valkey)
    MemoryOnly,
    /// Both disk and memory storage for redundancy
    DiskAndMemory,
}

impl Default for StorageMethod {
    fn default() -> Self {
        StorageMethod::DiskOnly
    }
}

/// Storage manager that orchestrates all storage tiers
#[derive(Clone)]
pub struct StorageManager {
    /// Local in-memory cache for hot data
    cache: Arc<MultiGraphManager>,

    /// Single storage driver instance (RocksDB/Sled) - created once at initialization
    storage_driver: Option<Arc<Box<dyn StorageDriver<Tree = Box<dyn StorageTree>>>>>,

    /// Optional disk-based persistent storage (RocksDB/Sled)
    persistent_store: Option<Arc<DataAdapter>>,

    /// Optional external memory store (Redis/Valkey)
    memory_store: Option<Arc<DataAdapter>>,

    /// Storage type being used
    storage_type: StorageType,

    /// Index manager for text indexes
    index_manager: Option<Arc<IndexManager>>,
}

impl StorageManager {
    /// Parse a graph path in format /<schema_name>/<graph_name> into catalog IDs
    /// Returns None if path format is invalid

    /// Create a new storage manager with the specified method and configuration
    pub fn new<P: AsRef<Path>>(
        path: P,
        method: StorageMethod,
        storage_type: StorageType,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        info!(
            "Creating storage manager with method: {:?}, storage type: {:?}",
            method, storage_type
        );

        match method {
            StorageMethod::DiskOnly => Self::init_disk_only(path, storage_type),
            StorageMethod::MemoryOnly => Self::init_memory_only(path, storage_type),
            StorageMethod::DiskAndMemory => Self::init_disk_and_memory(path, storage_type),
        }
    }

    /// Initialize storage manager with disk-only storage (RocksDB/Sled)
    fn init_disk_only<P: AsRef<Path>>(
        path: P,
        storage_type: StorageType,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        info!(
            "Initializing disk-only storage with {} at path: {:?}",
            storage_type,
            path.as_ref()
        );

        // Create single storage driver instance
        let driver = create_storage_driver(storage_type, path.as_ref())?;

        // Pre-create commonly used column families/trees
        let common_trees = vec!["nodes", "edges", "metadata", "catalog", "auth"];
        for tree_name in &common_trees {
            driver.open_tree(tree_name)?;
            debug!("Pre-created tree: {}", tree_name);
        }

        info!(
            "Storage driver initialized with {} pre-created trees",
            common_trees.len()
        );

        // Create DataAdapter (now stateless)
        let persistent_store = Arc::new(DataAdapter::new());

        // Create IndexManager for graph indexes
        let driver_arc = Arc::new(driver);
        let index_manager = Arc::new(IndexManager::new());

        Ok(Self {
            cache: Arc::new(MultiGraphManager::new()),
            storage_driver: Some(driver_arc),
            persistent_store: Some(persistent_store),
            memory_store: None,
            storage_type,
            index_manager: Some(index_manager),
        })
    }

    /// Initialize storage manager with memory-only storage (Redis/Valkey)
    fn init_memory_only<P: AsRef<Path>>(
        _path: P,
        storage_type: StorageType,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        info!("Initializing memory-only storage with {:?}", storage_type);

        // TODO: Implement Redis/Valkey memory store initialization
        Err("Memory-only storage not yet implemented".into())
    }

    /// Initialize storage manager with both disk and memory storage
    fn init_disk_and_memory<P: AsRef<Path>>(
        path: P,
        storage_type: StorageType,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        info!(
            "Initializing disk and memory storage with {} at path: {:?}",
            storage_type,
            path.as_ref()
        );

        // TODO: Implement both disk and memory storage initialization
        // For now, fall back to disk-only
        Self::init_disk_only(path, storage_type)
    }

    /// Get a graph by name
    /// Checks cache first, then memory store, then persistent storage
    pub fn get_graph(&self, name: &str) -> Result<Option<GraphCache>, StorageError> {
        debug!("Getting graph '{}' from storage manager", name);

        // 1. Check local cache first
        match self.cache.get_graph(name) {
            Ok(Some(graph)) => {
                debug!("Graph '{}' found in local cache", name);
                return Ok(Some(graph));
            }
            Ok(None) => {
                debug!("Graph '{}' not found in local cache", name);
            }
            Err(e) => {
                error!("Error checking cache for graph '{}': {}", name, e);
                return Err(e);
            }
        }

        // No fallback logic - use exact names for consistency

        // 2. Check memory store if available
        if let Some(_memory_store) = &self.memory_store {
            debug!("Memory store not yet implemented for graph '{}'", name);
        }

        // 3. Check persistent disk storage if available
        debug!(
            "Graph '{}' not in memory, checking persistent storage",
            name
        );

        if let Some(persistent_store) = &self.persistent_store {
            if let Some(driver) = &self.storage_driver {
                match persistent_store.load_graph_by_path(driver.as_ref().as_ref(), name) {
                    Ok(graph) => {
                        debug!("Graph '{}' loaded from persistent storage", name);

                        // Add to cache for future access
                        self.cache.add_graph(name.to_string(), graph.clone())?;
                        return Ok(Some(graph));
                    }
                    Err(e) => {
                        debug!(
                            "Failed to load graph '{}' from persistent storage: {}",
                            name, e
                        );
                    }
                }
            }
        }

        Ok(None)
    }

    /// Save a graph
    /// Updates cache, memory store (if available), and persistent storage
    pub fn save_graph(&self, name: &str, graph: GraphCache) -> Result<(), StorageError> {
        debug!("Saving graph '{}' to storage manager", name);

        // Use the provided name consistently - don't try to normalize to short names
        let cache_name = name.to_string();

        // 1. Update cache
        match self.cache.add_graph(cache_name.clone(), graph.clone()) {
            Ok(_) => {
                debug!("Successfully added graph '{}' to cache", cache_name);
            }
            Err(e) => {
                error!("Failed to add graph '{}' to cache: {}", cache_name, e);
                return Err(e);
            }
        }

        // 2. Update memory store if available (TODO: Implement memory store save)
        if let Some(_memory_store) = &self.memory_store {
            debug!("Memory store save not yet implemented for graph '{}'", name);
        }

        // 3. Persist to disk storage if available
        if let Some(persistent_store) = &self.persistent_store {
            if let Some(driver) = &self.storage_driver {
                debug!("Attempting to persist graph '{}' to disk", name);
                persistent_store
                    .save_graph_by_path(driver.as_ref().as_ref(), &graph, name)
                    .map_err(|e| {
                        error!("Failed to persist graph '{}': {}", name, e);
                        StorageError::PersistenceError(format!(
                            "Failed to persist graph '{}': {}",
                            name, e
                        ))
                    })?;
                debug!("Successfully persisted graph '{}' to disk", name);
            } else {
                debug!(
                    "No storage driver available, skipping disk persistence for '{}'",
                    name
                );
            }
        } else {
            debug!(
                "No persistent store available, skipping disk persistence for '{}'",
                name
            );
        }

        debug!("Successfully saved graph '{}' to all storage tiers", name);
        Ok(())
    }

    /// Get all graph names from cache and storage tiers
    pub fn get_graph_names(&self) -> Result<Vec<String>, StorageError> {
        debug!("Getting all graph names from storage manager");

        // Get names from cache
        let mut names = self.cache.get_graph_names()?;

        // Get names from persistent storage and merge
        if let Some(persistent_store) = &self.persistent_store {
            if let Some(driver) = &self.storage_driver {
                if let Ok(persistent_names) = persistent_store.list_graphs(driver.as_ref().as_ref())
                {
                    for name in persistent_names {
                        if !names.contains(&name) {
                            names.push(name);
                        }
                    }
                }
            }
        }

        // Get names from memory store and merge
        if let Some(memory_store) = &self.memory_store {
            if let Some(driver) = &self.storage_driver {
                if let Ok(memory_names) = memory_store.list_graphs(driver.as_ref().as_ref()) {
                    for name in memory_names {
                        if !names.contains(&name) {
                            names.push(name);
                        }
                    }
                }
            }
        }

        names.sort();
        debug!("Found {} graphs in storage manager", names.len());
        Ok(names)
    }

    /// Delete a graph from all storage tiers
    pub fn delete_graph(&self, name: &str) -> Result<(), StorageError> {
        debug!("Deleting graph '{}' from storage manager", name);

        // 1. Remove from cache
        self.cache.remove_graph(name)?;

        // 2. Remove from memory store if available
        if let Some(memory_store) = &self.memory_store {
            // Use the new delete_graph method to delete only this graph's data
            if let Some(driver) = &self.storage_driver {
                memory_store
                    .delete_graph(driver.as_ref().as_ref(), name)
                    .map_err(|e| {
                        error!("Failed to delete graph '{}' from memory store: {}", name, e);
                        StorageError::PersistenceError(format!(
                            "Failed to delete graph '{}' from memory: {}",
                            name, e
                        ))
                    })?;
            }
            debug!("Successfully deleted graph '{}' from memory store", name);
        }

        // 3. Delete from persistent storage if available
        if let Some(persistent_store) = &self.persistent_store {
            // Use delete_graph instead of clear() to only delete this specific graph
            if let Some(driver) = &self.storage_driver {
                persistent_store
                    .delete_graph(driver.as_ref().as_ref(), name)
                    .map_err(|e| {
                        error!(
                            "Failed to delete graph '{}' from persistent storage: {}",
                            name, e
                        );
                        StorageError::PersistenceError(format!(
                            "Failed to delete graph '{}': {}",
                            name, e
                        ))
                    })?;
            }
            debug!(
                "Successfully deleted graph '{}' from persistent storage",
                name
            );
        }

        debug!(
            "Successfully deleted graph '{}' from all storage tiers",
            name
        );
        Ok(())
    }

    /// List all available graphs
    pub fn list_graphs(&self) -> Result<Vec<String>, StorageError> {
        self.get_graph_names()
    }

    /// Get a mutable reference to a graph in the cache
    pub fn get_graph_mut(&self, name: &str) -> Result<Option<GraphCache>, StorageError> {
        // First ensure the graph is in the cache
        if self.cache.get_graph(name)?.is_none() {
            // Try to load it
            if let Some(graph) = self.get_graph(name)? {
                self.cache.add_graph(name.to_string(), graph)?;
            }
        }

        self.cache.get_graph_mut(name)
    }

    /// Get access to the storage driver for metrics collection
    pub fn get_storage_driver(
        &self,
    ) -> Option<&Arc<Box<dyn StorageDriver<Tree = Box<dyn StorageTree>>>>> {
        self.storage_driver.as_ref()
    }

    /// Get access to the cache for metrics collection
    pub fn get_cache(&self) -> &Arc<MultiGraphManager> {
        &self.cache
    }

    /// Get access to the index manager (Phase 5: Week 6.4)
    pub fn get_index_manager(&self) -> Option<&Arc<IndexManager>> {
        self.index_manager.as_ref()
    }

    /// Check if a text index exists for a given field (Phase 5: Week 6.4)
    pub fn has_text_index(&self, index_name: &str) -> bool {
        if let Some(index_manager) = &self.index_manager {
            index_manager.index_exists(index_name)
        } else {
            false
        }
    }

    /// Create a graph union (for UNION operations)
    pub fn create_graph_union(&self, graph_names: Vec<String>) -> Result<GraphCache, StorageError> {
        let mut graphs = Vec::new();
        for name in graph_names {
            if let Some(graph) = self.get_graph(&name)? {
                graphs.push(graph);
            } else {
                return Err(StorageError::GraphNotFound(name));
            }
        }

        self.cache.union_graphs(graphs)
    }

    /// Save catalog to persistent storage
    pub fn save_catalog(&self, catalog_manager: &CatalogManager) -> Result<(), StorageError> {
        if let Some(persistent_store) = &self.persistent_store {
            persistent_store.save_catalog(catalog_manager).map_err(|e| {
                StorageError::PersistenceError(format!("Failed to save catalog: {}", e))
            })
        } else if let Some(memory_store) = &self.memory_store {
            memory_store.save_catalog(catalog_manager).map_err(|e| {
                StorageError::PersistenceError(format!("Failed to save catalog: {}", e))
            })
        } else {
            Err(StorageError::PersistenceError(
                "No storage backend available".to_string(),
            ))
        }
    }

    /// Load catalog from persistent storage
    pub fn load_catalog(&self, catalog_manager: &mut CatalogManager) -> Result<(), StorageError> {
        if let Some(persistent_store) = &self.persistent_store {
            persistent_store.load_catalog(catalog_manager).map_err(|e| {
                StorageError::PersistenceError(format!("Failed to load catalog: {}", e))
            })
        } else if let Some(memory_store) = &self.memory_store {
            memory_store.load_catalog(catalog_manager).map_err(|e| {
                StorageError::PersistenceError(format!("Failed to load catalog: {}", e))
            })
        } else {
            debug!("No storage backend available for loading catalog");
            Ok(())
        }
    }

    /// Get access to the cache manager (for compatibility)
    pub fn cache(&self) -> Arc<MultiGraphManager> {
        self.cache.clone()
    }

    /// Get access to the cache manager (legacy name for compatibility)
    pub fn working_set(&self) -> Arc<MultiGraphManager> {
        self.cache.clone()
    }

    /// Get access to persistent storage (for compatibility during migration)
    pub fn persistent_storage(&self) -> Option<Arc<DataAdapter>> {
        self.persistent_store.clone()
    }

    /// Get access to the single storage driver instance
    /// This ensures all components use the same driver with consistent configuration
    pub fn storage_driver(
        &self,
    ) -> Option<Arc<Box<dyn StorageDriver<Tree = Box<dyn StorageTree>>>>> {
        self.storage_driver.clone()
    }

    /// Check if catalog data exists
    pub fn has_catalog_data(&self) -> Result<bool, StorageError> {
        if let Some(persistent_store) = &self.persistent_store {
            if let Some(driver) = &self.storage_driver {
                persistent_store
                    .has_catalog_data(driver.as_ref().as_ref())
                    .map_err(|e| {
                        StorageError::PersistenceError(format!(
                            "Failed to check catalog data: {}",
                            e
                        ))
                    })
            } else {
                Ok(false)
            }
        } else if let Some(memory_store) = &self.memory_store {
            if let Some(driver) = &self.storage_driver {
                memory_store
                    .has_catalog_data(driver.as_ref().as_ref())
                    .map_err(|e| {
                        StorageError::PersistenceError(format!(
                            "Failed to check catalog data: {}",
                            e
                        ))
                    })
            } else {
                Ok(false)
            }
        } else {
            Ok(false)
        }
    }

    /// Clear only the in-memory cache (not persistent storage)
    pub fn clear_cache(&self) -> Result<(), StorageError> {
        debug!("Clearing storage cache");
        self.cache.clear()?;
        debug!("Successfully cleared storage cache");
        Ok(())
    }

    /// Get cache statistics (entries count, memory bytes estimate)
    pub fn get_cache_stats(&self) -> (usize, usize) {
        let entries = self.cache.graph_count();
        // Rough estimate: 1KB per cached graph entry
        let memory_bytes = entries * 1024;
        (entries, memory_bytes)
    }

    /// Clear all stored data
    pub fn clear_all_data(&self) -> Result<(), StorageError> {
        // Clear cache
        self.clear_cache()?;

        // Clear persistent store
        if let Some(persistent_store) = &self.persistent_store {
            if let Some(driver) = &self.storage_driver {
                persistent_store
                    .clear(driver.as_ref().as_ref())
                    .map_err(|e| {
                        StorageError::PersistenceError(format!(
                            "Failed to clear persistent data: {}",
                            e
                        ))
                    })?;
            }
        }

        // Clear memory store
        if let Some(memory_store) = &self.memory_store {
            if let Some(driver) = &self.storage_driver {
                memory_store.clear(driver.as_ref().as_ref()).map_err(|e| {
                    StorageError::PersistenceError(format!("Failed to clear memory data: {}", e))
                })?;
            }
        }

        Ok(())
    }

    /// Save catalog provider data to persistent storage
    pub fn save_catalog_provider(&self, name: &str, data: &[u8]) -> Result<(), StorageError> {
        if let Some(persistent_store) = &self.persistent_store {
            if let Some(driver) = &self.storage_driver {
                persistent_store
                    .save_catalog_provider(driver.as_ref().as_ref(), name, data)
                    .map_err(|e| {
                        StorageError::PersistenceError(format!(
                            "Failed to save catalog provider '{}': {}",
                            name, e
                        ))
                    })
            } else {
                Err(StorageError::PersistenceError(
                    "No storage driver available".to_string(),
                ))
            }
        } else if let Some(memory_store) = &self.memory_store {
            if let Some(driver) = &self.storage_driver {
                memory_store
                    .save_catalog_provider(driver.as_ref().as_ref(), name, data)
                    .map_err(|e| {
                        StorageError::PersistenceError(format!(
                            "Failed to save catalog provider '{}': {}",
                            name, e
                        ))
                    })
            } else {
                Err(StorageError::PersistenceError(
                    "No storage driver available".to_string(),
                ))
            }
        } else {
            Err(StorageError::PersistenceError(
                "No storage backend available".to_string(),
            ))
        }
    }

    /// Load catalog provider data from persistent storage
    pub fn load_catalog_provider(&self, name: &str) -> Result<Option<Vec<u8>>, StorageError> {
        if let Some(persistent_store) = &self.persistent_store {
            if let Some(driver) = &self.storage_driver {
                match persistent_store.load_catalog_provider(driver.as_ref().as_ref(), name) {
                    Ok(data) => Ok(Some(data)),
                    Err(e) => {
                        debug!(
                            "Could not load catalog provider '{}' from persistent store: {}",
                            name, e
                        );
                        Ok(None)
                    }
                }
            } else {
                Ok(None)
            }
        } else if let Some(memory_store) = &self.memory_store {
            if let Some(driver) = &self.storage_driver {
                match memory_store.load_catalog_provider(driver.as_ref().as_ref(), name) {
                    Ok(data) => Ok(Some(data)),
                    Err(e) => {
                        debug!(
                            "Could not load catalog provider '{}' from memory store: {}",
                            name, e
                        );
                        Ok(None)
                    }
                }
            } else {
                Ok(None)
            }
        } else {
            Ok(None)
        }
    }

    /// Check if catalog provider data exists
    pub fn has_catalog_provider(&self, name: &str) -> Result<bool, StorageError> {
        if let Some(persistent_store) = &self.persistent_store {
            if let Some(driver) = &self.storage_driver {
                persistent_store
                    .has_catalog_provider(driver.as_ref().as_ref(), name)
                    .map_err(|e| {
                        StorageError::PersistenceError(format!(
                            "Failed to check catalog provider '{}': {}",
                            name, e
                        ))
                    })
            } else {
                Ok(false)
            }
        } else if let Some(memory_store) = &self.memory_store {
            if let Some(driver) = &self.storage_driver {
                memory_store
                    .has_catalog_provider(driver.as_ref().as_ref(), name)
                    .map_err(|e| {
                        StorageError::PersistenceError(format!(
                            "Failed to check catalog provider '{}': {}",
                            name, e
                        ))
                    })
            } else {
                Ok(false)
            }
        } else {
            Ok(false)
        }
    }

    /// List all catalog provider names
    pub fn list_catalog_providers(&self) -> Result<Vec<String>, StorageError> {
        if let Some(persistent_store) = &self.persistent_store {
            if let Some(driver) = &self.storage_driver {
                persistent_store
                    .list_catalog_providers(driver.as_ref().as_ref())
                    .map_err(|e| {
                        StorageError::PersistenceError(format!(
                            "Failed to list catalog providers: {}",
                            e
                        ))
                    })
            } else {
                Ok(Vec::new())
            }
        } else if let Some(memory_store) = &self.memory_store {
            if let Some(driver) = &self.storage_driver {
                memory_store
                    .list_catalog_providers(driver.as_ref().as_ref())
                    .map_err(|e| {
                        StorageError::PersistenceError(format!(
                            "Failed to list catalog providers: {}",
                            e
                        ))
                    })
            } else {
                Ok(Vec::new())
            }
        } else {
            Ok(Vec::new())
        }
    }

    /// Explicitly shutdown the storage manager and release file locks
    /// This should be called before dropping to ensure clean resource cleanup
    pub fn shutdown(&self) -> Result<(), StorageError> {
        // Just flush for now - the main issue is ensuring proper drop order
        // in the test environments

        // Flush persistent store
        if let Some(_persistent_store) = &self.persistent_store {
            // We can't call shutdown since it requires &mut, but flush should be sufficient
            // The key is ensuring proper drop order in tests
            debug!("Flushing storage manager during shutdown");
        }

        Ok(())
    }
}

impl std::fmt::Debug for StorageManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StorageManager")
            .field("storage_type", &self.storage_type)
            .field("has_storage_driver", &self.storage_driver.is_some())
            .field("has_persistent_store", &self.persistent_store.is_some())
            .field("has_memory_store", &self.memory_store.is_some())
            .finish_non_exhaustive()
    }
}