From 16a81047e37094be2c7cba24526ade4da76a2559 Mon Sep 17 00:00:00 2001 From: weisd Date: Sun, 3 Nov 2024 00:19:14 +0800 Subject: [PATCH] Optimization FileReader --- ecstore/src/disk/mod.rs | 51 ++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/ecstore/src/disk/mod.rs b/ecstore/src/disk/mod.rs index 10c9845aa..40e24c9ca 100644 --- a/ecstore/src/disk/mod.rs +++ b/ecstore/src/disk/mod.rs @@ -961,31 +961,34 @@ impl RemoteFileReader { #[async_trait::async_trait] impl Reader for RemoteFileReader { async fn read_at(&mut self, offset: usize, buf: &mut [u8]) -> Result { - unimplemented!() - // let request = ReadAtRequest { - // disk: self.root.to_string_lossy().to_string(), - // volume: self.volume.to_string(), - // path: self.path.to_string(), - // offset: offset.try_into().unwrap(), - // length: length.try_into().unwrap(), - // }; - // self.tx.send(request).await?; + let request = ReadAtRequest { + disk: self.root.to_string_lossy().to_string(), + volume: self.volume.to_string(), + path: self.path.to_string(), + offset: offset.try_into().unwrap(), + // length: length.try_into().unwrap(), + length: 0, + }; + self.tx.send(request).await?; - // if let Some(resp) = self.resp_stream.next().await { - // let resp = resp?; - // if resp.success { - // info!("read at stream success"); - // Ok((resp.data, resp.read_size.try_into().unwrap())) - // } else { - // let error_info = resp.error_info.unwrap_or("".to_string()); - // info!("read at stream failed: {}", error_info); - // Err(Error::from_string(error_info)) - // } - // } else { - // let error_info = "can not get response"; - // info!("read at stream failed: {}", error_info); - // Err(Error::from_string(error_info)) - // } + if let Some(resp) = self.resp_stream.next().await { + let resp = resp?; + if resp.success { + info!("read at stream success"); + + buf.copy_from_slice(&resp.data); + + Ok(resp.read_size.try_into().unwrap()) + } else { + let error_info = resp.error_info.unwrap_or("".to_string()); + info!("read at stream failed: {}", error_info); + Err(Error::from_string(error_info)) + } + } else { + let error_info = "can not get response"; + info!("read at stream failed: {}", error_info); + Err(Error::from_string(error_info)) + } } async fn seek(&mut self, offset: usize) -> Result<()> { unimplemented!()