absurder_sql/storage/
vfs_sync.rs

1//! VFS Sync module extracted from block_storage.rs
2//! This module contains the ACTUAL VFS sync and global storage management logic
3
4use std::collections::{HashMap, HashSet};
5use std::cell::RefCell;
6#[allow(unused_imports)]
7use crate::types::DatabaseError;
8#[allow(unused_imports)]
9use super::metadata::BlockMetadataPersist;
10
11// Global storage for WASM to maintain data across instances
12#[cfg(target_arch = "wasm32")]
13thread_local! {
14    pub static GLOBAL_STORAGE: RefCell<HashMap<String, HashMap<u64, Vec<u8>>>> = RefCell::new(HashMap::new());
15    static GLOBAL_ALLOCATION_MAP: RefCell<HashMap<String, HashSet<u64>>> = RefCell::new(HashMap::new());
16}
17
18// Global storage mirrors for native builds
19#[cfg(not(target_arch = "wasm32"))]
20thread_local! {
21    static GLOBAL_STORAGE_TEST: RefCell<HashMap<String, HashMap<u64, Vec<u8>>>> = RefCell::new(HashMap::new());
22    static GLOBAL_ALLOCATION_MAP_TEST: RefCell<HashMap<String, HashSet<u64>>> = RefCell::new(HashMap::new());
23}
24
25#[cfg(target_arch = "wasm32")]
26thread_local! {
27    static GLOBAL_METADATA: RefCell<HashMap<String, HashMap<u64, BlockMetadataPersist>>> = RefCell::new(HashMap::new());
28}
29// Per-DB commit marker for WASM builds to simulate atomic commit semantics
30#[cfg(target_arch = "wasm32")]
31thread_local! {
32    pub static GLOBAL_COMMIT_MARKER: RefCell<HashMap<String, u64>> = RefCell::new(HashMap::new());
33}
34
35// Global registry of active BlockStorage instances for VFS sync
36#[cfg(target_arch = "wasm32")]
37thread_local! {
38    static STORAGE_REGISTRY: RefCell<HashMap<String, std::rc::Weak<std::cell::RefCell<super::BlockStorage>>>> = RefCell::new(HashMap::new());
39}
40
41/// Access to global storage for BlockStorage (internal use)
42#[cfg(target_arch = "wasm32")]
43pub fn with_global_storage<F, R>(f: F) -> R
44where
45    F: FnOnce(&RefCell<HashMap<String, HashMap<u64, Vec<u8>>>>) -> R
46{
47    GLOBAL_STORAGE.with(f)
48}
49
50#[cfg(not(target_arch = "wasm32"))]
51pub fn with_global_storage<F, R>(f: F) -> R
52where
53    F: FnOnce(&RefCell<HashMap<String, HashMap<u64, Vec<u8>>>>) -> R
54{
55    GLOBAL_STORAGE_TEST.with(f)
56}
57
58/// Access to global metadata for BlockStorage (internal use)
59#[cfg(target_arch = "wasm32")]
60pub fn with_global_metadata<F, R>(f: F) -> R
61where
62    F: FnOnce(&RefCell<HashMap<String, HashMap<u64, BlockMetadataPersist>>>) -> R
63{
64    GLOBAL_METADATA.with(f)
65}
66
67#[cfg(not(target_arch = "wasm32"))]
68pub fn with_global_metadata<F, R>(f: F) -> R
69where
70    F: FnOnce(&RefCell<HashMap<String, HashMap<u64, BlockMetadataPersist>>>) -> R
71{
72    // For native tests, use the shared GLOBAL_METADATA_TEST from block_storage
73    use super::block_storage::GLOBAL_METADATA_TEST;
74    GLOBAL_METADATA_TEST.with(f)
75}
76
77/// Access to global commit marker for BlockStorage (internal use)
78#[cfg(target_arch = "wasm32")]
79pub fn with_global_commit_marker<F, R>(f: F) -> R
80where
81    F: FnOnce(&RefCell<HashMap<String, u64>>) -> R
82{
83    GLOBAL_COMMIT_MARKER.with(f)
84}
85
86#[cfg(not(target_arch = "wasm32"))]
87pub fn with_global_commit_marker<F, R>(f: F) -> R
88where
89    F: FnOnce(&RefCell<HashMap<String, u64>>) -> R
90{
91    // For native tests, we need a test-only commit marker storage
92    thread_local! {
93        static GLOBAL_COMMIT_MARKER_TEST: RefCell<HashMap<String, u64>> = RefCell::new(HashMap::new());
94    }
95    GLOBAL_COMMIT_MARKER_TEST.with(f)
96}
97
98/// Access to allocation map (internal use)
99#[cfg(target_arch = "wasm32")]
100pub fn with_global_allocation_map<F, R>(f: F) -> R
101where
102    F: FnOnce(&RefCell<HashMap<String, HashSet<u64>>>) -> R
103{
104    GLOBAL_ALLOCATION_MAP.with(f)
105}
106
107#[cfg(not(target_arch = "wasm32"))]
108pub fn with_global_allocation_map<F, R>(f: F) -> R
109where
110    F: FnOnce(&RefCell<HashMap<String, HashSet<u64>>>) -> R
111{
112    GLOBAL_ALLOCATION_MAP_TEST.with(f)
113}
114
115/// Access to storage registry (internal use)
116#[cfg(target_arch = "wasm32")]
117pub fn with_storage_registry<F, R>(f: F) -> R
118where
119    F: FnOnce(&RefCell<HashMap<String, std::rc::Weak<std::cell::RefCell<super::BlockStorage>>>>) -> R
120{
121    STORAGE_REGISTRY.with(f)
122}