Moved complete save-load-logic to unshared site info

This commit is contained in:
p11 2025-05-31 14:58:33 +02:00
parent 9016a32b59
commit 1af69ca8e5

View File

@ -49,6 +49,7 @@ struct SharedSiteInfo {
down: AtomicUsize, down: AtomicUsize,
} }
#[derive(Default)]
struct SiteInfo { struct SiteInfo {
comments: Vec<Comment>, comments: Vec<Comment>,
visits: usize, visits: usize,
@ -57,6 +58,22 @@ struct SiteInfo {
} }
impl SharedSiteInfo { impl SharedSiteInfo {
fn new(info: SiteInfo) -> Self {
let SiteInfo {
comments,
visits,
up,
down,
} = info;
Self {
comments: Mutex::new(comments),
visits: visits.into(),
up: up.into(),
down: down.into(),
}
}
fn update( fn update(
self: &Arc<Self>, self: &Arc<Self>,
name: Option<Box<str>>, name: Option<Box<str>>,
@ -199,11 +216,10 @@ impl<S: SizeSettings> ToStream<S> for SiteInfo {
} }
} }
impl<S: SizeSettings> FromStream<S> for SharedSiteInfo { impl<S: SizeSettings> FromStream<S> for SiteInfo {
fn from_stream<R: Read>(stream: &mut R) -> Result<Self> { fn from_stream<R: Read>(stream: &mut R) -> Result<Self> {
let size = S::size_from_stream(stream)?; let size = S::size_from_stream(stream)?;
let comments = Mutex::new( let comments = (0..size)
(0..size)
.map(|_| { .map(|_| {
let name_bytes = <Vec<_> as FromStream<S>>::from_stream(stream)?; let name_bytes = <Vec<_> as FromStream<S>>::from_stream(stream)?;
let name = std::str::from_utf8(&name_bytes) let name = std::str::from_utf8(&name_bytes)
@ -217,12 +233,11 @@ impl<S: SizeSettings> FromStream<S> for SharedSiteInfo {
Ok(Comment { name, text }) Ok(Comment { name, text })
}) })
.collect::<Result<Vec<_>>>()?, .collect::<Result<Vec<_>>>()?;
);
let visits = S::size_from_stream(stream)?.into(); let visits = S::size_from_stream(stream)?;
let up = S::size_from_stream(stream)?.into(); let up = S::size_from_stream(stream)?;
let down = S::size_from_stream(stream)?.into(); let down = S::size_from_stream(stream)?;
Ok(Self { Ok(Self {
comments, comments,
@ -455,7 +470,10 @@ fn handle_connection(
.insert(Arc::new( .insert(Arc::new(
File::open(&data_path) File::open(&data_path)
.map(|mut file| { .map(|mut file| {
from_stream::<PortableSettings, _, _>(&mut file).unwrap_or_default() SharedSiteInfo::new(
from_stream::<PortableSettings, _, _>(&mut file)
.unwrap_or_default(),
)
}) })
.unwrap_or_default(), .unwrap_or_default(),
)) ))