|
| 1 | +use std::ops::Range; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use async_tiff::error::{AsyncTiffError, AsyncTiffResult}; |
| 5 | +use async_tiff::reader::{AsyncFileReader, ObjectReader}; |
| 6 | +use bytes::Bytes; |
| 7 | +use futures::future::BoxFuture; |
| 8 | +use futures::FutureExt; |
| 9 | +use pyo3::exceptions::PyTypeError; |
| 10 | +use pyo3::intern; |
| 11 | +use pyo3::prelude::*; |
| 12 | +use pyo3::types::PyDict; |
| 13 | +use pyo3_async_runtimes::tokio::into_future; |
| 14 | +use pyo3_bytes::PyBytes; |
| 15 | +use pyo3_object_store::PyObjectStore; |
| 16 | + |
| 17 | +#[derive(FromPyObject)] |
| 18 | +pub(crate) enum StoreInput { |
| 19 | + ObjectStore(PyObjectStore), |
| 20 | + ObspecBackend(ObspecBackend), |
| 21 | +} |
| 22 | + |
| 23 | +impl StoreInput { |
| 24 | + pub(crate) fn into_async_file_reader(self, path: String) -> Arc<dyn AsyncFileReader> { |
| 25 | + match self { |
| 26 | + Self::ObjectStore(store) => { |
| 27 | + Arc::new(ObjectReader::new(store.into_inner(), path.into())) |
| 28 | + } |
| 29 | + Self::ObspecBackend(backend) => Arc::new(ObspecReader { backend, path }), |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/// A Python backend for making requests that conforms to the GetRangeAsync and GetRangesAsync |
| 35 | +/// protocols defined by obspec. |
| 36 | +/// https://developmentseed.org/obspec/latest/api/get/#obspec.GetRangeAsync |
| 37 | +/// https://developmentseed.org/obspec/latest/api/get/#obspec.GetRangesAsync |
| 38 | +#[derive(Debug)] |
| 39 | +pub(crate) struct ObspecBackend(PyObject); |
| 40 | + |
| 41 | +impl ObspecBackend { |
| 42 | + async fn get_range(&self, path: &str, range: Range<u64>) -> PyResult<PyBytes> { |
| 43 | + let future = Python::with_gil(|py| { |
| 44 | + let kwargs = PyDict::new(py); |
| 45 | + kwargs.set_item(intern!(py, "path"), path)?; |
| 46 | + kwargs.set_item(intern!(py, "start"), range.start)?; |
| 47 | + kwargs.set_item(intern!(py, "end"), range.end)?; |
| 48 | + |
| 49 | + let coroutine = self |
| 50 | + .0 |
| 51 | + .call_method(py, intern!(py, "get_range"), (), Some(&kwargs))?; |
| 52 | + into_future(coroutine.bind(py).clone()) |
| 53 | + })?; |
| 54 | + let result = future.await?; |
| 55 | + Python::with_gil(|py| result.extract(py)) |
| 56 | + } |
| 57 | + |
| 58 | + async fn get_ranges(&self, path: &str, ranges: &[Range<u64>]) -> PyResult<Vec<PyBytes>> { |
| 59 | + let starts = ranges.iter().map(|r| r.start).collect::<Vec<_>>(); |
| 60 | + let ends = ranges.iter().map(|r| r.end).collect::<Vec<_>>(); |
| 61 | + |
| 62 | + let future = Python::with_gil(|py| { |
| 63 | + let kwargs = PyDict::new(py); |
| 64 | + kwargs.set_item(intern!(py, "path"), path)?; |
| 65 | + kwargs.set_item(intern!(py, "starts"), starts)?; |
| 66 | + kwargs.set_item(intern!(py, "ends"), ends)?; |
| 67 | + |
| 68 | + let coroutine = self |
| 69 | + .0 |
| 70 | + .call_method(py, intern!(py, "get_range"), (), Some(&kwargs))?; |
| 71 | + into_future(coroutine.bind(py).clone()) |
| 72 | + })?; |
| 73 | + let result = future.await?; |
| 74 | + Python::with_gil(|py| result.extract(py)) |
| 75 | + } |
| 76 | + |
| 77 | + async fn get_range_wrapper(&self, path: &str, range: Range<u64>) -> AsyncTiffResult<Bytes> { |
| 78 | + let result = self |
| 79 | + .get_range(path, range) |
| 80 | + .await |
| 81 | + .map_err(|err| AsyncTiffError::External(Box::new(err)))?; |
| 82 | + Ok(result.into_inner()) |
| 83 | + } |
| 84 | + |
| 85 | + async fn get_ranges_wrapper( |
| 86 | + &self, |
| 87 | + path: &str, |
| 88 | + ranges: Vec<Range<u64>>, |
| 89 | + ) -> AsyncTiffResult<Vec<Bytes>> { |
| 90 | + let result = self |
| 91 | + .get_ranges(path, &ranges) |
| 92 | + .await |
| 93 | + .map_err(|err| AsyncTiffError::External(Box::new(err)))?; |
| 94 | + Ok(result.into_iter().map(|b| b.into_inner()).collect()) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl<'py> FromPyObject<'py> for ObspecBackend { |
| 99 | + fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> { |
| 100 | + let py = ob.py(); |
| 101 | + if ob.hasattr(intern!(py, "get_range_async"))? |
| 102 | + && ob.hasattr(intern!(py, "get_ranges_async"))? |
| 103 | + { |
| 104 | + Ok(Self(ob.clone().unbind())) |
| 105 | + } else { |
| 106 | + Err(PyTypeError::new_err("Expected obspec-compatible class with `get_range_async` and `get_ranges_async` method.")) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[derive(Debug)] |
| 112 | +struct ObspecReader { |
| 113 | + backend: ObspecBackend, |
| 114 | + path: String, |
| 115 | +} |
| 116 | + |
| 117 | +impl AsyncFileReader for ObspecReader { |
| 118 | + fn get_bytes(&self, range: Range<u64>) -> BoxFuture<'_, AsyncTiffResult<Bytes>> { |
| 119 | + self.backend.get_range_wrapper(&self.path, range).boxed() |
| 120 | + } |
| 121 | + |
| 122 | + fn get_byte_ranges( |
| 123 | + &self, |
| 124 | + ranges: Vec<Range<u64>>, |
| 125 | + ) -> BoxFuture<'_, AsyncTiffResult<Vec<Bytes>>> { |
| 126 | + self.backend.get_ranges_wrapper(&self.path, ranges).boxed() |
| 127 | + } |
| 128 | +} |
0 commit comments