Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Mar 4, 2024
1 parent 29baeb3 commit b932284
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 85 deletions.
49 changes: 42 additions & 7 deletions crates/re_sdk/src/recording_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,10 +1095,11 @@ impl RecordingStream {
#[cfg(feature = "data_loaders")]
pub fn log_file_from_path(
&self,
settings: &re_data_source::DataLoaderSettings,
filepath: impl AsRef<std::path::Path>,
entity_path_prefix: Option<EntityPath>,
timeless: bool,
) -> RecordingStreamResult<()> {
self.log_file(settings, filepath, None)
self.log_file(filepath, None, entity_path_prefix, timeless)
}

/// Logs the given `contents` using all [`re_data_source::DataLoader`]s available.
Expand All @@ -1112,20 +1113,27 @@ impl RecordingStream {
#[cfg(feature = "data_loaders")]
pub fn log_file_from_contents(
&self,
settings: &re_data_source::DataLoaderSettings,
filepath: impl AsRef<std::path::Path>,
contents: std::borrow::Cow<'_, [u8]>,
entity_path_prefix: Option<EntityPath>,
timeless: bool,
) -> RecordingStreamResult<()> {
self.log_file(settings, filepath, Some(contents))
self.log_file(filepath, Some(contents), entity_path_prefix, timeless)
}

#[cfg(feature = "data_loaders")]
fn log_file(
&self,
settings: &re_data_source::DataLoaderSettings,
filepath: impl AsRef<std::path::Path>,
contents: Option<std::borrow::Cow<'_, [u8]>>,
entity_path_prefix: Option<EntityPath>,
timeless: bool,
) -> RecordingStreamResult<()> {
let Some(store_info) = self.store_info().clone() else {
re_log::warn!("Ignored call to log_file() because RecordingStream has not been properly initialized");
return Ok(());
};

let filepath = filepath.as_ref();
let has_contents = contents.is_some();

Expand All @@ -1134,16 +1142,43 @@ impl RecordingStream {
re_smart_channel::SmartChannelSource::File(filepath.into()),
);

let settings = crate::DataLoaderSettings {
store_id: store_info.store_id,
opened_store_id: None,
entity_path_prefix,
timepoint: (!timeless).then(|| {
self.with(|inner| {
// Get the current time on all timelines, for the current recording, on the current
// thread…
let mut now = self.now();

// …and then also inject the current recording tick into it.
let tick = inner
.tick
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
now.insert(Timeline::log_tick(), tick.into());

now
})
.unwrap_or_default()
}), // timepoint: self.time,
};

if let Some(contents) = contents {
re_data_source::load_from_file_contents(
settings,
&settings,
re_log_types::FileSource::Sdk,
filepath,
contents,
&tx,
)?;
} else {
re_data_source::load_from_path(settings, re_log_types::FileSource::Sdk, filepath, &tx)?;
re_data_source::load_from_path(
&settings,
re_log_types::FileSource::Sdk,
filepath,
&tx,
)?;
}
drop(tx);

Expand Down
28 changes: 4 additions & 24 deletions crates/rerun_c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,20 +734,10 @@ fn rr_log_file_from_path_impl(
) -> Result<(), CError> {
let stream = recording_stream(stream)?;

let recording_id = stream
.store_info()
.ok_or_else(|| {
CError::new(
CErrorCode::RecordingStreamRuntimeFailure,
&format!("Couldn't load file {filepath:?}: no recording available"),
)
})?
.store_id;
let settings = re_sdk::DataLoaderSettings::recommended(recording_id);

let filepath = filepath.as_str("filepath")?;
stream
.log_file_from_path(&settings, filepath)
// TODO(cmc): expose settings
.log_file_from_path(filepath, None, true)
.map_err(|err| {
CError::new(
CErrorCode::RecordingStreamRuntimeFailure,
Expand Down Expand Up @@ -779,22 +769,12 @@ fn rr_log_file_from_contents_impl(
) -> Result<(), CError> {
let stream = recording_stream(stream)?;

let recording_id = stream
.store_info()
.ok_or_else(|| {
CError::new(
CErrorCode::RecordingStreamRuntimeFailure,
&format!("Couldn't load file {filepath:?}: no recording available"),
)
})?
.store_id;
let settings = re_sdk::DataLoaderSettings::recommended(recording_id);

let filepath = filepath.as_str("filepath")?;
let contents = contents.as_bytes("contents")?;

stream
.log_file_from_contents(&settings, filepath, std::borrow::Cow::Borrowed(contents))
// TODO(cmc): expose settings
.log_file_from_contents(filepath, std::borrow::Cow::Borrowed(contents), None, true)
.map_err(|err| {
CError::new(
CErrorCode::RecordingStreamRuntimeFailure,
Expand Down
7 changes: 5 additions & 2 deletions examples/rust/log_file/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,23 @@ fn run(rec: &rerun::RecordingStream, args: &Args) -> anyhow::Result<()> {
let mut settings = rerun::DataLoaderSettings::recommended(rec.store_info().unwrap().store_id);
settings.entity_path_prefix = Some("log_file_example".into());

let prefix = Some("log_file_example".into());

for filepath in &args.filepaths {
let filepath = filepath.as_path();

if !args.from_contents {
// Either log the file using its path…
rec.log_file_from_path(&settings, filepath)?;
rec.log_file_from_path(filepath, prefix.clone(), true /* timeless */)?;
} else {
// …or using its contents if you already have them loaded for some reason.
if filepath.is_file() {
let contents = std::fs::read(filepath)?;
rec.log_file_from_contents(
&settings,
filepath,
std::borrow::Cow::Borrowed(&contents),
prefix.clone(),
true, /* timeless */
)?;
}
}
Expand Down
16 changes: 3 additions & 13 deletions rerun_py/rerun_sdk/rerun/_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,8 @@ def log_components(
def log_file_from_path(
file_path: str | Path,
*,
recording_id: str | None = None,
entity_path_prefix: str | None = None,
timeless: bool | None = None,
timeless: bool = False,
recording: RecordingStream | None = None,
) -> None:
r"""
Expand All @@ -308,14 +307,11 @@ def log_file_from_path(
file_path:
Path to the file to be logged.
recording_id:
The recommended `RecordingId` to log the data to.
entity_path_prefix:
What should the logged entity paths be prefixed with?
timeless:
Should the logged data be timeless?
Should the logged data be timeless? (default: False)
recording:
Specifies the [`rerun.RecordingStream`][] to use. If left unspecified,
Expand All @@ -326,7 +322,6 @@ def log_file_from_path(

bindings.log_file_from_path(
Path(file_path),
recording_id=recording_id,
entity_path_prefix=entity_path_prefix,
timeless=timeless,
recording=recording,
Expand All @@ -339,7 +334,6 @@ def log_file_from_contents(
file_path: str | Path,
file_contents: bytes,
*,
recording_id: str | None = None,
entity_path_prefix: str | None = None,
timeless: bool | None = None,
recording: RecordingStream | None = None,
Expand All @@ -362,14 +356,11 @@ def log_file_from_contents(
file_contents:
Contents to be logged.
recording_id:
The recommended `RecordingId` to log the data to.
entity_path_prefix:
What should the logged entity paths be prefixed with?
timeless:
Should the logged data be timeless?
Should the logged data be timeless? (default: False)
recording:
Specifies the [`rerun.RecordingStream`][] to use. If left unspecified,
Expand All @@ -381,7 +372,6 @@ def log_file_from_contents(
bindings.log_file_from_contents(
Path(file_path),
file_contents,
recording_id=recording_id,
entity_path_prefix=entity_path_prefix,
timeless=timeless,
recording=recording,
Expand Down
54 changes: 15 additions & 39 deletions rerun_py/src/python_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,53 +967,40 @@ fn log_arrow_msg(
#[pyfunction]
#[pyo3(signature = (
file_path,
recording_id = None,
entity_path_prefix = None,
timeless = None,
recording=None,
timeless = false,
recording = None,
))]
fn log_file_from_path(
py: Python<'_>,
file_path: std::path::PathBuf,
recording_id: Option<String>,
entity_path_prefix: Option<String>,
timeless: Option<bool>,
timeless: bool,
recording: Option<&PyRecordingStream>,
) -> PyResult<()> {
log_file(
py,
file_path,
None,
recording_id,
entity_path_prefix,
timeless,
recording,
)
log_file(py, file_path, None, entity_path_prefix, timeless, recording)
}

#[pyfunction]
#[pyo3(signature = (
file_path,
file_contents,
recording_id = None,
entity_path_prefix = None,
timeless = None,
recording=None,
timeless = false,
recording = None,
))]
fn log_file_from_contents(
py: Python<'_>,
file_path: std::path::PathBuf,
file_contents: &[u8],
recording_id: Option<String>,
entity_path_prefix: Option<String>,
timeless: Option<bool>,
timeless: bool,
recording: Option<&PyRecordingStream>,
) -> PyResult<()> {
log_file(
py,
file_path,
Some(file_contents),
recording_id,
entity_path_prefix,
timeless,
recording,
Expand All @@ -1024,37 +1011,26 @@ fn log_file(
py: Python<'_>,
file_path: std::path::PathBuf,
file_contents: Option<&[u8]>,
recording_id: Option<String>,
entity_path_prefix: Option<String>,
timeless: Option<bool>,
timeless: bool,
recording: Option<&PyRecordingStream>,
) -> PyResult<()> {
let Some(recording) = get_data_recording(recording) else {
return Ok(());
};

let Some(recording_id) = recording
.store_info()
.map(|info| info.store_id.clone())
.or(recording_id.map(|id| StoreId::from_string(StoreKind::Recording, id)))
else {
return Ok(());
};

let settings = rerun::DataLoaderSettings {
store_id: recording_id,
opened_store_id: None,
entity_path_prefix: entity_path_prefix.map(Into::into),
timepoint: timeless.unwrap_or(false).then(TimePoint::timeless),
};

if let Some(contents) = file_contents {
recording
.log_file_from_contents(&settings, file_path, std::borrow::Cow::Borrowed(contents))
.log_file_from_contents(
file_path,
std::borrow::Cow::Borrowed(contents),
entity_path_prefix.map(Into::into),
timeless,
)
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?;
} else {
recording
.log_file_from_path(&settings, file_path)
.log_file_from_path(file_path, entity_path_prefix.map(Into::into), timeless)
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?;
}

Expand Down

0 comments on commit b932284

Please sign in to comment.