#[cfg(target_arch = "wasm32")]
use std::sync::Arc;
#[cfg(target_arch = "wasm32")]
use dashmap::{DashMap, DashSet};
#[cfg(target_arch = "wasm32")]
use tokio::sync::broadcast;
use crate::engine::Db;
#[cfg(target_arch = "wasm32")]
use crate::engine::DbError;
#[cfg(target_arch = "wasm32")]
use crate::engine::config::DbConfig;
#[cfg(target_arch = "wasm32")]
use crate::engine::storage;
impl Db {
#[cfg(target_arch = "wasm32")]
pub async fn open_wasm(config: DbConfig) -> Result<Self, DbError> {
let db_name = &config.path;
let hot_threshold = config.hot_threshold;
let rate_limit_requests = config.rate_limit_requests.unwrap_or(1000);
let rate_limit_window = config.rate_limit_window.unwrap_or(60);
let max_body_size = config.max_body_size;
let max_keys_per_request = config.max_keys_per_request;
let encryption_key = config.encryption_key;
let sync_mode = config.sync_mode;
let post_backup_script = config.post_backup_script;
let state = Arc::new(DashMap::new());
let (tx, _rx) = broadcast::channel(1000);
let indexes: Arc<DashMap<String, DashMap<String, DashSet<String>>>> =
Arc::new(Default::default());
let query_heatmap = Arc::new(Default::default());
#[cfg(feature = "schema")]
let schemas = Arc::new(DashMap::new());
let storage: Arc<dyn crate::engine::storage::StorageBackend> = if config.in_memory {
Arc::new(storage::InMemoryStorage)
} else {
let base: Arc<dyn crate::engine::storage::StorageBackend> =
Arc::new(storage::OpfsStorage::new(db_name, sync_mode).await?);
let wrapped = if let Some(key) = encryption_key {
Arc::new(storage::EncryptedStorage::new(base, &key)) as Arc<dyn crate::engine::storage::StorageBackend>
} else {
base
};
storage::stream_into_state(
&*wrapped,
&state,
&indexes,
#[cfg(feature = "schema")] &schemas,
)?;
wrapped
};
Ok(Self {
state,
storage,
tx,
indexes,
query_heatmap,
hot_threshold,
rate_limit_requests,
rate_limit_window,
max_body_size,
max_keys_per_request,
#[cfg(feature = "schema")]
schemas,
post_backup_script,
tiered_mode: config.tiered_mode,
})
}
}