use std::sync::Arc;
use crate::store::Shards;
use crate::store_glue::lock_write;
#[cfg(all(feature = "replicate", not(target_arch = "wasm32")))]
#[allow(clippy::type_complexity)] pub(crate) fn wire_replication(
config: &crate::config::Config,
shards: &Shards,
) -> crate::KevyResult<(
Option<crate::replica_runner::ReplicaRunner>,
Option<crate::replica_source::ReplicaSource>,
Option<Arc<std::sync::Mutex<kevy_replicate::feed::FeedSource>>>,
)> {
let replica_runner = crate::replica_glue::spawn_replica_runner(config, shards);
let replica_source = spawn_writer_source(config, shards)?;
let feed = crate::store::Store::feed_open(config)?;
if let Some(f) = &feed {
for shard in shards.iter() {
let mut g = lock_write(shard);
g.feed = Some(f.clone());
}
}
Ok((replica_runner, replica_source, feed))
}
#[cfg(all(feature = "replicate", not(target_arch = "wasm32")))]
fn spawn_writer_source(
config: &crate::config::Config,
shards: &Shards,
) -> crate::KevyResult<Option<crate::replica_source::ReplicaSource>> {
let Some(addr) = config.embed_writer_listen_addr.as_ref() else {
return Ok(None);
};
let shards_for_snap: Shards = Arc::clone(shards);
let snapshot: crate::replica_source::SnapshotProvider =
Arc::new(move || crate::replica_source::freeze_and_serialize(&shards_for_snap));
let rs = crate::replica_source::ReplicaSource::spawn(
addr,
config.embed_writer_backlog_bytes,
snapshot,
)?;
let shared = rs.shared_source();
for shard in shards.iter() {
let mut g = lock_write(shard);
g.writer_source = Some(shared.clone());
}
Ok(Some(rs))
}
pub(crate) fn wire_blocker(shards: &Shards) -> Arc<crate::ops_blocking::Blocker> {
let blocker = Arc::new(crate::ops_blocking::Blocker::new());
for shard in shards.iter() {
lock_write(shard).blocker = Some(blocker.clone());
}
blocker
}
#[cfg(feature = "index")]
pub(crate) fn wire_registries(
shards: &Shards,
) -> (Arc<crate::ops_index::IndexReg>, Arc<crate::ops_view::ViewReg>) {
let indexes = Arc::new(crate::ops_index::IndexReg::default());
let views = Arc::new(crate::ops_view::ViewReg::default());
for shard in shards.iter() {
let mut g = lock_write(shard);
g.idx_reg = Some(indexes.clone());
g.view_reg = Some(views.clone());
}
(indexes, views)
}
#[allow(clippy::too_many_arguments)] pub(crate) fn build_guard(
open_report: &Arc<crate::metric::OpenReport>,
reaper_stop: Option<Arc<std::sync::atomic::AtomicBool>>,
reaper_join: Option<std::thread::JoinHandle<()>>,
shards: &Shards,
#[cfg(all(feature = "replicate", not(target_arch = "wasm32")))]
replica_runner: Option<crate::replica_runner::ReplicaRunner>,
#[cfg(all(feature = "replicate", not(target_arch = "wasm32")))]
replica_source: Option<crate::replica_source::ReplicaSource>,
#[cfg(all(feature = "replicate", not(target_arch = "wasm32")))]
feed: &Option<Arc<std::sync::Mutex<kevy_replicate::feed::FeedSource>>>,
config: &crate::config::Config,
) -> Arc<crate::store_inner::DropGuard> {
#[cfg(any(target_arch = "wasm32", not(feature = "replicate")))]
let _ = config;
Arc::new(crate::store_inner::DropGuard {
shutdown: std::sync::atomic::AtomicBool::new(false),
open_report: open_report.clone(),
#[cfg(feature = "index")]
tables: Arc::new(crate::ops_table::TableReg::default()),
reaper_stop,
reaper_join: std::sync::Mutex::new(reaper_join),
shards_for_flush: shards.clone(),
#[cfg(all(feature = "replicate", not(target_arch = "wasm32")))]
replica_runner,
#[cfg(all(feature = "replicate", not(target_arch = "wasm32")))]
feed_close: match (feed, &config.data_dir) {
(Some(f), Some(d)) => Some((f.clone(), d.clone())),
_ => None,
},
#[cfg(all(feature = "replicate", not(target_arch = "wasm32")))]
replica_source,
})
}