Elffuss BraidHub

kikocis/lancedb public

main · 1.4 KiBRawPermalink
1// use arrow::datatypes::SchemaRef;
2// use lancedb::arrow::SendableRecordBatchStream;
3
4use std::sync::Arc;
5
6use arrow::{
7 datatypes::SchemaRef,
8 pyarrow::{IntoPyArrow, ToPyArrow},
9};
10use futures::stream::StreamExt;
11use lancedb::arrow::SendableRecordBatchStream;
12use pyo3::{pyclass, pymethods, Bound, PyAny, PyObject, PyRef, PyResult, Python};
13use pyo3_asyncio_0_21::tokio::future_into_py;
14
15use crate::error::PythonErrorExt;
16
17#[pyclass]
18pub struct RecordBatchStream {
19 schema: SchemaRef,
20 inner: Arc<tokio::sync::Mutex<SendableRecordBatchStream>>,
21}
22
23impl RecordBatchStream {
24 pub fn new(inner: SendableRecordBatchStream) -> Self {
25 let schema = inner.schema().clone();
26 Self {
27 schema,
28 inner: Arc::new(tokio::sync::Mutex::new(inner)),
29 }
30 }
31}
32
33#[pymethods]
34impl RecordBatchStream {
35 pub fn schema(&self, py: Python) -> PyResult<PyObject> {
36 (*self.schema).clone().into_pyarrow(py)
37 }
38
39 pub fn next(self_: PyRef<'_, Self>) -> PyResult<Bound<'_, PyAny>> {
40 let inner = self_.inner.clone();
41 future_into_py(self_.py(), async move {
42 let inner_next = inner.lock().await.next().await;
43 inner_next
44 .map(|item| {
45 let item = item.infer_error()?;
46 Python::with_gil(|py| item.to_pyarrow(py))
47 })
48 .transpose()
49 })
50 }
51}