public bool TryWrite <T>(T item, IAirlockSerializer <T> serializer, DateTimeOffset timestamp, IBufferPool bufferPool) { if (!bufferPool.TryAcquire(out var buffer)) { return(false); } var startingPosition = buffer.Position; var serializationSucceeded = false; try { serializationSucceeded = recordSerializer.TrySerialize(item, serializer, timestamp, buffer); } finally { if (serializationSucceeded) { buffer.WrittenRecords++; } else { buffer.Position = startingPosition; } bufferPool.Release(buffer); } return(serializationSucceeded); }
public void TestSetup() { timestamp = DateTimeOffset.UtcNow; item = Guid.NewGuid().ToString(); itemSerializer = Substitute.For <IAirlockSerializer <string> >(); buffer = Substitute.For <IBuffer>(); buffer.WrittenRecords.Returns(5); buffer.Position.Returns(10); bufferPool = Substitute.For <IBufferPool>(); bufferPool .TryAcquire(out _) .Returns( x => { x[0] = buffer; return(buffer != null); }); recordSerializer = Substitute.For <IRecordSerializer>(); recordSerializer .TrySerialize(item, itemSerializer, timestamp, buffer) .Returns(true); recordWriter = new RecordWriter(recordSerializer); }
public static void Register <T>(IAirlockSerializer <T> serializer) { if (serializer == null) { throw new ArgumentNullException(nameof(serializer)); } Interlocked.Exchange(ref Container <T> .Serializer, serializer); }
public void TestSetup() { manager = new MemoryManager(bufferSize * 2, bufferSize); buffer = new Buffer(new BinaryBufferWriter(bufferSize), manager); serializer = new RecordSerializer(maxRecordSize.Bytes(), new ConsoleLog()); timestamp = DateTimeOffset.Now; item = "Hello!"; itemSerializer = Substitute.For <IAirlockSerializer <string> >(); itemSerializer .WhenForAnyArgs(s => s.Serialize(null, null)) .Do(info => info.Arg <IAirlockSink>().Writer.Write(info.Arg <string>())); }
public bool TrySerialize <T>(T item, IAirlockSerializer <T> serializer, DateTimeOffset timestamp, IBuffer buffer) { try { var unixTimestamp = timestamp.ToUniversalTime().ToUnixTimeMilliseconds(); var writer = buffer.Writer; writer.Write(unixTimestamp); var payloadLengthPosition = writer.Position; writer.Write(0); var positionBeforeSerialization = writer.Position; serializer.Serialize(item, buffer); var positionAfterSerialization = writer.Position; var recordSize = positionAfterSerialization - positionBeforeSerialization; if (recordSize > maximumRecordSize.Bytes) { LogDiscardingLargeRecord(recordSize); return(false); } writer.Position = payloadLengthPosition; writer.Write(recordSize); writer.Position = positionAfterSerialization; return(true); } catch (InternalBufferOverflowException) { return(false); } }
public static bool TryGet <T>(out IAirlockSerializer <T> serializer) { return((serializer = Container <T> .Serializer) != null); }