From 6cdb0e4f3525412a04bdb41665878d6c818cc837 Mon Sep 17 00:00:00 2001 From: John Spray Date: Tue, 15 Jun 2021 16:08:04 +0100 Subject: [PATCH] Additional async methods --- src/ceph.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/ceph.rs b/src/ceph.rs index 502b4df..f7a2419 100644 --- a/src/ceph.rs +++ b/src/ceph.rs @@ -26,7 +26,7 @@ use nom::number::complete::le_u32; use nom::IResult; use serde_json; -use crate::completion::{with_completion, Completion}; +use crate::completion::with_completion; use crate::rados::*; #[cfg(feature = "rados_striper")] use crate::rados_striper::*; @@ -50,6 +50,8 @@ const CEPH_OSD_TMAP_SET: char = 's'; const CEPH_OSD_TMAP_CREATE: char = 'c'; const CEPH_OSD_TMAP_RM: char = 'r'; +const DEFAULT_READ_BYTES: usize = 64 * 1024; + #[derive(Debug, Clone)] pub enum CephHealth { Ok, @@ -962,6 +964,27 @@ impl IoCtx { .await } + /// Async variant of rados_object_append + pub async fn rados_async_object_append( + self: &Arc, + object_name: &str, + buffer: &[u8], + ) -> RadosResult { + self.ioctx_guard()?; + let obj_name_str = CString::new(object_name)?; + + with_completion(self.clone(), |c| unsafe { + rados_aio_append( + self.ioctx, + obj_name_str.as_ptr(), + c, + buffer.as_ptr() as *const ::libc::c_char, + buffer.len(), + ) + }) + .await + } + /// Async variant of rados_object_write_full pub async fn rados_async_object_write_full( self: &Arc, @@ -995,6 +1018,33 @@ impl IoCtx { .map(|_r| ()) } + /// Async variant of rados_object_read + pub async fn rados_async_object_read( + self: &Arc, + object_name: &str, + fill_buffer: &mut Vec, + read_offset: u64, + ) -> RadosResult { + self.ioctx_guard()?; + let obj_name_str = CString::new(object_name)?; + + if fill_buffer.capacity() == 0 { + fill_buffer.reserve_exact(DEFAULT_READ_BYTES); + } + + with_completion(self.clone(), |c| unsafe { + rados_aio_read( + self.ioctx, + obj_name_str.as_ptr(), + c, + fill_buffer.as_mut_ptr() as *mut c_char, + fill_buffer.capacity(), + read_offset, + ) + }) + .await + } + /// Efficiently copy a portion of one object to another /// If the underlying filesystem on the OSD supports it, this will be a /// copy-on-write clone. @@ -1052,7 +1102,7 @@ impl IoCtx { /// amount of bytes read /// The io context determines the snapshot to read from, if any was set by /// rados_ioctx_snap_set_read(). - /// Default read size is 64K unless you call Vec::with_capacity(1024*128) + /// Default read size is 64K unless you call Vec::with_capacity /// with a larger size. pub fn rados_object_read( &self, @@ -1064,7 +1114,7 @@ impl IoCtx { let object_name_str = CString::new(object_name)?; let mut len = fill_buffer.capacity(); if len == 0 { - fill_buffer.reserve_exact(1024 * 64); + fill_buffer.reserve_exact(DEFAULT_READ_BYTES); len = fill_buffer.capacity(); }