Skip to content

Commit a10656c

Browse files
author
Marco De Salvo
committed
RDFGraph and RDFMemoryStore are now compaitble with IAsyncDisposable
1 parent 33c2571 commit a10656c

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

RDFSharp.Test/Model/RDFGraphTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,21 @@ public async Task ShouldEnumerateGraphAsync()
18181818
Assert.AreEqual(2, z);
18191819
}
18201820

1821+
[TestMethod]
1822+
public async Task ShouldDisposeGraphWithUsingAsync()
1823+
{
1824+
RDFGraph graph;
1825+
await using (graph = new RDFGraph([
1826+
new RDFTriple(new RDFResource("http://subj/"),new RDFResource("http://pred/"),new RDFResource("http://obj/")),
1827+
new RDFTriple(new RDFResource("http://subj/"),new RDFResource("http://pred/"),new RDFPlainLiteral("lit")) ]))
1828+
{
1829+
Assert.IsFalse(graph.Disposed);
1830+
Assert.IsNotNull(graph.Index);
1831+
}
1832+
Assert.IsTrue(graph.Disposed);
1833+
Assert.IsNull(graph.Index);
1834+
}
1835+
18211836
[TestMethod]
18221837
public async Task ShouldAddContainerAsync()
18231838
{

RDFSharp.Test/Store/Engines/RDFMemoryStoreTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,20 @@ public async Task ShouldEnumerateQuadruplesAsync()
18631863
Assert.AreEqual(2, z);
18641864
}
18651865

1866+
[TestMethod]
1867+
public async Task ShouldDisposeMemoryStoreWithUsingAsync()
1868+
{
1869+
RDFMemoryStore store;
1870+
await using (store = new RDFMemoryStore([
1871+
new RDFQuadruple(new RDFContext("ex:c"), new RDFResource("ex:s"), new RDFResource("ex:p"), new RDFResource("ex:o")) ]))
1872+
{
1873+
Assert.IsFalse(store.Disposed);
1874+
Assert.IsNotNull(store.Index);
1875+
}
1876+
Assert.IsTrue(store.Disposed);
1877+
Assert.IsNull(store.Index);
1878+
}
1879+
18661880
[TestMethod]
18671881
public async Task ShouldMergeGraphAsync()
18681882
{

RDFSharp/Model/RDFGraph.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace RDFSharp.Model
3535
/// RDFGraph represents an Uri-named collection of triples
3636
/// </summary>
3737
#if NET8_0_OR_GREATER
38-
public sealed class RDFGraph : RDFDataSource, IEquatable<RDFGraph>, IEnumerable<RDFTriple>, IAsyncEnumerable<RDFTriple>, IDisposable
38+
public sealed class RDFGraph : RDFDataSource, IEquatable<RDFGraph>, IEnumerable<RDFTriple>, IAsyncEnumerable<RDFTriple>, IDisposable, IAsyncDisposable
3939
#else
4040
public sealed class RDFGraph : RDFDataSource, IEquatable<RDFGraph>, IEnumerable<RDFTriple>, IDisposable
4141
#endif
@@ -179,6 +179,18 @@ public void Dispose()
179179
GC.SuppressFinalize(this);
180180
}
181181

182+
#if NET8_0_OR_GREATER
183+
/// <summary>
184+
/// Asynchronously disposes the graph (IAsyncDisposable)
185+
/// </summary>
186+
ValueTask IAsyncDisposable.DisposeAsync()
187+
{
188+
Dispose(true);
189+
GC.SuppressFinalize(this);
190+
return ValueTask.CompletedTask;
191+
}
192+
#endif
193+
182194
/// <summary>
183195
/// Disposes the graph
184196
/// </summary>

RDFSharp/Store/Engines/RDFMemoryStore.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ limitations under the License.
2323
using System.IO;
2424
using System.Linq;
2525
using System.Net;
26-
using System.Runtime.CompilerServices;
2726
using System.Text;
28-
using System.Threading;
2927
using System.Threading.Tasks;
28+
#if NET8_0_OR_GREATER
29+
using System.Runtime.CompilerServices;
30+
using System.Threading;
31+
#endif
3032

3133
namespace RDFSharp.Store
3234
{
3335
/// <summary>
3436
/// RDFMemoryStore represents an in-memory RDF store engine.
3537
/// </summary>
3638
#if NET8_0_OR_GREATER
37-
public sealed class RDFMemoryStore : RDFStore, IEnumerable<RDFQuadruple>, IAsyncEnumerable<RDFQuadruple>, IDisposable
39+
public sealed class RDFMemoryStore : RDFStore, IEnumerable<RDFQuadruple>, IAsyncEnumerable<RDFQuadruple>, IDisposable, IAsyncDisposable
3840
#else
3941
public sealed class RDFMemoryStore : RDFStore, IEnumerable<RDFQuadruple>, IDisposable
4042
#endif
@@ -170,14 +172,26 @@ public IAsyncEnumerator<RDFQuadruple> GetAsyncEnumerator(CancellationToken cance
170172
#endif
171173

172174
/// <summary>
173-
/// Disposes the memory store (IDisposable)
175+
/// Disposes the store (IDisposable)
174176
/// </summary>
175177
public void Dispose()
176178
{
177179
Dispose(true);
178180
GC.SuppressFinalize(this);
179181
}
180182

183+
#if NET8_0_OR_GREATER
184+
/// <summary>
185+
/// Asynchronously disposes the store (IAsyncDisposable)
186+
/// </summary>
187+
ValueTask IAsyncDisposable.DisposeAsync()
188+
{
189+
Dispose(true);
190+
GC.SuppressFinalize(this);
191+
return ValueTask.CompletedTask;
192+
}
193+
#endif
194+
181195
/// <summary>
182196
/// Disposes the memory store
183197
/// </summary>

RDFSharp/Store/RDFStore.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ limitations under the License.
2121
using System.Linq;
2222
using System.Threading.Tasks;
2323
using RDFSharp.Model;
24-
using RDFSharp.Query;
2524

2625
namespace RDFSharp.Store
2726
{

0 commit comments

Comments
 (0)