use std::collections::BTreeSet;
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use super::snapshot::Snapshot;
use crate::store::fileio::{atomic_write, private, private_mkdir};
const FILE: &str = "notices.ack";
const LOCK: &str = "notices.lock";
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Told {
pub blocks: Vec<Block>,
pub failing: Vec<Failing>,
pub held: BTreeSet<String>,
pub unserved: BTreeSet<String>,
pub dropped: Option<Dropped>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Block {
pub key: String,
pub host: String,
pub reason: String,
pub since: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Failing {
pub key: String,
pub since: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Dropped {
pub first: Option<String>,
pub turns: u64,
pub bytes: u64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Claim {
Block(Block),
Unblock(String),
Failing(Failing),
Held(Vec<String>),
Unserved(Vec<String>),
Dropped(Dropped),
}
fn path(landing: &Path) -> PathBuf {
landing.join(FILE)
}
pub fn read(landing: &Path) -> (Option<Vec<u8>>, Told) {
let bytes = fs::read(path(landing)).ok();
let told = bytes
.as_deref()
.and_then(|bytes| serde_json::from_slice(bytes).ok())
.unwrap_or_default();
(bytes, told)
}
pub fn try_lock(landing: &Path) -> Option<File> {
private_mkdir(landing).ok()?;
let file = private()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(landing.join(LOCK))
.ok()?;
file.try_lock().ok()?;
Some(file)
}
pub fn write(landing: &Path, told: &Told) -> std::io::Result<()> {
let mut text = serde_json::to_string(told).expect("identities serialize");
text.push('\n');
atomic_write(&path(landing), &text, false)
}
impl Told {
pub fn after(&self, claims: &[&Claim], snapshot: &Snapshot) -> Self {
let mut told = self.clone();
for claim in claims {
match claim {
Claim::Block(block) => {
told.blocks.retain(|old| old.key != block.key);
told.blocks.push(block.clone());
}
Claim::Unblock(key) => told.blocks.retain(|old| old.key != *key),
Claim::Failing(failing) => {
told.failing.retain(|old| old.key != failing.key);
told.failing.push(failing.clone());
}
Claim::Held(names) => told.held.extend(names.iter().cloned()),
Claim::Unserved(names) => told.unserved.extend(names.iter().cloned()),
Claim::Dropped(dropped) => told.dropped = Some(dropped.clone()),
}
}
let (held, pending) = (snapshot.names(true), snapshot.names(false));
told.held.retain(|name| held.contains(name.as_str()));
told.unserved.retain(|name| pending.contains(name.as_str()));
told.failing.retain(|failing| {
snapshot.places.iter().any(|place| {
place.key == failing.key
&& place.record.as_ref().and_then(|r| r.failing_since.as_ref())
== Some(&failing.since)
})
});
told.blocks.sort_by(|a, b| a.key.cmp(&b.key));
told.failing.sort_by(|a, b| a.key.cmp(&b.key));
told
}
}