Skip to content

Commit

Permalink
SharedArrayData -> SharedPtr<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
a10y committed Dec 16, 2024
1 parent e50c864 commit a893a12
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 39 deletions.
33 changes: 17 additions & 16 deletions src/components/array.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
use std::{collections::VecDeque, sync::Arc};

use dioxus::{logger::tracing, prelude::*};
use vortex::{stats::ArrayStatistics, validity::ArrayValidity, ArrayDType};
use vortex::{stats::ArrayStatistics, validity::ArrayValidity, ArrayDType, ArrayData};

use crate::{
components::{dtype::DTypeInfo, stats::Statistics, Heading},
SharedArrayData,
SharedPtr,
};

/// Show some basic info about an ArrayView.
#[component]
pub fn ArrayView(file_name: String, history_stack: Signal<VecDeque<SharedArrayData>>) -> Element {
pub fn ArrayView(
file_name: String,
history_stack: Signal<VecDeque<SharedPtr<ArrayData>>>,
) -> Element {
// Use the history stack to take data from the front/back of the stack
let array = history_stack()[0].clone();
let stats = array.inner.statistics().to_set();
let stats = array.statistics().to_set();

rsx! {
div { class: "flex flex-col mt-4",
Expand Down Expand Up @@ -43,23 +46,23 @@ pub fn ArrayView(file_name: String, history_stack: Signal<VecDeque<SharedArrayDa

div { class: "my-12 h-0.5 border-t-0 bg-neutral-100/30" }

DTypeInfo { dtype: array.inner.dtype().clone() }
DTypeInfo { dtype: array.dtype().clone() }

div { class: "my-12 h-0.5 border-t-0 bg-neutral-100/30" }

if !array.inner.children().is_empty() {
if !array.children().is_empty() {
ArrayChildren { history_stack }
}
}
}
}

#[component]
fn ArraySummary(array: SharedArrayData, file_name: String) -> Element {
let size = humansize::format_size(array.inner.nbytes(), humansize::BINARY);
let row_count = array.inner.len();
let encoding_id = array.inner.encoding().id().to_string();
let null_count = array.inner.logical_validity().null_count()?;
fn ArraySummary(array: SharedPtr<ArrayData>, file_name: String) -> Element {
let size = humansize::format_size(array.nbytes(), humansize::BINARY);
let row_count = array.len();
let encoding_id = array.encoding().id().to_string();
let null_count = array.logical_validity().null_count()?;
let null_pct: f64 = 100. * (null_count as f64) / (row_count as f64);

rsx! {
Expand Down Expand Up @@ -137,7 +140,7 @@ fn ArraySummary(array: SharedArrayData, file_name: String) -> Element {
}

#[component]
pub fn ArrayChildren(mut history_stack: Signal<VecDeque<SharedArrayData>>) -> Element {
pub fn ArrayChildren(mut history_stack: Signal<VecDeque<SharedPtr<ArrayData>>>) -> Element {
let array = history_stack()[0].clone();
rsx! {
Heading { text: "Child Arrays" }
Expand All @@ -149,7 +152,7 @@ pub fn ArrayChildren(mut history_stack: Signal<VecDeque<SharedArrayData>>) -> El

table { class: "table-auto w-full min-w-max max-h-96 overflow-y-scroll text-left border-collapse",
tbody { class: "border-b border-1 border-zinc-50/10",
for (idx , child) in array.inner.children().iter().cloned().enumerate() {
for (idx , child) in array.children().iter().cloned().enumerate() {
tr {
class: "font-normal border-b border-1 border-zinc-50/10",
// Interactivity
Expand All @@ -161,9 +164,7 @@ pub fn ArrayChildren(mut history_stack: Signal<VecDeque<SharedArrayData>>) -> El
tracing::info!("descending into the {idx} child");
history_stack
.write()
.push_front(SharedArrayData {
inner: Arc::new(child),
});
.push_front(SharedPtr(Arc::new(child)));
},
td { class: "p-2",
p { class: "block font-sans text-sm antialiased leading-normal",
Expand Down
9 changes: 9 additions & 0 deletions src/components/array_info/dict.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use dioxus::prelude::*;
use vortex::dict::DictArray;

use crate::SharedPtr;

#[component]
pub fn DictInfo(array: SharedPtr<DictArray>) -> Element {
rsx! {}
}
9 changes: 9 additions & 0 deletions src/components/array_info/fsst.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use dioxus::prelude::*;
use vortex::fsst::FSSTArray;

use crate::SharedPtr;

#[component]
pub fn FSSTInfo(array: SharedPtr<FSSTArray>) -> Element {
rsx! {}
}
43 changes: 42 additions & 1 deletion src/components/array_info/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
pub mod chunked;
use std::sync::Arc;

use dict::DictInfo;
use dioxus::prelude::*;
use fsst::FSSTInfo;
use vortex::{
dict::{DictArray, DictEncoding},
encoding::Encoding,
fsst::{FSSTArray, FSSTEncoding},
ArrayData,
};

use crate::SharedPtr;

pub mod dict;
pub mod fsst;

/// Show encoding-specific information about an array.
///
/// This is a parent component that will dynamically delegate to the encoding-specific child component.
#[component]
pub fn EncodingInfo(array: SharedPtr<ArrayData>) -> Element {
let array = (*array).clone();
let encoding = array.encoding().id();

if encoding == FSSTEncoding::ID {
// Show FSST symbol table info.
let array = SharedPtr(Arc::new(FSSTArray::try_from(array)?));
rsx! {
FSSTInfo { array }
}
} else if encoding == DictEncoding::ID {
// Show dictionary size, value sample, histogram
let array = SharedPtr(Arc::new(DictArray::try_from(array)?));
rsx! {
DictInfo { array }
}
} else {
// Empty component
rsx! {}
}
}
30 changes: 19 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::VecDeque;
use std::sync::Arc;
use std::{collections::VecDeque, ops::Deref};

use bytes::Bytes;
use components::{array::ArrayView, AppHeader, ErrorMessage};
Expand All @@ -12,7 +12,6 @@ use vortex::{
};

mod components;
mod vortex_file;

const FAVICON: Asset = asset!("/assets/favicon.ico");
const MAIN_CSS: Asset = asset!("/assets/main.css");
Expand Down Expand Up @@ -40,7 +39,7 @@ fn Home() -> Element {
let mut read_error = use_signal::<Option<String>>(|| None);

// Push the latest history for each of these elements.
let mut history_stack: Signal<VecDeque<SharedArrayData>> = use_signal(VecDeque::new);
let mut history_stack: Signal<VecDeque<SharedPtr<ArrayData>>> = use_signal(VecDeque::new);

let read_files = move |file_engine: Arc<dyn FileEngine>| async move {
file_name.set(file_engine.files()[0].clone());
Expand All @@ -61,9 +60,7 @@ fn Home() -> Element {
Ok(array) => {
*read_error.write() = None;
// Push onto the front of the stack.
history_stack.write().push_front(SharedArrayData {
inner: Arc::new(array),
});
history_stack.write().push_front(SharedPtr(Arc::new(array)));
}
Err(err) => *read_error.write() = Some(err.to_string()),
},
Expand Down Expand Up @@ -102,14 +99,25 @@ fn Home() -> Element {
}
}

/// Wrapper around any Arc<T> to make it usable as a Dioxus Prop.
///
/// In Dioxus, all props need must be `PartialEq`. Not all of the Vortex types implement that trait,
/// so this makes it easy for us to pass anything as a component prop at the expense of an added allocation.
#[derive(Clone)]
pub struct SharedArrayData {
inner: Arc<ArrayData>,
pub struct SharedPtr<T>(pub Arc<T>);

// Deref impl allowing us to call immutable methods on `T` directly without unwrapping.
impl<T> Deref for SharedPtr<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
self.0.deref()
}
}

// impl PartialEq so we can use it as a Prop.
impl PartialEq for SharedArrayData {
// Impl PartialEq that ensures two SharedPtr's have the same pointee.
impl<T> PartialEq for SharedPtr<T> {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
Arc::ptr_eq(&self.0, &other.0)
}
}
11 changes: 0 additions & 11 deletions src/vortex_file.rs

This file was deleted.

0 comments on commit a893a12

Please sign in to comment.