rdf_fusion_encoding/object_id/
builder.rs

1use crate::TermEncoding;
2use crate::object_id::{ObjectIdArray, ObjectIdEncoding};
3use datafusion::arrow::array::UInt32Builder;
4use rdf_fusion_model::ObjectId;
5use std::sync::Arc;
6
7/// Provides a convenient API for building arrays of RDF terms with the [ObjectIdEncoding]. The
8/// documentation of the encoding provides additional information.
9pub struct ObjectIdArrayBuilder {
10    /// The mapping that is used for obtaining object ids.
11    encoding: ObjectIdEncoding,
12    /// The underlying [UInt32Builder].
13    builder: UInt32Builder,
14}
15
16impl ObjectIdArrayBuilder {
17    /// Create a [ObjectIdArrayBuilder] with the given `capacity`.
18    pub fn new(encoding: ObjectIdEncoding) -> Self {
19        let builder = UInt32Builder::new();
20        Self { encoding, builder }
21    }
22
23    /// Appends a null value to the array.
24    pub fn append_null(&mut self) {
25        self.builder.append_null()
26    }
27
28    /// Appends an object id.
29    pub fn append_object_id(&mut self, term: ObjectId) {
30        self.builder.append_value(term.0)
31    }
32
33    /// Appends an object id.
34    pub fn append_object_id_opt(&mut self, term: Option<ObjectId>) {
35        self.builder.append_option(term.map(|t| t.0))
36    }
37
38    #[allow(clippy::expect_used, reason = "Programming error")]
39    pub fn finish(mut self) -> ObjectIdArray {
40        let encoding = self.encoding;
41        let array = Arc::new(self.builder.finish());
42        encoding.try_new_array(array).expect("Builder fixed")
43    }
44}