use std::fs::File;
use std::io;
#[cfg(all(not(unix), not(windows)))]
use std::io::Read;
#[cfg(all(not(unix), not(windows)))]
use std::io::Seek;
#[cfg(unix)]
use std::os::unix::fs::FileExt;
#[cfg(windows)]
use std::os::windows::fs::FileExt;
use std::path::Path;
use std::sync::Arc;
use futures::FutureExt;
use futures::future::BoxFuture;
use vortex_array::buffer::BufferHandle;
use vortex_buffer::Alignment;
use vortex_buffer::ByteBufferMut;
use vortex_error::VortexResult;
use crate::CoalesceConfig;
use crate::VortexReadAt;
use crate::runtime::Handle;
#[cfg(not(target_arch = "wasm32"))]
pub fn read_exact_at(file: &File, buffer: &mut [u8], offset: u64) -> io::Result<()> {
#[cfg(unix)]
{
file.read_exact_at(buffer, offset)
}
#[cfg(windows)]
{
let mut bytes_read = 0;
while bytes_read < buffer.len() {
let read = file.seek_read(&mut buffer[bytes_read..], offset + bytes_read as u64)?;
if read == 0 {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"failed to fill whole buffer",
));
}
bytes_read += read;
}
Ok(())
}
#[cfg(all(not(unix), not(windows)))]
{
use std::io::SeekFrom;
let mut file_ref = file;
file_ref.seek(SeekFrom::Start(offset))?;
file_ref.read_exact(buffer)
}
}
pub const DEFAULT_CONCURRENCY: usize = 32;
pub struct FileReadAt {
uri: Arc<str>,
file: Arc<File>,
handle: Handle,
}
impl FileReadAt {
pub fn open(path: impl AsRef<Path>, handle: Handle) -> VortexResult<Self> {
let path = path.as_ref();
let uri = path.to_string_lossy().to_string().into();
let file = Arc::new(File::open(path)?);
Ok(Self { uri, file, handle })
}
}
impl VortexReadAt for FileReadAt {
fn uri(&self) -> Option<&Arc<str>> {
Some(&self.uri)
}
fn coalesce_config(&self) -> Option<CoalesceConfig> {
Some(CoalesceConfig::file())
}
fn concurrency(&self) -> usize {
DEFAULT_CONCURRENCY
}
fn size(&self) -> BoxFuture<'static, VortexResult<u64>> {
let file = self.file.clone();
async move {
let metadata = file.metadata()?;
Ok(metadata.len())
}
.boxed()
}
fn read_at(
&self,
offset: u64,
length: usize,
alignment: Alignment,
) -> BoxFuture<'static, VortexResult<BufferHandle>> {
let file = self.file.clone();
let handle = self.handle.clone();
async move {
handle
.spawn_blocking(move || {
let mut buffer = ByteBufferMut::with_capacity_aligned(length, alignment);
unsafe { buffer.set_len(length) };
read_exact_at(&file, &mut buffer, offset)?;
Ok(BufferHandle::new_host(buffer.freeze()))
})
.await
}
.boxed()
}
}