/// <summary> /// Saves the given Triple Store to the Target Store that this class was instantiated with /// </summary> /// <param name="store">Store to Save</param> /// <param name="parameters">Parameters for the Store</param> public override void Save(ITripleStore store, IStoreParams parameters) { if (parameters is ThreadedSqlIOParams) { //Setup context ThreadedSqlStoreWriterContext context = new ThreadedSqlStoreWriterContext(store, (ThreadedSqlIOParams)parameters); bool transSetting = context.Manager.DisableTransactions; context.Manager.DisableTransactions = true; //Queue Graphs for Writing foreach (IGraph g in store.Graphs) { context.Add(g); } //Start making the async calls List<IAsyncResult> results = new List<IAsyncResult>(); WriteGraphsDelegate d = new WriteGraphsDelegate(this.WriteGraphs); for (int i = 0; i < context.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("Folder Store")); foreach (IAsyncResult result in results) { try { d.EndInvoke(result); } catch (Exception ex) { outputEx.AddException(ex); } } //Reset Transactions setting context.Manager.DisableTransactions = transSetting; //If there were any errors we'll throw an RdfThreadedOutputException now if (outputEx.InnerExceptions.Any()) throw outputEx; } else { throw new RdfStorageException("Parameters for the ThreadedSQLStoreWriter must be of type ThreadedSQLIOParams"); } }
/// <summary> /// Internal Worker method for Writer Threads /// </summary> private void WriteGraphs(ThreadedSqlStoreWriterContext context) { try { int thread = Thread.CurrentThread.ManagedThreadId; SqlWriter writer = new SqlWriter(context.Manager); IGraph g = context.GetNextGraph(); while (g != null) { //Write the Graph to the Target Store writer.Save(g, context.ClearIfExists); //Get Next Graph g = context.GetNextGraph(); } } 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); } }