Пример #1
0
        /// <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
            {
                Uri u = globalContext.GetNextUri();
                while (u != null)
                {
                    //Get the Graph from the Store
                    if (WriterHelper.IsDefaultGraph(u) && !globalContext.Store.HasGraph(u)) u = null;
                    IGraph g = globalContext.Store.Graphs[u];

                    //Generate the Graph Output and add to Stream
                    NTriplesWriterContext context = new NTriplesWriterContext(g, new System.IO.StringWriter(), globalContext.PrettyPrint, globalContext.HighSpeedModePermitted);
                    String graphContent = this.GraphToNQuads(globalContext, context);
                    if (!graphContent.Equals(String.Empty))
                    {
                        try
                        {
                            Monitor.Enter(globalContext.Output);
                            globalContext.Output.WriteLine(graphContent);
                            globalContext.Output.Flush();
                        }
                        catch
                        {
                            throw;
                        }
                        finally
                        {
                            Monitor.Exit(globalContext.Output);
                        }
                    }

                    //Get the Next Uri
                    u = globalContext.GetNextUri();
                }
            }
            catch (ThreadAbortException)
            {
                //We've been terminated, don't do anything
            #if !SILVERLIGHT
                Thread.ResetAbort();
            #endif
            }
            catch (Exception ex)
            {
                throw new RdfStorageException("Error in Threaded Writer in Thread ID " + Thread.CurrentThread.ManagedThreadId, ex);
            }
        }
Пример #2
0
        private String GraphToNQuads(ThreadedStoreWriterContext globalContext, NTriplesWriterContext context)
        {
            if (context.Graph.IsEmpty) return String.Empty;
            if (context.PrettyPrint && !WriterHelper.IsDefaultGraph(context.Graph.BaseUri))
            {
                context.Output.WriteLine("# Graph: " + context.Graph.BaseUri.ToString());
            }
            foreach (Triple t in context.Graph.Triples)
            {
                context.Output.WriteLine(this.TripleToNQuads(context, t));
            }
            context.Output.WriteLine();

            return context.Output.ToString();
        }
Пример #3
0
        /// <summary>
        /// Saves a Store in NQuads format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating a Stream to write to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            ThreadedStoreWriterContext context = null;
            if (parameters is StreamParams)
            {
                //Create a new Writer Context
            #if !SILVERLIGHT
                ((StreamParams)parameters).Encoding = Encoding.ASCII;
            #endif
                context = new ThreadedStoreWriterContext(store, ((StreamParams)parameters).StreamWriter, this._prettyPrint, false);
            }
            else if (parameters is TextWriterParams)
            {
                context = new ThreadedStoreWriterContext(store, ((TextWriterParams)parameters).TextWriter, this._prettyPrint, false);
            }

            if (context != null)
            {
                //Check there's something to do
                if (context.Store.Graphs.Count == 0)
                {
                    context.Output.Close();
                    return;
                }

                try
                {
                    if (this._multiThreaded)
                    {
                        //Queue the Graphs to be written
                        foreach (IGraph g in context.Store.Graphs)
                        {
                            if (g.BaseUri == null)
                            {
                                context.Add(UriFactory.Create(GraphCollection.DefaultGraphUri));
                            }
                            else
                            {
                                context.Add(g.BaseUri);
                            }
                        }

                        //Start making the async calls
                        List<IAsyncResult> results = new List<IAsyncResult>();
                        SaveGraphsDelegate d = new SaveGraphsDelegate(this.SaveGraphs);
                        for (int i = 0; i < this._threads; i++)
                        {
                            results.Add(d.BeginInvoke(context, null, null));
                        }

                        //Wait for all the async calls to complete
                        WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                        RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                        foreach (IAsyncResult result in results)
                        {
                            try
                            {
                                d.EndInvoke(result);
                            }
                            catch (Exception ex)
                            {
                                outputEx.AddException(ex);
                            }
                        }
                        context.Output.Close();

                        //If there were any errors we'll throw an RdfThreadedOutputException now
                        if (outputEx.InnerExceptions.Any()) throw outputEx;
                    }
                    else
                    {
                        foreach (IGraph g in context.Store.Graphs)
                        {
                            NTriplesWriterContext graphContext = new NTriplesWriterContext(g, context.Output);
                            foreach (Triple t in g.Triples)
                            {
                                context.Output.WriteLine(this.TripleToNQuads(graphContext, t));
                            }
                        }
                        context.Output.Close();
                    }
                }
                catch
                {
                    try
                    {
                        context.Output.Close();
                    }
                    catch
                    {
                        //Just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the NQuadsWriter must be of the type StreamParams/TextWriterParams");
            }
        }
Пример #4
0
        /// <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
            {
                Uri u = globalContext.GetNextUri();
                while (u != null)
                {
                    //Get the Graph from the Store
                    IGraph g = globalContext.Store.Graphs[u];

                    //Generate the Graph Output and add to Stream
                    BaseWriterContext context = new BaseWriterContext(g, new System.IO.StringWriter());
                    String graphContent = this.GenerateGraphOutput(globalContext, context);
                    try
                    {
                        Monitor.Enter(globalContext.Output);
                        globalContext.Output.WriteLine(graphContent);
                        globalContext.Output.Flush();
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        Monitor.Exit(globalContext.Output);
                    }

                    //Get the Next Uri
                    u = globalContext.GetNextUri();
                }
            }
            catch (ThreadAbortException)
            {
                //We've been terminated, don't do anything
            #if !SILVERLIGHT
                Thread.ResetAbort();
            #endif
            }
            catch (Exception ex)
            {
                throw new RdfStorageException("Error in Threaded Writer in Thread ID " + Thread.CurrentThread.ManagedThreadId, ex);
            }
        }
Пример #5
0
        /// <summary>
        /// Generates the Output for a Graph as a String in CSV syntax
        /// </summary>
        /// <param name="globalContext">Context for writing the Store</param>
        /// <param name="context">Context for writing the Graph</param>
        /// <returns></returns>
        private String GenerateGraphOutput(ThreadedStoreWriterContext globalContext, BaseWriterContext context)
        {
            if (!WriterHelper.IsDefaultGraph(context.Graph.BaseUri))
            {
                //Named Graphs have a fourth context field added
                foreach (Triple t in context.Graph.Triples)
                {
                    this.GenerateNodeOutput(context, t.Subject, TripleSegment.Subject);
                    context.Output.Write(',');
                    this.GenerateNodeOutput(context, t.Predicate, TripleSegment.Predicate);
                    context.Output.Write(',');
                    this.GenerateNodeOutput(context, t.Object, TripleSegment.Object);
                    context.Output.Write(',');
                    context.Output.Write(this._formatter.FormatUri(context.Graph.BaseUri));
                    context.Output.Write("\r\n");
                }
            }
            else
            {
                //Default Graph has an empty field added
                foreach (Triple t in context.Graph.Triples)
                {
                    this.GenerateNodeOutput(context, t.Subject, TripleSegment.Subject);
                    context.Output.Write(',');
                    this.GenerateNodeOutput(context, t.Predicate, TripleSegment.Predicate);
                    context.Output.Write(',');
                    this.GenerateNodeOutput(context, t.Object, TripleSegment.Object);
                    context.Output.Write(',');
                    context.Output.Write("\r\n");
                }
            }

            return context.Output.ToString();
        }
Пример #6
0
        /// <summary>
        /// Saves a Triple Store to CSV Format
        /// </summary>
        /// <param name="store">Triple Store to save</param>
        /// <param name="parameters">A set of <see cref="StreamParams">StreamParams</see></param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            ThreadedStoreWriterContext context = null;
            if (parameters is StreamParams)
            {
                //Create a new Writer Context
                context = new ThreadedStoreWriterContext(store, ((StreamParams)parameters).StreamWriter);
            }
            else if (parameters is TextWriterParams)
            {
                context = new ThreadedStoreWriterContext(store, ((TextWriterParams)parameters).TextWriter);
            }

            if (context != null)
            {
                //Check there's something to do
                if (context.Store.Graphs.Count == 0)
                {
                    context.Output.Close();
                    return;
                }

                //Queue the Graphs to be written
                foreach (IGraph g in context.Store.Graphs)
                {
                    context.Add(g.BaseUri);
                }

                //Start making the async calls
                List<IAsyncResult> results = new List<IAsyncResult>();
                SaveGraphsDeletegate d = new SaveGraphsDeletegate(this.SaveGraphs);
                for (int i = 0; i < this._threads; i++)
                {
                    results.Add(d.BeginInvoke(context, null, null));
                }

                //Wait for all the async calls to complete
                WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("CSV"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }
                context.Output.Close();

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any()) throw outputEx;
            }
            else
            {
                throw new RdfStorageException("Parameters for the CsvStoreWriter must be of the type StreamParams/TextWriterParams");
            }
        }