/
TsvStoreWriter.cs
231 lines (213 loc) · 8.68 KB
/
TsvStoreWriter.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
/*
// <copyright>
// dotNetRDF is free and open source software licensed under the MIT License
// -------------------------------------------------------------------------
//
// Copyright (c) 2009-2023 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.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using VDS.RDF.Storage;
using VDS.RDF.Writing.Contexts;
using VDS.RDF.Writing.Formatting;
namespace VDS.RDF.Writing
{
/// <summary>
/// Class for generating TSV output from RDF datasets.
/// </summary>
public class TsvStoreWriter
: BaseStoreWriter, IFormatterBasedWriter
{
private int _threads = 4;
private readonly TsvFormatter _formatter = new TsvFormatter();
/// <summary>
/// Gets the type of the Triple Formatter used by this writer.
/// </summary>
public Type TripleFormatterType => _formatter.GetType();
/// <summary>
/// Saves a Triple Store to TSV format.
/// </summary>
/// <param name="store">Triple Store to save.</param>
/// <param name="writer">Writer to save to.</param>
/// <param name="leaveOpen">Boolean flag indicating if <paramref name="writer"/> should be left open after the store is saved.</param>
public override void Save(ITripleStore store, TextWriter writer, bool leaveOpen)
{
if (store == null) throw new RdfOutputException("Cannot output a null Triple Store");
if (writer == null) throw new RdfOutputException("Cannot output to a null writer");
var context = new ThreadedStoreWriterContext(store, writer);
// Check there's something to do
if (context.Store.Graphs.Count == 0)
{
if (!leaveOpen)
{
context.Output.Close();
}
return;
}
// Queue the Graphs to be written
foreach (IGraph g in context.Store.Graphs)
{
context.Add(g.Name);
}
// Start making the async calls
var tasks = new List<Task>();
for (var i = 0; i < _threads; i++)
{
tasks.Add(Task.Factory.StartNew(()=>SaveGraphs(context)));
}
// Wait for all the async calls to complete
var outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
try
{
Task.WaitAll(tasks.ToArray());
}
catch (AggregateException ex)
{
foreach (Exception innerEx in ex.InnerExceptions)
{
outputEx.AddException(innerEx);
}
}
if (!leaveOpen)
{
context.Output.Close();
}
// If there were any errors we'll throw an RdfThreadedOutputException now
if (outputEx.InnerExceptions.Any()) throw outputEx;
}
/// <summary>
/// Thread Worker method which writes Graphs to the output.
/// </summary>
/// <param name="globalContext">Context for writing the Store.</param>
private void SaveGraphs(ThreadedStoreWriterContext globalContext)
{
try
{
while (globalContext.TryGetNextGraphName(out IRefNode u))
{
// Get the Graph from the Store
IGraph g = globalContext.Store.Graphs[u];
// Generate the Graph Output and add to Stream
var context = new BaseWriterContext(g, new System.IO.StringWriter());
var graphContent = GenerateGraphOutput(context);
try
{
Monitor.Enter(globalContext.Output);
globalContext.Output.WriteLine(graphContent);
globalContext.Output.Flush();
}
finally
{
Monitor.Exit(globalContext.Output);
}
}
}
catch (ThreadAbortException)
{
// We've been terminated, don't do anything
Thread.ResetAbort();
}
catch (Exception ex)
{
throw new RdfStorageException("Error in Threaded Writer in Thread ID " + Thread.CurrentThread.ManagedThreadId, ex);
}
}
/// <summary>
/// Generates the Output for a Graph as a String in TSV syntax.
/// </summary>
/// <param name="context">Context for writing the Graph.</param>
/// <returns></returns>
private string GenerateGraphOutput(IWriterContext context)
{
if (context.Graph.BaseUri != null)
{
// Named Graphs have a fourth context field added
foreach (Triple t in context.Graph.Triples)
{
GenerateNodeOutput(context, t.Subject);
context.Output.Write('\t');
GenerateNodeOutput(context, t.Predicate);
context.Output.Write('\t');
GenerateNodeOutput(context, t.Object);
context.Output.Write('\t');
context.Output.Write('<');
context.Output.Write(_formatter.FormatUri(context.Graph.BaseUri));
context.Output.Write('>');
context.Output.Write('\n');
}
}
else
{
// Default Graph has an empty field added
foreach (Triple t in context.Graph.Triples)
{
GenerateNodeOutput(context, t.Subject);
context.Output.Write('\t');
GenerateNodeOutput(context, t.Predicate);
context.Output.Write('\t');
GenerateNodeOutput(context, t.Object);
context.Output.Write('\t');
context.Output.Write('\n');
}
}
return context.Output.ToString();
}
/// <summary>
/// Generates Output for the given Node.
/// </summary>
/// <param name="context">Writer Context.</param>
/// <param name="n">Node.</param>
private void GenerateNodeOutput(IWriterContext context, INode n)
{
switch (n.NodeType)
{
case NodeType.GraphLiteral:
throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("TSV"));
case NodeType.Blank:
case NodeType.Literal:
case NodeType.Uri:
context.Output.Write(_formatter.Format(n));
break;
default:
throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("TSV"));
}
}
/// <summary>
/// Event which is raised if the Writer detects a non-fatal error with the RDF being output
/// </summary>
/// <remarks>This class does not raise this event.</remarks>
#pragma warning disable CS0067
public override event StoreWriterWarning Warning;
#pragma warning restore CS0067
/// <summary>
/// Gets the String representation of the writer which is a description of the syntax it produces.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "TSV";
}
}
}