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
4#[allow(unused_imports)]
5use super::metadata::BlockMetadataPersist;
6#[allow(unused_imports)]
7use crate::types::DatabaseError;
8use std::cell::RefCell;
9use std::collections::{HashMap, HashSet};
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/// Access to global storage for BlockStorage (internal use)
36#[cfg(target_arch = "wasm32")]
37pub fn with_global_storage<F, R>(f: F) -> R
38where
39    F: FnOnce(&RefCell<HashMap<String, HashMap<u64, Vec<u8>>>>) -> R,
40{
41    GLOBAL_STORAGE.with(f)
42}
43
44#[cfg(not(target_arch = "wasm32"))]
45pub fn with_global_storage<F, R>(f: F) -> R
46where
47    F: FnOnce(&RefCell<HashMap<String, HashMap<u64, Vec<u8>>>>) -> R,
48{
49    GLOBAL_STORAGE_TEST.with(f)
50}
51
52/// Access to global metadata for BlockStorage (internal use)
53#[cfg(target_arch = "wasm32")]
54pub fn with_global_metadata<F, R>(f: F) -> R
55where
56    F: FnOnce(&RefCell<HashMap<String, HashMap<u64, BlockMetadataPersist>>>) -> R,
57{
58    GLOBAL_METADATA.with(f)
59}
60
61#[cfg(not(target_arch = "wasm32"))]
62pub fn with_global_metadata<F, R>(f: F) -> R
63where
64    F: FnOnce(&parking_lot::Mutex<HashMap<String, HashMap<u64, BlockMetadataPersist>>>) -> R,
65{
66    // For native tests, use the shared GLOBAL_METADATA_TEST from block_storage
67    use super::block_storage::GLOBAL_METADATA_TEST;
68    GLOBAL_METADATA_TEST.with(f)
69}
70
71/// Access to global commit marker for BlockStorage (internal use)
72#[cfg(target_arch = "wasm32")]
73pub fn with_global_commit_marker<F, R>(f: F) -> R
74where
75    F: FnOnce(&RefCell<HashMap<String, u64>>) -> R,
76{
77    GLOBAL_COMMIT_MARKER.with(f)
78}
79
80#[cfg(not(target_arch = "wasm32"))]
81pub fn with_global_commit_marker<F, R>(f: F) -> R
82where
83    F: FnOnce(&RefCell<HashMap<String, u64>>) -> R,
84{
85    // For native tests, we need a test-only commit marker storage
86    thread_local! {
87        static GLOBAL_COMMIT_MARKER_TEST: RefCell<HashMap<String, u64>> = RefCell::new(HashMap::new());
88    }
89    GLOBAL_COMMIT_MARKER_TEST.with(f)
90}
91
92/// Access to allocation map (internal use)
93#[cfg(target_arch = "wasm32")]
94pub fn with_global_allocation_map<F, R>(f: F) -> R
95where
96    F: FnOnce(&RefCell<HashMap<String, HashSet<u64>>>) -> R,
97{
98    GLOBAL_ALLOCATION_MAP.with(f)
99}
100
101#[cfg(not(target_arch = "wasm32"))]
102pub fn with_global_allocation_map<F, R>(f: F) -> R
103where
104    F: FnOnce(&RefCell<HashMap<String, HashSet<u64>>>) -> R,
105{
106    GLOBAL_ALLOCATION_MAP_TEST.with(f)
107}