-
Notifications
You must be signed in to change notification settings - Fork 87
/
InMemoryDataset.cs
319 lines (288 loc) · 11.4 KB
/
InMemoryDataset.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
/*
// <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 System.Threading;
namespace VDS.RDF.Query.Datasets
{
/// <summary>
/// Represents an in-memory dataset (i.e. a <see cref="IInMemoryQueryableStore">InMemoryQueryableStore</see>) for querying and updating using SPARQL.
/// </summary>
public class InMemoryDataset
: BaseTransactionalDataset
, IThreadSafeDataset
{
private IInMemoryQueryableStore _store;
private ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
/// <summary>
/// Creates a new in-memory dataset using the default in-memory <see cref="TripleStore">TripleStore</see> as the underlying storage.
/// </summary>
public InMemoryDataset()
: this(new TripleStore()) { }
/// <summary>
/// Creates a new in-memory dataset using the default in-memory <see cref="TripleStore">TripleStore</see> as the underlying storage.
/// </summary>
/// <param name="unionDefaultGraph">Whether the Default Graph when no Active/Default Graph is explicitly set should be the union of all Graphs in the Dataset.</param>
public InMemoryDataset(bool unionDefaultGraph)
: this(new TripleStore(), unionDefaultGraph) { }
/// <summary>
/// Creates a new in-memory dataset containing initially just the given graph and treating the given graph as the default graph of the dataset.
/// </summary>
/// <param name="g">Graph.</param>
public InMemoryDataset(IGraph g)
: this(g.AsTripleStore(), g.BaseUri) { }
/// <summary>
/// Creates a new In-Memory dataset.
/// </summary>
/// <param name="store">In-Memory queryable store.</param>
public InMemoryDataset(IInMemoryQueryableStore store)
: this(store, false) { }
/// <summary>
/// Creates a new In-Memory dataset.
/// </summary>
/// <param name="store">In-Memory queryable store.</param>
/// <param name="unionDefaultGraph">Whether the Default Graph when no Active/Default Graph is explicitly set should be the union of all Graphs in the Dataset.</param>
public InMemoryDataset(IInMemoryQueryableStore store, bool unionDefaultGraph)
: base(unionDefaultGraph)
{
if (store == null) throw new ArgumentNullException("store");
_store = store;
if (!_store.HasGraph(null))
{
_store.Add(new Graph());
}
}
/// <summary>
/// Creates a new In-Memory dataset.
/// </summary>
/// <param name="store">In-Memory queryable store.</param>
/// <param name="defaultGraphUri">Default Graph URI.</param>
public InMemoryDataset(IInMemoryQueryableStore store, Uri defaultGraphUri)
: base(defaultGraphUri)
{
if (store == null) throw new ArgumentNullException("store");
_store = store;
if (!_store.HasGraph(defaultGraphUri))
{
Graph g = new Graph();
g.BaseUri = defaultGraphUri;
_store.Add(g);
}
}
/// <summary>
/// Gets the Lock used to ensure MRSW concurrency on the dataset when available.
/// </summary>
public ReaderWriterLockSlim Lock
{
get
{
return _lock;
}
}
#region Graph Existence and Retrieval
/// <summary>
/// Adds a Graph to the Dataset merging it with any existing Graph with the same URI.
/// </summary>
/// <param name="g">Graph.</param>
protected override bool AddGraphInternal(IGraph g)
{
return _store.Add(g, true);
}
/// <summary>
/// Removes a Graph from the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
protected override bool RemoveGraphInternal(Uri graphUri)
{
if (graphUri == null)
{
if (_store.HasGraph(null))
{
_store.Graphs[null].Clear();
return true;
}
return false;
}
else
{
return _store.Remove(graphUri);
}
}
/// <summary>
/// Gets whether a Graph with the given URI is the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
/// <returns></returns>
protected override bool HasGraphInternal(Uri graphUri)
{
return _store.HasGraph(graphUri);
}
/// <summary>
/// Gets all the Graphs in the Dataset.
/// </summary>
public override IEnumerable<IGraph> Graphs
{
get
{
return _store.Graphs;
}
}
/// <summary>
/// Gets all the URIs of Graphs in the Dataset.
/// </summary>
public override IEnumerable<Uri> GraphUris
{
get
{
return (from g in _store.Graphs
select g.BaseUri);
}
}
/// <summary>
/// Gets the Graph with the given URI from the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
/// <returns></returns>
/// <remarks>
/// <para>
/// For In-Memory datasets the Graph returned from this property is no different from the Graph returned by the <see cref="InMemoryDataset.GetModifiableGraphInternal(Uri)">GetModifiableGraphInternal()</see> method.
/// </para>
/// </remarks>
protected override IGraph GetGraphInternal(Uri graphUri)
{
return _store[graphUri];
}
/// <summary>
/// Gets a Modifiable wrapper around a Graph in the Dataset.
/// </summary>
/// <param name="graphUri">Graph URI.</param>
/// <returns></returns>
protected override ITransactionalGraph GetModifiableGraphInternal(Uri graphUri)
{
return new GraphPersistenceWrapper(this[graphUri]);
}
#endregion
#region Triple Existence and Retrieval
/// <summary>
/// Gets whether the Dataset contains a specific Triple.
/// </summary>
/// <param name="t">Triple.</param>
/// <returns></returns>
protected override bool ContainsTripleInternal(Triple t)
{
return _store.Contains(t);
}
/// <summary>
/// Gets all the Triples in the underlying in-memory store.
/// </summary>
/// <returns></returns>
protected override IEnumerable<Triple> GetAllTriples()
{
return _store.Triples;
}
/// <summary>
/// Gets all the Triples in the Dataset with the given Subject.
/// </summary>
/// <param name="subj">Subject.</param>
/// <returns></returns>
protected override IEnumerable<Triple> GetTriplesWithSubjectInternal(INode subj)
{
return (from g in Graphs
from t in g.GetTriplesWithSubject(subj)
select t);
}
/// <summary>
/// Gets all the Triples in the Dataset with the given Predicate.
/// </summary>
/// <param name="pred">Predicate.</param>
/// <returns></returns>
protected override IEnumerable<Triple> GetTriplesWithPredicateInternal(INode pred)
{
return (from g in Graphs
from t in g.GetTriplesWithPredicate(pred)
select t);
}
/// <summary>
/// Gets all the Triples in the Dataset with the given Object.
/// </summary>
/// <param name="obj">Object.</param>
/// <returns></returns>
protected override IEnumerable<Triple> GetTriplesWithObjectInternal(INode obj)
{
return (from g in Graphs
from t in g.GetTriplesWithObject(obj)
select t);
}
/// <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 override IEnumerable<Triple> GetTriplesWithSubjectPredicateInternal(INode subj, INode pred)
{
return (from g in Graphs
from t in g.GetTriplesWithSubjectPredicate(subj, pred)
select t);
}
/// <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 override IEnumerable<Triple> GetTriplesWithSubjectObjectInternal(INode subj, INode obj)
{
return (from g in Graphs
from t in g.GetTriplesWithSubjectObject(subj, obj)
select t);
}
/// <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 override IEnumerable<Triple> GetTriplesWithPredicateObjectInternal(INode pred, INode obj)
{
return (from g in Graphs
from t in g.GetTriplesWithPredicateObject(pred, obj)
select t);
}
#endregion
/// <summary>
/// If there have been changes made to the Dataset and the underlying in-memory store is a <see cref="ITransactionalStore">ITransactionalStore</see> ensures the underlying store is notified to flush those changes.
/// </summary>
protected override void FlushInternal()
{
if (_store is ITransactionalStore)
{
((ITransactionalStore)_store).Flush();
}
}
}
}