11using System ;
2+ using System . Collections . Generic ;
23using System . Runtime . Serialization ;
34using System . IO ;
45using System . Text ;
56using OrigoDB . Core ;
6- using OrigoDB . Core . Journaling ;
77using ProtoBuf ;
88using ProtoBuf . Meta ;
99using OrigoDB . Core . Utilities ;
@@ -38,15 +38,16 @@ public class ProtoBufFormatter : IFormatter
3838 /// </summary>
3939 public SerializationBinder Binder
4040 {
41- get ; set ;
41+ get ;
42+ set ;
4243 }
4344
4445 /// <summary>
4546 /// The associated streaming context.
4647 /// </summary>
4748 public StreamingContext Context
4849 {
49- get ;
50+ get ;
5051 set ;
5152 }
5253
@@ -70,18 +71,6 @@ public ProtoBufFormatter(RuntimeTypeModel typeModel = null, bool includeTypeName
7071 Context = new StreamingContext ( StreamingContextStates . Persistence ) ;
7172 }
7273
73- /// <summary>
74- /// Dynamically configure the origodb types written to the journal
75- /// </summary>
76- private void RegisterJournalTypes ( )
77- {
78- //TODO: incomplete
79- var je = _typeModel . Add ( typeof ( JournalEntry ) , false ) ;
80- je . AddField ( 1 , "Id" ) ;
81- je . AddField ( 2 , "Created" ) ;
82- je . AddSubType ( 3 , typeof ( JournalEntry < RollbackMarker > ) ) ;
83- je . AddSubType ( 4 , typeof ( JournalEntry < Command > ) ) . Add ( 1 , "Item" ) ;
84- }
8574
8675 /// <summary>
8776 /// Deserializes the data on the provided stream and
@@ -112,7 +101,7 @@ public void Serialize(Stream stream, object graph)
112101 Ensure . NotNull ( stream , "stream" ) ;
113102 Ensure . NotNull ( graph , "graph" ) ;
114103
115- if ( IncludeTypeName )
104+ if ( IncludeTypeName )
116105 new BinaryWriter ( stream , Encoding . UTF8 )
117106 . Write ( graph . GetType ( ) . AssemblyQualifiedName ) ;
118107
@@ -139,41 +128,37 @@ protected virtual Type GetType(Stream stream)
139128 }
140129
141130 /// <summary>
142- /// Modify the given configuration to use ProtoBuf formatting
131+ /// Modify the given configuration to use ProtoBuf formatting to clone results
143132 /// </summary>
144133 /// <param name="config"></param>
145- /// <param name="usage">the usage to configure</param>
146134 /// <param name="typeModel">An optional typemodel</param>
147- public static void Configure ( EngineConfiguration config , FormatterUsage usage , RuntimeTypeModel typeModel = null )
135+ public static void ConfigureResultCloning ( EngineConfiguration config , RuntimeTypeModel typeModel = null )
148136 {
149- switch ( usage )
150- {
151- case FormatterUsage . Default :
152- throw new NotSupportedException ( "Call with a specific usage, not FormatterUsage.Default" ) ;
153- case FormatterUsage . Snapshot :
154- throw new NotSupportedException ( "Can't configure for snapshots without a type parameter, call ConfigureSnapshots<T> passing the type of the model instead" ) ;
155- case FormatterUsage . Journal :
156- config . SetFormatterFactory ( ( cfg , fu ) => new ProtoBufFormatter < JournalEntry > ( typeModel : typeModel , useLengthPrefix : true ) , usage ) ;
157- break ;
158- case FormatterUsage . Results :
159- config . SetFormatterFactory ( ( cfg , fu ) => new ProtoBufFormatter ( typeModel , true , true ) , usage ) ;
160- break ;
161- case FormatterUsage . Messages :
162- throw new NotSupportedException ( "Not supported in this version of the module" ) ;
163- default :
164- throw new ArgumentOutOfRangeException ( "usage" ) ;
165- }
137+ config . SetFormatterFactory ( ( cfg , fu ) => new ProtoBufFormatter ( typeModel , true , true ) , FormatterUsage . Results ) ;
166138 }
167139
168140 /// <summary>
169- /// Modify the given configuration to use ProtoBuf formatting for snapshots.
141+ /// Modify the given configuration to use ProtoBuf formatting for snapshots of type T .
170142 /// </summary>
171143 /// <typeparam name="T">The concrete type of the model</typeparam>
172- /// <param name="config">the configuration to modify</param>
173- /// <param name="typeModel">An optional runtime type configuration</param>
174- public static void ConfigureSnapshots < T > ( EngineConfiguration config , RuntimeTypeModel typeModel )
144+ public static void ConfigureSnapshots < T > ( EngineConfiguration config , RuntimeTypeModel typeModel = null ) where T : Model
175145 {
176- config . SetFormatterFactory ( ( cfg , fu ) => new ProtoBufFormatter < T > ( typeModel : typeModel ) , FormatterUsage . Snapshot ) ;
146+ config . SetFormatterFactory ( ( cfg , fu ) => new ProtoBufFormatter < T > ( typeModel ) , FormatterUsage . Snapshot ) ;
147+ }
148+
149+ /// <summary>
150+ /// Modify the given EngineConfiguration to use ProtoBuf for journaling. Pass unique ints for each type of command.
151+ /// The id's must be maintained across versions of your assembly.
152+ /// </summary>
153+ public static void ConfigureJournaling ( EngineConfiguration config , IDictionary < Type , int > commandTypeTags , RuntimeTypeModel typeModel = null )
154+ {
155+ config . SetFormatterFactory ( ( cfg , fu ) =>
156+ {
157+ var formatter = new ProtoBufFormatter < JournalEntry > ( typeModel , includeTypeName : false , useLengthPrefix : true ) ;
158+ typeModel . RegisterCommandSubTypes ( commandTypeTags ) ;
159+ return formatter ;
160+ } ,
161+ FormatterUsage . Journal ) ;
177162 }
178163 }
179164
@@ -186,11 +171,9 @@ public sealed class ProtoBufFormatter<T> : ProtoBufFormatter
186171 /// <summary>
187172 /// Create a typed formatter. Type name won't be prepended to the stream because the type is known.
188173 /// </summary>
189- /// <param name="typeModel"></param>
190- /// <param name="useLengthPrefix"></param>
191- public ProtoBufFormatter ( RuntimeTypeModel typeModel = null , bool useLengthPrefix = false )
192- : base ( includeTypeName : false , typeModel : typeModel , useLengthPrefix : useLengthPrefix )
193- { }
174+ public ProtoBufFormatter ( RuntimeTypeModel typeModel = null , bool useLengthPrefix = false , bool includeTypeName = false )
175+ : base ( includeTypeName : includeTypeName , typeModel : typeModel , useLengthPrefix : useLengthPrefix )
176+ { }
194177
195178 /// <summary>
196179 /// Derive the type from the generic type parameter as opposed to reading the type name from the stream
@@ -199,7 +182,7 @@ public ProtoBufFormatter(RuntimeTypeModel typeModel = null, bool useLengthPrefix
199182 /// <returns></returns>
200183 protected override Type GetType ( Stream stream )
201184 {
202- return typeof ( T ) ;
185+ return typeof ( T ) ;
203186 }
204187 }
205188}
0 commit comments