rdf_fusion_encoding/object_id/
mapping.rs1use crate::object_id::{ObjectIdArray, ObjectIdEncoding, ObjectIdScalar};
2use crate::plain_term::{PlainTermArray, PlainTermScalar};
3use crate::typed_value::{TypedValueArray, TypedValueScalar};
4use crate::{EncodingArray, EncodingScalar};
5use datafusion::arrow::error::ArrowError;
6use datafusion::error::DataFusionError;
7use rdf_fusion_model::{CorruptionError, StorageError};
8use std::error::Error;
9use std::fmt::Debug;
10use thiserror::Error;
11
12#[derive(Error, Debug)]
14pub enum ObjectIdMappingError {
15 #[error("An error occurred while encoding the result. {0}")]
16 ArrowError(ArrowError),
17 #[error("A literal was encountered at a position where a graph name is expected.")]
18 LiteralAsGraphName,
19 #[error("An unknown object ID was encountered in an unexpected place.")]
20 UnknownObjectId,
21 #[error("An error occurred while accessing the object id storage.")]
22 Storage(Box<dyn Error + Sync + Send>),
23}
24
25#[derive(Error, Debug)]
26#[error("An unknown object ID was encountered in an unexpected place.")]
27pub struct UnknownObjectIdError;
28
29impl From<ArrowError> for ObjectIdMappingError {
30 fn from(value: ArrowError) -> Self {
31 ObjectIdMappingError::ArrowError(value)
32 }
33}
34
35impl From<ObjectIdMappingError> for DataFusionError {
36 fn from(value: ObjectIdMappingError) -> Self {
37 DataFusionError::External(Box::new(value))
38 }
39}
40
41impl From<ObjectIdMappingError> for StorageError {
42 fn from(value: ObjectIdMappingError) -> Self {
43 StorageError::Corruption(CorruptionError::new(value))
44 }
45}
46
47pub trait ObjectIdMapping: Debug + Send + Sync {
67 fn encoding(&self) -> ObjectIdEncoding;
69
70 fn try_get_object_id(
75 &self,
76 scalar: &PlainTermScalar,
77 ) -> Result<Option<ObjectIdScalar>, ObjectIdMappingError>;
78
79 fn encode_array(
82 &self,
83 array: &PlainTermArray,
84 ) -> Result<ObjectIdArray, ObjectIdMappingError>;
85
86 fn encode_scalar(
89 &self,
90 scalar: &PlainTermScalar,
91 ) -> Result<ObjectIdScalar, ObjectIdMappingError> {
92 let array = scalar
93 .to_array(1)
94 .expect("Data type is supported for to_array");
95 let encoded = self.encode_array(&array)?;
96 Ok(encoded.try_as_scalar(0).expect("Row 0 always exists"))
97 }
98
99 fn decode_array(
101 &self,
102 array: &ObjectIdArray,
103 ) -> Result<PlainTermArray, ObjectIdMappingError>;
104
105 fn decode_array_to_typed_value(
107 &self,
108 array: &ObjectIdArray,
109 ) -> Result<TypedValueArray, ObjectIdMappingError>;
110
111 fn decode_scalar(
113 &self,
114 scalar: &ObjectIdScalar,
115 ) -> Result<PlainTermScalar, ObjectIdMappingError> {
116 let array = scalar
117 .to_array(1)
118 .expect("Data type is supported for to_array");
119 let encoded = self.decode_array(&array)?;
120 Ok(encoded.try_as_scalar(0).expect("Row 0 always exists"))
121 }
122
123 fn decode_scalar_to_typed_value(
125 &self,
126 scalar: &ObjectIdScalar,
127 ) -> Result<TypedValueScalar, ObjectIdMappingError> {
128 let array = scalar
129 .to_array(1)
130 .expect("Data type is supported for to_array");
131 let decoded = self.decode_array_to_typed_value(&array)?;
132 Ok(decoded.try_as_scalar(0).expect("Row 0 always exists"))
133 }
134}