pub trait AsyncFileReader: Send {
// Required method
fn get_bytes(
&mut self,
range: Range<u64>,
) -> BoxFuture<'_, Result<Bytes, AvroError>>;
// Provided method
fn get_byte_ranges(
&mut self,
ranges: Vec<Range<u64>>,
) -> BoxFuture<'_, Result<Vec<Bytes>, AvroError>> { ... }
}Expand description
The asynchronous interface used by super::AsyncAvroFileReader to read avro files
Notes:
-
There is a default implementation for types that implement [
AsyncRead] and [AsyncSeek], for exampletokio::fs::File. -
Implementations for remote storage, such as the
object_storecrate, can implement this interface directly, typically by pairing a store handle with an object path and delegatingSelf::get_bytesandSelf::get_byte_rangesto ranged reads.super::SpawnedReadercan wrap such a reader to perform its I/O on a dedicated tokio runtime.
§Example: implementing AsyncFileReader for the object_store crate
use arrow_avro::errors::AvroError;
use arrow_avro::reader::AsyncFileReader;
use bytes::Bytes;
use futures::FutureExt;
use futures::future::BoxFuture;
use object_store::path::Path;
use object_store::{ObjectStore, ObjectStoreExt};
#[derive(Clone, Debug)]
struct ObjectStoreReader {
store: Arc<dyn ObjectStore>,
path: Path,
}
impl AsyncFileReader for ObjectStoreReader {
fn get_bytes(&mut self, range: Range<u64>) -> BoxFuture<'_, Result<Bytes, AvroError>> {
async move {
self.store
.get_range(&self.path, range)
.await
.map_err(|e| AvroError::General(e.to_string()))
}
.boxed()
}
fn get_byte_ranges(
&mut self,
ranges: Vec<Range<u64>>,
) -> BoxFuture<'_, Result<Vec<Bytes>, AvroError>> {
async move {
self.store
.get_ranges(&self.path, &ranges)
.await
.map_err(|e| AvroError::General(e.to_string()))
}
.boxed()
}
}Required Methods§
Provided Methods§
Trait Implementations§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl AsyncFileReader for Box<dyn AsyncFileReader + '_>
This allows Box<dyn AsyncFileReader + ’_> to be used as an AsyncFileReader,
impl AsyncFileReader for Box<dyn AsyncFileReader + '_>
This allows Box<dyn AsyncFileReader + ’_> to be used as an AsyncFileReader,