Skip to content

Commit

Permalink
Refactored Storage API
Browse files Browse the repository at this point in the history
Split IStore into ISnapshotStore and ICommandStore allowing easier
composition and added Model.Revision property.
  • Loading branch information
rofr committed Jun 28, 2014
1 parent d9c7748 commit 6ad8cfe
Show file tree
Hide file tree
Showing 45 changed files with 1,045 additions and 957 deletions.
104 changes: 0 additions & 104 deletions src/OrigoDB.Core.Test/ByteCountingNullStreamTest.cs

This file was deleted.

25 changes: 9 additions & 16 deletions src/OrigoDB.Core.Test/EngineConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,13 @@ public void PacketingFormatterIsReturnedWhenPaketOptionsArePresent()
Assert.IsInstanceOfType(actual, typeof(PacketingFormatter));
}

[TestMethod()]
public void InjectingStorageSetsModeToCustom()
{
var config = new EngineConfiguration();
config.SetStoreFactory((c) => null);
Assert.AreEqual(Stores.Custom, config.StoreType);
}

[TestMethod()]
public void FileStorageIsDefault()
{
var config = new EngineConfiguration().WithRandomLocation();
var storage = config.CreateStore();
Assert.IsTrue(storage is FileStore);
var storage = config.CreateCommandStore();
Assert.IsTrue(storage is FileCommandStore);
Directory.Delete(config.Location.OfJournal, true);
}

Expand All @@ -81,9 +74,9 @@ public void InjectedStorageIsResolved()

var config = new EngineConfiguration()
.WithRandomLocation();
var expected = new FileStore(config);
config.SetStoreFactory((c) => expected);
var actual = config.CreateStore();
var expected = new FileCommandStore(config);
config.SetCommandStoreFactory((c) => expected);
var actual = config.CreateCommandStore();
Assert.AreSame(expected, actual);
Directory.Delete(config.Location.OfJournal, true);
}
Expand Down Expand Up @@ -129,8 +122,8 @@ public void AsyncJournalingYieldsAsyncWriter()
{
var config = new EngineConfiguration(Guid.NewGuid().ToString());
config.AsyncronousJournaling = true;
config.SetStoreFactory(c => new InMemoryStore(c));
var store = config.CreateStore();
config.SetCommandStoreFactory(c => new InMemoryCommandStore(c));
var store = config.CreateCommandStore();
var writer = store.CreateJournalWriter(1);
Assert.IsTrue(writer is AsynchronousJournalWriter);
}
Expand All @@ -140,8 +133,8 @@ public void SyncJournalingYieldsSyncWriter()
{
var config = new EngineConfiguration(Guid.NewGuid().ToString());
config.AsyncronousJournaling = false;
config.SetStoreFactory(c => new InMemoryStore(c));
var store = config.CreateStore();
config.SetCommandStoreFactory(c => new InMemoryCommandStore(c));
var store = config.CreateCommandStore();
var writer = store.CreateJournalWriter(1);
Assert.IsTrue(writer is StreamJournalWriter);
}
Expand Down
13 changes: 5 additions & 8 deletions src/OrigoDB.Core.Test/EngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void JournalEntriesAreSequentiallyNumberedFromOne()
ExecuteCommands(engine,1000);

engine.Close();
var store = config.CreateStore();
var store = config.CreateCommandStore();
AssertJournalEntriesAreSequential(store);
}

Expand All @@ -160,15 +160,15 @@ public void JournalEntryIdSequenceIsContinuedAfterRestore()
db = engine.GetProxy();
db.AddCustomer("Bart");
engine.Close();
AssertJournalEntriesAreSequential(config.CreateStore());
AssertJournalEntriesAreSequential(config.CreateCommandStore());

}

private void AssertJournalEntriesAreSequential(IStore storage)
private void AssertJournalEntriesAreSequential(ICommandStore storage)
{
ulong expected = 1;
Console.WriteLine("JournalEntry Ids:");
foreach (var journalEntry in storage.GetJournalEntries())
foreach (var journalEntry in storage.CommandEntries())
{
Console.WriteLine(journalEntry.Id);
Assert.AreEqual(expected, journalEntry.Id);
Expand All @@ -189,11 +189,8 @@ public void EntriesAreSequentiallyNumberedAfterReload()
ExecuteCommands(engine,60);
engine.Close();

var store = config.CreateStore();


var store = config.CreateCommandStore();
AssertJournalEntriesAreSequential(store);
Assert.AreEqual(120, store.GetJournalEntries().Count());
}

private void ExecuteCommands(Engine engine, int count)
Expand Down
10 changes: 4 additions & 6 deletions src/OrigoDB.Core.Test/FileStoreTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.IO;
using System.Linq;
using OrigoDB.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace OrigoDB.Core.Test
{
Expand All @@ -19,7 +17,7 @@ public class FileStoreTest


private TestContext testContextInstance;
static IStore _store;
static ICommandStore _store;
EngineConfiguration _config;
static string _path = Guid.NewGuid().ToString();

Expand Down Expand Up @@ -65,8 +63,8 @@ public void MyTestInitialize()
_config = EngineConfiguration.Create();
_config.Location.OfJournal = _path;
_config.MaxEntriesPerJournalSegment = 10;
_store = new FileStore(_config);
_store.Init();
_store = new FileCommandStore(_config);
_store.Initialize();

var writer = _store.CreateJournalWriter(0);
for (ulong i = 0; i < 30; i++)
Expand All @@ -91,7 +89,7 @@ public void JournalReadThrowsIfSequenceStartIsMissing()
{
var files = Directory.GetFiles(_path).OrderBy(f => f).ToArray();
File.Delete(files[0]);
_store.Init(); // Reload store to update journalfile list.
_store.Initialize(); // Reload store to update journalfile list.
_store.GetJournalEntriesFrom(1).Count();
Assert.Fail();
}
Expand Down
1 change: 0 additions & 1 deletion src/OrigoDB.Core.Test/OrigoDB.Core.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
<Compile Include="ByteCountingNullStreamTest.cs" />
<Compile Include="ExtensionsTest.cs" />
<Compile Include="CachingLinqCompilerTest.cs" />
<Compile Include="Customer.cs" />
Expand Down
68 changes: 68 additions & 0 deletions src/OrigoDB.Core.UnitTests/ByteCountingStreamTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using NUnit.Framework;
using OrigoDB.Core.Storage;
using System;

namespace OrigoDB.Core.Test
{

[TestFixture]
public class ByteCountingStreamTest
{

[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Write_throws_when_buffer_is_null()
{
var target = new ByteCountingStream();
target.Write(null, 0, 1);
}

[Test]
[ExpectedException(typeof(ArgumentException))]
public void Write_throws_when_buffer_offset_equals_length()
{
var target = new ByteCountingStream();
var buffer = new byte[100];
target.Write(buffer, 100, 1);
}

[Test]
[ExpectedException(typeof(ArgumentException))]
public void Write_throws_when_buffer_offset_and_count_exceed_length()
{
var target = new ByteCountingStream();
var buffer = new byte[100];
target.Write(buffer, 50, 51);
}

[Test]
public void Write_accepts_last_byte_of_buffer()
{
var target = new ByteCountingStream();
var buffer = new byte[100];
target.Write(buffer, 99, 1);
Assert.AreEqual(1, target.Length);
}

[Test]
public void ByteCountingStream_count_tests()
{
var data = Enumerable.Range(1, 1000).ToList();
var formatter = new BinaryFormatter();
var memStream = new MemoryStream();
var target = new ByteCountingStream(memStream);
formatter.Serialize(target, data);
Assert.AreEqual(target.Length, memStream.Length);
memStream.Position = 0;
var clone = (List<int>) formatter.Deserialize(memStream);
CollectionAssert.AreEqual(data, clone);
}


}
}
15 changes: 8 additions & 7 deletions src/OrigoDB.Core.UnitTests/CommandJournalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using System.Collections.Generic;
using OrigoDB.Core.Journaling;
using NUnit.Framework;
using OrigoDB.Core;

using OrigoDB.Core;
using OrigoDB.Core.Storage;

namespace OrigoDB.Core.Test
{

Expand Down Expand Up @@ -33,13 +34,13 @@ public class CommandJournalTest
[TestCaseSource("_testCases")]
public void RolledBackCommandsAreSkipped(Tuple<List<JournalEntry>, string> testCase)
{
IStore target = new InMemoryStore(EngineConfiguration.Create().ForIsolatedTest());
ICommandStore target = new InMemoryCommandStore(EngineConfiguration.Create().ForIsolatedTest());

string failureMessage = testCase.Item2;
var testEntries = testCase.Item1;

//Act
var actualCommandEntries = StoreExtensions.CommittedCommandEntries(() => testEntries).ToArray();
var actualCommandEntries = CommandStore.CommittedCommandEntries(() => testEntries).ToArray();

ulong[] rolledBackIds = testEntries.OfType<JournalEntry<RollbackMarker>>().Select(e => e.Id).ToArray();
int expectedNumberOfCommandEntries = testEntries.Count - rolledBackIds.Length * 2;
Expand Down Expand Up @@ -78,9 +79,9 @@ public void RollbackMarkerIsWrittenOnRollback()
{
//Arrange
var config = EngineConfiguration.Create().ForIsolatedTest();
var store = new InMemoryStore(config);
store.Init();
var target = new JournalAppender(1, new StreamJournalWriter(store, config));
var store = new InMemoryCommandStore(config);
store.Initialize();
var target = new JournalAppender(1, new StreamJournalWriter(config, store.CreateJournalWriterStream));

var command = new ACommand();
command.Timestamp = DateTime.Now;
Expand Down
2 changes: 1 addition & 1 deletion src/OrigoDB.Core.UnitTests/CommandTimestampTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void Timestamp_preserved_on_restore()
engine.Execute(command);


var store = config.CreateStore();
var store = config.CreateCommandStore();
var entry = store.CommandEntries().Single();
Assert.AreEqual(entry.Created, entry.Item.Timestamp);
Assert.AreEqual(command.Timestamp, entry.Created);
Expand Down

0 comments on commit 6ad8cfe

Please sign in to comment.