forked from ASOS/SimpleEventStore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dave Thompson
committed
Oct 5, 2018
1 parent
9c9049f
commit e0e4763
Showing
10 changed files
with
217 additions
and
20 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
SimpleEventStore.AzureDocumentDb.Tests/AzureDocumentDbEventStoreDeletingStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using SimpleEventStore.Tests; | ||
|
||
namespace SimpleEventStore.AzureDocumentDb.Tests | ||
{ | ||
[TestFixture] | ||
public class AzureDocumentDbEventStoreDeletingStream : EventStoreDeletingStream | ||
{ | ||
protected override Task<IStorageEngine> CreateStorageEngine() | ||
{ | ||
return StorageEngineFactory.Create("DeletingStreamTests"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
SimpleEventStore.AzureDocumentDb/Resources/deleteStream.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
function deleteStream(streamId) { | ||
var context = getContext(); | ||
var collection = context.getCollection(); | ||
var collectionLink = collection.getSelfLink(); | ||
var response = getContext().getResponse(); | ||
|
||
// Based on https://raw.githubusercontent.com/Azure/azure-cosmosdb-js-server/master/samples/stored-procedures/bulkDelete.js | ||
var query = { | ||
query: "SELECT c._self FROM Commits c WHERE c.streamId = @streamId ORDER BY c.eventNumber ASC", | ||
parameters: [{ name: "@streamId", value: streamId }] | ||
}; | ||
|
||
var responseBody = { | ||
deleted: 0, | ||
continuation: true | ||
}; | ||
|
||
tryQueryAndDelete(); | ||
|
||
// Recursively runs the query w/ support for continuation tokens. | ||
// Calls tryDelete(documents) as soon as the query returns documents. | ||
function tryQueryAndDelete(continuation) { | ||
var requestOptions = { continuation: continuation }; | ||
|
||
var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, retrievedDocs, responseOptions) { | ||
if (err) throw err; | ||
|
||
if (retrievedDocs.length > 0) { | ||
// Begin deleting documents as soon as documents are returned form the query results. | ||
// tryDelete() resumes querying after deleting; no need to page through continuation tokens. | ||
// - this is to prioritize writes over reads given timeout constraints. | ||
tryDelete(retrievedDocs); | ||
} else if (responseOptions.continuation) { | ||
// Else if the query came back empty, but with a continuation token; repeat the query w/ the token. | ||
tryQueryAndDelete(responseOptions.continuation); | ||
} else { | ||
// Else if there are no more documents and no continuation token - we are finished deleting documents. | ||
responseBody.continuation = false; | ||
response.setBody(responseBody); | ||
} | ||
}); | ||
|
||
// If we hit execution bounds - return continuation: true. | ||
if (!isAccepted) { | ||
response.setBody(responseBody); | ||
} | ||
} | ||
|
||
// Recursively deletes documents passed in as an array argument. | ||
// Attempts to query for more on empty array. | ||
function tryDelete(documents) { | ||
if (documents.length > 0) { | ||
// Delete the first document in the array. | ||
var isAccepted = collection.deleteDocument(documents[0]._self, {}, function (err, responseOptions) { | ||
if (err) throw err; | ||
|
||
responseBody.deleted++; | ||
documents.shift(); | ||
|
||
// Delete the next document in the array. | ||
tryDelete(documents); | ||
}); | ||
|
||
// If we hit execution bounds - return continuation: true. | ||
if (!isAccepted) { | ||
response.setBody(responseBody); | ||
} | ||
} else { | ||
// If the document array is empty, query for more documents. | ||
tryQueryAndDelete(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using SimpleEventStore.Tests.Events; | ||
|
||
namespace SimpleEventStore.Tests | ||
{ | ||
[TestFixture] | ||
public abstract class EventStoreDeletingStream : EventStoreTestBase | ||
{ | ||
[Test] | ||
public async Task when_deleting_stream_all_events_in_stream_are_deleted() | ||
{ | ||
var streamId = Guid.NewGuid().ToString(); | ||
var subject = await GetEventStore(); | ||
var @event = new EventData(Guid.NewGuid(), new OrderCreated(streamId)); | ||
|
||
await subject.AppendToStream(streamId, 0, @event); | ||
|
||
await subject.DeleteStream(streamId); | ||
|
||
var stream = await subject.ReadStreamForwards(streamId); | ||
Assert.That(stream.Count, Is.EqualTo(0)); | ||
} | ||
|
||
[Test] | ||
public async Task when_deleting_stream_events_in_other_streams_are_preserved() | ||
{ | ||
var deleteStreamId = Guid.NewGuid().ToString(); | ||
var keepStreamId = Guid.NewGuid().ToString(); | ||
var subject = await GetEventStore(); | ||
|
||
await subject.AppendToStream(keepStreamId, 0, new EventData(Guid.NewGuid(), new OrderCreated(keepStreamId))); | ||
await subject.AppendToStream(deleteStreamId, 0, new EventData(Guid.NewGuid(), new OrderCreated(deleteStreamId))); | ||
|
||
await subject.DeleteStream(deleteStreamId); | ||
|
||
var stream = await subject.ReadStreamForwards(keepStreamId); | ||
Assert.That(stream.Count, Is.EqualTo(1)); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
SimpleEventStore.Tests/InMemory/InMemoryEventStoreDeletingStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using SimpleEventStore.InMemory; | ||
|
||
namespace SimpleEventStore.Tests.InMemory | ||
{ | ||
[TestFixture] | ||
public class InMemoryEventStoreDeletingStream : EventStoreDeletingStream | ||
{ | ||
protected override Task<IStorageEngine> CreateStorageEngine() | ||
{ | ||
return Task.FromResult((IStorageEngine)new InMemoryStorageEngine()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters