use std::collections::BTreeMap;
use std::path::Path;
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use serde_json::{Map, Value, json};
use super::DIR;
use super::node::State;
use crate::sync::lock;
use crate::{Error, Result};
const FILE: &str = "links";
const EVERY: Duration = Duration::from_secs(5);
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Links {
pub written: u64,
pub from: Option<String>,
pub followers: Vec<Follower>,
pub unheld: u64,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Follower {
pub address: String,
pub applied: BTreeMap<String, u64>,
}
pub fn links(dir: &Path) -> Result<Option<Links>> {
let path = dir.join(DIR).join(FILE);
let bytes = match std::fs::read(&path) {
Ok(bytes) => bytes,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(error) => return Err(Error::io(path.display().to_string(), error)),
};
let unreadable = |why: &str| Error::corrupt(format!("{}: {why}", path.display()));
let value: Value = serde_json::from_slice(&bytes).map_err(|error| unreadable(&error.to_string()))?;
Ok(Some(read(&value).ok_or_else(|| unreadable("not a node's links"))?))
}
fn read(value: &Value) -> Option<Links> {
let followers = value.get("followers")?.as_array()?.iter().map(read_follower).collect::<Option<Vec<_>>>()?;
Some(Links {
written: value.get("written")?.as_u64()?,
unheld: value.get("unheld").and_then(Value::as_u64).unwrap_or(0),
from: value.get("from").and_then(Value::as_str).map(str::to_owned),
followers,
})
}
fn read_follower(value: &Value) -> Option<Follower> {
let applied = value.get("applied")?.as_object()?;
Some(Follower {
address: value.get("address")?.as_str()?.to_owned(),
applied: applied.iter().map(|(file, at)| Some((file.clone(), at.as_u64()?))).collect::<Option<_>>()?,
})
}
pub(crate) fn keep_written(state: &Arc<State>) {
let path = state.dir.join(DIR).join(FILE);
while !state.stopping() {
if let Err(error) = write(state) {
tracing::debug!(target: "tinystore", %error, "a node could not write its links");
}
state.pause(EVERY);
}
let _ = std::fs::remove_file(path);
}
fn write(state: &State) -> Result<()> {
let logs = state.dir.join(DIR);
std::fs::create_dir_all(&logs).map_err(|error| Error::io(logs.display().to_string(), error))?;
let written = SystemTime::now().duration_since(UNIX_EPOCH).map_or(0, |since| since.as_millis() as u64);
let followers: Vec<Value> = lock(&state.followers)
.values()
.map(|follower| {
let applied: Map<String, Value> =
follower.applied.iter().map(|(file, at)| (file.clone(), Value::from(*at))).collect();
json!({ "address": follower.address, "applied": applied })
})
.collect();
let unheld = state.acks.unheld.load(std::sync::atomic::Ordering::Relaxed);
let from = lock(&state.upstream_at).clone();
let links = json!({ "written": written, "from": from, "followers": followers, "unheld": unheld });
let (path, next) = (logs.join(FILE), logs.join(format!("{FILE}.next")));
std::fs::write(&next, links.to_string()).map_err(|error| Error::io(next.display().to_string(), error))?;
std::fs::rename(&next, &path).map_err(|error| Error::io(path.display().to_string(), error))
}