Elffuss BraidHub

kikocis/lancedb public

main · 1.6 KiBRawPermalink
1// Copyright 2024 Lance Developers.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use futures::StreamExt;
16use lancedb::arrow::SendableRecordBatchStream;
17use lancedb::ipc::batches_to_ipc_file;
18use napi::bindgen_prelude::*;
19use napi_derive::napi;
20
21/** Typescript-style Async Iterator over RecordBatches */
22#[napi]
23pub struct RecordBatchIterator {
24 inner: SendableRecordBatchStream,
25}
26
27#[napi]
28impl RecordBatchIterator {
29 pub(crate) fn new(inner: SendableRecordBatchStream) -> Self {
30 Self { inner }
31 }
32
33 #[napi(catch_unwind)]
34 pub async unsafe fn next(&mut self) -> napi::Result<Option<Buffer>> {
35 if let Some(rst) = self.inner.next().await {
36 let batch = rst.map_err(|e| {
37 napi::Error::from_reason(format!("Failed to get next batch from stream: {}", e))
38 })?;
39 batches_to_ipc_file(&[batch])
40 .map_err(|e| napi::Error::from_reason(format!("Failed to write IPC file: {}", e)))
41 .map(|buf| Some(Buffer::from(buf)))
42 } else {
43 // We are done with the stream.
44 Ok(None)
45 }
46 }
47}