rdf_borsh/
borsh_dataset.rs1extern crate alloc;
4
5use crate::{BorshQuad, BorshTerm, BorshTermId, BorshTriple};
6use alloc::{
7 collections::{BTreeMap, BTreeSet},
8 vec::Vec,
9};
10use borsh::{BorshDeserialize, BorshSerialize};
11use num_traits::FromPrimitive;
12
13#[derive(Clone, Debug, Default, Eq, PartialEq, BorshSerialize, BorshDeserialize)]
14pub struct BorshDataset {
15 pub terms_dict: Vec<BorshTerm>,
16 pub quads_set: BTreeSet<BorshQuad<u16>>,
17 #[borsh(skip)]
18 terms_map: BTreeMap<BorshTerm, BorshTermId<u16>>,
19}
20
21impl BorshDataset {
22 pub fn new() -> Self {
23 Self {
24 terms_dict: Vec::new(),
25 quads_set: BTreeSet::new(),
26 terms_map: BTreeMap::new(),
27 }
28 }
29
30 pub fn quad_count(&self) -> usize {
31 self.quads_set.len()
32 }
33
34 pub fn intern_term(&mut self, term: BorshTerm) -> core::result::Result<BorshTermId<u16>, ()> {
35 if let Some(&term_id) = self.terms_map.get(&term) {
36 return Ok(term_id);
37 }
38 let term_id: BorshTermId<u16> =
39 BorshTermId::from_usize(self.terms_dict.len() + 1).ok_or(())?;
40 self.terms_dict.push(term.clone());
41 self.terms_map.insert(term, term_id);
42 Ok(term_id)
43 }
44
45 pub fn insert_triple(&mut self, triple: BorshTriple<u16>) -> bool {
46 self.quads_set.insert(triple.into())
47 }
48
49 pub fn insert_quad(&mut self, quad: BorshQuad<u16>) -> bool {
50 self.quads_set.insert(quad)
51 }
52}