Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/reader_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use crate::common::fetch::{
use crate::error::{Result, WasmResult};
use crate::read_options::{JsReaderOptions, ReaderOptions};
use crate::reader::cast_metadata_view_types;
use crate::utils;
use futures::channel::oneshot;
use futures::future::BoxFuture;
use object_store::coalesce_ranges;
use std::ops::Range;
use std::sync::Arc;
use std::io;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::spawn_local;

Expand Down Expand Up @@ -320,21 +322,20 @@ impl WrappedFile {
Self { inner, size }
}

pub async fn get_bytes(&mut self, range: Range<u64>) -> Vec<u8> {
pub async fn get_bytes(&mut self, range: Range<u64>) -> io::Result<Vec<u8>> {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a result type in this crate

pub type Result<T> = std::result::Result<T, ParquetWasmError>;
pub type WasmResult<T> = std::result::Result<T, JsError>;

use js_sys::Uint8Array;
use wasm_bindgen_futures::JsFuture;
let (sender, receiver) = oneshot::channel();
let file = self.inner.clone();
spawn_local(async move {
let subset_blob = file
.slice_with_i32_and_i32(
range.start.try_into().unwrap(),
range.end.try_into().unwrap(),
)
.unwrap();
let buf = JsFuture::from(subset_blob.array_buffer()).await.unwrap();
let out_vec = Uint8Array::new_with_byte_offset(&buf, 0).to_vec();
sender.send(out_vec).unwrap();
if range.start <= utils::MAX_EXACT_INTEGER && range.end <= utils::MAX_EXACT_INTEGER {
let subset_blob = file.slice_with_f64_and_f64(range.start as f64, range.end as f64).unwrap();
let buf = JsFuture::from(subset_blob.array_buffer()).await.unwrap();
let out_vec = Uint8Array::new_with_byte_offset(&buf, 0).to_vec();
sender.send(Ok(out_vec)).unwrap();
} else {
sender.send(Err(io::Error::new(io::ErrorKind::Unsupported, format!("{range:?} is too large to convert into a Blob slice")))).unwrap();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a PlatformSupportError

#[error("Platform error: `{0}`")]
PlatformSupportError(String),

since JS in the web doesn't support a slice larger than a number

};
});

receiver.await.unwrap()
Expand All @@ -347,10 +348,13 @@ async fn get_bytes_file(
) -> parquet::errors::Result<Bytes> {
let (sender, receiver) = oneshot::channel();
spawn_local(async move {
let result: Bytes = file.get_bytes(range).await.into();
let result = match file.get_bytes(range).await {
Ok(result) => Ok(Bytes::from(result)),
Err(e) => Err(e)
};

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use ? after the await

sender.send(result).unwrap()
});
let data = receiver.await.unwrap();
let data = receiver.await.unwrap()?;
Ok(data)
}

Expand All @@ -375,11 +379,11 @@ impl AsyncFileReader for JsFileReader {
let (sender, receiver) = oneshot::channel();
let mut file = self.file.clone();
spawn_local(async move {
let result: Bytes = file.get_bytes(range).await.into();
let result = file.get_bytes(range).await.map(Bytes::from);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like you did here

sender.send(result).unwrap()
});
let data = receiver.await.unwrap();
Ok(data)
Ok(data?)
}
.boxed()
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use wasm_bindgen::prelude::*;

pub const MAX_EXACT_INTEGER: u64 = ((1u64 << f64::MANTISSA_DIGITS) - 1) as u64;

/// Call this function at least once during initialization to get better error
// messages if the underlying Rust code ever panics (creates uncaught errors).
#[cfg(feature = "console_error_panic_hook")]
Expand Down
Loading