Skip to main content

rdf_fusion_encoding/string/
array.rs

1use crate::compute::with_plain_term_encoding_from_string;
2use crate::plain_term::PlainTermArray;
3use crate::string::{STRING_ENCODING, StringEncoding, StringEncodingRef};
4use crate::{EncodingArray, EncodingDatum};
5use datafusion::arrow::array::{ArrayRef, StringArray};
6use rdf_fusion_common::AResult;
7use std::sync::Arc;
8
9#[derive(Debug, Clone)]
10pub struct StringTermArray {
11    /// The inner array.
12    inner: ArrayRef,
13    /// The encoding of the array.
14    encoding: StringEncodingRef,
15}
16
17impl StringTermArray {
18    /// Creates a new [`StringTermArray`] without validating the invariants.
19    pub fn new_unchecked(inner: ArrayRef) -> Self {
20        Self {
21            inner,
22            encoding: Arc::clone(&STRING_ENCODING),
23        }
24    }
25
26    /// Creates a new [StringTermArray] with `number_rows` null values.
27    pub fn new_null(number_rows: usize) -> Self {
28        Self::new_unchecked(Arc::new(StringArray::new_null(number_rows)))
29    }
30
31    /// Returns the inner array.
32    pub fn inner(&self) -> &ArrayRef {
33        &self.inner
34    }
35
36    /// Returns the plain term representation of this array.
37    pub fn as_plain_term_array(&self) -> AResult<PlainTermArray> {
38        let result =
39            with_plain_term_encoding_from_string(&EncodingDatum::Array(self.clone()))?;
40        Ok(result)
41    }
42}
43
44impl EncodingArray for StringTermArray {
45    type Encoding = StringEncoding;
46
47    fn encoding(&self) -> &Arc<Self::Encoding> {
48        &self.encoding
49    }
50
51    fn inner(&self) -> &ArrayRef {
52        &self.inner
53    }
54
55    fn into_array_ref(self) -> ArrayRef {
56        self.inner
57    }
58}