mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-04 17:23:33 +01:00
io/storage: split out inner part of read/write
Allow reusing the read/write logic without requiring control over the file open.
This commit is contained in:
parent
f0e0db63d3
commit
5d40d061a4
3 changed files with 31 additions and 24 deletions
|
@ -145,7 +145,7 @@ impl<'a> SingleRegionProcessor<'a> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
storage::write(
|
storage::write_file(
|
||||||
&self.output_path,
|
&self.output_path,
|
||||||
&self.processed_region,
|
&self.processed_region,
|
||||||
REGION_FILE_META_VERSION,
|
REGION_FILE_META_VERSION,
|
||||||
|
|
|
@ -105,7 +105,7 @@ impl<'a> TileRenderer<'a> {
|
||||||
|
|
||||||
region_loader
|
region_loader
|
||||||
.get_or_try_init(|| async {
|
.get_or_try_init(|| async {
|
||||||
storage::read(&processed_path).context("Failed to load processed region data")
|
storage::read_file(&processed_path).context("Failed to load processed region data")
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.cloned()
|
.cloned()
|
||||||
|
|
|
@ -14,43 +14,50 @@ use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
use super::fs;
|
use super::fs;
|
||||||
|
|
||||||
|
/// Serializes data and writes it to a writer
|
||||||
|
pub fn write<W: Write, T: Serialize>(writer: &mut W, value: &T) -> Result<()> {
|
||||||
|
let data = bincode::serialize(value)?;
|
||||||
|
let len = u32::try_from(data.len())?;
|
||||||
|
let compressed = zstd::bulk::compress(&data, 1)?;
|
||||||
|
drop(data);
|
||||||
|
|
||||||
|
writer.write_all(&len.to_be_bytes())?;
|
||||||
|
writer.write_all(&compressed)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Serializes data and stores it in a file
|
/// Serializes data and stores it in a file
|
||||||
///
|
///
|
||||||
/// A timestamp is stored in an assiciated metadata file.
|
/// A timestamp is stored in an assiciated metadata file.
|
||||||
pub fn write<T: Serialize>(
|
pub fn write_file<T: Serialize>(
|
||||||
path: &Path,
|
path: &Path,
|
||||||
value: &T,
|
value: &T,
|
||||||
version: fs::FileMetaVersion,
|
version: fs::FileMetaVersion,
|
||||||
timestamp: SystemTime,
|
timestamp: SystemTime,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
fs::create_with_timestamp(path, version, timestamp, |file| {
|
fs::create_with_timestamp(path, version, timestamp, |file| write(file, value))
|
||||||
let data = bincode::serialize(value)?;
|
}
|
||||||
let len = u32::try_from(data.len())?;
|
|
||||||
let compressed = zstd::bulk::compress(&data, 1)?;
|
|
||||||
drop(data);
|
|
||||||
|
|
||||||
file.write_all(&len.to_be_bytes())?;
|
/// Reads data from a reader and deserializes it
|
||||||
file.write_all(&compressed)?;
|
pub fn read<R: Read, T: DeserializeOwned>(reader: &mut R) -> Result<T> {
|
||||||
|
let mut len_buf = [0u8; 4];
|
||||||
|
reader.read_exact(&mut len_buf)?;
|
||||||
|
let len = usize::try_from(u32::from_be_bytes(len_buf))?;
|
||||||
|
|
||||||
Ok(())
|
let mut compressed = vec![];
|
||||||
})
|
reader.read_to_end(&mut compressed)?;
|
||||||
|
let data = zstd::bulk::decompress(&compressed, len)?;
|
||||||
|
drop(compressed);
|
||||||
|
|
||||||
|
Ok(bincode::deserialize(&data)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads data from a file and deserializes it
|
/// Reads data from a file and deserializes it
|
||||||
pub fn read<T: DeserializeOwned>(path: &Path) -> Result<T> {
|
pub fn read_file<T: DeserializeOwned>(path: &Path) -> Result<T> {
|
||||||
(|| -> Result<T> {
|
(|| -> Result<T> {
|
||||||
let mut file = File::open(path)?;
|
let mut file = File::open(path)?;
|
||||||
|
read(&mut file)
|
||||||
let mut len_buf = [0u8; 4];
|
|
||||||
file.read_exact(&mut len_buf)?;
|
|
||||||
let len = usize::try_from(u32::from_be_bytes(len_buf))?;
|
|
||||||
|
|
||||||
let mut compressed = vec![];
|
|
||||||
file.read_to_end(&mut compressed)?;
|
|
||||||
let data = zstd::bulk::decompress(&compressed, len)?;
|
|
||||||
drop(compressed);
|
|
||||||
|
|
||||||
Ok(bincode::deserialize(&data)?)
|
|
||||||
})()
|
})()
|
||||||
.with_context(|| format!("Failed to read file {}", path.display()))
|
.with_context(|| format!("Failed to read file {}", path.display()))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue