|  |  | 1 |  | // Copyright (c) Microsoft Corporation. All rights reserved. | 
|  |  | 2 |  | // Licensed under the MIT License. | 
|  |  | 3 |  |  | 
|  |  | 4 |  | using System; | 
|  |  | 5 |  | using System.ComponentModel; | 
|  |  | 6 |  | using System.Globalization; | 
|  |  | 7 |  | using Azure.Core; | 
|  |  | 8 |  |  | 
|  |  | 9 |  | namespace Azure.Messaging.EventHubs.Consumer | 
|  |  | 10 |  | { | 
|  |  | 11 |  |     /// <summary> | 
|  |  | 12 |  |     ///   The position of events in an Event Hub partition, typically used in the creation of | 
|  |  | 13 |  |     ///   an <see cref="EventHubConsumerClient" />. | 
|  |  | 14 |  |     /// </summary> | 
|  |  | 15 |  |     /// | 
|  |  | 16 |  |     public struct EventPosition : IEquatable<EventPosition> | 
|  |  | 17 |  |     { | 
|  |  | 18 |  |         /// <summary>The token that represents the beginning event in the stream of a partition.</summary> | 
|  |  | 19 |  |         private const string StartOfStreamOffset = "-1"; | 
|  |  | 20 |  |  | 
|  |  | 21 |  |         /// <summary>The token that represents the last event in the stream of a partition.</summary> | 
|  |  | 22 |  |         private const string EndOfStreamOffset = "@latest"; | 
|  |  | 23 |  |  | 
|  |  | 24 |  |         /// <summary> | 
|  |  | 25 |  |         ///   Corresponds to the location of the first event present in the partition.  Use this | 
|  |  | 26 |  |         ///   position to begin receiving from the first event that was enqueued in the partition | 
|  |  | 27 |  |         ///   which has not expired due to the retention policy. | 
|  |  | 28 |  |         /// </summary> | 
|  |  | 29 |  |         /// | 
|  | 660 | 30 |  |         public static EventPosition Earliest => FromOffset(StartOfStreamOffset, false); | 
|  |  | 31 |  |  | 
|  |  | 32 |  |         /// <summary> | 
|  |  | 33 |  |         ///   Corresponds to the end of the partition, where no more events are currently enqueued.  Use this | 
|  |  | 34 |  |         ///   position to begin receiving from the next event to be enqueued in the partition after an <see cref="EventH | 
|  |  | 35 |  |         ///   is created with this position. | 
|  |  | 36 |  |         /// </summary> | 
|  |  | 37 |  |         /// | 
|  | 38 | 38 |  |         public static EventPosition Latest => FromOffset(EndOfStreamOffset, false); | 
|  |  | 39 |  |  | 
|  |  | 40 |  |         /// <summary> | 
|  |  | 41 |  |         ///   The offset of the event identified by this position. | 
|  |  | 42 |  |         /// </summary> | 
|  |  | 43 |  |         /// | 
|  |  | 44 |  |         /// <value>Expected to be <c>null</c> if the event position represents a sequence number or enqueue time.</value | 
|  |  | 45 |  |         /// | 
|  |  | 46 |  |         /// <remarks> | 
|  |  | 47 |  |         ///   The offset is the relative position for event in the context of the stream.  The offset | 
|  |  | 48 |  |         ///   should not be considered a stable value, as the same offset may refer to a different event | 
|  |  | 49 |  |         ///   as events reach the age limit for retention and are no longer visible within the stream. | 
|  |  | 50 |  |         /// </remarks> | 
|  |  | 51 |  |         /// | 
|  | 1364 | 52 |  |         internal string Offset { get; set; } | 
|  |  | 53 |  |  | 
|  |  | 54 |  |         /// <summary> | 
|  |  | 55 |  |         ///   Indicates if the specified offset is inclusive of the event which it identifies.  This | 
|  |  | 56 |  |         ///   information is only relevant if the event position was identified by an offset or sequence number. | 
|  |  | 57 |  |         /// </summary> | 
|  |  | 58 |  |         /// | 
|  |  | 59 |  |         /// <value><c>true</c> if the offset is inclusive; otherwise, <c>false</c>.</value> | 
|  |  | 60 |  |         /// | 
|  | 1198 | 61 |  |         internal bool IsInclusive { get; set; } | 
|  |  | 62 |  |  | 
|  |  | 63 |  |         /// <summary> | 
|  |  | 64 |  |         ///   The enqueue time of the event identified by this position. | 
|  |  | 65 |  |         /// </summary> | 
|  |  | 66 |  |         /// | 
|  |  | 67 |  |         /// <value>Expected to be <c>null</c> if the event position represents an offset or sequence number.</value> | 
|  |  | 68 |  |         /// | 
|  | 330 | 69 |  |         internal DateTimeOffset? EnqueuedTime { get; set; } | 
|  |  | 70 |  |  | 
|  |  | 71 |  |         /// <summary> | 
|  |  | 72 |  |         ///   The sequence number of the event identified by this position. | 
|  |  | 73 |  |         /// </summary> | 
|  |  | 74 |  |         /// | 
|  |  | 75 |  |         /// <value>Expected to be <c>null</c> if the event position represents an offset or enqueue time.</value> | 
|  |  | 76 |  |         /// | 
|  | 384 | 77 |  |         internal long? SequenceNumber { get; set; } | 
|  |  | 78 |  |  | 
|  |  | 79 |  |         /// <summary> | 
|  |  | 80 |  |         ///   Corresponds to the event in the partition at the provided offset, inclusive of that event. | 
|  |  | 81 |  |         /// </summary> | 
|  |  | 82 |  |         /// | 
|  |  | 83 |  |         /// <param name="offset">The offset of an event with respect to its relative position in the partition.</param> | 
|  |  | 84 |  |         /// <param name="isInclusive">If true, the event with the <paramref name="offset"/> is included; otherwise the n | 
|  |  | 85 |  |         /// | 
|  |  | 86 |  |         /// <returns>The position of the specified event.</returns> | 
|  |  | 87 |  |         /// | 
|  |  | 88 |  |         public static EventPosition FromOffset(long offset, | 
|  | 146 | 89 |  |                                                bool isInclusive = true) => FromOffset(offset.ToString(CultureInfo.Invari | 
|  |  | 90 |  |  | 
|  |  | 91 |  |         /// <summary> | 
|  |  | 92 |  |         ///   Corresponds to the event in the partition having a specified sequence number associated with it. | 
|  |  | 93 |  |         /// </summary> | 
|  |  | 94 |  |         /// | 
|  |  | 95 |  |         /// <param name="sequenceNumber">The sequence number assigned to an event when it was enqueued in the partition. | 
|  |  | 96 |  |         /// <param name="isInclusive">If true, the event with the <paramref name="sequenceNumber"/> is included; otherwi | 
|  |  | 97 |  |         /// | 
|  |  | 98 |  |         /// <returns>The position of the specified event.</returns> | 
|  |  | 99 |  |         /// | 
|  |  | 100 |  |         public static EventPosition FromSequenceNumber(long sequenceNumber, | 
|  |  | 101 |  |                                                        bool isInclusive = true) | 
|  |  | 102 |  |         { | 
|  | 44 | 103 |  |             return new EventPosition | 
|  | 44 | 104 |  |             { | 
|  | 44 | 105 |  |                 SequenceNumber = sequenceNumber, | 
|  | 44 | 106 |  |                 IsInclusive = isInclusive | 
|  | 44 | 107 |  |             }; | 
|  |  | 108 |  |         } | 
|  |  | 109 |  |  | 
|  |  | 110 |  |         /// <summary> | 
|  |  | 111 |  |         ///   Corresponds to a specific date and time within the partition to begin seeking an event; the event enqueued | 
|  |  | 112 |  |         ///   requested <paramref name="enqueuedTime" /> will become the current position. | 
|  |  | 113 |  |         /// </summary> | 
|  |  | 114 |  |         /// | 
|  |  | 115 |  |         /// <param name="enqueuedTime">The date and time, in UTC, from which the next available event should be chosen.< | 
|  |  | 116 |  |         /// | 
|  |  | 117 |  |         /// <returns>The position of the specified event.</returns> | 
|  |  | 118 |  |         /// | 
|  |  | 119 |  |         public static EventPosition FromEnqueuedTime(DateTimeOffset enqueuedTime) | 
|  |  | 120 |  |         { | 
|  | 16 | 121 |  |             return new EventPosition | 
|  | 16 | 122 |  |             { | 
|  | 16 | 123 |  |                 EnqueuedTime = enqueuedTime | 
|  | 16 | 124 |  |             }; | 
|  |  | 125 |  |         } | 
|  |  | 126 |  |  | 
|  |  | 127 |  |         /// <summary> | 
|  |  | 128 |  |         ///   Determines whether the specified <see cref="EventPosition" /> is equal to this instance. | 
|  |  | 129 |  |         /// </summary> | 
|  |  | 130 |  |         /// | 
|  |  | 131 |  |         /// <param name="other">The <see cref="EventPosition" /> to compare with this instance.</param> | 
|  |  | 132 |  |         /// | 
|  |  | 133 |  |         /// <returns><c>true</c> if the specified <see cref="EventPosition" /> is equal to this instance; otherwise, <c> | 
|  |  | 134 |  |         /// | 
|  |  | 135 |  |         public bool Equals(EventPosition other) | 
|  |  | 136 |  |         { | 
|  | 180 | 137 |  |             return (Offset == other.Offset) | 
|  | 180 | 138 |  |                 && (SequenceNumber == other.SequenceNumber) | 
|  | 180 | 139 |  |                 && (EnqueuedTime == other.EnqueuedTime) | 
|  | 180 | 140 |  |                 && (IsInclusive == other.IsInclusive); | 
|  |  | 141 |  |         } | 
|  |  | 142 |  |  | 
|  |  | 143 |  |         /// <summary> | 
|  |  | 144 |  |         ///   Determines whether the specified <see cref="System.Object" /> is equal to this instance. | 
|  |  | 145 |  |         /// </summary> | 
|  |  | 146 |  |         /// | 
|  |  | 147 |  |         /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param> | 
|  |  | 148 |  |         /// | 
|  |  | 149 |  |         /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c> | 
|  |  | 150 |  |         /// | 
|  |  | 151 |  |         [EditorBrowsable(EditorBrowsableState.Never)] | 
|  |  | 152 |  |         public override bool Equals(object obj) => | 
|  | 64 | 153 |  |             obj switch | 
|  | 64 | 154 |  |             { | 
|  | 128 | 155 |  |                 EventPosition other => Equals(other), | 
|  | 0 | 156 |  |                 _ => false | 
|  | 64 | 157 |  |             }; | 
|  |  | 158 |  |  | 
|  |  | 159 |  |         /// <summary> | 
|  |  | 160 |  |         ///   Returns a hash code for this instance. | 
|  |  | 161 |  |         /// </summary> | 
|  |  | 162 |  |         /// | 
|  |  | 163 |  |         /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a ha | 
|  |  | 164 |  |         /// | 
|  |  | 165 |  |         [EditorBrowsable(EditorBrowsableState.Never)] | 
|  |  | 166 |  |         public override int GetHashCode() | 
|  |  | 167 |  |         { | 
|  | 4 | 168 |  |             var hashCode = new HashCodeBuilder(); | 
|  | 4 | 169 |  |             hashCode.Add(Offset); | 
|  | 4 | 170 |  |             hashCode.Add(SequenceNumber); | 
|  | 4 | 171 |  |             hashCode.Add(EnqueuedTime); | 
|  | 4 | 172 |  |             hashCode.Add(IsInclusive); | 
|  |  | 173 |  |  | 
|  | 4 | 174 |  |             return hashCode.ToHashCode(); | 
|  |  | 175 |  |         } | 
|  |  | 176 |  |  | 
|  |  | 177 |  |         /// <summary> | 
|  |  | 178 |  |         ///   Converts the instance to string representation. | 
|  |  | 179 |  |         /// </summary> | 
|  |  | 180 |  |         /// | 
|  |  | 181 |  |         /// <returns>A <see cref="System.String" /> that represents this instance.</returns> | 
|  |  | 182 |  |         /// | 
|  |  | 183 |  |         [EditorBrowsable(EditorBrowsableState.Never)] | 
|  |  | 184 |  |         public override string ToString() => | 
|  |  | 185 |  |             this switch | 
|  |  | 186 |  |             { | 
|  | 122 | 187 |  |                 EventPosition _ when (Offset == StartOfStreamOffset) => nameof(Earliest), | 
|  | 16 | 188 |  |                 EventPosition _ when (Offset == EndOfStreamOffset) => nameof(Latest), | 
|  | 18 | 189 |  |                 EventPosition _ when (!string.IsNullOrEmpty(Offset)) => $"Offset: [{ Offset }] | Inclusive: [{ IsInclusi | 
|  | 10 | 190 |  |                 EventPosition _ when (SequenceNumber.HasValue) => $"Sequence Number: [{ SequenceNumber }] | Inclusive: [ | 
|  | 4 | 191 |  |                 EventPosition _ when (EnqueuedTime.HasValue) => $"Enqueued: [{ EnqueuedTime }]", | 
|  | 0 | 192 |  |                 _ => base.ToString() | 
|  |  | 193 |  |             }; | 
|  |  | 194 |  |  | 
|  |  | 195 |  |         /// <summary> | 
|  |  | 196 |  |         ///   Corresponds to the event in the partition at the provided offset. | 
|  |  | 197 |  |         /// </summary> | 
|  |  | 198 |  |         /// | 
|  |  | 199 |  |         /// <param name="offset">The offset of an event with respect to its relative position in the partition.</param> | 
|  |  | 200 |  |         /// <param name="isInclusive">If true, the event at the <paramref name="offset"/> is included; otherwise the nex | 
|  |  | 201 |  |         /// | 
|  |  | 202 |  |         /// <returns>The position of the specified event.</returns> | 
|  |  | 203 |  |         /// | 
|  |  | 204 |  |         private static EventPosition FromOffset(string offset, | 
|  |  | 205 |  |                                                 bool isInclusive) | 
|  |  | 206 |  |         { | 
|  | 844 | 207 |  |             Argument.AssertNotNullOrWhiteSpace(nameof(offset), offset); | 
|  |  | 208 |  |  | 
|  | 844 | 209 |  |             return new EventPosition | 
|  | 844 | 210 |  |             { | 
|  | 844 | 211 |  |                 Offset = offset, | 
|  | 844 | 212 |  |                 IsInclusive = isInclusive | 
|  | 844 | 213 |  |             }; | 
|  |  | 214 |  |         } | 
|  |  | 215 |  |  | 
|  |  | 216 |  |         /// <summary> | 
|  |  | 217 |  |         ///   Determines whether the specified <see cref="EventPosition" /> instances are equal to each other. | 
|  |  | 218 |  |         /// </summary> | 
|  |  | 219 |  |         /// | 
|  |  | 220 |  |         /// <param name="left">The first <see cref="EventPosition" /> to consider.</param> | 
|  |  | 221 |  |         /// <param name="right">The second <see cref="EventPosition" /> to consider.</param> | 
|  |  | 222 |  |         /// | 
|  |  | 223 |  |         /// <returns><c>true</c> if the two specified <see cref="EventPosition" /> instances are equal; otherwise, <c>fa | 
|  |  | 224 |  |         /// | 
|  |  | 225 |  |         public static bool operator ==(EventPosition left, | 
|  | 58 | 226 |  |                                        EventPosition right) => left.Equals(right); | 
|  |  | 227 |  |  | 
|  |  | 228 |  |         /// <summary> | 
|  |  | 229 |  |         ///   Determines whether the specified <see cref="EventPosition" /> instances are not equal to each other. | 
|  |  | 230 |  |         /// </summary> | 
|  |  | 231 |  |         /// | 
|  |  | 232 |  |         /// <param name="left">The first <see cref="EventPosition" /> to consider.</param> | 
|  |  | 233 |  |         /// <param name="right">The second <see cref="EventPosition" /> to consider.</param> | 
|  |  | 234 |  |         /// | 
|  |  | 235 |  |         /// <returns><c>true</c> if the two specified <see cref="EventPosition" /> instances are not equal; otherwise, < | 
|  |  | 236 |  |         /// | 
|  |  | 237 |  |         public static bool operator !=(EventPosition left, | 
|  | 22 | 238 |  |                                        EventPosition right) => (!left.Equals(right)); | 
|  |  | 239 |  |     } | 
|  |  | 240 |  | } |