private void TestWriteToStoreDatasetsHandler(IStorageProvider manager) { NodeFactory factory = new NodeFactory(); INode a = factory.CreateUriNode(new Uri("http://example.org/a")); INode b = factory.CreateUriNode(new Uri("http://example.org/b")); INode c = factory.CreateUriNode(new Uri("http://example.org/c")); INode d = factory.CreateUriNode(new Uri("http://example.org/d")); Uri graphB = new Uri("http://example.org/graphs/b"); Uri graphD = new Uri("http://example.org/graphs/d"); //Try to ensure that the target Graphs do not exist if (manager.DeleteSupported) { manager.DeleteGraph(TestGraphUri); manager.DeleteGraph(graphB); manager.DeleteGraph(graphD); } else { Graph g = new Graph(); g.BaseUri = TestGraphUri; manager.SaveGraph(g); g.BaseUri = graphB; manager.SaveGraph(g); g.BaseUri = graphD; manager.SaveGraph(g); } //Do the parsing and thus the loading WriteToStoreHandler handler = new WriteToStoreHandler(manager, TestGraphUri); NQuadsParser parser = new NQuadsParser(); parser.Load(handler, new StreamReader("writetostore.nq")); //Load the expected Graphs Graph def = new Graph(); manager.LoadGraph(def, TestGraphUri); Graph gB = new Graph(); manager.LoadGraph(gB, graphB); Graph gD = new Graph(); manager.LoadGraph(gD, graphD); Assert.AreEqual(2, def.Triples.Count, "Should be two triples in the default Graph"); Assert.IsTrue(def.ContainsTriple(new Triple(a, a, a)), "Default Graph should have the a triple"); Assert.AreEqual(1, gB.Triples.Count, "Should be one triple in the b Graph"); Assert.IsTrue(gB.ContainsTriple(new Triple(b, b, b)), "b Graph should have the b triple"); Assert.IsTrue(def.ContainsTriple(new Triple(c, c, c)), "Default Graph should have the c triple"); Assert.AreEqual(1, gD.Triples.Count, "Should be one triple in the d Graph"); Assert.IsTrue(gD.ContainsTriple(new Triple(d, d, d)), "d Graph should have the d triple"); }
/// <summary> /// Processes a DELETE operation. /// </summary> /// <param name="context">HTTP Context.</param> /// <remarks> /// <para> /// <strong>Warning: </strong> If the underlying <see cref="IStorageProvider">IStorageProvider</see> is read-only then this operation returns a 403 Forbidden. /// </para> /// <para> /// The delete operation does not explicitly remove the Graph but simply replaces it with an empty Graph. /// </para> /// </remarks> public override void ProcessDelete(IHttpContext context) { // If the Manager is read-only then a 403 Forbidden will be returned if (_manager.IsReadOnly) { context.Response.StatusCode = (int)HttpStatusCode.Forbidden; return; } Uri graphUri = ResolveGraphUri(context); if (HasGraph(graphUri)) { if (_manager.DeleteSupported) { _manager.DeleteGraph(graphUri); } else { // Have to simulate deletion by replacing with an empty graph IGraph g = new Graph { BaseUri = graphUri, }; _manager.SaveGraph(g); } } else { // If no Graph MUST respond 404 context.Response.StatusCode = (int)HttpStatusCode.NotFound; } }
private void EnsureGraphDeleted(IStorageProvider manager, Uri graphUri) { if (manager.DeleteSupported) { manager.DeleteGraph(graphUri); } else { throw new SkipTestException("Unable to conduct this test as it requires ensuring a Graph is deleted from the underlying store which the IStorageProvider instance does not support"); } }
private void TestWriteToStoreHandler(IStorageProvider manager) { //First ensure that our test file exists EnsureTestData(); //Try to ensure that the target Graph does not exist if (manager.DeleteSupported) { manager.DeleteGraph(TestGraphUri); } else { Graph g = new Graph(); g.BaseUri = TestGraphUri; manager.SaveGraph(g); } Graph temp = new Graph(); try { manager.LoadGraph(temp, TestGraphUri); Assert.IsTrue(temp.IsEmpty, "Unable to ensure that Target Graph in Store is empty prior to running Test"); } catch { //An Error Loading the Graph is OK } WriteToStoreHandler handler = new WriteToStoreHandler(manager, TestGraphUri, 100); TurtleParser parser = new TurtleParser(); parser.Load(handler, "temp.ttl"); manager.LoadGraph(temp, TestGraphUri); Assert.IsFalse(temp.IsEmpty, "Graph should not be empty"); Graph orig = new Graph(); orig.LoadFromFile("temp.ttl"); Assert.AreEqual(orig, temp, "Graphs should be equal"); }
/// <summary> /// Deletes all data from the specified store /// </summary> /// <param name="storeName"></param> /// <remarks>This implementation of DeleteStore differs slightly from the basic BrightstarDB implementation. Rather than /// removing the store completely, this method simply clears all RDF data in the store. It can only be executed against /// DotNetRDF storage providers that support listing of graphs and graph deletion. For stores that either do not support /// these operaitons or which are marked as readonly, this method will raise a <see cref="NotSupportedException"/></remarks> public override void DeleteStore(string storeName) { IStorageProvider storageProvider = GetStorageProvider(storeName); if (storageProvider == null) { throw new BrightstarClientException(Strings.BrightstarServiceClient_StoreDoesNotExist); } if (storageProvider.IsReadOnly || !storageProvider.ListGraphsSupported || !storageProvider.DeleteSupported) { throw new NotSupportedException(Strings.DotNetRdf_StoreDoesNotSupportDelete); } foreach (var g in storageProvider.ListGraphs()) { storageProvider.DeleteGraph(g); } }
private void TestWriteToStoreHandlerWithBNodes(IStorageProvider manager) { String fragment = "@prefix : <http://example.org/>. :subj :has [ a :BNode ; :with \"value\" ] ."; //Try to ensure that the target Graph does not exist if (manager.DeleteSupported) { manager.DeleteGraph(TestBNodeUri); } else { Graph temp = new Graph(); temp.BaseUri = TestBNodeUri; manager.SaveGraph(temp); } //Then write to the store TurtleParser parser = new TurtleParser(); WriteToStoreHandler handler = new WriteToStoreHandler(manager, TestBNodeUri, 1); parser.Load(handler, new StringReader(fragment)); //Then load back the data and check it Graph g = new Graph(); manager.LoadGraph(g, TestBNodeUri); Assert.AreEqual(3, g.Triples.Count, "Should be 3 Triples"); List <IBlankNode> nodes = g.Nodes.BlankNodes().ToList(); for (int i = 0; i < nodes.Count; i++) { for (int j = 0; j < nodes.Count; j++) { if (i == j) { continue; } Assert.AreEqual(nodes[i], nodes[j], "All Blank Nodes should be the same"); } } }
private static void DeleteGraph(IStorageProvider storage, Uri graphUri) { storage.DeleteGraph(graphUri); }
private void EnsureGraphDeleted(IStorageProvider manager, Uri graphUri) { Skip.IfNot(manager.DeleteSupported, "Unable to conduct this test as it requires ensuring a Graph is deleted from the underlying store which the IStorageProvider instance does not support"); manager.DeleteGraph(graphUri); }
internal void Flush() { try { _persisting = true; _removedGraphs.Clear(); // Read-Only managers have no persistence if (_manager.IsReadOnly) { return; } // No actions means no persistence necessary if (_actions.Count == 0) { return; } if (_manager.UpdateSupported) { // Persist based on Triple level actions // First group Triple together based on Graph URI while (_actions.Count > 0) { TripleStorePersistenceAction action = _actions[0]; if (action.IsTripleAction) { Queue <TriplePersistenceAction> actions = new Queue <TriplePersistenceAction>(); Uri currUri = action.TripleAction.Triple.GraphUri; actions.Enqueue(_actions[0].TripleAction); _actions.RemoveAt(0); // Find all the Triple actions related to this Graph up to the next non-Triple action for (int i = 0; i < _actions.Count && _actions[i].IsTripleAction; i++) { if (EqualityHelper.AreUrisEqual(currUri, _actions[i].TripleAction.Triple.GraphUri)) { actions.Enqueue(_actions[i].TripleAction); _actions.RemoveAt(i); i--; } } // Split the Triple Actions for this Graph into batches of adds and deletes to ensure // accurate persistence of the actions bool toDelete = false; List <Triple> batch = new List <Triple>(); while (actions.Count > 0) { TriplePersistenceAction next = actions.Dequeue(); if (next.IsDelete != toDelete) { if (batch.Count > 0) { // Process a batch whenever we find a switch between additions and removals // This ensures that regardless of the logic in UpdateGraph() we force // additions and removals to happen in the order we care about if (toDelete) { _manager.UpdateGraph(currUri, null, batch); } else { _manager.UpdateGraph(currUri, batch, null); } batch.Clear(); } toDelete = next.IsDelete; } batch.Add(next.Triple); } // Ensure the final batch (if any) gets processed if (batch.Count > 0) { if (toDelete) { _manager.UpdateGraph(currUri, null, batch); } else { _manager.UpdateGraph(currUri, batch, null); } } } else { switch (action.GraphAction.Action) { case GraphPersistenceActionType.Added: // No need to do anything in-memory as will be in the graph collection // Call SaveGraph() with an empty graph to create the relevant graph // If Triples were added these will be persisted separately with // TriplePersistenceActions Graph g = new Graph(); g.BaseUri = action.GraphAction.Graph.BaseUri; _manager.SaveGraph(g); break; case GraphPersistenceActionType.Deleted: // No need to do anything in-memory as won't be in the graph collection // If DeleteGraph() is supported call it to delete the relevant graph if (_manager.DeleteSupported) { _manager.DeleteGraph(action.GraphAction.Graph.BaseUri); } break; } _actions.RemoveAt(0); } } } else { // Persist based on Graph level actions foreach (TripleStorePersistenceAction action in _actions) { if (action.IsGraphAction) { if (action.GraphAction.Action == GraphPersistenceActionType.Added) { _manager.SaveGraph(action.GraphAction.Graph); } else if (action.GraphAction.Action == GraphPersistenceActionType.Deleted && _manager.DeleteSupported) { // Can only delete graphs if deletion is supported _manager.DeleteGraph(action.GraphAction.Graph.BaseUri); } } } } } finally { _persisting = false; } }