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
/*!
Provides an implementation of the W3C
[RDF 1.1: On Semantics of RDF Datasets](https://www.w3.org/TR/rdf11-datasets/) recommendation.
Additional semantics taken from [RDF 1.1 TriG](https://www.w3.org/TR/trig/), _RDF Dataset Language_.

# Example

*/

use crate::MemGraph;
use rdftk_core::data_set::{DataSet, DataSetIndex, GraphNameRef, MutableDataSet};
use std::collections::HashMap;

// ------------------------------------------------------------------------------------------------
// Public Types
// ------------------------------------------------------------------------------------------------

///
/// This implementation of the core `DataSet` and `MutableDataSet` traits is a simple in-memory hash
/// from graph name to a `MemGraph` implementation.
///
#[derive(Debug)]
pub struct MemDataSet {
    default_graph: Option<MemGraph>,
    graphs: HashMap<GraphNameRef, MemGraph>,
}

// ------------------------------------------------------------------------------------------------
// Implementations
// ------------------------------------------------------------------------------------------------

impl Default for MemDataSet {
    fn default() -> Self {
        Self {
            default_graph: None,
            graphs: Default::default(),
        }
    }
}

impl From<MemGraph> for MemDataSet {
    fn from(v: MemGraph) -> Self {
        Self {
            default_graph: Some(v),
            graphs: Default::default(),
        }
    }
}

impl From<HashMap<GraphNameRef, MemGraph>> for MemDataSet {
    fn from(graphs: HashMap<GraphNameRef, MemGraph>) -> Self {
        Self {
            default_graph: None,
            graphs,
        }
    }
}

impl<'a> DataSet<'a, MemGraph> for MemDataSet {
    type GraphIter = std::collections::hash_map::Iter<'a, GraphNameRef, MemGraph>;

    fn is_empty(&self) -> bool {
        todo!()
    }

    fn len(&self) -> usize {
        todo!()
    }

    fn has_default_graph(&self) -> bool {
        self.default_graph.is_some()
    }

    fn default_graph(&self) -> &Option<MemGraph> {
        &self.default_graph
    }

    fn has_graph_named(&self, name: &GraphNameRef) -> bool {
        self.graphs.contains_key(name)
    }

    fn graph_named(&self, name: &GraphNameRef) -> Option<&MemGraph> {
        self.graphs.get(name)
    }

    fn graphs(&'a self) -> Self::GraphIter {
        self.graphs.iter()
    }

    fn has_index(&self, _: &DataSetIndex) -> bool {
        false
    }
}

impl<'a> MutableDataSet<'a, MemGraph> for MemDataSet {
    fn set_default_graph(&mut self, graph: MemGraph) {
        self.default_graph = Some(graph);
    }

    fn unset_default_graph(&mut self) {
        self.default_graph = None;
    }

    fn insert(&mut self, name: GraphNameRef, graph: MemGraph) {
        let _ = self.graphs.insert(name, graph);
    }

    fn remove(&mut self, name: &GraphNameRef) {
        let _ = self.graphs.remove(name);
    }

    fn clear(&mut self) {
        self.graphs.clear();
        self.default_graph = None;
    }
}