84 lines
3.4 KiB
Rust
84 lines
3.4 KiB
Rust
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
//! A dedicated thread that owns every open region file and answers load requests over a channel.
|
|
|
|
use std::collections::HashMap;
|
|
use std::collections::hash_map::Entry;
|
|
use std::path::{Path, PathBuf};
|
|
use std::thread::{self, JoinHandle};
|
|
|
|
use crossbeam_channel::{Receiver, Sender};
|
|
use shared::save::SaveError;
|
|
use shared::world::{ChunkData, ChunkPos};
|
|
|
|
use super::region_file::{RegionFile, region_coords, region_path};
|
|
|
|
/// A request sent to the save actor. Each variant carries its own one-shot reply channel.
|
|
pub enum SaveRequest {
|
|
/// Reads the stored chunk at a position, replying with the saved modification if one exists.
|
|
Read {
|
|
/// The chunk position to look up.
|
|
pos: ChunkPos,
|
|
/// The one-shot channel the actor replies on: `Ok(Some(data))` for a saved modification, `Ok(None)` when the chunk was never modified, or `Err` on a save-layer failure.
|
|
reply: Sender<Result<Option<ChunkData>, SaveError>>,
|
|
},
|
|
}
|
|
|
|
/// A handle to the running save actor: the request sender plus the owning thread's join handle.
|
|
pub struct SaveActor {
|
|
/// The sending end of the request channel; cloned into every worker so it can issue reads.
|
|
request_tx: Sender<SaveRequest>,
|
|
/// The actor thread handle, retained so it can be joined on shutdown.
|
|
// * Retained ahead of a dedicated shutdown path; not yet read because the server has no graceful-stop sequence.
|
|
#[expect(dead_code)]
|
|
handle: JoinHandle<()>,
|
|
}
|
|
|
|
impl SaveActor {
|
|
/// Spawns the actor thread, which owns the region files beneath `region_dir` for its lifetime.
|
|
#[must_use]
|
|
pub fn spawn(region_dir: PathBuf) -> Self {
|
|
let (request_tx, request_rx) = crossbeam_channel::unbounded::<SaveRequest>();
|
|
let handle = thread::spawn(move || actor_loop(®ion_dir, &request_rx));
|
|
Self { request_tx, handle }
|
|
}
|
|
|
|
/// Returns a fresh sender for a worker to issue requests through.
|
|
#[must_use]
|
|
pub fn sender(&self) -> Sender<SaveRequest> {
|
|
self.request_tx.clone()
|
|
}
|
|
}
|
|
|
|
/// The actor's run loop: it owns the region-file map and answers requests until every sender is dropped.
|
|
fn actor_loop(region_dir: &Path, request_rx: &Receiver<SaveRequest>) {
|
|
// The actor is the sole owner of this map, so region files need no lock of their own.
|
|
let mut regions: HashMap<(i32, i32), RegionFile> = HashMap::new();
|
|
while let Ok(request) = request_rx.recv() {
|
|
match request {
|
|
SaveRequest::Read { pos, reply } => {
|
|
let result = read_chunk(&mut regions, region_dir, pos);
|
|
// A send error means the requesting worker has gone away; the reply is simply dropped.
|
|
let _ = reply.send(result);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Reads the stored chunk at `pos`, opening and caching its region file on first access.
|
|
fn read_chunk(
|
|
regions: &mut HashMap<(i32, i32), RegionFile>,
|
|
region_dir: &Path,
|
|
pos: ChunkPos,
|
|
) -> Result<Option<ChunkData>, SaveError> {
|
|
let key = region_coords(pos.x, pos.z);
|
|
// The region file is opened once on first touch; every later read of a chunk in it hits the in-memory copy.
|
|
let region = match regions.entry(key) {
|
|
Entry::Occupied(entry) => entry.into_mut(),
|
|
Entry::Vacant(entry) => {
|
|
entry.insert(RegionFile::open(region_path(region_dir, pos.x, pos.z))?)
|
|
}
|
|
};
|
|
region.read_chunk(pos)
|
|
}
|