Skip to content

Commit 4d8b9ee

Browse files
committed
and more cleanup
1 parent 297b918 commit 4d8b9ee

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

crates/top/re_sdk/src/lenses.rs

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl<S: LogSink> LensesSink<S> {
2727
}
2828

2929
/// Adds a [`Lens`] to this sink.
30-
pub fn with_lens(mut self, lens: LensN) -> Self {
30+
pub fn with_lens(mut self, lens: Lens) -> Self {
3131
self.registry.lenses.push(lens);
3232
self
3333
}
@@ -79,11 +79,11 @@ impl<S: LogSink> LogSink for LensesSink<S> {
7979

8080
#[derive(Default)]
8181
struct LensRegistry {
82-
lenses: Vec<LensN>,
82+
lenses: Vec<Lens>,
8383
}
8484

8585
impl LensRegistry {
86-
fn relevant(&self, chunk: &Chunk) -> impl Iterator<Item = &LensN> {
86+
fn relevant(&self, chunk: &Chunk) -> impl Iterator<Item = &Lens> {
8787
self.lenses
8888
.iter()
8989
.filter(|lens| lens.input.entity_path_filter.matches(chunk.entity_path()))
@@ -205,29 +205,40 @@ pub enum Error {
205205
///
206206
/// Individual operations are wrapped to hide their implementation details.
207207
pub enum Op {
208-
Cast(op::Cast),
208+
/// Extracts a specific field from a `StructArray`.
209209
AccessField(op::AccessField),
210+
211+
/// Efficiently casts a component to a new `DataType`.
212+
Cast(op::Cast),
213+
214+
/// A user-defined arbitrary function to convert a component column.
210215
Func(Box<dyn Fn(ListArray) -> Result<ListArray, Error> + Sync + Send>),
211216
}
212217

213218
impl Op {
219+
/// Extracts a specific field from a `StructArray`.
214220
pub fn access_field(field_name: impl Into<String>) -> Self {
215221
Self::AccessField(op::AccessField {
216222
field_name: field_name.into(),
217223
})
218224
}
219225

226+
/// Efficiently casts a component to a new `DataType`.
220227
pub fn cast(data_type: DataType) -> Self {
221228
Self::Cast(op::Cast {
222229
to_inner_type: data_type,
223230
})
224231
}
225232

226-
// TODO: this should be improved
233+
/// Ignores any input and returns a constant `ListArray`.
234+
///
235+
/// Mostl commonly used with [`LensBuilder::static_output_column`].
236+
/// When used in non-static columns this function will _not_ guarantee the correct amount of rows.
227237
pub fn constant(value: ListArray) -> Self {
228238
Self::func(move |_| Ok(value.clone()))
229239
}
230240

241+
/// A user-defined arbitrary function to convert a component column.
231242
pub fn func<F>(func: F) -> Self
232243
where
233244
F: Fn(ListArray) -> Result<ListArray, Error> + Send + Sync + 'static,
@@ -259,14 +270,15 @@ struct OutputColumn {
259270
}
260271

261272
/// Provides convenient function to create a [`Lens`].
262-
pub struct LensBuilder(LensN);
273+
pub struct LensBuilder(Lens);
263274

264275
impl LensBuilder {
276+
/// The column on which this [`Lens`] will operate on.
265277
pub fn input_column(
266278
entity_path_filter: EntityPathFilter,
267279
component: impl Into<ComponentIdentifier>,
268280
) -> Self {
269-
Self(LensN {
281+
Self(Lens {
270282
input: InputColumn {
271283
entity_path_filter: entity_path_filter.resolve_without_substitutions(),
272284
component: component.into(),
@@ -275,6 +287,7 @@ impl LensBuilder {
275287
})
276288
}
277289

290+
/// Can be used to define one or more output columns that are derived from the [`Self::input_column`].
278291
pub fn output_column(
279292
mut self,
280293
entity_path: impl Into<EntityPath>,
@@ -291,6 +304,7 @@ impl LensBuilder {
291304
self
292305
}
293306

307+
/// Can be used to define one or more output columns that are derived from the [`Self::input_column`].
294308
pub fn static_output_column(
295309
mut self,
296310
entity_path: impl Into<EntityPath>,
@@ -307,7 +321,8 @@ impl LensBuilder {
307321
self
308322
}
309323

310-
pub fn build(self) -> LensN {
324+
// Finalizes this builder and returns the corresponding lens.
325+
pub fn build(self) -> Lens {
311326
self.0
312327
}
313328
}
@@ -320,15 +335,15 @@ impl LensBuilder {
320335
///
321336
/// # Assumptions
322337
///
323-
/// Works on component column within a chunk. Because what goes into a chunk
324-
/// is non-deterministic, and dependent on the batcher no assumptions should be
325-
/// made for values contained in each row of a column.
326-
pub struct LensN {
338+
/// Works on component columns within a chunk. Because what goes into a chunk
339+
/// is non-deterministic, and dependent on the batcher, no assumptions should be
340+
/// made for values across rows.
341+
pub struct Lens {
327342
input: InputColumn,
328343
outputs: Vec<OutputColumn>,
329344
}
330345

331-
impl LensN {
346+
impl Lens {
332347
/// Returns a new [`LensBuilder`] with the given input column.
333348
pub fn input_column(
334349
entity_path_filter: EntityPathFilter,
@@ -338,15 +353,15 @@ impl LensN {
338353
}
339354
}
340355

341-
impl LensN {
356+
impl Lens {
342357
/// Applies this lens and crates one or more chunks.
343358
fn apply(&self, chunk: &Chunk) -> Vec<Chunk> {
344359
let found = chunk
345360
.components()
346361
.iter()
347362
.find(|(descr, _array)| descr.component == self.input.component);
348363

349-
// TODO: This means we drop chunks that belong to the same entity but don't have the component.
364+
// This means we drop chunks that belong to the same entity but don't have the component.
350365
let Some((_component_descr, list_array)) = found else {
351366
return Default::default();
352367
};
@@ -566,7 +581,7 @@ mod test {
566581
println!("{original_chunk}");
567582

568583
let destructure =
569-
LensN::input_column(EntityPathFilter::parse_forgiving("nullability"), "structs")
584+
Lens::input_column(EntityPathFilter::parse_forgiving("nullability"), "structs")
570585
.output_column(
571586
"nullability/a",
572587
Scalars::descriptor_scalars(),
@@ -591,7 +606,7 @@ mod test {
591606
println!("{original_chunk}");
592607

593608
let destructure =
594-
LensN::input_column(EntityPathFilter::parse_forgiving("nullability"), "structs")
609+
Lens::input_column(EntityPathFilter::parse_forgiving("nullability"), "structs")
595610
.output_column(
596611
"nullability/b",
597612
Scalars::descriptor_scalars(),
@@ -633,19 +648,18 @@ mod test {
633648
Ok(builder.finish())
634649
};
635650

636-
let count =
637-
LensN::input_column(EntityPathFilter::parse_forgiving("nullability"), "strings")
638-
.output_column(
639-
"nullability/b_count",
640-
ComponentDescriptor::partial("counts"),
641-
[Op::func(count_fn)],
642-
)
643-
.output_column(
644-
"nullability/b_count",
645-
ComponentDescriptor::partial("original"),
646-
[], // no operations
647-
)
648-
.build();
651+
let count = Lens::input_column(EntityPathFilter::parse_forgiving("nullability"), "strings")
652+
.output_column(
653+
"nullability/b_count",
654+
ComponentDescriptor::partial("counts"),
655+
[Op::func(count_fn)],
656+
)
657+
.output_column(
658+
"nullability/b_count",
659+
ComponentDescriptor::partial("original"),
660+
[], // no operations
661+
)
662+
.build();
649663

650664
let pipeline = LensRegistry {
651665
lenses: vec![count],
@@ -675,7 +689,7 @@ mod test {
675689
metadata_builder_b.append(true);
676690

677691
let static_lens =
678-
LensN::input_column(EntityPathFilter::parse_forgiving("nullability"), "strings")
692+
Lens::input_column(EntityPathFilter::parse_forgiving("nullability"), "strings")
679693
.static_output_column(
680694
"nullability/static",
681695
ComponentDescriptor::partial("static_metadata_a"),

examples/rust/lenses/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use arrow::{
77
use rerun::{
88
DynamicArchetype, RecordingStream, Scalars, SeriesLines, SeriesPoints, TextDocument, TimeCell,
99
external::re_log,
10-
lenses::{LensN, LensesSink, Op},
10+
lenses::{Lens, LensesSink, Op},
1111
sink::GrpcSink,
1212
};
1313

14-
fn lens_flag() -> anyhow::Result<LensN> {
14+
fn lens_flag() -> anyhow::Result<Lens> {
1515
let step_fn = |list_array: ListArray| {
1616
let (_, offsets, values, nulls) = list_array.into_parts();
1717
let flag_array = values.as_any().downcast_ref::<StringArray>().unwrap();
@@ -52,7 +52,7 @@ fn lens_flag() -> anyhow::Result<LensN> {
5252
.next()
5353
.unwrap();
5454

55-
let lens = LensN::input_column("/flag".parse()?, "com.Example.Flag:flag")
55+
let lens = Lens::input_column("/flag".parse()?, "com.Example.Flag:flag")
5656
.output_column("/flag", Scalars::descriptor_scalars(), [Op::func(step_fn)])
5757
.static_output_column(
5858
"/flag",
@@ -72,11 +72,11 @@ fn lens_flag() -> anyhow::Result<LensN> {
7272
fn main() -> anyhow::Result<()> {
7373
re_log::setup_logging();
7474

75-
let instruction = LensN::input_column("/instructions".parse()?, "com.Example.Instruction:text")
75+
let instruction = Lens::input_column("/instructions".parse()?, "com.Example.Instruction:text")
7676
.output_column("instructions", TextDocument::descriptor_text(), [])
7777
.build();
7878

79-
let destructure = LensN::input_column("/nested".parse()?, "com.Example.Nested:payload")
79+
let destructure = Lens::input_column("/nested".parse()?, "com.Example.Nested:payload")
8080
.output_column(
8181
"nested/a",
8282
Scalars::descriptor_scalars(),

0 commit comments

Comments
 (0)