1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// this module is transparently re-exported by its parent `adapter`

use std::borrow::{Borrow, BorrowMut};
use std::marker::PhantomData;

use resiter::Map;

use crate::dataset::{Dataset, MutableDataset, SetDataset};
use crate::graph::*;
use crate::term::matcher::{GraphNameMatcher, ANY};
use crate::term::TTerm;
use crate::triple::streaming_mode::{FromQuad, StreamedTriple};

/// The adapter returned by
/// [`Dataset::graph`],
/// [`Dataset::graph_mut`], and
/// [`Dataset::union_graph`].
pub struct DatasetGraph<D: ?Sized, E, M: GraphNameMatcher> {
    dataset: E,
    gmatcher: M,
    _phantom: PhantomData<D>,
}

impl<D: ?Sized, E, M: GraphNameMatcher> DatasetGraph<D, E, M> {
    /// Wrap a dataset as a graph
    pub fn new(dataset: E, gmatcher: M) -> DatasetGraph<D, E, M> {
        DatasetGraph {
            dataset,
            gmatcher,
            _phantom: PhantomData,
        }
    }

    /// Unwrap this adapter to get the original graph.
    pub fn unwrap(self) -> E {
        self.dataset
    }
}

impl<D, E, M> Graph for DatasetGraph<D, E, M>
where
    D: Dataset + ?Sized,
    E: Borrow<D>,
    M: GraphNameMatcher,
{
    type Triple = FromQuad<D::Quad>;
    type Error = D::Error;

    fn triples(&self) -> GTripleSource<Self> {
        Box::new(
            self.dataset
                .borrow()
                .quads_matching(&ANY, &ANY, &ANY, &self.gmatcher)
                .map_ok(StreamedTriple::from_quad),
        )
    }
    fn triples_with_s<'s, TS>(&'s self, s: &'s TS) -> GTripleSource<'s, Self>
    where
        TS: TTerm + ?Sized,
    {
        Box::new(
            self.dataset
                .borrow()
                .quads_matching(s, &ANY, &ANY, &self.gmatcher)
                .map_ok(StreamedTriple::from_quad),
        )
    }
    fn triples_with_p<'s, TP>(&'s self, p: &'s TP) -> GTripleSource<'s, Self>
    where
        TP: TTerm + ?Sized,
    {
        Box::new(
            self.dataset
                .borrow()
                .quads_matching(&ANY, p, &ANY, &self.gmatcher)
                .map_ok(StreamedTriple::from_quad),
        )
    }
    fn triples_with_o<'s, TO>(&'s self, o: &'s TO) -> GTripleSource<'s, Self>
    where
        TO: TTerm + ?Sized,
    {
        Box::new(
            self.dataset
                .borrow()
                .quads_matching(&ANY, &ANY, o, &self.gmatcher)
                .map_ok(StreamedTriple::from_quad),
        )
    }
    fn triples_with_sp<'s, TS, TP>(&'s self, s: &'s TS, p: &'s TP) -> GTripleSource<'s, Self>
    where
        TS: TTerm + ?Sized,
        TP: TTerm + ?Sized,
    {
        Box::new(
            self.dataset
                .borrow()
                .quads_matching(s, p, &ANY, &self.gmatcher)
                .map_ok(StreamedTriple::from_quad),
        )
    }
    fn triples_with_so<'s, TS, TO>(&'s self, s: &'s TS, o: &'s TO) -> GTripleSource<'s, Self>
    where
        TS: TTerm + ?Sized,
        TO: TTerm + ?Sized,
    {
        Box::new(
            self.dataset
                .borrow()
                .quads_matching(s, &ANY, o, &self.gmatcher)
                .map_ok(StreamedTriple::from_quad),
        )
    }
    fn triples_with_po<'s, TP, TO>(&'s self, p: &'s TP, o: &'s TO) -> GTripleSource<'s, Self>
    where
        TP: TTerm + ?Sized,
        TO: TTerm + ?Sized,
    {
        Box::new(
            self.dataset
                .borrow()
                .quads_matching(&ANY, p, o, &self.gmatcher)
                .map_ok(StreamedTriple::from_quad),
        )
    }
    fn triples_with_spo<'s, TS, TP, TO>(
        &'s self,
        s: &'s TS,
        p: &'s TP,
        o: &'s TO,
    ) -> GTripleSource<'s, Self>
    where
        TS: TTerm + ?Sized,
        TP: TTerm + ?Sized,
        TO: TTerm + ?Sized,
    {
        Box::new(
            self.dataset
                .borrow()
                .quads_matching(s, p, o, &self.gmatcher)
                .map_ok(StreamedTriple::from_quad),
        )
    }
}

impl<D, E, T> MutableGraph for DatasetGraph<D, E, Option<&T>>
where
    D: MutableDataset,
    E: BorrowMut<D>,
    T: TTerm + ?Sized,
{
    type MutationError = D::MutationError;

    fn insert<TS, TP, TO>(&mut self, s: &TS, p: &TP, o: &TO) -> MgResult<Self, bool>
    where
        TS: TTerm + ?Sized,
        TP: TTerm + ?Sized,
        TO: TTerm + ?Sized,
    {
        self.dataset.borrow_mut().insert(s, p, o, self.gmatcher)
    }

    fn remove<TS, TP, TO>(&mut self, s: &TS, p: &TP, o: &TO) -> MgResult<Self, bool>
    where
        TS: TTerm + ?Sized,
        TP: TTerm + ?Sized,
        TO: TTerm + ?Sized,
    {
        self.dataset.borrow_mut().remove(s, p, o, self.gmatcher)
    }
}

impl<D, E, T> SetGraph for DatasetGraph<D, E, Option<&T>>
where
    D: Dataset + SetDataset,
    E: Borrow<D>,
    T: TTerm + ?Sized,
{
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::dataset::test::*;
    use crate::dataset::MdResult;
    use crate::dataset::*;
    use crate::quad::stream::QuadSource;
    use crate::term::{same_graph_name, SimpleIri, TTerm};
    use crate::triple::stream::TripleSource;
    use std::collections::HashSet;

    type BoxTerm = crate::term::test::TestTerm<Box<str>>;

    type MyQuad = ([BoxTerm; 3], Option<BoxTerm>);
    type MyDataset = HashSet<MyQuad>;
    type MyDatasetGraph = DatasetGraph<MyDataset, MyDataset, Option<&'static SimpleIri<'static>>>;

    fn make_default_graph<TS>(ts: TS) -> Result<MyDatasetGraph, ()>
    where
        TS: TripleSource,
    {
        let mut g = DatasetGraph {
            dataset: MyDataset::new(),
            gmatcher: None,
            _phantom: PhantomData,
        };
        g.insert_all(ts).unwrap();
        Ok(g)
    }

    #[cfg(feature = "all_tests")]
    fn make_named_graph<TS>(ts: TS) -> Result<MyDatasetGraph, ()>
    where
        TS: TripleSource,
    {
        let mut g = DatasetGraph {
            dataset: MyDataset::new(),
            gmatcher: Some(&crate::ns::rdfs::Resource),
            _phantom: PhantomData,
        };
        g.insert_all(ts).unwrap();
        Ok(g)
    }

    crate::test_graph_impl!(dflt_graph, MyDatasetGraph, true, true, make_default_graph);
    #[cfg(feature = "all_tests")]
    crate::test_graph_impl!(named_graph, MyDatasetGraph, true, true, make_named_graph);

    #[test]
    fn test_graph_default() -> MdResult<MyDataset, ()> {
        let d: MyDataset = some_quads().collect_quads().unwrap();
        assert_eq!(d.graph(DG.as_ref()).triples().count(), 4);
        assert_eq!(d.graph(GN1.as_ref()).triples().count(), 7);
        assert_eq!(d.graph(GN2.as_ref()).triples().count(), 7);
        assert_eq!(d.union_graph(ANY).triples().count(), 18);
        assert_eq!(
            d.union_graph(vec![DG.as_ref(), GN1.as_ref()])
                .triples()
                .count(),
            11
        );
        assert_eq!(
            d.union_graph([|x: Option<&dyn TTerm>| same_graph_name(x, DG.as_ref())
                || same_graph_name(x, GN2.as_ref())])
                .triples()
                .count(),
            11
        );
        Ok(())
    }
}