-
Notifications
You must be signed in to change notification settings - Fork 87
/
BaseDataset.cs
1091 lines (1013 loc) · 38.1 KB
/
BaseDataset.cs
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
// <copyright>
// dotNetRDF is free and open source software licensed under the MIT License
// -------------------------------------------------------------------------
//
// Copyright (c) 2009-2021 dotNetRDF Project (http://dotnetrdf.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
*/
using System;
using System.Collections.Generic;
using System.Linq;
using VDS.Common.References;
namespace VDS.RDF.Query.Datasets
{
/// <summary>
/// Abstract Base Class for Datasets which provides implementation of Active and Default Graph management.
/// </summary>
public abstract class BaseDataset
: ISparqlDataset
{
/// <summary>
/// Reference to the Active Graph being used for executing a SPARQL Query.
/// </summary>
private readonly ThreadIsolatedReference<IGraph> _activeGraph;
/// <summary>
/// Default Graph for executing SPARQL Queries against.
/// </summary>
private readonly ThreadIsolatedReference<IGraph> _defaultGraph;
/// <summary>
/// Stack of Default Graph References used for executing a SPARQL Query when a Query may choose to change the Default Graph from the Dataset defined one.
/// </summary>
private readonly ThreadIsolatedReference<Stack<IGraph>> _defaultGraphs;
/// <summary>
/// Stack of Active Graph References used for executing a SPARQL Query when there are nested GRAPH Clauses.
/// </summary>
private readonly ThreadIsolatedReference<Stack<IGraph>> _activeGraphs;
private readonly ThreadIsolatedReference<Stack<IEnumerable<Uri>>> _defaultGraphUris;
private readonly ThreadIsolatedReference<Stack<IEnumerable<Uri>>> _activeGraphUris;
private readonly bool _unionDefaultGraph = true;
private readonly Uri _defaultGraphUri;
/// <summary>
/// Creates a new Dataset.
/// </summary>
public BaseDataset()
{
_activeGraph = new ThreadIsolatedReference<IGraph>();
_defaultGraph = new ThreadIsolatedReference<IGraph>(InitDefaultGraph);
_defaultGraphs = new ThreadIsolatedReference<Stack<IGraph>>(InitGraphStack);
_activeGraphs = new ThreadIsolatedReference<Stack<IGraph>>(InitGraphStack);
_defaultGraphUris = new ThreadIsolatedReference<Stack<IEnumerable<Uri>>>(InitDefaultGraphUriStack);
_activeGraphUris = new ThreadIsolatedReference<Stack<IEnumerable<Uri>>>(InitGraphUriStack);
}
/// <summary>
/// Creates a new Dataset with the given Union Default Graph setting.
/// </summary>
/// <param name="unionDefaultGraph">Whether to use a Union Default Graph.</param>
public BaseDataset(bool unionDefaultGraph)
: this()
{
_unionDefaultGraph = unionDefaultGraph;
}
/// <summary>
/// Creates a new Dataset with a fixed Default Graph and without a Union Default Graph.
/// </summary>
/// <param name="defaultGraphUri"></param>
public BaseDataset(Uri defaultGraphUri)
: this()
{
_unionDefaultGraph = false;
_defaultGraphUri = defaultGraphUri;
}
private IGraph InitDefaultGraph()
{
if (_unionDefaultGraph)
{
return null;
}
else
{
return GetGraphInternal(_defaultGraphUri);
}
}
private Stack<IGraph> InitGraphStack()
{
return new Stack<IGraph>();
}
private Stack<IEnumerable<Uri>> InitDefaultGraphUriStack()
{
Stack<IEnumerable<Uri>> s = new Stack<IEnumerable<Uri>>();
if (!_unionDefaultGraph)
{
s.Push(new Uri[] { _defaultGraphUri });
}
return s;
}
private Stack<IEnumerable<Uri>> InitGraphUriStack()
{
return new Stack<IEnumerable<Uri>>();
}
#region Active and Default Graph Management
/// <summary>
/// Gets a reference to the actual <see cref="IGraph">IGraph</see> that is currently treated as the default graph.
/// </summary>
protected IGraph InternalDefaultGraph
{
get
{
return _defaultGraph.Value;
}
}
/// <summary>
/// Sets the Default Graph for the SPARQL Query.
/// </summary>
/// <param name="g"></param>
private void SetDefaultGraphInternal(IGraph g)
{
_defaultGraphs.Value.Push(_defaultGraph.Value);
_defaultGraph.Value = g;
}
/// <summary>
/// Sets the Default Graph.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
public void SetDefaultGraph(Uri graphUri)
{
if (HasGraph(graphUri))
{
SetDefaultGraphInternal(this[graphUri]);
_defaultGraphUris.Value.Push(new Uri[] { graphUri });
}
else
{
SetDefaultGraphInternal(new Graph());
_defaultGraphUris.Value.Push(Enumerable.Empty<Uri>());
}
}
/// <summary>
/// Sets the Default Graph.
/// </summary>
/// <param name="graphUris">Graph URIs.</param>
public void SetDefaultGraph(IEnumerable<Uri> graphUris)
{
if (!graphUris.Any())
{
SetDefaultGraphInternal(new Graph());
_defaultGraphUris.Value.Push(Enumerable.Empty<Uri>());
}
else if (graphUris.Count() == 1)
{
SetDefaultGraph(graphUris.First());
}
else
{
// Multiple Graph URIs
// Build a merged Graph of all the Graph URIs
Graph g = new Graph();
foreach (Uri u in graphUris)
{
if (HasGraph(u))
{
g.Merge(this[u], true);
}
}
SetDefaultGraphInternal(g);
_defaultGraphUris.Value.Push(graphUris.ToList());
}
}
/// <summary>
/// Sets the Active Graph for the SPARQL Query.
/// </summary>
/// <param name="g">Active Graph.</param>
private void SetActiveGraphInternal(IGraph g)
{
_activeGraphs.Value.Push(_activeGraph.Value);
_activeGraph.Value = g;
}
/// <summary>
/// Sets the Active Graph for the SPARQL query.
/// </summary>
/// <param name="graphUri">Uri of the Active Graph.</param>
/// <remarks>
/// Helper function used primarily in the execution of GRAPH Clauses.
/// </remarks>
public void SetActiveGraph(Uri graphUri)
{
if (graphUri == null)
{
// Change the Active Graph so that the query operates over the default graph
// If the default graph is null then it operates over the entire dataset
_activeGraphs.Value.Push(_activeGraph.Value);
_activeGraph.Value = _defaultGraph.Value;
_activeGraphUris.Value.Push(_defaultGraphUris.Value.Count > 0 ? _defaultGraphUris.Value.Peek() : Enumerable.Empty<Uri>());
}
else if (HasGraph(graphUri))
{
SetActiveGraphInternal(this[graphUri]);
_activeGraphUris.Value.Push(new Uri[] { graphUri });
}
else
{
// Active Graph is an empty Graph in the case where the Graph is not present in the Dataset
SetActiveGraphInternal(new Graph());
_activeGraphUris.Value.Push(Enumerable.Empty<Uri>());
}
}
/// <summary>
/// Sets the Active Graph for the SPARQL query.
/// </summary>
/// <param name="graphUris">URIs of the Graphs which form the Active Graph.</param>
/// <remarks>Helper function used primarily in the execution of GRAPH Clauses.</remarks>
public void SetActiveGraph(IEnumerable<Uri> graphUris)
{
if (!graphUris.Any())
{
SetActiveGraph((Uri)null);
}
else if (graphUris.Count() == 1)
{
// If only 1 Graph Uri call the simpler SetActiveGraph method which will be quicker
SetActiveGraph(graphUris.First());
}
else
{
// Multiple Graph URIs
// Build a merged Graph of all the Graph URIs
Graph g = new Graph();
foreach (Uri u in graphUris)
{
if (HasGraph(u))
{
g.Merge(this[u], true);
}
}
SetActiveGraphInternal(g);
_activeGraphUris.Value.Push(graphUris.ToList());
}
}
/// <summary>
/// Sets the Active Graph for the SPARQL query to be the previous Active Graph.
/// </summary>
public void ResetActiveGraph()
{
if (_activeGraphs.Value.Count > 0)
{
_activeGraph.Value = _activeGraphs.Value.Pop();
_activeGraphUris.Value.Pop();
}
else
{
throw new RdfQueryException("Unable to reset the Active Graph since no previous Active Graphs exist");
}
}
/// <summary>
/// Sets the Default Graph for the SPARQL Query to be the previous Default Graph.
/// </summary>
public void ResetDefaultGraph()
{
if (_defaultGraphs.Value.Count > 0)
{
_defaultGraph.Value = _defaultGraphs.Value.Pop();
_defaultGraphUris.Value.Pop();
}
else
{
throw new RdfQueryException("Unable to reset the Default Graph since no previous Default Graphs exist");
}
}
/// <summary>
/// Gets the Default Graph URIs.
/// </summary>
public IEnumerable<Uri> DefaultGraphUris
{
get
{
if (_defaultGraphUris.Value.Count > 0)
{
return _defaultGraphUris.Value.Peek();
}
else
{
return Enumerable.Empty<Uri>();
}
}
}
/// <summary>
/// Gets the Active Graph URIs.
/// </summary>
public IEnumerable<Uri> ActiveGraphUris
{
get
{
if (_activeGraphUris.Value.Count > 0)
{
return _activeGraphUris.Value.Peek();
}
else
{
return Enumerable.Empty<Uri>();
}
}
}
/// <summary>
/// Gets whether the Default Graph is treated as being the union of all Graphs in the dataset when no Default Graph is otherwise set.
/// </summary>
public bool UsesUnionDefaultGraph
{
get
{
return _unionDefaultGraph;
}
}
#endregion
#region General Implementation
/// <summary>
/// Adds a Graph to the Dataset.
/// </summary>
/// <param name="g">Graph.</param>
public abstract bool AddGraph(IGraph g);
/// <summary>
/// Removes a Graph from the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
public virtual bool RemoveGraph(Uri graphUri)
{
if (graphUri == null)
{
if (_defaultGraph != null)
{
_defaultGraph.Value.Clear();
return true;
}
else if (HasGraph(graphUri))
{
return RemoveGraphInternal(graphUri);
}
}
else if (HasGraph(graphUri))
{
return RemoveGraphInternal(graphUri);
}
return false;
}
/// <summary>
/// Removes a Graph from the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
protected abstract bool RemoveGraphInternal(Uri graphUri);
/// <summary>
/// Gets whether a Graph with the given URI is the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
/// <returns></returns>
public bool HasGraph(Uri graphUri)
{
if (graphUri == null)
{
if (_defaultGraph != null)
{
return true;
}
else
{
return HasGraphInternal(null);
}
}
else
{
return HasGraphInternal(graphUri);
}
}
/// <summary>
/// Determines whether a given Graph exists in the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
/// <returns></returns>
protected abstract bool HasGraphInternal(Uri graphUri);
/// <summary>
/// Gets all the Graphs in the Dataset.
/// </summary>
public abstract IEnumerable<IGraph> Graphs
{
get;
}
/// <summary>
/// Gets all the URIs of Graphs in the Dataset.
/// </summary>
public abstract IEnumerable<Uri> GraphUris
{
get;
}
/// <summary>
/// Gets the Graph with the given URI from the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
/// <returns></returns>
/// <remarks>
/// <para>
/// This property need only return a read-only view of the Graph, code which wishes to modify Graphs should use the <see cref="ISparqlDataset.GetModifiableGraph">GetModifiableGraph()</see> method to guarantee a Graph they can modify and will be persisted to the underlying storage.
/// </para>
/// </remarks>
public virtual IGraph this[Uri graphUri]
{
get
{
if (graphUri == null)
{
if (_defaultGraph != null)
{
return _defaultGraph.Value;
}
else
{
return GetGraphInternal(null);
}
}
else
{
return GetGraphInternal(graphUri);
}
}
}
/// <summary>
/// Gets the given Graph from the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
/// <returns></returns>
protected abstract IGraph GetGraphInternal(Uri graphUri);
/// <summary>
/// Gets the Graph with the given URI from the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
/// <returns></returns>
/// <remarks>
/// <para>
/// Graphs returned from this method must be modifiable and the Dataset must guarantee that when it is Flushed or Disposed of that any changes to the Graph are persisted.
/// </para>
/// </remarks>
public abstract IGraph GetModifiableGraph(Uri graphUri);
/// <summary>
/// Gets whether the Dataset has any Triples.
/// </summary>
public virtual bool HasTriples
{
get
{
return Triples.Any();
}
}
/// <summary>
/// Gets whether the Dataset contains a specific Triple.
/// </summary>
/// <param name="t">Triple.</param>
/// <returns></returns>
public bool ContainsTriple(Triple t)
{
if (_activeGraph.Value == null)
{
if (_defaultGraph.Value == null)
{
return ContainsTripleInternal(t);
}
else
{
return _defaultGraph.Value.ContainsTriple(t);
}
}
else
{
return _activeGraph.Value.ContainsTriple(t);
}
}
/// <summary>
/// Determines whether the Dataset contains a specific Triple.
/// </summary>
/// <param name="t">Triple to search for.</param>
/// <returns></returns>
protected abstract bool ContainsTripleInternal(Triple t);
/// <summary>
/// Gets all the Triples in the Dataset.
/// </summary>
public IEnumerable<Triple> Triples
{
get
{
if (_activeGraph.Value == null)
{
if (_defaultGraph.Value == null)
{
// No specific Active Graph which implies that the Default Graph is the entire Triple Store
return GetAllTriples();
}
else
{
// Specific Default Graph so return that
return _defaultGraph.Value.Triples;
}
}
else
{
// Active Graph is used (which may happen to be the Default Graph)
return _activeGraph.Value.Triples;
}
}
}
/// <summary>
/// Abstract method that concrete implementations must implement to return an enumerable of all the Triples in the Dataset.
/// </summary>
/// <returns></returns>
protected abstract IEnumerable<Triple> GetAllTriples();
/// <summary>
/// Gets all the Triples in the Dataset with the given Subject.
/// </summary>
/// <param name="subj">Subject.</param>
/// <returns></returns>
public IEnumerable<Triple> GetTriplesWithSubject(INode subj)
{
if (_activeGraph.Value == null)
{
if (_defaultGraph.Value == null)
{
return GetTriplesWithSubjectInternal(subj);
}
else
{
return _defaultGraph.Value.GetTriplesWithSubject(subj);
}
}
else
{
return _activeGraph.Value.GetTriplesWithSubject(subj);
}
}
/// <summary>
/// Gets all the Triples in the Dataset with the given Subject.
/// </summary>
/// <param name="subj">Subject.</param>
/// <returns></returns>
protected abstract IEnumerable<Triple> GetTriplesWithSubjectInternal(INode subj);
/// <summary>
/// Gets all the Triples in the Dataset with the given Predicate.
/// </summary>
/// <param name="pred">Predicate.</param>
/// <returns></returns>
public IEnumerable<Triple> GetTriplesWithPredicate(INode pred)
{
if (_activeGraph.Value == null)
{
if (_defaultGraph.Value == null)
{
return GetTriplesWithPredicateInternal(pred);
}
else
{
return _defaultGraph.Value.GetTriplesWithPredicate(pred);
}
}
else
{
return _activeGraph.Value.GetTriplesWithPredicate(pred);
}
}
/// <summary>
/// Gets all the Triples in the Dataset with the given Predicate.
/// </summary>
/// <param name="pred">Predicate.</param>
/// <returns></returns>
protected abstract IEnumerable<Triple> GetTriplesWithPredicateInternal(INode pred);
/// <summary>
/// Gets all the Triples in the Dataset with the given Object.
/// </summary>
/// <param name="obj">Object.</param>
/// <returns></returns>
public IEnumerable<Triple> GetTriplesWithObject(INode obj)
{
if (_activeGraph.Value == null)
{
if (_defaultGraph.Value == null)
{
return GetTriplesWithObjectInternal(obj);
}
else
{
return _defaultGraph.Value.GetTriplesWithObject(obj);
}
}
else
{
return _activeGraph.Value.GetTriplesWithObject(obj);
}
}
/// <summary>
/// Gets all the Triples in the Dataset with the given Object.
/// </summary>
/// <param name="obj">Object.</param>
/// <returns></returns>
protected abstract IEnumerable<Triple> GetTriplesWithObjectInternal(INode obj);
/// <summary>
/// Gets all the Triples in the Dataset with the given Subject and Predicate.
/// </summary>
/// <param name="subj">Subject.</param>
/// <param name="pred">Predicate.</param>
/// <returns></returns>
public IEnumerable<Triple> GetTriplesWithSubjectPredicate(INode subj, INode pred)
{
if (_activeGraph.Value == null)
{
if (_defaultGraph.Value == null)
{
return GetTriplesWithSubjectPredicateInternal(subj, pred);
}
else
{
return _defaultGraph.Value.GetTriplesWithSubjectPredicate(subj, pred);
}
}
else
{
return _activeGraph.Value.GetTriplesWithSubjectPredicate(subj, pred);
}
}
/// <summary>
/// Gets all the Triples in the Dataset with the given Subject and Predicate.
/// </summary>
/// <param name="subj">Subject.</param>
/// <param name="pred">Predicate.</param>
/// <returns></returns>
protected abstract IEnumerable<Triple> GetTriplesWithSubjectPredicateInternal(INode subj, INode pred);
/// <summary>
/// Gets all the Triples in the Dataset with the given Subject and Object.
/// </summary>
/// <param name="subj">Subject.</param>
/// <param name="obj">Object.</param>
/// <returns></returns>
public IEnumerable<Triple> GetTriplesWithSubjectObject(INode subj, INode obj)
{
if (_activeGraph.Value == null)
{
if (_defaultGraph.Value == null)
{
return GetTriplesWithSubjectObjectInternal(subj, obj);
}
else
{
return _defaultGraph.Value.GetTriplesWithSubjectObject(subj, obj);
}
}
else
{
return _activeGraph.Value.GetTriplesWithSubjectObject(subj, obj);
}
}
/// <summary>
/// Gets all the Triples in the Dataset with the given Subject and Object.
/// </summary>
/// <param name="subj">Subject.</param>
/// <param name="obj">Object.</param>
/// <returns></returns>
protected abstract IEnumerable<Triple> GetTriplesWithSubjectObjectInternal(INode subj, INode obj);
/// <summary>
/// Gets all the Triples in the Dataset with the given Predicate and Object.
/// </summary>
/// <param name="pred">Predicate.</param>
/// <param name="obj">Object.</param>
/// <returns></returns>
public IEnumerable<Triple> GetTriplesWithPredicateObject(INode pred, INode obj)
{
if (_activeGraph.Value == null)
{
if (_defaultGraph.Value == null)
{
return GetTriplesWithPredicateObjectInternal(pred, obj);
}
else
{
return _defaultGraph.Value.GetTriplesWithPredicateObject(pred, obj);
}
}
else
{
return _activeGraph.Value.GetTriplesWithPredicateObject(pred, obj);
}
}
/// <summary>
/// Gets all the Triples in the Dataset with the given Predicate and Object.
/// </summary>
/// <param name="pred">Predicate.</param>
/// <param name="obj">Object.</param>
/// <returns></returns>
protected abstract IEnumerable<Triple> GetTriplesWithPredicateObjectInternal(INode pred, INode obj);
#endregion
/// <summary>
/// Ensures that any changes to the Dataset (if any) are flushed to the underlying Storage.
/// </summary>
public abstract void Flush();
/// <summary>
/// Ensures that any changes to the Dataset (if any) are discarded.
/// </summary>
public abstract void Discard();
}
/// <summary>
/// Abstract Base Class for Immutable Datasets.
/// </summary>
public abstract class BaseImmutableDataset
: BaseDataset
{
/// <summary>
/// Throws an exception since Immutable Datasets cannot be altered.
/// </summary>
/// <param name="g">Graph to add.</param>
public override bool AddGraph(IGraph g)
{
throw new NotSupportedException("Cannot add a Graph to an immutable Dataset");
}
/// <summary>
/// Throws an exception since Immutable Datasets cannot be altered.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
public override bool RemoveGraph(Uri graphUri)
{
throw new NotSupportedException("Cannot remove a Graph from an immutable Dataset");
}
/// <summary>
/// Throws an exception since Immutable Datasets cannot be altered.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
public override IGraph GetModifiableGraph(Uri graphUri)
{
throw new NotSupportedException("Cannot retrieve a Modifiable Graph from an immutable Dataset");
}
/// <summary>
/// Ensures that any changes to the Dataset (if any) are flushed to the underlying Storage.
/// </summary>
public sealed override void Flush()
{
// Does Nothing
}
/// <summary>
/// Ensures that any changes to the Dataset (if any) are discarded.
/// </summary>
public sealed override void Discard()
{
// Does Nothing
}
}
/// <summary>
/// Abstract Base Class for Mutable Datasets that support Transactions.
/// </summary>
/// <remarks>
/// <para>
/// The Transaction implementation of dotNetRDF is based upon a MRSW concurrency model, since only one writer may be active changes are immediately pushed to the dataset and visible within the transaction and they are committed or rolled back when <see cref="BaseTransactionalDataset.Flush()">Flush()</see> or <see cref="BaseTransactionalDataset.Discard()">Discard()</see> are called.
/// </para>
/// <para>
/// So in practical terms it is perfectly OK for the storage to be updated during a transaction because if the transaction fails the changes will be rolled back because all changes are stored in-memory until the end of the transaction. This may not be an ideal transaction model for all scenarios so you may wish to implement your own version of transactions or code your implementations of the abstract methods accordingly to limit actual persistence to the end of a transaction.
/// </para>
/// </remarks>
public abstract class BaseTransactionalDataset
: BaseDataset
{
private List<GraphPersistenceAction> _actions = new List<GraphPersistenceAction>();
private TripleStore _modifiableGraphs = new TripleStore();
/// <summary>
/// Creates a new Transactional Dataset.
/// </summary>
public BaseTransactionalDataset()
: base() { }
/// <summary>
/// Creates a new Transactional Dataset with the given Union Default Graph setting.
/// </summary>
/// <param name="unionDefaultGraph">Whether to use a Union Default Graph.</param>
public BaseTransactionalDataset(bool unionDefaultGraph)
: base(unionDefaultGraph) { }
/// <summary>
/// Creates a new Transactional Dataset with a fixed Default Graph and no Union Default Graph.
/// </summary>
/// <param name="defaultGraphUri">Default Graph URI.</param>
public BaseTransactionalDataset(Uri defaultGraphUri)
: base(defaultGraphUri) { }
/// <summary>
/// Adds a Graph to the Dataset.
/// </summary>
/// <param name="g">Graph to add.</param>
public sealed override bool AddGraph(IGraph g)
{
if (HasGraph(g.BaseUri))
{
ITransactionalGraph existing = (ITransactionalGraph)GetModifiableGraph(g.BaseUri);
_actions.Add(new GraphPersistenceAction(existing, GraphPersistenceActionType.Modified));
}
else
{
_actions.Add(new GraphPersistenceAction(g, GraphPersistenceActionType.Added));
}
return AddGraphInternal(g);
}
/// <summary>
/// Adds a Graph to the Dataset.
/// </summary>
/// <param name="g">Graph to add.</param>
protected abstract bool AddGraphInternal(IGraph g);
/// <summary>
/// Removes a Graph from the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
public sealed override bool RemoveGraph(Uri graphUri)
{
if (graphUri == null)
{
if (InternalDefaultGraph != null)
{
GraphPersistenceWrapper wrapper = new GraphPersistenceWrapper(InternalDefaultGraph);
wrapper.Clear();
_actions.Add(new GraphPersistenceAction(wrapper, GraphPersistenceActionType.Modified));
return true;
}
else if (HasGraph(graphUri))
{
_actions.Add(new GraphPersistenceAction(this[graphUri], GraphPersistenceActionType.Deleted));
return RemoveGraphInternal(graphUri);
}
}
else if (HasGraph(graphUri))
{
_actions.Add(new GraphPersistenceAction(this[graphUri], GraphPersistenceActionType.Deleted));
return RemoveGraphInternal(graphUri);
}
return false;
}
/// <summary>
/// Gets a Graph from the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
/// <returns></returns>
/// <remarks>
/// If the Graph has been modified during the active Transaction the modified version is returned rather than the original version.
/// </remarks>
public sealed override IGraph this[Uri graphUri]
{
get
{
if (graphUri == null)
{
if (InternalDefaultGraph != null)
{
return InternalDefaultGraph;
}
else if (_modifiableGraphs.HasGraph(graphUri))
{
return _modifiableGraphs[graphUri];
}
else
{
return GetGraphInternal(null);
}
}
else if (_modifiableGraphs.HasGraph(graphUri))
{
return _modifiableGraphs[graphUri];
}
else
{
return GetGraphInternal(graphUri);
}
}
}
/// <summary>
/// Gets a Graph from the Dataset that can be modified.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
/// <returns></returns>
public sealed override IGraph GetModifiableGraph(Uri graphUri)
{
if (!_modifiableGraphs.HasGraph(graphUri))
{
IGraph current = GetModifiableGraphInternal(graphUri);
if (!_modifiableGraphs.HasGraph(current.BaseUri))
{
_modifiableGraphs.Add(current);
}
graphUri = current.BaseUri;
}
ITransactionalGraph existing = (ITransactionalGraph)_modifiableGraphs[graphUri];
_actions.Add(new GraphPersistenceAction(existing, GraphPersistenceActionType.Modified));
return existing;
}
/// <summary>
/// Gets a Graph from the Dataset that can be modified transactionally.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
/// <returns></returns>
protected abstract ITransactionalGraph GetModifiableGraphInternal(Uri graphUri);
/// <summary>
/// Ensures that any changes to the Dataset (if any) are flushed to the underlying Storage.
/// </summary>
/// <remarks>
/// Commits the Active Transaction.
/// </remarks>
public sealed override void Flush()
{
int i = 0;
while (i < _actions.Count)
{
GraphPersistenceAction action = _actions[i];
switch (action.Action)
{
case GraphPersistenceActionType.Added:
// If Graph was added ensure any changes were flushed
action.Graph.Flush();
break;
case GraphPersistenceActionType.Deleted:
// If Graph was deleted can discard any changes