< Summary

Class:Azure.Messaging.EventHubs.AmqpFilter
Assembly:Azure.Messaging.EventHubs
File(s):C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs\src\Amqp\AmqpFilter.cs
Covered lines:9
Uncovered lines:0
Coverable lines:9
Total lines:80
Line coverage:100% (9 of 9)
Covered branches:10
Total branches:10
Branch coverage:100% (10 of 10)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
CreateConsumerFilter(...)-100%100%
BuildFilterExpression(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs\src\Amqp\AmqpFilter.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using Azure.Core;
 6using Azure.Messaging.EventHubs.Consumer;
 7using Microsoft.Azure.Amqp;
 8using Microsoft.Azure.Amqp.Framing;
 9
 10namespace Azure.Messaging.EventHubs
 11{
 12    /// <summary>
 13    ///   The set of filters associated with an AMQP messages and
 14    ///   entities.
 15    /// </summary>
 16    ///
 17    internal static class AmqpFilter
 18    {
 19        /// <summary>Indicates filtering based on the sequence number of a message.</summary>
 20        public const string SequenceNumberName = "amqp.annotation.x-opt-sequence-number";
 21
 22        /// <summary>Indicates filtering based on the offset of a message.</summary>
 23        public const string OffsetName = "amqp.annotation.x-opt-offset";
 24
 25        /// <summary>Indicates filtering based on time that a message was enqueued.</summary>
 26        public const string EnqueuedTimeName = "amqp.annotation.x-opt-enqueued-time";
 27
 28        /// <summary>Identifies the filter type name.</summary>
 29        public const string ConsumerFilterName = AmqpConstants.Apache + ":selector-filter:string";
 30
 31        /// <summary>Identifies the filter type code.</summary>
 32        public const ulong ConsumerFilterCode = 0x00000137000000A;
 33
 34        /// <summary>
 35        ///   Creates an event consumer filter based on the specified expression.
 36        /// </summary>
 37        ///
 38        /// <param name="filterExpression">The SQL-like expression to use for filtering events in the partition.</param>
 39        ///
 40        /// <returns>An <see cref="AmqpDescribed"/> type to use in the filter map for a consumer AMQP link.</returns>
 41        ///
 42        public static AmqpDescribed CreateConsumerFilter(string filterExpression)
 43        {
 2244            Argument.AssertNotNullOrEmpty(filterExpression, nameof(filterExpression));
 1845            return new AmqpDescribed(ConsumerFilterName, ConsumerFilterCode) { Value = filterExpression };
 46        }
 47
 48        /// <summary>
 49        ///   Builds an AMQP filter expression for the specified event position.
 50        /// </summary>
 51        ///
 52        /// <param name="eventPosition">The event position to use as the source for filtering.</param>
 53        ///
 54        /// <returns>The AMQP filter expression that corresponds to the <paramref name="eventPosition"/>.</returns>
 55        ///
 56        public static string BuildFilterExpression(EventPosition eventPosition)
 57        {
 58            // Build the filter expression, in the order of significance.
 59
 4060            if (!string.IsNullOrEmpty(eventPosition.Offset))
 61            {
 2662                return $"{ OffsetName } { (eventPosition.IsInclusive ? ">=" : ">") } { eventPosition.Offset }";
 63            }
 64
 1465            if (eventPosition.SequenceNumber.HasValue)
 66            {
 667                return $"{ SequenceNumberName } { (eventPosition.IsInclusive ? ">=" : ">") } { eventPosition.SequenceNum
 68            }
 69
 870            if (eventPosition.EnqueuedTime.HasValue)
 71            {
 672                return $"{ EnqueuedTimeName } > { eventPosition.EnqueuedTime.Value.ToUnixTimeMilliseconds() }";
 73            }
 74
 75            // If no filter was built, than the event position is not valid for filtering.
 76
 277            throw new ArgumentException(Resources.InvalidEventPositionForFilter, nameof(eventPosition));
 78        }
 79    }
 80}