/// <summary> /// Saves the given Store to Disk using the settings the Writer was instantiated with /// </summary> /// <param name="store">Store to save</param> /// <param name="parameters">Parameters indicating where to save to</param> public void Save(ITripleStore store, IStoreParams parameters) { if (parameters is FolderStoreParams) { //Get the Parameters FolderStoreWriterContext context = new FolderStoreWriterContext(store, (FolderStoreParams)parameters); //Create the Folder if (!Directory.Exists(context.Folder)) { Directory.CreateDirectory(context.Folder); } //Write list of Graphs and Queue URIs of Graphs for writing by the async calls StreamWriter graphlist = new StreamWriter(Path.Combine(context.Folder,"graphs.fstore")); String ext; switch (context.Format) { case FolderStoreFormat.Turtle: ext = ".ttl"; break; case FolderStoreFormat.Notation3: ext = ".n3"; break; case FolderStoreFormat.RdfXml: ext = ".rdf"; break; default: ext = ".ttl"; break; } graphlist.WriteLine(ext); foreach (Uri u in context.Store.Graphs.GraphUris) { context.Add(u); graphlist.WriteLine(u.GetSha256Hash() + ext); } graphlist.Close(); //Start making the async calls List<IAsyncResult> results = new List<IAsyncResult>(); SaveGraphsDelegate d = new SaveGraphsDelegate(this.SaveGraphs); 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); } } //If there were any errors we'll throw an RdfThreadedOutputException now if (outputEx.InnerExceptions.Any()) throw outputEx; } else { throw new RdfStorageException("Parameters for the FolderStoreWriter must be of type FolderStoreParams"); } }
/// <summary> /// Internal Method which performs multi-threaded writing of data /// </summary> private void SaveGraphs(FolderStoreWriterContext context) { //Create the relevant Writer IRdfWriter writer; switch (context.Format) { case FolderStoreFormat.Turtle: writer = new TurtleWriter(); break; case FolderStoreFormat.Notation3: writer = new Notation3Writer(); break; case FolderStoreFormat.RdfXml: #if !NO_XMLDOM writer = new FastRdfXmlWriter(); #else writer = new RdfXmlWriter(); #endif break; default: writer = new TurtleWriter(); break; } try { Uri u = context.GetNextURI(); while (u != null) { //Get the Graph from the Store IGraph g = context.Store.Graphs[u]; //Write to Disk String destFile = Path.Combine(context.Folder, u.GetSha256Hash()); switch (context.Format) { case FolderStoreFormat.Turtle: destFile += ".ttl"; break; case FolderStoreFormat.Notation3: destFile += ".n3"; break; case FolderStoreFormat.RdfXml: destFile += ".rdf"; break; default: destFile += ".ttl"; break; } writer.Save(g, destFile); //Get the Next Uri u = context.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); } }