/// <summary> /// Runs the task /// </summary> /// <returns></returns> protected override TaskResult RunTaskInternal() { MimeTypeDefinition def = MimeTypesHelper.GetDefinitionsByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._file)).FirstOrDefault(d => d.CanWriteRdfDatasets); if (def == null) { throw new RdfOutputException("Cannot Export the Store to the selected File since dotNetRDF was unable to select a writer to use based on the File Extension"); } IStoreWriter writer = def.GetRdfDatasetWriter(); if (writer is IMultiThreadedWriter) { ((IMultiThreadedWriter)writer).UseMultiThreadedWriting = false; } TripleStore store = new TripleStore(); List <Uri> graphUris = this.ListGraphs().ToList(); if (writer is TriXWriter) { //For TriX must load all into memory and then write out all at once // Make sure we always include the default graph in the export if (!graphUris.Contains(null)) { graphUris.Add(null); } foreach (Uri u in graphUris) { Graph g = new Graph(); this._manager.LoadGraph(g, u); g.BaseUri = u; store.Add(g); this.Information = "Loading into memory prior to export, loaded " + store.Graphs.Sum(x => x.Triples.Count) + " Triple(s) in " + store.Graphs.Count + " Graph(s) so far..."; if (this.HasBeenCancelled) { this.Information = "Export Cancelled"; return(new TaskResult(true)); } } this.Information = "Exporting Data all at once, have " + store.Graphs.Sum(x => x.Triples.Count) + " Triple(s) in " + store.Graphs.Count + " Graph(s) to export..."; writer.Save(store, new StreamWriter(this._file)); this.Information = "Exported " + store.Graphs.Sum(x => x.Triples.Count) + " Triple(s) in " + store.Graphs.Count + " Graph(s)"; } else { if (File.Exists(this._file)) { File.Delete(this._file); } //For non-TriX formats assume it is safe to append one Graph at a time to the file int graphCount = 0, tripleCount = 0; // Make sure we always include the default graph in the export if (!graphUris.Contains(null)) { graphUris.Add(null); } // Write each graph out foreach (Uri u in graphUris) { using (FileStream stream = new FileStream(this._file, FileMode.Append)) { if (writer is IFormatterBasedWriter) { //Stream via a WriteThroughHandler this.Information = "Stream Exporting Graph " + (u != null ? u.AbsoluteUri : "Default"); IRdfHandler handler = new WriteThroughHandler(((IFormatterBasedWriter)writer).TripleFormatterType, new StreamWriter(stream), true); if (u != null) { handler = new GraphUriRewriteHandler(handler, u); } ExportProgressHandler progHandler = new ExportProgressHandler(handler, this, tripleCount); this._manager.LoadGraph(progHandler, u); graphCount++; tripleCount = progHandler.TripleCount; this.Information = "Finished Stream Exporting Graph " + (u != null ? u.AbsoluteUri : "Default") + ", exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s) so far..."; } else { //Load Graph into memory Graph g = new Graph(); g.BaseUri = u; this.Information = "Loading Graph " + (u != null ? u.AbsoluteUri : "Default"); this._manager.LoadGraph(g, u); g.BaseUri = u; if (this.HasBeenCancelled) { stream.Close(); this.Information = "Export Cancelled, exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s)"; return(new TaskResult(true)); } graphCount++; tripleCount += g.Triples.Count; //Save it store.Add(g); writer.Save(store, new StreamWriter(stream, def.Encoding)); store.Remove(u); this.Information = "Exporting Data graph by graph, exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s) so far..."; } //Check for cancellation if (this.HasBeenCancelled) { stream.Close(); this.Information = "Export Cancelled, exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s)"; return(new TaskResult(true)); } } } this.Information = "Exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s)"; } return(new TaskResult(true)); }
/// <summary> /// Runs the task /// </summary> /// <returns></returns> protected override TaskResult RunTaskInternal() { if (this.Target.IsReadOnly) { throw new RdfStorageException("Cannot Copy/Move a Graph when the Target is a read-only Store!"); } switch (this.Name) { case "Rename": case "Move": //Move/Rename a Graph if (ReferenceEquals(this.Source, this.Target) && this.Source.StorageProvider is IUpdateableStorage) { //If the Source and Target are identical and it supports SPARQL Update natively then we'll just issue a MOVE command this.Information = "Issuing a MOVE command to rename Graph '" + this._sourceUri.ToSafeString() + "' to '" + this._targetUri.ToSafeString() + "'"; SparqlParameterizedString update = new SparqlParameterizedString { CommandText = "MOVE" }; if (this._sourceUri == null) { update.CommandText += " DEFAULT TO"; } else { update.CommandText += " GRAPH @source TO"; update.SetUri("source", this._sourceUri); } if (this._targetUri == null) { update.CommandText += " DEFAULT"; } else { update.CommandText += " GRAPH @target"; update.SetUri("target", this._targetUri); } ((IUpdateableStorage)this.Source.StorageProvider).Update(update.ToString()); this.Information = "MOVE command completed OK, Graph renamed to '" + this._targetUri.ToSafeString() + "'"; } else { //Otherwise do a load of the source graph writing through to the target graph IRdfHandler handler; IGraph g = null; if (this.Target.StorageProvider.UpdateSupported) { //If Target supports update then we'll use a WriteToStoreHandler combined with a GraphUriRewriteHandler handler = new WriteToStoreHandler(this.Target.StorageProvider, this._targetUri); handler = new GraphUriRewriteHandler(handler, this._targetUri); } else { //Otherwise we'll use a GraphHandler and do a save at the end g = new Graph(); handler = new GraphHandler(g); } handler = new CopyMoveProgressHandler(handler, this, "Moving", this.Target.StorageProvider.UpdateSupported); this._canceller = new CancellableHandler(handler); if (this.HasBeenCancelled) { this._canceller.Cancel(); } //Now start reading out the data this.Information = "Copying data from Graph '" + this._sourceUri.ToSafeString() + "' to '" + this._targetUri.ToSafeString() + "'"; this.Source.StorageProvider.LoadGraph(this._canceller, this._sourceUri); //If we weren't moving the data directly need to save the resulting graph now if (g != null) { this.Information = "Saving copied data to Target Store..."; this.Target.StorageProvider.SaveGraph(g); } //And finally since we've done a copy (not a move) so far we need to delete the original graph //to effect a rename if (this.Source.StorageProvider.DeleteSupported) { this.Information = "Removing source graph to complete the move operation"; this.Source.StorageProvider.DeleteGraph(this._sourceUri); this.Information = "Move completed OK, Graph moved to '" + this._targetUri.ToSafeString() + "'" + (ReferenceEquals(this.Source, this.Target) ? String.Empty : " on " + this.Target); } else { this.Information = "Copy completed OK, Graph copied to '" + this._targetUri.ToSafeString() + "'" + (ReferenceEquals(this.Source, this.Target) ? String.Empty : " on " + this.Target) + ". Please note that as the Source Triple Store does not support deleting Graphs so the Graph remains present in the Source Store"; } } break; case "Copy": if (ReferenceEquals(this.Source, this.Target) && this.Source.StorageProvider is IUpdateableStorage) { //If the Source and Target are identical and it supports SPARQL Update natively then we'll just issue a COPY command this.Information = "Issuing a COPY command to copy Graph '" + this._sourceUri.ToSafeString() + "' to '" + this._targetUri.ToSafeString() + "'"; SparqlParameterizedString update = new SparqlParameterizedString(); update.CommandText = "COPY"; if (this._sourceUri == null) { update.CommandText += " DEFAULT TO"; } else { update.CommandText += " GRAPH @source TO"; update.SetUri("source", this._sourceUri); } if (this._targetUri == null) { update.CommandText += " DEFAULT"; } else { update.CommandText += " GRAPH @target"; update.SetUri("target", this._targetUri); } ((IUpdateableStorage)this.Source.StorageProvider).Update(update.ToString()); this.Information = "COPY command completed OK, Graph copied to '" + this._targetUri.ToSafeString() + "'"; } else { //Otherwise do a load of the source graph writing through to the target graph IRdfHandler handler; IGraph g = null; if (this.Target.StorageProvider.UpdateSupported) { //If Target supports update then we'll use a WriteToStoreHandler combined with a GraphUriRewriteHandler handler = new WriteToStoreHandler(this.Target.StorageProvider, this._targetUri); handler = new GraphUriRewriteHandler(handler, this._targetUri); } else { //Otherwise we'll use a GraphHandler and do a save at the end g = new Graph(); handler = new GraphHandler(g); } handler = new CopyMoveProgressHandler(handler, this, "Copying", this.Target.StorageProvider.UpdateSupported); this._canceller = new CancellableHandler(handler); if (this.HasBeenCancelled) { this._canceller.Cancel(); } //Now start reading out the data this.Information = "Copying data from Graph '" + this._sourceUri.ToSafeString() + "' to '" + this._targetUri.ToSafeString() + "'"; this.Source.StorageProvider.LoadGraph(this._canceller, this._sourceUri); //If we weren't moving the data directly need to save the resulting graph now if (g != null) { this.Information = "Saving copied data to Store..."; this.Target.StorageProvider.SaveGraph(g); } this.Information = "Copy completed OK, Graph copied to '" + this._targetUri.ToSafeString() + "'" + (ReferenceEquals(this.Source, this.Target) ? String.Empty : " on " + this.Target.ToString()); } break; } return(new TaskResult(true)); }