Skip to content

Commit d8648b7

Browse files
committed
Merge pull request #50 from tpluscode/delete-commit-bug
delete first, check for orphans later
2 parents acdf104 + b728a1f commit d8648b7

File tree

2 files changed

+68
-17
lines changed

2 files changed

+68
-17
lines changed

RomanticWeb/EntityStore.cs

+23-17
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,7 @@ public void ReplacePredicateValues(EntityId entityId, Node propertyUri, Func<IEn
9999

100100
public void Delete(EntityId entityId, DeleteBehaviour deleteBehaviour = DeleteBehaviour.Default)
101101
{
102-
var deletes = from entityQuad in _entityQuads[Node.FromEntityId(entityId)].ToList()
103-
from removedQuad in RemoveTriple(entityQuad)
104-
select removedQuad;
105-
106-
if (entityId is BlankId)
107-
{
108-
deletes = deletes.Union(_entityQuads.RemoveWhereObject(Node.FromEntityId(entityId)));
109-
}
102+
IEnumerable<EntityQuad> deletes = DeleteQuads(entityId);
110103

111104
var deletesGrouped = (from removedQuad in deletes
112105
group removedQuad by removedQuad.Graph into g
@@ -133,15 +126,6 @@ group removedQuad by removedQuad.Graph into g
133126
_changesTracker.Add(new RemoveReferences(entityId));
134127
}
135128
}
136-
137-
var orphanedBlankEntities = from removed in deletesGrouped
138-
from quad in removed
139-
where quad.Object.IsBlank && !IsReferenced(quad.Object)
140-
select quad;
141-
foreach (var quad in orphanedBlankEntities)
142-
{
143-
Delete(quad.Object.ToEntityId(), deleteBehaviour);
144-
}
145129
}
146130

147131
public void ResetState()
@@ -178,6 +162,28 @@ public void Dispose()
178162
_disposed = true;
179163
}
180164

165+
private IEnumerable<EntityQuad> DeleteQuads(EntityId entityId)
166+
{
167+
var deletes = (from entityQuad in _entityQuads[Node.FromEntityId(entityId)].ToList()
168+
from removedQuad in RemoveTriple(entityQuad)
169+
select removedQuad).ToList();
170+
171+
if (entityId is BlankId)
172+
{
173+
deletes.AddRange(_entityQuads.RemoveWhereObject(Node.FromEntityId(entityId)));
174+
}
175+
176+
var orphanedBlankEntities = (from quad in deletes
177+
where quad.Object.IsBlank && !IsReferenced(quad.Object)
178+
select quad).ToArray();
179+
foreach (var quad in orphanedBlankEntities)
180+
{
181+
deletes.AddRange(DeleteQuads(quad.Object.ToEntityId()));
182+
}
183+
184+
return deletes;
185+
}
186+
181187
private DatasetChange CreateChangeForUpdate(EntityId entityId, EntityId graphUri, EntityQuad[] removedQuads, EntityQuad[] addedQuads)
182188
{
183189
var update = new GraphUpdate(entityId, graphUri, removedQuads, addedQuads);

Tests/RomanticWeb.Tests/IntegrationTests/WritingTestsBase.cs

+45
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
using FluentAssertions;
44
using NUnit.Framework;
55
using RomanticWeb.Entities;
6+
using RomanticWeb.TestEntities;
67
using RomanticWeb.TestEntities.Foaf;
78
using RomanticWeb.Tests.Helpers;
89
using RomanticWeb.Tests.Stubs;
910
using RomanticWeb.Vocabularies;
1011
using VDS.RDF;
1112
using VDS.RDF.Query.Builder;
13+
using IPerson = RomanticWeb.TestEntities.Foaf.IPerson;
1214

1315
namespace RomanticWeb.Tests.IntegrationTests
1416
{
@@ -353,6 +355,49 @@ public void Should_allow_modifying_retrieved_nodes_where_some_are_blank()
353355
eb => eb.Str(eb.Variable("street")) == "Litzmannstadt");
354356
}
355357

358+
[Test]
359+
public void Shoud_not_commit_volatile_deleted_entity()
360+
{
361+
// given
362+
var entityId = new Uri("http://magi/people/Tomasz");
363+
var tempId = new Uri("http://magi/people/TomaszTwo");
364+
365+
// when
366+
var person = EntityContext.Create<IPerson>(entityId);
367+
var tempPerson = EntityContext.Create<IPerson>(tempId);
368+
tempPerson.Name = "Tomasz";
369+
person.Name = person.Name;
370+
EntityContext.Delete(tempId);
371+
EntityContext.Commit();
372+
373+
// then
374+
Store.Should().NotMatchAsk(tpb => tpb.Subject(tempId).Predicate("p").Object("o"));
375+
Store.Should().NotMatchAsk(tpb => tpb.Subject("s").Predicate("p").Object(tempId));
376+
}
377+
378+
[Test]
379+
public void Shoud_not_commit_volatile_deleted_entity_with_list()
380+
{
381+
// given
382+
var tempId = new Uri("temp://magi/people/Tomasz");
383+
var saved = EntityContext.Create<IEntityWithCollections>(new Uri("http://magi/people/Tomasz"));
384+
var temp = EntityContext.Create<IEntityWithCollections>(tempId);
385+
temp.DefaultListMapping.Add("test 1");
386+
temp.DefaultListMapping.Add("test 2");
387+
foreach (var val in temp.DefaultListMapping)
388+
{
389+
saved.DefaultListMapping.Add(val);
390+
}
391+
392+
// when
393+
EntityContext.Delete(tempId);
394+
EntityContext.Commit();
395+
396+
// then
397+
Store.Should().NotMatchAsk(tpb => tpb.Subject(tempId).Predicate("p").Object("o"));
398+
Store.Should().NotMatchAsk(tpb => tpb.Subject("s").Predicate("p").Object(tempId));
399+
}
400+
356401
protected override void ChildSetup()
357402
{
358403
Factory.WithNamedGraphSelector(new TestGraphSelector());

0 commit comments

Comments
 (0)