simple_triplestore/sled/
query.rs

1use serde::{de::DeserializeOwned, Serialize};
2
3use crate::{
4    prelude::*,
5    traits::{IdType, Property},
6    MemTripleStore, Query, QueryError, Triple,
7};
8
9use super::SledTripleStore;
10
11impl<
12        Id: IdType,
13        NodeProps: Property + Serialize + DeserializeOwned,
14        EdgeProps: Property + Serialize + DeserializeOwned,
15    > TripleStoreQuery<Id, NodeProps, EdgeProps> for SledTripleStore<Id, NodeProps, EdgeProps>
16{
17    type QueryResult = MemTripleStore<Id, NodeProps, EdgeProps>;
18    type QueryResultError = ();
19
20    fn run(
21        &self,
22        query: Query<Id>,
23    ) -> Result<Self::QueryResult, QueryError<Self::Error, Self::QueryResultError>> {
24        Ok(match query {
25            Query::NodeProps(nodes) => {
26                let mut result =
27                    MemTripleStore::new_from_boxed_id_generator(self.id_generator.clone());
28                for node in nodes {
29                    if let Some(data) = self
30                        .node_props
31                        .get(&node.to_be_bytes())
32                        .map_err(|e| QueryError::Left(super::SledTripleStoreError::SledError(e)))?
33                    {
34                        result
35                            .insert_node(
36                                node,
37                                bincode::deserialize(&data).map_err(|e| {
38                                    QueryError::Left(
39                                        super::SledTripleStoreError::SerializationError(e),
40                                    )
41                                })?,
42                            )
43                            .map_err(|e| QueryError::Right(e))?;
44                    }
45                }
46                result
47            }
48
49            Query::SPO(triples) => {
50                let mut result =
51                    MemTripleStore::new_from_boxed_id_generator(self.id_generator.clone());
52                for (sub, pred, obj) in triples.into_iter() {
53                    let triple = Triple { sub, pred, obj };
54                    if let Some(data_id) = self
55                        .spo_data
56                        .get(&Id::encode_spo_triple(&triple))
57                        .map_err(|e| QueryError::Left(super::SledTripleStoreError::SledError(e)))?
58                    {
59                        if let Some(data) = self.edge_props.get(&data_id).map_err(|e| {
60                            QueryError::Left(super::SledTripleStoreError::SledError(e))
61                        })? {
62                            result
63                                .insert_edge(
64                                    triple,
65                                    bincode::deserialize(&data).map_err(|e| {
66                                        QueryError::Left(
67                                            super::SledTripleStoreError::SerializationError(e),
68                                        )
69                                    })?,
70                                )
71                                .map_err(|e| QueryError::Right(e))?;
72                        }
73                    }
74                }
75                result
76            }
77
78            Query::S(items) => {
79                let mut result =
80                    MemTripleStore::new_from_boxed_id_generator(self.id_generator.clone());
81                for sub in items {
82                    for r in self.spo_data.range(Id::key_bounds_1(sub)) {
83                        let (key, data_id) = r.map_err(|e| {
84                            QueryError::Left(super::SledTripleStoreError::SledError(e))
85                        })?;
86                        if let Some(data) = self.edge_props.get(&data_id).map_err(|e| {
87                            QueryError::Left(super::SledTripleStoreError::SledError(e))
88                        })? {
89                            result
90                                .insert_edge(
91                                    Id::decode_spo_triple(&key[..].try_into().map_err(|_| {
92                                        QueryError::Left(super::SledTripleStoreError::KeySizeError)
93                                    })?),
94                                    bincode::deserialize(&data).map_err(|e| {
95                                        QueryError::Left(
96                                            super::SledTripleStoreError::SerializationError(e),
97                                        )
98                                    })?,
99                                )
100                                .map_err(|e| QueryError::Right(e))?;
101                        }
102                    }
103                }
104                result
105            }
106
107            Query::SP(items) => {
108                let mut result =
109                    MemTripleStore::new_from_boxed_id_generator(self.id_generator.clone());
110                for (sub, pred) in items {
111                    for r in self.spo_data.range(Id::key_bounds_2(sub, pred)) {
112                        let (key, data_id) = r.map_err(|e| {
113                            QueryError::Left(super::SledTripleStoreError::SledError(e))
114                        })?;
115                        if let Some(data) = self.edge_props.get(&data_id).map_err(|e| {
116                            QueryError::Left(super::SledTripleStoreError::SledError(e))
117                        })? {
118                            result
119                                .insert_edge(
120                                    Id::decode_spo_triple(&key[..].try_into().map_err(|_| {
121                                        QueryError::Left(super::SledTripleStoreError::KeySizeError)
122                                    })?),
123                                    bincode::deserialize(&data).map_err(|e| {
124                                        QueryError::Left(
125                                            super::SledTripleStoreError::SerializationError(e),
126                                        )
127                                    })?,
128                                )
129                                .map_err(|e| QueryError::Right(e))?;
130                        }
131                    }
132                }
133                result
134            }
135
136            Query::SO(items) => {
137                let mut result =
138                    MemTripleStore::new_from_boxed_id_generator(self.id_generator.clone());
139                for (sub, obj) in items {
140                    for r in self.osp_data.range(Id::key_bounds_2(obj, sub)) {
141                        let (key, data_id) = r.map_err(|e| {
142                            QueryError::Left(super::SledTripleStoreError::SledError(e))
143                        })?;
144                        if let Some(data) = self.edge_props.get(&data_id).map_err(|e| {
145                            QueryError::Left(super::SledTripleStoreError::SledError(e))
146                        })? {
147                            result
148                                .insert_edge(
149                                    Id::decode_osp_triple(&key[..].try_into().map_err(|_| {
150                                        QueryError::Left(super::SledTripleStoreError::KeySizeError)
151                                    })?),
152                                    bincode::deserialize(&data).map_err(|e| {
153                                        QueryError::Left(
154                                            super::SledTripleStoreError::SerializationError(e),
155                                        )
156                                    })?,
157                                )
158                                .map_err(|e| QueryError::Right(e))?;
159                        }
160                    }
161                }
162                result
163            }
164
165            Query::P(items) => {
166                let mut result =
167                    MemTripleStore::new_from_boxed_id_generator(self.id_generator.clone());
168                for pred in items {
169                    for r in self.pos_data.range(Id::key_bounds_1(pred)) {
170                        let (key, data_id) = r.map_err(|e| {
171                            QueryError::Left(super::SledTripleStoreError::SledError(e))
172                        })?;
173                        if let Some(data) = self.edge_props.get(&data_id).map_err(|e| {
174                            QueryError::Left(super::SledTripleStoreError::SledError(e))
175                        })? {
176                            result
177                                .insert_edge(
178                                    Id::decode_pos_triple(&key[..].try_into().map_err(|_| {
179                                        QueryError::Left(super::SledTripleStoreError::KeySizeError)
180                                    })?),
181                                    bincode::deserialize(&data).map_err(|e| {
182                                        QueryError::Left(
183                                            super::SledTripleStoreError::SerializationError(e),
184                                        )
185                                    })?,
186                                )
187                                .map_err(|e| QueryError::Right(e))?;
188                        }
189                    }
190                }
191                result
192            }
193
194            Query::PO(items) => {
195                let mut result =
196                    MemTripleStore::new_from_boxed_id_generator(self.id_generator.clone());
197                for (pred, obj) in items {
198                    for r in self.pos_data.range(Id::key_bounds_2(pred, obj)) {
199                        let (key, data_id) = r.map_err(|e| {
200                            QueryError::Left(super::SledTripleStoreError::SledError(e))
201                        })?;
202                        if let Some(data) = self.edge_props.get(&data_id).map_err(|e| {
203                            QueryError::Left(super::SledTripleStoreError::SledError(e))
204                        })? {
205                            result
206                                .insert_edge(
207                                    Id::decode_pos_triple(&key[..].try_into().map_err(|_| {
208                                        QueryError::Left(super::SledTripleStoreError::KeySizeError)
209                                    })?),
210                                    bincode::deserialize(&data).map_err(|e| {
211                                        QueryError::Left(
212                                            super::SledTripleStoreError::SerializationError(e),
213                                        )
214                                    })?,
215                                )
216                                .map_err(|e| QueryError::Right(e))?;
217                        }
218                    }
219                }
220                result
221            }
222
223            Query::O(items) => {
224                let mut result =
225                    MemTripleStore::new_from_boxed_id_generator(self.id_generator.clone());
226                for obj in items {
227                    for r in self.osp_data.range(Id::key_bounds_1(obj)) {
228                        let (key, data_id) = r.map_err(|e| {
229                            QueryError::Left(super::SledTripleStoreError::SledError(e))
230                        })?;
231                        if let Some(data) = self.edge_props.get(&data_id).map_err(|e| {
232                            QueryError::Left(super::SledTripleStoreError::SledError(e))
233                        })? {
234                            result
235                                .insert_edge(
236                                    Id::decode_osp_triple(&key[..].try_into().map_err(|_| {
237                                        QueryError::Left(super::SledTripleStoreError::KeySizeError)
238                                    })?),
239                                    bincode::deserialize(&data).map_err(|e| {
240                                        QueryError::Left(
241                                            super::SledTripleStoreError::SerializationError(e),
242                                        )
243                                    })?,
244                                )
245                                .map_err(|e| QueryError::Right(e))?;
246                        }
247                    }
248                }
249                result
250            }
251        })
252    }
253}
254
255#[cfg(test)]
256mod test {
257    use crate::{SledTripleStore, UlidIdGenerator};
258
259    #[test]
260    fn test_query_node_props() {
261        let (_tempdir, db) = crate::sled::create_test_db().expect("ok");
262        let sled_db = SledTripleStore::new(&db, UlidIdGenerator::new()).expect("ok");
263        crate::conformance::query::test_query_node_props(sled_db);
264    }
265
266    #[test]
267    fn test_query_edge_props() {
268        let (_tempdir, db) = crate::sled::create_test_db().expect("ok");
269        let sled_db = SledTripleStore::new(&db, UlidIdGenerator::new()).expect("ok");
270        crate::conformance::query::test_query_edge_props(sled_db);
271    }
272
273    #[test]
274    fn test_query_s() {
275        let (_tempdir, db) = crate::sled::create_test_db().expect("ok");
276        let sled_db = SledTripleStore::new(&db, UlidIdGenerator::new()).expect("ok");
277        crate::conformance::query::test_query_s(sled_db);
278    }
279
280    #[test]
281    fn test_query_sp() {
282        let (_tempdir, db) = crate::sled::create_test_db().expect("ok");
283        let sled_db = SledTripleStore::new(&db, UlidIdGenerator::new()).expect("ok");
284        crate::conformance::query::test_query_sp(sled_db);
285    }
286
287    #[test]
288    fn test_query_p() {
289        let (_tempdir, db) = crate::sled::create_test_db().expect("ok");
290        let sled_db = SledTripleStore::new(&db, UlidIdGenerator::new()).expect("ok");
291        crate::conformance::query::test_query_p(sled_db);
292    }
293
294    #[test]
295    fn test_query_po() {
296        let (_tempdir, db) = crate::sled::create_test_db().expect("ok");
297        let sled_db = SledTripleStore::new(&db, UlidIdGenerator::new()).expect("ok");
298        crate::conformance::query::test_query_po(sled_db);
299    }
300
301    #[test]
302    fn test_query_o() {
303        let (_tempdir, db) = crate::sled::create_test_db().expect("ok");
304        let sled_db = SledTripleStore::new(&db, UlidIdGenerator::new()).expect("ok");
305        crate::conformance::query::test_query_o(sled_db);
306    }
307
308    #[test]
309    fn test_query_os() {
310        let (_tempdir, db) = crate::sled::create_test_db().expect("ok");
311        let sled_db = SledTripleStore::new(&db, UlidIdGenerator::new()).expect("ok");
312        crate::conformance::query::test_query_os(sled_db);
313    }
314}