Skip to content

Commit 26110a1

Browse files
committed
implement folder loading support
1 parent 6592efb commit 26110a1

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// TODO: issue -> HIVE partitiong, timestamp regexes, zip files, that kinda thing
2+
3+
/// Loads entire directories, using the appropriate [`crate::DataLoader`]:s for each files within.
4+
pub struct DirectoryLoader;
5+
6+
impl crate::DataLoader for DirectoryLoader {
7+
#[inline]
8+
fn name(&self) -> String {
9+
"rerun.data_loaders.Directory".into()
10+
}
11+
12+
#[cfg(not(target_arch = "wasm32"))]
13+
fn load_from_file(
14+
&self,
15+
store_id: re_log_types::StoreId,
16+
dirpath: std::path::PathBuf,
17+
tx: std::sync::mpsc::Sender<crate::LoadedData>,
18+
) -> Result<(), crate::DataLoaderError> {
19+
if dirpath.is_file() {
20+
return Ok(()); // simply not interested
21+
}
22+
23+
re_tracing::profile_function!(dirpath.display().to_string());
24+
25+
re_log::debug!(?dirpath, loader = self.name(), "Loading directory…",);
26+
27+
for entry in walkdir::WalkDir::new(&dirpath) {
28+
let entry = match entry {
29+
Ok(entry) => entry,
30+
Err(err) => {
31+
re_log::error!(loader = self.name(), ?dirpath, %err, "Failed to open filesystem entry");
32+
continue;
33+
}
34+
};
35+
36+
let filepath = entry.path();
37+
if filepath.is_file() {
38+
let store_id = store_id.clone();
39+
let filepath = filepath.to_owned();
40+
let tx = tx.clone();
41+
42+
// NOTE: spawn is fine, this whole function is native-only.
43+
rayon::spawn(move || {
44+
let data = match crate::load_file::load(&store_id, &filepath, false, None) {
45+
Ok(data) => data,
46+
Err(err) => {
47+
re_log::error!(?filepath, %err, "Failed to load directory entry");
48+
return;
49+
}
50+
};
51+
52+
for datum in data {
53+
if tx.send(datum).is_err() {
54+
break;
55+
}
56+
}
57+
});
58+
}
59+
}
60+
61+
Ok(())
62+
}
63+
64+
#[inline]
65+
fn load_from_file_contents(
66+
&self,
67+
_store_id: re_log_types::StoreId,
68+
_path: std::path::PathBuf,
69+
_contents: std::borrow::Cow<'_, [u8]>,
70+
_tx: std::sync::mpsc::Sender<crate::LoadedData>,
71+
) -> Result<(), crate::DataLoaderError> {
72+
// TODO: zip file supports
73+
Ok(()) // simply not interested
74+
}
75+
}

crates/re_data_source/src/data_loader/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ static BUILTIN_LOADERS: Lazy<Vec<Arc<dyn DataLoader>>> = Lazy::new(|| {
157157
vec![
158158
Arc::new(RrdLoader) as Arc<dyn DataLoader>,
159159
Arc::new(ArchetypeLoader),
160+
Arc::new(DirectoryLoader),
160161
]
161162
});
162163

@@ -169,7 +170,9 @@ pub fn iter_loaders() -> impl ExactSizeIterator<Item = Arc<dyn DataLoader>> {
169170
// ---
170171

171172
mod loader_archetype;
173+
mod loader_directory;
172174
mod loader_rrd;
173175

174176
pub use self::loader_archetype::ArchetypeLoader;
177+
pub use self::loader_directory::DirectoryLoader;
175178
pub use self::loader_rrd::RrdLoader;

crates/re_data_source/src/load_file.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ fn extension(path: &std::path::Path) -> String {
9090
/// This does _not_ access the filesystem.
9191
#[inline]
9292
fn is_builtin(path: &std::path::Path, is_dir: bool) -> bool {
93-
!is_dir && crate::is_known_file_extension(&extension(path))
93+
is_dir || crate::is_known_file_extension(&extension(path))
9494
}
9595

9696
/// Prepares an adequate [`re_log_types::StoreInfo`] [`LogMsg`] given the input.
97-
fn prepare_store_info(
97+
pub(crate) fn prepare_store_info(
9898
store_id: &re_log_types::StoreId,
9999
file_source: FileSource,
100100
path: &std::path::Path,
@@ -131,7 +131,7 @@ fn prepare_store_info(
131131
/// - On native, this is filled asynchronously from other threads.
132132
/// - On wasm, this is pre-filled synchronously.
133133
#[cfg_attr(target_arch = "wasm32", allow(clippy::needless_pass_by_value))]
134-
fn load(
134+
pub(crate) fn load(
135135
store_id: &re_log_types::StoreId,
136136
path: &std::path::Path,
137137
is_dir: bool,
@@ -193,7 +193,7 @@ fn load(
193193
/// Forwards the data in `rx_loader` to `tx`, taking care of necessary conversions, if any.
194194
///
195195
/// Runs asynchronously from another thread on native, synchronously on wasm.
196-
fn send(
196+
pub(crate) fn send(
197197
store_id: &re_log_types::StoreId,
198198
rx_loader: std::sync::mpsc::Receiver<LoadedData>,
199199
tx: &Sender<LogMsg>,

0 commit comments

Comments
 (0)