Skip to content

Commit f3cff17

Browse files
committed
implement folder loading support
1 parent e9ac988 commit f3cff17

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
@@ -161,6 +161,7 @@ static BUILTIN_LOADERS: Lazy<Vec<Arc<dyn DataLoader>>> = Lazy::new(|| {
161161
vec![
162162
Arc::new(RrdLoader) as Arc<dyn DataLoader>,
163163
Arc::new(ArchetypeLoader),
164+
Arc::new(DirectoryLoader),
164165
]
165166
});
166167

@@ -173,7 +174,9 @@ pub fn iter_loaders() -> impl ExactSizeIterator<Item = Arc<dyn DataLoader>> {
173174
// ---
174175

175176
mod loader_archetype;
177+
mod loader_directory;
176178
mod loader_rrd;
177179

178180
pub use self::loader_archetype::ArchetypeLoader;
181+
pub use self::loader_directory::DirectoryLoader;
179182
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,
@@ -210,7 +210,7 @@ fn load(
210210
/// Forwards the data in `rx_loader` to `tx`, taking care of necessary conversions, if any.
211211
///
212212
/// Runs asynchronously from another thread on native, synchronously on wasm.
213-
fn send(
213+
pub(crate) fn send(
214214
store_id: &re_log_types::StoreId,
215215
rx_loader: std::sync::mpsc::Receiver<LoadedData>,
216216
tx: &Sender<LogMsg>,

0 commit comments

Comments
 (0)