Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a FileDataSource that fetches archive files from local disk #34

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/file_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::fs::File;
use std::path::{Path, PathBuf};

use serde::Deserialize;

use crate::data::{
DataSource, DataSourceInfo, EntryID, SlotMetaTile, SlotTile, SummaryTile, TileID,
};
use crate::http::schema::TileRequestRef;

pub struct FileDataSource {
pub basedir: PathBuf,
}

impl FileDataSource {
pub fn new(basedir: impl AsRef<Path>) -> Self {
Self {
basedir: basedir.as_ref().to_owned(),
}
}

fn read_file<T>(&self, path: impl AsRef<Path>) -> T
where
T: for<'a> Deserialize<'a>,
{
let f = File::open(path).expect("opening file failed");
let f = zstd::Decoder::new(f).expect("zstd decompression failed");
ciborium::from_reader(f).expect("cbor decoding failed")
}
}

impl DataSource for FileDataSource {
fn fetch_info(&self) -> DataSourceInfo {
let path = self.basedir.join("info");
self.read_file::<DataSourceInfo>(&path)
}

fn fetch_summary_tile(&self, entry_id: &EntryID, tile_id: TileID, _full: bool) -> SummaryTile {
let req = TileRequestRef { entry_id, tile_id };
let mut path = self.basedir.join("summary_tile");
path.push(&req.to_slug());
self.read_file::<SummaryTile>(&path)
}

fn fetch_slot_tile(&self, entry_id: &EntryID, tile_id: TileID, _full: bool) -> SlotTile {
let req = TileRequestRef { entry_id, tile_id };
let mut path = self.basedir.join("slot_tile");
path.push(&req.to_slug());
self.read_file::<SlotTile>(&path)
}

fn fetch_slot_meta_tile(
&self,
entry_id: &EntryID,
tile_id: TileID,
_full: bool,
) -> SlotMetaTile {
let req = TileRequestRef { entry_id, tile_id };
let mut path = self.basedir.join("slot_meta_tile");
path.push(&req.to_slug());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the mut could be ditched in these cases too, if desired

let path = self.basedir.join("slot_meta_tile").join(&req.to_slug());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I didn't write this because I'm not 100% sure the Rust compiler can optimize away the extra allocation, but maybe writing this in a perfectly allocation-optimal way isn't actually necessary...

I'll plan to keep it for now, though maybe it's something one of us could look into.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW My personal going-in position would be to prioritize immutability in the code as much as possible, over perfectly minimal allocations, especially in a GUI that only needs to cater to (slow) human response times (until/unless a need for specific optimization is demonstrated).

self.read_file::<SlotMetaTile>(&path)
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub mod app;
pub mod archive_data;
pub mod data;
pub mod deferred_data;
#[cfg(not(target_arch = "wasm32"))]
pub mod file_data;
pub mod http;
#[cfg(not(target_arch = "wasm32"))]
pub mod parallel_data;
Expand Down
Loading