|
5 | 5 |
|
6 | 6 | use std::ffi::CStr; |
7 | 7 | use std::ffi::c_char; |
| 8 | +use std::ffi::c_void; |
8 | 9 | use std::ptr; |
| 10 | +use std::slice; |
9 | 11 | use std::sync::Arc; |
10 | 12 |
|
| 13 | +use bytes::Bytes; |
| 14 | +use vortex::buffer::ByteBuffer; |
11 | 15 | use vortex::error::VortexResult; |
12 | 16 | use vortex::error::vortex_ensure; |
13 | 17 | use vortex::expr::stats::Precision::Absent; |
14 | 18 | use vortex::expr::stats::Precision::Exact; |
15 | 19 | use vortex::expr::stats::Precision::Inexact; |
| 20 | +use vortex::file::OpenOptionsSessionExt; |
16 | 21 | use vortex::file::multi::MultiFileDataSource; |
17 | 22 | use vortex::io::runtime::BlockingRuntime; |
| 23 | +use vortex::layout::scan::multi::MultiLayoutDataSource; |
18 | 24 | use vortex::scan::DataSource; |
19 | 25 | use vortex::scan::DataSourceRef; |
20 | 26 |
|
@@ -104,6 +110,38 @@ pub unsafe extern "C-unwind" fn vx_data_source_new( |
104 | 110 | }) |
105 | 111 | } |
106 | 112 |
|
| 113 | +/// Create a data source from a single in-memory Vortex file. |
| 114 | +/// |
| 115 | +/// "buffer_len" is the length of "buffer" in bytes. |
| 116 | +/// The bytes are borrowed, not copied: the caller must keep "buffer" alive and |
| 117 | +/// unmodified until the data source is freed. |
| 118 | +/// |
| 119 | +/// The returned pointer is owned by the caller and must be freed with |
| 120 | +/// vx_data_source_free. |
| 121 | +/// |
| 122 | +/// On error, returns NULL and sets "err". |
| 123 | +#[unsafe(no_mangle)] |
| 124 | +pub unsafe extern "C-unwind" fn vx_data_source_new_buffer( |
| 125 | + session: *const vx_session, |
| 126 | + buffer: *const c_void, |
| 127 | + buffer_len: usize, |
| 128 | + err: *mut *mut vx_error, |
| 129 | +) -> *const vx_data_source { |
| 130 | + try_or(err, ptr::null(), || { |
| 131 | + vortex_ensure!(!session.is_null()); |
| 132 | + vortex_ensure!(!buffer.is_null()); |
| 133 | + |
| 134 | + let session = vx_session::as_ref(session); |
| 135 | + let bytes: &'static [u8] = |
| 136 | + unsafe { slice::from_raw_parts(buffer.cast::<u8>(), buffer_len) }; |
| 137 | + let buffer = ByteBuffer::from(Bytes::from_static(bytes)); |
| 138 | + let file = session.open_options().open_buffer(buffer)?; |
| 139 | + let ds = MultiLayoutDataSource::new_with_first(file.layout_reader()?, Vec::new(), session); |
| 140 | + |
| 141 | + Ok(vx_data_source::new(Arc::new(ds) as DataSourceRef)) |
| 142 | + }) |
| 143 | +} |
| 144 | + |
107 | 145 | /// Return the schema of the data source as a non-owned dtype. |
108 | 146 | /// The returned pointer is valid as long as "ds" is alive. Do not free it. |
109 | 147 | #[unsafe(no_mangle)] |
@@ -140,12 +178,15 @@ pub unsafe extern "C-unwind" fn vx_data_source_get_row_count( |
140 | 178 | #[cfg(test)] |
141 | 179 | mod tests { |
142 | 180 | use std::ffi::CString; |
| 181 | + use std::ffi::c_void; |
| 182 | + use std::fs::read; |
143 | 183 | use std::ptr; |
144 | 184 |
|
145 | 185 | use crate::data_source::vx_data_source_dtype; |
146 | 186 | use crate::data_source::vx_data_source_free; |
147 | 187 | use crate::data_source::vx_data_source_get_row_count; |
148 | 188 | use crate::data_source::vx_data_source_new; |
| 189 | + use crate::data_source::vx_data_source_new_buffer; |
149 | 190 | use crate::data_source::vx_data_source_options; |
150 | 191 | use crate::dtype::vx_dtype; |
151 | 192 | use crate::scan::vx_estimate; |
@@ -220,4 +261,39 @@ mod tests { |
220 | 261 | vx_session_free(session); |
221 | 262 | } |
222 | 263 | } |
| 264 | + |
| 265 | + #[test] |
| 266 | + #[cfg_attr(miri, ignore)] |
| 267 | + fn test_buffer() { |
| 268 | + unsafe { |
| 269 | + let session = vx_session_new(); |
| 270 | + let (sample, struct_array) = write_sample(session); |
| 271 | + |
| 272 | + let mut error = ptr::null_mut(); |
| 273 | + let ds = vx_data_source_new_buffer(session, ptr::null(), 0, &raw mut error); |
| 274 | + assert_error(error); |
| 275 | + assert!(ds.is_null()); |
| 276 | + |
| 277 | + let file = read(sample).unwrap(); |
| 278 | + let ds = vx_data_source_new_buffer( |
| 279 | + session, |
| 280 | + file.as_ptr() as *const c_void, |
| 281 | + file.len(), |
| 282 | + &raw mut error, |
| 283 | + ); |
| 284 | + assert_no_error(error); |
| 285 | + assert!(!ds.is_null()); |
| 286 | + |
| 287 | + let dtype = vx_dtype::as_ref(vx_data_source_dtype(ds)); |
| 288 | + assert_eq!(dtype, struct_array.dtype()); |
| 289 | + |
| 290 | + let mut row_count = vx_estimate::default(); |
| 291 | + vx_data_source_get_row_count(ds, &raw mut row_count); |
| 292 | + assert_eq!(row_count.r#type, vx_estimate_type::VX_ESTIMATE_EXACT); |
| 293 | + assert_eq!(row_count.estimate, SAMPLE_ROWS as u64); |
| 294 | + |
| 295 | + vx_data_source_free(ds); |
| 296 | + vx_session_free(session); |
| 297 | + } |
| 298 | + } |
223 | 299 | } |
0 commit comments