exon_common/
array_builder.rs

1// Copyright 2023 WHERE TRUE Technologies.
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 std::sync::Arc;
16
17use arrow::{array::ArrayRef, datatypes::Schema, record_batch::RecordBatchOptions};
18
19/// The `ExonArrayBuilder` trait defines the interface for building data arrays.
20pub trait ExonArrayBuilder {
21    /// Finishes building the internal data structures and returns the built arrays.
22    fn finish(&mut self) -> Vec<ArrayRef>;
23
24    /// Creates a record batch from the built arrays.
25    fn try_into_record_batch(
26        &mut self,
27        schema: Arc<Schema>,
28    ) -> arrow::error::Result<arrow::record_batch::RecordBatch> {
29        let columns = self.finish();
30        let options = RecordBatchOptions::default().with_row_count(Some(self.len()));
31
32        let record_batch =
33            arrow::record_batch::RecordBatch::try_new_with_options(schema, columns, &options)?;
34
35        Ok(record_batch)
36    }
37
38    /// Returns the number of elements in the array.
39    fn len(&self) -> usize;
40
41    /// Returns `true` if the array contains no elements.
42    fn is_empty(&self) -> bool {
43        self.len() == 0
44    }
45}