fresh-borzoni commented on code in PR #452: URL: https://github.com/apache/fluss-rust/pull/452#discussion_r3070286553
########## bindings/elixir/native/fluss_nif/src/log_scanner.rs: ########## @@ -0,0 +1,169 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::RUNTIME; +use crate::atoms::{self, to_nif_err}; +use crate::row_convert; +use crate::table::TableResource; +use fluss::client::LogScanner; +use fluss::metadata::Column; +use fluss::record::ChangeType; +use rustler::env::OwnedEnv; +use rustler::types::LocalPid; +use rustler::{Atom, Encoder, Env, ResourceArc}; +use std::collections::HashMap; +use std::time::Duration; + +pub struct LogScannerResource { + pub scanner: LogScanner, + pub columns: Vec<Column>, +} + +impl std::panic::RefUnwindSafe for LogScannerResource {} + +#[rustler::resource_impl] +impl rustler::Resource for LogScannerResource {} + +#[rustler::nif] +fn log_scanner_new( + table: ResourceArc<TableResource>, +) -> Result<ResourceArc<LogScannerResource>, rustler::Error> { + let _guard = RUNTIME.enter(); + let (scanner, columns) = table.with_table(|t| { + let scanner = t.new_scan().create_log_scanner().map_err(to_nif_err)?; + Ok((scanner, t.get_table_info().schema.columns().to_vec())) + })?; + Ok(ResourceArc::new(LogScannerResource { scanner, columns })) +} + +#[rustler::nif(schedule = "DirtyIo")] +fn log_scanner_subscribe( + scanner: ResourceArc<LogScannerResource>, + bucket: i32, + offset: i64, +) -> Result<Atom, rustler::Error> { + RUNTIME + .block_on(scanner.scanner.subscribe(bucket, offset)) + .map_err(to_nif_err)?; + Ok(atoms::ok()) +} + +#[rustler::nif(schedule = "DirtyIo")] +fn log_scanner_subscribe_buckets( + scanner: ResourceArc<LogScannerResource>, + bucket_offsets: Vec<(i32, i64)>, +) -> Result<Atom, rustler::Error> { + let map: HashMap<i32, i64> = bucket_offsets.into_iter().collect(); + RUNTIME + .block_on(scanner.scanner.subscribe_buckets(&map)) + .map_err(to_nif_err)?; + Ok(atoms::ok()) +} + +#[rustler::nif(schedule = "DirtyIo")] +fn log_scanner_unsubscribe( + scanner: ResourceArc<LogScannerResource>, + bucket: i32, +) -> Result<Atom, rustler::Error> { + RUNTIME + .block_on(scanner.scanner.unsubscribe(bucket)) + .map_err(to_nif_err)?; + Ok(atoms::ok()) +} + +#[rustler::nif] +fn log_scanner_poll(env: Env, scanner: ResourceArc<LogScannerResource>, timeout_ms: u64) -> Atom { + let pid = env.pid(); + let scanner = scanner.clone(); + + RUNTIME.spawn(async move { + let result = scanner + .scanner + .poll(Duration::from_millis(timeout_ms)) + .await; + send_poll_result(&pid, result, &scanner.columns); + }); + + atoms::ok() +} + +fn send_poll_result( + pid: &LocalPid, + result: Result<fluss::record::ScanRecords, fluss::error::Error>, + columns: &[Column], +) { + let mut msg_env = OwnedEnv::new(); + + match result { + Ok(scan_records) => { + let _ = msg_env.send_and_clear(pid, |env| { + match encode_scan_records(env, scan_records, columns) { + Ok(records) => (atoms::fluss_records(), records).encode(env), + Err(e) => (atoms::fluss_poll_error(), e).encode(env), + } + }); + } + Err(e) => { + let _ = msg_env.send_and_clear(pid, |env| { + (atoms::fluss_poll_error(), e.to_string()).encode(env) + }); + } + } +} + +fn encode_scan_records<'a>( + env: Env<'a>, + scan_records: fluss::record::ScanRecords, + columns: &[Column], +) -> Result<rustler::Term<'a>, String> { + let column_atoms = row_convert::intern_column_atoms(env, columns); + let mut result = Vec::new(); + + for record in scan_records { + let row_map = row_convert::row_to_term(env, record.row(), columns, &column_atoms) + .map_err(|e| format!("failed to convert row at offset {}: {e}", record.offset()))?; Review Comment: Row conversion error means a bug in the NIF type mapping, it's not retryable, and in practice every row in the batch would fail the same way. Java poll() doesn't distinguish error types either, exceptions propagate opaquely. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
