Пример #1
0
        public void EnumerateTagValuesEmpty()
        {
            Activity activity = new Activity("Test");

            TagEnumerator state = default;

            activity.EnumerateTags(ref state);

            Assert.Equal(0, state.Count);
            Assert.False(state.LastTag.HasValue);
        }
Пример #2
0
        public void EnumerateTagValuesAll()
        {
            Activity activity = new Activity("Test");
            activity.SetTag("Tag1", "Value1");
            activity.SetTag("Tag2", "Value2");
            activity.SetTag("Tag3", "Value3");

            TagEnumerator state = default;

            activity.EnumerateTags(ref state);

            Assert.Equal(3, state.Count);
            Assert.True(state.LastTag.HasValue);
            Assert.Equal("Tag3", state.LastTag?.Key);
            Assert.Equal("Value3", state.LastTag?.Value);
        }
Пример #3
0
        private void WriteTag(Span <byte> tag, ref BitsBuffer tempBitsBuffer, SegmentHeader *tempHeader, int baseNumberOfBits)
        {
            if (tag.Length > byte.MaxValue)
            {
                ThrowInvalidTagLength();
            }

            var actualBitsBuffer = GetBitsBuffer();
            int a       = 136;
            var r       = actualBitsBuffer.ReadValue(ref a, 11);
            var tagEnum = new TagEnumerator(actualBitsBuffer /* need to read the previous values */,
                                            tempHeader->PreviousTagPosition);

            if (tagEnum.TryGetPrevious(out var prevTag, out var previousIndex))
            {
                if (prevTag.SequenceEqual(tag))
                {
                    tempBitsBuffer.AddValue(0, 1); // reuse previous buffer
                    return;
                }
                // go back a maximum of 8 tags, to avoid N**2 operations
                // after 8 tags, we'll just write the tag again
                for (int i = 0; i < 8; i++)
                {
                    if (tagEnum.TryGetPrevious(out prevTag, out previousIndex) == false)
                    {
                        break;
                    }
                    if (prevTag.SequenceEqual(tag))
                    {
                        tempBitsBuffer.AddValue(1, 1);
                        tempBitsBuffer.AddValue((ulong)previousIndex, BitsForTagLen);
                        if (previousIndex != 0)
                        {
                            // re-arrange the previous pointer for the previous value
                            // to point to the current one
                            actualBitsBuffer.SetBits(
                                (previousIndex + 1 + tag.Length) * 8,
                                tempHeader->PreviousTagPosition,
                                BitsForTagLen
                                );
                            tempHeader->PreviousTagPosition = (ushort)previousIndex;
                        }
                        return;
                    }
                }
            }
            tempBitsBuffer.AddValue(1, 1);

            int currentBitPosition = baseNumberOfBits + tempBitsBuffer.NumberOfBits + BitsForTagLen;
            int currentTagPosition = GetByteIndex(currentBitPosition);
            var bitsToSkip         = ToByteAlignment(currentBitPosition);

            tempBitsBuffer.AddValue((ulong)currentTagPosition, BitsForTagLen);


            tempBitsBuffer.Header->BitsPosition += (ushort)bitsToSkip;

            tempBitsBuffer.AddValue((ulong)tag.Length, 8);
            for (int i = 0; i < tag.Length; i++)
            {
                tempBitsBuffer.AddValue(tag[i], 8);
            }
            tempBitsBuffer.AddValue(tempHeader->PreviousTagPosition, BitsForTagLen);
            tempHeader->PreviousTagPosition = (ushort)(currentTagPosition);
        }