oxirs-arq 0.3.1

Jena-style SPARQL algebra with extension points and query optimization
Documentation
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! SPARQL 1.1 UPDATE Graph Management — Operation Execution
//!
//! Implements the [`GraphManagementExecutor`] which applies LOAD, CLEAR, DROP,
//! CREATE, COPY, MOVE, and ADD operations to a [`GraphManagementDataset`].

use anyhow::{anyhow, Result};

use crate::update_graph_management_types::{
    GraphManagementDataset, GraphManagementOp, GraphManagementResult, GraphManagementTarget, Triple,
};

// ---------------------------------------------------------------------------
// Executor
// ---------------------------------------------------------------------------

/// Executor for SPARQL 1.1 UPDATE graph management operations.
///
/// All operations are applied to a [`GraphManagementDataset`] in memory.
/// HTTP-based `LOAD` is intentionally not implemented (see
/// [`Self::execute`] for details).
pub struct GraphManagementExecutor;

impl GraphManagementExecutor {
    /// Execute a graph management operation against the supplied dataset.
    ///
    /// ### Notes on `LOAD`
    /// `LOAD` requires an HTTP (or file-system) client and is therefore **not**
    /// implemented here.  Calling `LOAD` with `silent = false` returns an
    /// error; with `silent = true` it returns a no-op success.  Real
    /// implementations should use `reqwest` or `ureq` to fetch the document.
    pub fn execute(
        op: &GraphManagementOp,
        dataset: &mut GraphManagementDataset,
    ) -> Result<GraphManagementResult> {
        match op {
            GraphManagementOp::Load {
                iri,
                into_graph,
                silent,
            } => Self::execute_load(iri, into_graph.as_deref(), *silent),
            GraphManagementOp::Clear { target, silent } => {
                Self::execute_clear(target, *silent, dataset)
            }
            GraphManagementOp::Drop { target, silent } => {
                Self::execute_drop(target, *silent, dataset)
            }
            GraphManagementOp::Create { graph, silent } => {
                Self::execute_create(graph, *silent, dataset)
            }
            GraphManagementOp::Copy {
                source,
                destination,
                silent,
            } => Self::execute_copy(source, destination, *silent, dataset),
            GraphManagementOp::Move {
                source,
                destination,
                silent,
            } => Self::execute_move(source, destination, *silent, dataset),
            GraphManagementOp::Add {
                source,
                destination,
                silent,
            } => Self::execute_add(source, destination, *silent, dataset),
        }
    }

    // -----------------------------------------------------------------------
    // LOAD
    // -----------------------------------------------------------------------

    fn execute_load(
        iri: &str,
        _into_graph: Option<&str>,
        silent: bool,
    ) -> Result<GraphManagementResult> {
        // HTTP loading is not implemented in this in-memory executor.
        if silent {
            Ok(GraphManagementResult::default())
        } else {
            Err(anyhow!(
                "LOAD <{iri}> is not supported by the in-memory graph management executor. \
                 Use a network-capable executor or specify SILENT to suppress this error."
            ))
        }
    }

    // -----------------------------------------------------------------------
    // CLEAR
    // -----------------------------------------------------------------------

    fn execute_clear(
        target: &GraphManagementTarget,
        silent: bool,
        dataset: &mut GraphManagementDataset,
    ) -> Result<GraphManagementResult> {
        let mut result = GraphManagementResult::default();

        match target {
            GraphManagementTarget::Default => {
                result.triples_affected = dataset.default_graph.len();
                dataset.default_graph.clear();
                result.graphs_affected.push("DEFAULT".to_owned());
            }
            GraphManagementTarget::Named(iri) => match dataset.named_graphs.get_mut(iri) {
                Some(triples) => {
                    result.triples_affected = triples.len();
                    triples.clear();
                    result.graphs_affected.push(iri.clone());
                }
                None => {
                    if !silent {
                        return Err(anyhow!("CLEAR GRAPH <{iri}>: named graph does not exist"));
                    }
                }
            },
            GraphManagementTarget::AllNamed => {
                for (iri, triples) in &mut dataset.named_graphs {
                    result.triples_affected += triples.len();
                    triples.clear();
                    result.graphs_affected.push(iri.clone());
                }
            }
            GraphManagementTarget::All => {
                result.triples_affected += dataset.default_graph.len();
                dataset.default_graph.clear();
                result.graphs_affected.push("DEFAULT".to_owned());

                for (iri, triples) in &mut dataset.named_graphs {
                    result.triples_affected += triples.len();
                    triples.clear();
                    result.graphs_affected.push(iri.clone());
                }
            }
        }

        Ok(result)
    }

    // -----------------------------------------------------------------------
    // DROP
    // -----------------------------------------------------------------------

    fn execute_drop(
        target: &GraphManagementTarget,
        silent: bool,
        dataset: &mut GraphManagementDataset,
    ) -> Result<GraphManagementResult> {
        let mut result = GraphManagementResult::default();

        match target {
            GraphManagementTarget::Default => {
                result.triples_affected = dataset.default_graph.len();
                dataset.default_graph.clear();
                result.graphs_affected.push("DEFAULT".to_owned());
            }
            GraphManagementTarget::Named(iri) => match dataset.named_graphs.remove(iri) {
                Some(triples) => {
                    result.triples_affected = triples.len();
                    result.graphs_affected.push(iri.clone());
                }
                None => {
                    if !silent {
                        return Err(anyhow!("DROP GRAPH <{iri}>: named graph does not exist"));
                    }
                }
            },
            GraphManagementTarget::AllNamed => {
                for (iri, triples) in dataset.named_graphs.drain() {
                    result.triples_affected += triples.len();
                    result.graphs_affected.push(iri);
                }
            }
            GraphManagementTarget::All => {
                result.triples_affected += dataset.default_graph.len();
                dataset.default_graph.clear();
                result.graphs_affected.push("DEFAULT".to_owned());

                for (iri, triples) in dataset.named_graphs.drain() {
                    result.triples_affected += triples.len();
                    result.graphs_affected.push(iri);
                }
            }
        }

        Ok(result)
    }

    // -----------------------------------------------------------------------
    // CREATE
    // -----------------------------------------------------------------------

    fn execute_create(
        graph: &str,
        silent: bool,
        dataset: &mut GraphManagementDataset,
    ) -> Result<GraphManagementResult> {
        if dataset.named_graphs.contains_key(graph) {
            if !silent {
                return Err(anyhow!("CREATE GRAPH <{graph}>: graph already exists"));
            }
            return Ok(GraphManagementResult::default());
        }

        dataset.named_graphs.insert(graph.to_owned(), Vec::new());

        Ok(GraphManagementResult {
            triples_affected: 0,
            graphs_affected: vec![graph.to_owned()],
        })
    }

    // -----------------------------------------------------------------------
    // Helpers for resolving graph contents
    // -----------------------------------------------------------------------

    /// Retrieve a *clone* of all triples in the given target, validating that
    /// it exists when `silent` is false.
    fn get_triples_for_target(
        target: &GraphManagementTarget,
        silent: bool,
        dataset: &GraphManagementDataset,
    ) -> Result<Vec<Triple>> {
        match target {
            GraphManagementTarget::Default => Ok(dataset.default_graph.clone()),
            GraphManagementTarget::Named(iri) => match dataset.named_graphs.get(iri) {
                Some(triples) => Ok(triples.clone()),
                None => {
                    if silent {
                        Ok(vec![])
                    } else {
                        Err(anyhow!("Graph <{iri}> does not exist"))
                    }
                }
            },
            // COPY/MOVE/ADD with AllNamed or All as source is a SPARQL error
            // (the spec requires a single graph as source).
            GraphManagementTarget::AllNamed | GraphManagementTarget::All => {
                if silent {
                    Ok(vec![])
                } else {
                    Err(anyhow!(
                        "COPY/MOVE/ADD source must be a single graph (DEFAULT or a named graph IRI), \
                         not ALL or NAMED"
                    ))
                }
            }
        }
    }

    /// Clear the destination graph storage.
    fn clear_destination(
        target: &GraphManagementTarget,
        dataset: &mut GraphManagementDataset,
    ) -> Result<()> {
        match target {
            GraphManagementTarget::Default => {
                dataset.default_graph.clear();
            }
            GraphManagementTarget::Named(iri) => {
                dataset.named_graphs.entry(iri.clone()).or_default().clear();
            }
            GraphManagementTarget::AllNamed | GraphManagementTarget::All => {
                return Err(anyhow!(
                    "Destination must be a single graph (DEFAULT or a named graph IRI)"
                ));
            }
        }
        Ok(())
    }

    /// Write triples into the destination graph (appending).
    fn write_triples_to_destination(
        target: &GraphManagementTarget,
        triples: Vec<Triple>,
        dataset: &mut GraphManagementDataset,
    ) -> Result<usize> {
        let count = triples.len();
        match target {
            GraphManagementTarget::Default => {
                dataset.default_graph.extend(triples);
            }
            GraphManagementTarget::Named(iri) => {
                dataset
                    .named_graphs
                    .entry(iri.clone())
                    .or_default()
                    .extend(triples);
            }
            GraphManagementTarget::AllNamed | GraphManagementTarget::All => {
                return Err(anyhow!(
                    "Destination must be a single graph (DEFAULT or a named graph IRI)"
                ));
            }
        }
        Ok(count)
    }

    /// Remove the source graph from the dataset (used by MOVE).
    fn drop_source(target: &GraphManagementTarget, dataset: &mut GraphManagementDataset) {
        match target {
            GraphManagementTarget::Default => {
                dataset.default_graph.clear();
            }
            GraphManagementTarget::Named(iri) => {
                dataset.named_graphs.remove(iri);
            }
            // These variants are caught earlier; safe to no-op here.
            GraphManagementTarget::AllNamed | GraphManagementTarget::All => {}
        }
    }

    /// Return the canonical string label for a target (for reporting).
    pub fn target_label(target: &GraphManagementTarget) -> String {
        match target {
            GraphManagementTarget::Default => "DEFAULT".to_owned(),
            GraphManagementTarget::Named(iri) => iri.clone(),
            GraphManagementTarget::AllNamed => "NAMED".to_owned(),
            GraphManagementTarget::All => "ALL".to_owned(),
        }
    }

    // -----------------------------------------------------------------------
    // COPY
    // -----------------------------------------------------------------------

    fn execute_copy(
        source: &GraphManagementTarget,
        dest: &GraphManagementTarget,
        silent: bool,
        dataset: &mut GraphManagementDataset,
    ) -> Result<GraphManagementResult> {
        // If source == destination this is a no-op per the W3C spec.
        if source == dest {
            return Ok(GraphManagementResult {
                triples_affected: 0,
                graphs_affected: vec![Self::target_label(dest)],
            });
        }

        let source_triples = Self::get_triples_for_target(source, silent, dataset)?;
        let count = source_triples.len();

        Self::clear_destination(dest, dataset)?;
        Self::write_triples_to_destination(dest, source_triples, dataset)?;

        Ok(GraphManagementResult {
            triples_affected: count,
            graphs_affected: vec![Self::target_label(source), Self::target_label(dest)],
        })
    }

    // -----------------------------------------------------------------------
    // MOVE
    // -----------------------------------------------------------------------

    fn execute_move(
        source: &GraphManagementTarget,
        dest: &GraphManagementTarget,
        silent: bool,
        dataset: &mut GraphManagementDataset,
    ) -> Result<GraphManagementResult> {
        // MOVE to self is a no-op.
        if source == dest {
            return Ok(GraphManagementResult {
                triples_affected: 0,
                graphs_affected: vec![Self::target_label(dest)],
            });
        }

        let source_triples = Self::get_triples_for_target(source, silent, dataset)?;
        let count = source_triples.len();

        Self::clear_destination(dest, dataset)?;
        Self::write_triples_to_destination(dest, source_triples, dataset)?;
        Self::drop_source(source, dataset);

        Ok(GraphManagementResult {
            triples_affected: count,
            graphs_affected: vec![Self::target_label(source), Self::target_label(dest)],
        })
    }

    // -----------------------------------------------------------------------
    // ADD
    // -----------------------------------------------------------------------

    fn execute_add(
        source: &GraphManagementTarget,
        dest: &GraphManagementTarget,
        silent: bool,
        dataset: &mut GraphManagementDataset,
    ) -> Result<GraphManagementResult> {
        // ADD to self is a no-op.
        if source == dest {
            return Ok(GraphManagementResult {
                triples_affected: 0,
                graphs_affected: vec![Self::target_label(dest)],
            });
        }

        let source_triples = Self::get_triples_for_target(source, silent, dataset)?;
        let count = source_triples.len();

        Self::write_triples_to_destination(dest, source_triples, dataset)?;

        Ok(GraphManagementResult {
            triples_affected: count,
            graphs_affected: vec![Self::target_label(source), Self::target_label(dest)],
        })
    }
}