use std::time::Duration;
use zenoh::qos::Priority;
use zenoh::query::ConsolidationMode;
use crate::compress::{ChunkCompression, pack};
use crate::error::{BlobError, Result};
use crate::hash::Hash;
use crate::store::ContentStore;
use crate::tree::TreeIndex;
use crate::wire::{ENC_CHUNK, ENC_INDEX, encode};
use crate::{store_key, tree_key};
pub async fn publish_chunk(
session: &zenoh::Session,
store_prefix: &str,
hash: &Hash,
bytes: &[u8],
compression: ChunkCompression,
) -> Result<()> {
crate::paths::validate_serve_prefix(store_prefix)?;
session
.put(
store_key(store_prefix, Hash::ALGO, hash),
pack(bytes, compression)?,
)
.encoding(ENC_CHUNK)
.await
.map_err(BlobError::zenoh)
}
pub async fn publish_store(
session: &zenoh::Session,
store_prefix: &str,
store: &dyn ContentStore,
compression: ChunkCompression,
) -> Result<u32> {
let mut published = 0u32;
for hash in store.hashes()? {
let bytes = store
.get(&hash)
.ok_or_else(|| BlobError::NotFound(hash.to_string()))?;
publish_chunk(session, store_prefix, &hash, &bytes, compression).await?;
published += 1;
}
Ok(published)
}
pub async fn publish_index(
session: &zenoh::Session,
tree_prefix: &str,
index: &TreeIndex,
) -> Result<()> {
crate::paths::validate_serve_prefix(tree_prefix)?;
let payload = encode(index)?;
session
.put(tree_key(tree_prefix, &index.id), payload)
.encoding(ENC_INDEX)
.priority(Priority::DataLow)
.await
.map_err(BlobError::zenoh)
}
pub async fn publish_snapshot(
session: &zenoh::Session,
store_prefix: &str,
tree_prefix: &str,
index: &TreeIndex,
store: &dyn ContentStore,
compression: ChunkCompression,
settle: Duration,
) -> Result<()> {
publish_store(session, store_prefix, store, compression).await?;
publish_index(session, tree_prefix, index).await?;
let needed = index.needed_chunks();
let mut probes: Vec<String> = vec![tree_key(tree_prefix, &index.id)];
let n = needed.len();
if n > 0 {
let step = (n / 6).max(1);
let mut picked: Vec<usize> = (0..n).step_by(step).collect();
picked.push(n - 1);
picked.dedup();
probes.extend(
picked
.into_iter()
.map(|i| store_key(store_prefix, Hash::ALGO, &needed[i])),
);
}
let deadline = tokio::time::Instant::now() + settle;
for key in probes {
loop {
if probe_key(session, &key).await? {
break;
}
if tokio::time::Instant::now() >= deadline {
return Err(BlobError::Protocol(format!(
"storage did not settle within {settle:?}: {key} still unanswered"
)));
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
Ok(())
}
async fn probe_key(session: &zenoh::Session, key: &str) -> Result<bool> {
let replies = session
.get(key)
.consolidation(ConsolidationMode::None)
.priority(Priority::DataLow)
.timeout(Duration::from_secs(2))
.await
.map_err(BlobError::zenoh)?;
while let Ok(reply) = replies.recv_async().await {
if reply.result().is_ok() {
return Ok(true);
}
}
Ok(false)
}