Esempio n. 1
0
        static void TestWriteRead(SpanWriterAction writeAction, byte[] checkResult,
                                  SpanReaderAction readAction, SpanReaderAction?skipAction)
        {
            Span <byte> buf = stackalloc byte[1];
            var         sw  = new SpanWriter(buf);

            writeAction(ref sw);
            Assert.Equal(checkResult, sw.GetSpan().ToArray());
            SpanReader sr;

            if (checkResult.Length > 1)
            {
                sw = new SpanWriter(new Span <byte>());
                writeAction(ref sw);
                Assert.Equal(checkResult, sw.GetSpanAndReset().ToArray());
                writeAction(ref sw);
                Assert.Equal(checkResult, sw.GetSpan().ToArray());
            }
            sr = new SpanReader(checkResult);
            readAction(ref sr);
            Assert.True(sr.Eof);
            sw = new SpanWriter();
            writeAction(ref sw);
            writeAction(ref sw);
            Assert.Equal(checkResult.Concat(checkResult).ToArray(), sw.GetByteBufferAndReset().ToByteArray());
            sr = new SpanReader(checkResult.Concat(checkResult).ToArray());
            readAction(ref sr);
            readAction(ref sr);
            if (skipAction != null)
            {
                sr = new SpanReader(checkResult.Concat(checkResult).ToArray());
                skipAction(ref sr);
                readAction(ref sr);
                Assert.True(sr.Eof);
                sr = new SpanReader(checkResult.Concat(checkResult).ToArray());
                readAction(ref sr);
                skipAction(ref sr);
                Assert.True(sr.Eof);
            }
        }
Esempio n. 2
0
        void SerializeIntoBuffer(object?metadata, IReadOnlyList <object>?events, out int startOffset,
                                 out IDescriptorSerializerContext serializerContext, out BlockType blockType,
                                 out int lenWithoutEndPadding, out ByteBuffer block)
        {
            startOffset = (int)EndBufferLen + HeaderSize;
            var writer = new SpanWriter();

            writer.WriteBlock(_zeroes.AsSpan(0, startOffset));
            serializerContext = Mapping;
            if (metadata != null)
            {
                serializerContext = serializerContext.StoreNewDescriptors(metadata);
            }
            if (events != null)
            {
                foreach (var o in events)
                {
                    serializerContext = serializerContext.StoreNewDescriptors(o);
                }
                if (events.Count == 0)
                {
                    events = null;
                }
            }
            serializerContext.FinishNewDescriptors(ref writer);
            blockType = BlockType.FirstBlock;
            if (serializerContext.SomeTypeStored)
            {
                blockType |= BlockType.HasTypeDeclaration;
            }
            if (metadata != null)
            {
                serializerContext.StoreObject(ref writer, metadata);
                blockType |= BlockType.HasMetadata;
            }
            if (events != null)
            {
                if (events.Count == 1)
                {
                    serializerContext.StoreObject(ref writer, events[0]);
                    blockType |= BlockType.HasOneEvent;
                }
                else
                {
                    writer.WriteVUInt32((uint)events.Count);
                    foreach (var o in events)
                    {
                        serializerContext.StoreObject(ref writer, o);
                    }
                    blockType |= BlockType.HasMoreEvents;
                }
            }
            lenWithoutEndPadding = (int)writer.GetCurrentPosition();
            writer.WriteBlock(_zeroes.AsSpan(0, (int)(SectorSize - 1)));
            block = writer.GetByteBufferAndReset();
            if (CompressionStrategy.ShouldTryToCompress(lenWithoutEndPadding - startOffset))
            {
                var compressedBlock = new ReadOnlySpan <byte>(block.Buffer, startOffset, lenWithoutEndPadding - startOffset);
                if (CompressionStrategy.Compress(ref compressedBlock))
                {
                    blockType |= BlockType.Compressed;
                    compressedBlock.CopyTo(new Span <byte>(block.Buffer, startOffset, compressedBlock.Length));
                    lenWithoutEndPadding = startOffset + compressedBlock.Length;
                    new Span <byte>(block.Buffer, lenWithoutEndPadding, (int)SectorSize - 1).Clear();
                }
            }
        }