Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
rofr committed May 2, 2014
2 parents 93d2968 + ab09f2b commit 9c1d1d6
Show file tree
Hide file tree
Showing 23 changed files with 202 additions and 260 deletions.
32 changes: 4 additions & 28 deletions src/OrigoDB.Core.Test/EngineConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,15 @@ public TestContext TestContext
}
}

[TestMethod()]
public void ObjectFormattingDefaultsToBinary()
{
var config = new EngineConfiguration();
Assert.AreEqual(ObjectFormatting.NetBinaryFormatter, config.ObjectFormatting);
}

[TestMethod()]
public void InjectingFormatterSetsModeToCustom()
{
var config = new EngineConfiguration();
config.SetFormatterFactory((c) => null);
Assert.AreEqual(ObjectFormatting.Custom, config.ObjectFormatting);
}

[TestMethod()]
public void InjectedFormatterIsResolved()
{
var config = new EngineConfiguration();
config.PacketOptions = null;
var expected = new BinaryFormatter();
config.SetFormatterFactory((c) => expected);
var actual = config.CreateFormatter();
config.SetFormatterFactory((c,f) => expected);
var actual = config.CreateFormatter(FormatterUsage.Default);
Assert.AreSame(expected, actual);
}

Expand All @@ -67,8 +53,8 @@ public void PacketingFormatterIsReturnedWhenPaketOptionsArePresent()
{
var config = new EngineConfiguration();
config.PacketOptions = PacketOptions.Checksum;
config.SetFormatterFactory((c) => new BinaryFormatter());
var actual = config.CreateFormatter();
config.SetFormatterFactory((c,f) => new BinaryFormatter());
var actual = config.CreateFormatter(FormatterUsage.Journal);
Assert.IsInstanceOfType(actual, typeof(PacketingFormatter));
}

Expand Down Expand Up @@ -128,16 +114,6 @@ public void InjectedSynchronizerIsResolved()
Assert.AreSame(expected,actual);
}

[TestMethod()]
public void InjectedSerializerIsResolved()
{
var config = new EngineConfiguration();
var expected = new Serializer(new BinaryFormatter());
config.SetSerializerFactory((c) => expected);
var actual = config.CreateSerializer();
Assert.AreSame(expected, actual);
}

[TestMethod()]
public void InjectedAuthorizerIsResolved()
{
Expand Down
8 changes: 0 additions & 8 deletions src/OrigoDB.Core.Test/EngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,6 @@ public void OnLoadIsCalledAfterRestore()
Assert.IsTrue(onLoadWasCalled);
}

[TestMethod]
public void CreateSerializerResolvesToDefault()
{
var config = new EngineConfiguration();
var serializer = config.CreateSerializer();
Assert.IsTrue(serializer is Serializer);
}

[TestMethod]
public void JournalEntriesAreSequentiallyNumberedFromOne()
{
Expand Down
2 changes: 1 addition & 1 deletion src/OrigoDB.Core.Test/GzipHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void CompressTest()
model.AddCustomer("Bart");
model.AddCustomer("Beavis");

byte[] expected = new Serializer(new BinaryFormatter()).Serialize(model);
byte[] expected = new BinaryFormatter().ToByteArray(model);
byte[] compressed = ByteArrayExtensions.Compress(expected);
Console.WriteLine("Bytes before: " + expected.Length + ", after: " + compressed.Length);
byte[] actual = ByteArrayExtensions.Decompress(compressed);
Expand Down
4 changes: 2 additions & 2 deletions src/OrigoDB.Core.Test/ProxyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public void CanCloneMarshalByRefModel()
{
var model = new TestModel();
model.AddCustomer("Zippy");
var serializer = new Serializer(new BinaryFormatter());
var clone = serializer.Clone(model);

var clone = new BinaryFormatter().Clone(model);
model.AddCustomer("asfafse");
Assert.IsTrue(clone.Customers.Count() == 1);
}
Expand Down
22 changes: 4 additions & 18 deletions src/OrigoDB.Core.Test/SerializerTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System.Runtime.Serialization.Formatters.Binary;
using OrigoDB.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Runtime.Serialization;
using System.IO;
using System.Collections.Generic;

namespace OrigoDB.Core.Test
{
Expand Down Expand Up @@ -68,25 +65,16 @@ public TestContext TestContext
#endregion


[TestMethod()]
[ExpectedException(typeof(ArgumentNullException))]
public void Serializer_throws_when_passed_a_null_argument()
{
new Serializer(null);
}



[TestMethod()]
public void SizeOf_reports_actual_size()
{
IFormatter formatter = new BinaryFormatter();
Serializer target = new Serializer(formatter);
IFormatter target = new BinaryFormatter();
var testModel = new TestModel();
testModel.AddCustomer("Homer");
testModel.AddCustomer("Bart");
long actual = target.SizeOf(testModel);
long expected = target.Serialize(testModel).Length;
long expected = target.ToByteArray(testModel).Length;
Console.WriteLine("SizeOf(TestModel with 2 customers) using BinaryFormatter = " + expected);
Assert.AreEqual(expected, actual);
}
Expand All @@ -95,10 +83,8 @@ public void SizeOf_reports_actual_size()
[ExpectedException(typeof(ArgumentNullException))]
public void SizeOf_throws_when_passed_null_graph()
{
IFormatter formatter = new BinaryFormatter();
Serializer target = new Serializer(formatter);
TestModel testModel = null;
long actual = target.SizeOf(testModel);
var target = new BinaryFormatter();
target.SizeOf(null);
}

}
Expand Down
22 changes: 13 additions & 9 deletions src/OrigoDB.Core.UnitTests/SchemaMigrationsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@ class GenericDummy<T,U>
[TestFixture]
public class SchemaMigrationTest
{

private BinaryFormatter _formatter;

[SetUp]
public void Setup()
{
Schema.Current.TypeSubstitutions.Clear();
_formatter = new BinaryFormatter();
_formatter.Binder = new CustomBinder(Schema.Current);
}



[Test]
public void CanRenameType()
{
Schema
.Substitute(typeof(MigrationTestTypeA).FullName)
.With<MigrationTestTypeB>();

var serializer = new Serializer(new BinaryFormatter());
var bytes = serializer.Serialize(new MigrationTestTypeA());
var actual = serializer.Deserialize<object>(bytes).GetType();
var bytes = _formatter.ToByteArray(new MigrationTestTypeA());
var actual = _formatter.FromByteArray<object>(bytes).GetType();
var expected = typeof(MigrationTestTypeB);
Assert.AreEqual(expected, actual);
}
Expand All @@ -52,9 +58,8 @@ public void CanRenameGenericTypeParameterSpecifically()
.Substitute(typeof(GenericDummy<MigrationTestTypeA, MigrationTestTypeA>).FullName)
.With<GenericDummy<MigrationTestTypeB, MigrationTestTypeB>>();

var serializer = new Serializer(new BinaryFormatter());
var bytes = serializer.Serialize(new GenericDummy<MigrationTestTypeA, MigrationTestTypeA>());
var actual = serializer.Deserialize<object>(bytes).GetType();
var bytes = _formatter.ToByteArray(new GenericDummy<MigrationTestTypeA, MigrationTestTypeA>());
var actual = _formatter.FromByteArray<object>(bytes).GetType();
var expected = typeof(GenericDummy<MigrationTestTypeB, MigrationTestTypeB>);
Assert.AreEqual(expected, actual);
}
Expand All @@ -66,9 +71,8 @@ public void CanRenameGenericTypeParameterType2()
.Substitute(typeof(MigrationTestTypeA).FullName)
.With<MigrationTestTypeB>();

var serializer = new Serializer(new BinaryFormatter());
var bytes = serializer.Serialize(new GenericDummy<MigrationTestTypeA, MigrationTestTypeA>());
var actual = serializer.Deserialize<object>(bytes).GetType();
var bytes = _formatter.ToByteArray(new GenericDummy<MigrationTestTypeA, MigrationTestTypeA>());
var actual = _formatter.FromByteArray<object>(bytes).GetType();
var expected = typeof(GenericDummy<MigrationTestTypeB, MigrationTestTypeB>);
Assert.AreEqual(expected, actual);
}
Expand Down
80 changes: 33 additions & 47 deletions src/OrigoDB.Core/Configuration/EngineConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace OrigoDB.Core
{

public class EngineConfiguration : ConfigurationBase
{
protected TeenyIoc _registry;
Expand All @@ -28,12 +27,20 @@ public EngineConfiguration ForImmutability()


/// <summary>
/// Write journal entries using a background thread
/// Append journal entries using a background thread
/// sacrificing durability under certain circumstances
/// for performance.
/// </summary>
public bool AsyncronousJournaling { get; set; }

/// <summary>
/// Set's the type of persistence used
/// </summary>
public PersistenceMode PersistenceMode { get; set; }

/// <summary>
/// Choose type of kernel
/// </summary>
public Kernels Kernel { get; set; }

/// <summary>
Expand Down Expand Up @@ -72,11 +79,6 @@ public EngineConfiguration ForImmutability()
/// </summary>
public SynchronizationMode Synchronization { get; set; }

/// <summary>
/// Serialization format, defaults to BinaryFormatter
/// </summary>
public ObjectFormatting ObjectFormatting { get; set; }

/// <summary>
/// Maximum number of journal entries per segment. Applies only to storage
/// providers which split up the journal in segments and ignored by others.
Expand Down Expand Up @@ -104,7 +106,6 @@ public EngineConfiguration(string targetLocation = null)
Kernel = Kernels.Optimistic;
LockTimeout = DefaultTimeout;
Synchronization = SynchronizationMode.ReadWrite;
ObjectFormatting = ObjectFormatting.NetBinaryFormatter;
AsyncronousJournaling = false;
MaxBytesPerJournalSegment = DefaultMaxBytesPerJournalSegment;
MaxEntriesPerJournalSegment = DefaultMaxCommandsPerJournalSegment;
Expand All @@ -115,15 +116,12 @@ public EngineConfiguration(string targetLocation = null)

_registry = new TeenyIoc();
Register<IAuthorizer<Type>>(c => new TypeBasedPermissionSet(Permission.Allowed));
Register<ISerializer>(c => new Serializer(CreateFormatter()));
Register<IFormatter>(c => new BinaryFormatter(), FormatterUsage.Default.ToString());
InitSynchronizers();
InitStoreTypes();
InitFormatters();
InitKernels();
}



private void InitKernels()
{
Register<Kernel>((cfg, args) => new OptimisticKernel(cfg, (Model) args["model"]), Kernels.Optimistic.ToString());
Expand All @@ -147,14 +145,6 @@ private void InitSynchronizers()

}

private void InitFormatters()
{
Register<IFormatter>(c => new BinaryFormatter(),
ObjectFormatting.NetBinaryFormatter.ToString());
Register(c => LoadFromConfig<IFormatter>(),
ObjectFormatting.Custom.ToString());
}


/// <summary>
/// Create a named registration for each StoreMode enumeration value
Expand All @@ -181,22 +171,24 @@ public static EngineConfiguration Create()
return bootloader.LoadFromConfigOrDefault(() => bootloader);
}

public virtual ISerializer CreateSerializer()
{
return _registry.Resolve<ISerializer>();
}

public virtual IFormatter CreateFormatter()
/// <summary>
/// Return an IFormatter by invoking the factory function associated
/// with the given FormatterUsage or FormatterUsage.Default if not registered.
/// </summary>
/// <param name="formatterUsage">The specific formatter</param>
/// <returns>An IFormatter instance provided by the </returns>
public virtual IFormatter CreateFormatter(FormatterUsage formatterUsage = FormatterUsage.Default)
{
string name = ObjectFormatting.ToString();
var formatter = _registry.Resolve<IFormatter>(name: name);
if (PacketOptions != null)
var formatter = _registry.CanResolve<IFormatter>(formatterUsage.ToString())
? _registry.Resolve<IFormatter>(formatterUsage.ToString())
: _registry.Resolve<IFormatter>(FormatterUsage.Default.ToString());

if (formatterUsage == FormatterUsage.Journal && PacketOptions != null)
{
formatter = new PacketingFormatter(formatter, PacketOptions.Value);
}
return formatter;


return formatter;
}

/// <summary>
Expand Down Expand Up @@ -225,33 +217,36 @@ public virtual IStore CreateStore()

protected void Register<T>(Func<EngineConfiguration, T> factory, string registrationName = "") where T : class
{
_registry.Register<T>(args => factory.Invoke(this), registrationName);
_registry.Register(args => factory.Invoke(this), registrationName);
}

protected void Register<T>(Func<EngineConfiguration, TeenyIoc.Args, T> factory,
string registrationName = "") where T : class
{
_registry.Register<T>(args => factory.Invoke(this, args), registrationName);
_registry.Register(args => factory.Invoke(this, args), registrationName);
}


public void SetSynchronizerFactory(Func<EngineConfiguration, ISynchronizer> factory)
{
Synchronization = SynchronizationMode.Custom;
string registrationName = Synchronization.ToString();
_registry.Register<ISynchronizer>(args => factory.Invoke(this), registrationName);
_registry.Register(args => factory.Invoke(this), registrationName);
}

public void SetAuthorizerFactory(Func<EngineConfiguration, IAuthorizer<Type>> factory)
{
Register(args => factory.Invoke(this));
}

public void SetFormatterFactory(Func<EngineConfiguration, IFormatter> factory)
/// <summary>
/// Inject a custom IFormatter factory.
/// </summary>
/// <param name="factory">Function that provides an IFormatter</param>
/// <param name="formatterUsage">The usage</param>
public void SetFormatterFactory(Func<EngineConfiguration, FormatterUsage, IFormatter> factory, FormatterUsage formatterUsage = FormatterUsage.Default)
{
ObjectFormatting = ObjectFormatting.Custom;
string registrationName = ObjectFormatting.ToString();
Register(args => factory.Invoke(this), registrationName);
Register(args => factory.Invoke(this, formatterUsage), formatterUsage.ToString());
}

/// <summary>
Expand All @@ -265,15 +260,6 @@ public void SetStoreFactory(Func<EngineConfiguration, IStore> factory)
Register(args => factory.Invoke(this), registrationName);
}

/// <summary>
/// Inject a custom serializer factory
/// </summary>
/// <param name="factory"></param>
public void SetSerializerFactory(Func<EngineConfiguration, ISerializer> factory)
{
Register(args => factory.Invoke(this));
}

/// <summary>
/// Rollover strategy is used by storage providers that split the journal into segments. The rollover strategy decides
/// when to create a new segment.
Expand Down
12 changes: 12 additions & 0 deletions src/OrigoDB.Core/Configuration/FormatterUsage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace OrigoDB.Core
{

public enum FormatterUsage
{
Default,
Snapshot,
Journal,
Results,
Messages
}
}

0 comments on commit 9c1d1d6

Please sign in to comment.