示例#1
0
    public static async IAsyncEnumerable <TValue> GetValuesAsync <TValue>(this IKeyValueStorage <string> storage, [EnumeratorCancellation] CancellationToken cancellationToken = default)
        where TValue : IRocketMessage <TValue>
    {
        var bytesPool = BytesPool.Shared;

        using var bytesPipe = new BytesPipe(bytesPool);

        await foreach (var key in storage.GetKeysAsync(cancellationToken))
        {
            if (!await storage.TryReadAsync(key, bytesPipe.Writer, cancellationToken))
            {
                continue;
            }

            var value = IRocketMessage <TValue> .Import(bytesPipe.Reader.GetSequence(), bytesPool);

            yield return(value);

            bytesPipe.Reset();
        }
    }
示例#2
0
    public void RandomTest()
    {
        var random = new Random(0);

        using (var bytesPipe = new BytesPipe())
        {
            for (int i = 0; i < 32; i++)
            {
                foreach (var result1 in GenRandomValue(random, 8).Select(n => (byte)n))
                {
                    Varint.SetUInt8(result1, bytesPipe.Writer);

                    var reader = new SequenceReader <byte>(bytesPipe.Reader.GetSequence());
                    Varint.TryGetUInt8(ref reader, out var result2);

                    Assert.Equal(result1, result2);

                    bytesPipe.Reset();
                }
            }
        }

        using (var bytesPipe = new BytesPipe())
        {
            for (int i = 0; i < 32; i++)
            {
                foreach (var result1 in GenRandomValue(random, 16).Select(n => (ushort)n))
                {
                    Varint.SetUInt16(result1, bytesPipe.Writer);

                    var reader = new SequenceReader <byte>(bytesPipe.Reader.GetSequence());
                    Varint.TryGetUInt16(ref reader, out var result2);

                    Assert.Equal(result1, result2);

                    bytesPipe.Reset();
                }
            }
        }

        using (var bytesPipe = new BytesPipe())
        {
            for (int i = 0; i < 32; i++)
            {
                foreach (var result1 in GenRandomValue(random, 32).Select(n => (uint)n))
                {
                    Varint.SetUInt32(result1, bytesPipe.Writer);

                    var reader = new SequenceReader <byte>(bytesPipe.Reader.GetSequence());
                    Varint.TryGetUInt32(ref reader, out var result2);

                    Assert.Equal(result1, result2);

                    bytesPipe.Reset();
                }
            }
        }

        using (var bytesPipe = new BytesPipe())
        {
            for (int i = 0; i < 32; i++)
            {
                foreach (var result1 in GenRandomValue(random, 64).Select(n => (ulong)n))
                {
                    Varint.SetUInt64(result1, bytesPipe.Writer);

                    var reader = new SequenceReader <byte>(bytesPipe.Reader.GetSequence());
                    Varint.TryGetUInt64(ref reader, out var result2);

                    Assert.Equal(result1, result2);

                    bytesPipe.Reset();
                }
            }
        }

        using (var bytesPipe = new BytesPipe())
        {
            for (int i = 0; i < 32; i++)
            {
                foreach (var result1 in GenRandomValue(random, 8).Select(n => (sbyte)n))
                {
                    Varint.SetInt8(result1, bytesPipe.Writer);

                    var reader = new SequenceReader <byte>(bytesPipe.Reader.GetSequence());
                    Varint.TryGetInt8(ref reader, out var result2);

                    Assert.Equal(result1, result2);

                    bytesPipe.Reset();
                }
            }
        }

        using (var bytesPipe = new BytesPipe())
        {
            for (int i = 0; i < 32; i++)
            {
                foreach (var result1 in GenRandomValue(random, 16).Select(n => (short)n))
                {
                    Varint.SetInt16(result1, bytesPipe.Writer);

                    var reader = new SequenceReader <byte>(bytesPipe.Reader.GetSequence());
                    Varint.TryGetInt16(ref reader, out var result2);

                    Assert.Equal(result1, result2);

                    bytesPipe.Reset();
                }
            }
        }

        using (var bytesPipe = new BytesPipe())
        {
            for (int i = 0; i < 32; i++)
            {
                foreach (var result1 in GenRandomValue(random, 32).Select(n => (int)n))
                {
                    Varint.SetInt32(result1, bytesPipe.Writer);

                    var reader = new SequenceReader <byte>(bytesPipe.Reader.GetSequence());
                    Varint.TryGetInt32(ref reader, out var result2);

                    Assert.Equal(result1, result2);

                    bytesPipe.Reset();
                }
            }
        }

        using (var bytesPipe = new BytesPipe())
        {
            for (int i = 0; i < 32; i++)
            {
                foreach (var result1 in GenRandomValue(random, 64).Select(n => (long)n))
                {
                    Varint.SetInt64(result1, bytesPipe.Writer);

                    var reader = new SequenceReader <byte>(bytesPipe.Reader.GetSequence());
                    Varint.TryGetInt64(ref reader, out var result2);

                    Assert.Equal(result1, result2);

                    bytesPipe.Reset();
                }
            }
        }
    }