Skip to content

Commit

Permalink
Add a FileDataSource that fetches archive files from local disk (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottslaughter authored Oct 19, 2023
1 parent 6417711 commit 9d587fb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
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());
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

0 comments on commit 9d587fb

Please sign in to comment.