Beispiel #1
0
 private BufferedRaftLogEntry(IGrowableBuffer <byte> buffer, long term, DateTimeOffset timestamp, int?id, bool snapshot)
 {
     Term       = term;
     Timestamp  = timestamp;
     commandId  = id;
     content    = buffer;
     IsSnapshot = snapshot;
     InMemory   = true;
 }
Beispiel #2
0
        /// <summary>
        /// Constructs the string from the buffer.
        /// </summary>
        /// <param name="writer">The buffer of characters.</param>
        /// <returns>The string constructed from the buffer.</returns>
        public static string BuildString(this IGrowableBuffer <char> writer)
        {
            var length = writer.WrittenCount;

            if (length == 0L)
            {
                return(string.Empty);
            }

            return(string.Create(checked ((int)length), writer, InitializeStringFromWriter));
        }
Beispiel #3
0
        /// <summary>
        /// Returns the memory to write to that is at least the requested size.
        /// </summary>
        /// <param name="sizeHint">The minimum length of the returned memory.</param>
        /// <returns>The memory block of at least the size <paramref name="sizeHint"/>.</returns>
        /// <exception cref="OutOfMemoryException">The requested buffer size is not available.</exception>
        public Span <T> GetSpan(int sizeHint = 0)
        {
            if (sizeHint < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sizeHint));
            }

            Span <T> result;
            int?     newSize;

            if (extraBuffer.IsEmpty)
            {
                newSize = IGrowableBuffer <T> .GetBufferSize(sizeHint, initialBuffer.Length, position);

                // need to copy initial buffer
                if (newSize.HasValue)
                {
                    extraBuffer = allocator.Invoke(newSize.GetValueOrDefault(), false);
                    initialBuffer.CopyTo(extraBuffer.Memory.Span);
                    initialBuffer.Clear();
                    result = extraBuffer.Memory.Span;
                }
                else
                {
                    result = initialBuffer;
                }
            }
            else
            {
                newSize = IGrowableBuffer <T> .GetBufferSize(sizeHint, extraBuffer.Length, position);

                // no need to copy initial buffer
                if (newSize.HasValue)
                {
                    var newBuffer = allocator.Invoke(newSize.GetValueOrDefault(), false);
                    extraBuffer.Memory.CopyTo(newBuffer.Memory);
                    extraBuffer.Dispose();
                    extraBuffer = newBuffer;
                }

                result = extraBuffer.Memory.Span;
            }

            return(result.Slice(position));
        }
Beispiel #4
0
        private static unsafe void ReadWriteTest(IGrowableBuffer <byte> writer)
        {
            // write a few bytes
            writer.Write(255);
            writer.Write(0);
            Equal(2L, writer.WrittenCount);
            var actual = new byte[2];

            Equal(2, writer.CopyTo(actual));
            Equal(new byte[] { 255, 0 }, actual);

            writer.Clear();
            var expected = RandomBytes(5000);

            writer.Write(expected);
            actual = new byte[expected.Length];
            writer.CopyTo <ReadOnlySpanConsumer <byte, ArrayCopyOperation> >(new ReadOnlySpanConsumer <byte, ArrayCopyOperation>(&ArrayCopyOperation.Append, new ArrayCopyOperation(actual)));
            Equal(expected, actual);
        }
Beispiel #5
0
 private static void InitializeString(Span <char> output, IGrowableBuffer <char> input)
 => input.CopyTo(output);