| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.ComponentModel; |
| | 7 | | using System.IO; |
| | 8 | | using Azure.Messaging.EventHubs.Consumer; |
| | 9 | |
|
| | 10 | | namespace Azure.Messaging.EventHubs |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A set of data encapsulating an event and the associated metadata for |
| | 14 | | /// use with Event Hubs operations. |
| | 15 | | /// </summary> |
| | 16 | | /// |
| | 17 | | public class EventData |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// The data associated with the event. |
| | 21 | | /// </summary> |
| | 22 | | /// |
| | 23 | | /// <remarks> |
| | 24 | | /// If the means for deserializaing the raw data is not apparent to consumers, a |
| | 25 | | /// common technique is to make use of <see cref="EventData.Properties" /> to associate serialization hints |
| | 26 | | /// as an aid to consumers who wish to deserialize the binary data. |
| | 27 | | /// </remarks> |
| | 28 | | /// |
| | 29 | | /// <seealso cref="EventData.Properties" /> |
| | 30 | | /// |
| 4700 | 31 | | public ReadOnlyMemory<byte> Body { get; } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// The data associated with the event, in stream form. |
| | 35 | | /// </summary> |
| | 36 | | /// |
| | 37 | | /// <value> |
| | 38 | | /// A <see cref="Stream" /> containing the raw data representing the <see cref="Body" /> |
| | 39 | | /// of the event. The caller is assumed to have ownership of the stream, including responsibility |
| | 40 | | /// for managing its lifespan and ensuring proper disposal. |
| | 41 | | /// </value> |
| | 42 | | /// |
| | 43 | | /// <remarks> |
| | 44 | | /// If the means for deserializing the raw data is not apparent to consumers, a |
| | 45 | | /// common technique is to make use of <see cref="EventData.Properties" /> to associate serialization hints |
| | 46 | | /// as an aid to consumers who wish to deserialize the binary data. |
| | 47 | | /// </remarks> |
| | 48 | | /// |
| | 49 | | /// <seealso cref="EventData.Properties" /> |
| | 50 | | /// |
| | 51 | | public Stream BodyAsStream |
| | 52 | | { |
| 4 | 53 | | get => new MemoryStream(Body.ToArray()); |
| | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// The set of free-form event properties which may be used for passing metadata associated with the event bod |
| | 58 | | /// during Event Hubs operations. |
| | 59 | | /// </summary> |
| | 60 | | /// |
| | 61 | | /// <remarks> |
| | 62 | | /// A common use case for <see cref="EventData.Properties" /> is to associate serialization hints for the <see |
| | 63 | | /// as an aid to consumers who wish to deserialize the binary data. |
| | 64 | | /// </remarks> |
| | 65 | | /// |
| | 66 | | /// <example> |
| | 67 | | /// <code> |
| | 68 | | /// var eventData = new EventData(serializedTelemetryData); |
| | 69 | | /// eventData.Properties["eventType"] = "com.microsoft.Azure.monitoring.EtlEvent"; |
| | 70 | | /// </code> |
| | 71 | | /// </example> |
| | 72 | | /// |
| 710 | 73 | | public IDictionary<string, object> Properties { get; } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// The set of free-form event properties which were provided by the Event Hubs service to pass metadata assoc |
| | 77 | | /// event or associated Event Hubs operation. |
| | 78 | | /// </summary> |
| | 79 | | /// |
| | 80 | | /// <remarks> |
| | 81 | | /// These properties are only populated for events received from the Event Hubs service. The set is otherwise |
| | 82 | | /// empty. |
| | 83 | | /// </remarks> |
| | 84 | | /// |
| 172 | 85 | | public IReadOnlyDictionary<string, object> SystemProperties { get; } |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// The sequence number assigned to the event when it was enqueued in the associated Event Hub partition. |
| | 89 | | /// </summary> |
| | 90 | | /// |
| | 91 | | /// <remarks> |
| | 92 | | /// This property is only populated for events received from the Event Hubs service. If this |
| | 93 | | /// EventData was not received from the Event Hubs service, the value is <see cref="long.MinValue"/>. |
| | 94 | | /// </remarks> |
| | 95 | | /// |
| 56 | 96 | | public long SequenceNumber { get; } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// The offset of the event when it was received from the associated Event Hub partition. |
| | 100 | | /// </summary> |
| | 101 | | /// |
| | 102 | | /// <remarks> |
| | 103 | | /// This property is only populated for events received from the Event Hubs service. If this |
| | 104 | | /// EventData was not received from the Event Hubs service, the value is <see cref="long.MinValue"/>. |
| | 105 | | /// </remarks> |
| | 106 | | /// |
| 2789178 | 107 | | public long Offset { get; } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// The date and time, in UTC, of when the event was enqueued in the Event Hub partition. |
| | 111 | | /// </summary> |
| | 112 | | /// |
| | 113 | | /// <remarks> |
| | 114 | | /// This property is only populated for events received from the Event Hubs service. If this |
| | 115 | | /// EventData was not received from the Event Hubs service, the value <c>default(DateTimeOffset)</c>. |
| | 116 | | /// </remarks> |
| | 117 | | /// |
| 74 | 118 | | public DateTimeOffset EnqueuedTime { get; } |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// The partition hashing key applied to the batch that the associated <see cref="EventData"/>, was published |
| | 122 | | /// </summary> |
| | 123 | | /// |
| | 124 | | /// <remarks> |
| | 125 | | /// This property is only populated for events received from the Event Hubs service. |
| | 126 | | /// </remarks> |
| | 127 | | /// |
| 64 | 128 | | public string PartitionKey { get; } |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// The sequence number of the event that was last enqueued into the Event Hub partition from which this |
| | 132 | | /// event was received. |
| | 133 | | /// </summary> |
| | 134 | | /// |
| | 135 | | /// <remarks> |
| | 136 | | /// This property is only populated for events received using an reader specifying |
| | 137 | | /// <see cref="ReadEventOptions.TrackLastEnqueuedEventProperties" /> as enabled. |
| | 138 | | /// </remarks> |
| | 139 | | /// |
| 64 | 140 | | internal long? LastPartitionSequenceNumber { get; } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// The offset of the event that was last enqueued into the Event Hub partition from which this event was |
| | 144 | | /// received. |
| | 145 | | /// </summary> |
| | 146 | | /// |
| | 147 | | /// <remarks> |
| | 148 | | /// This property is only populated for events received using an reader specifying |
| | 149 | | /// <see cref="ReadEventOptions.TrackLastEnqueuedEventProperties" /> as enabled. |
| | 150 | | /// </remarks> |
| | 151 | | /// |
| 64 | 152 | | internal long? LastPartitionOffset { get; } |
| | 153 | |
|
| | 154 | | /// <summary> |
| | 155 | | /// The date and time, in UTC, that the last event was enqueued into the Event Hub partition from |
| | 156 | | /// which this event was received. |
| | 157 | | /// </summary> |
| | 158 | | /// |
| | 159 | | /// <remarks> |
| | 160 | | /// This property is only populated for events received using an reader specifying |
| | 161 | | /// <see cref="ReadEventOptions.TrackLastEnqueuedEventProperties" /> as enabled. |
| | 162 | | /// </remarks> |
| | 163 | | /// |
| 68 | 164 | | internal DateTimeOffset? LastPartitionEnqueuedTime { get; } |
| | 165 | |
|
| | 166 | | /// <summary> |
| | 167 | | /// The date and time, in UTC, that the last event information for the Event Hub partition was retrieved |
| | 168 | | /// from the Event Hubs service. |
| | 169 | | /// </summary> |
| | 170 | | /// |
| | 171 | | /// <remarks> |
| | 172 | | /// This property is only populated for events received using an reader specifying |
| | 173 | | /// <see cref="ReadEventOptions.TrackLastEnqueuedEventProperties" /> as enabled. |
| | 174 | | /// </remarks> |
| | 175 | | /// |
| 68 | 176 | | internal DateTimeOffset? LastPartitionPropertiesRetrievalTime { get; } |
| | 177 | |
|
| | 178 | | /// <summary> |
| | 179 | | /// Initializes a new instance of the <see cref="EventData"/> class. |
| | 180 | | /// </summary> |
| | 181 | | /// |
| | 182 | | /// <param name="eventBody">The raw data to use as the body of the event.</param> |
| | 183 | | /// |
| 16612 | 184 | | public EventData(ReadOnlyMemory<byte> eventBody) : this(eventBody, lastPartitionSequenceNumber: null) |
| | 185 | | { |
| 16612 | 186 | | } |
| | 187 | |
|
| | 188 | | /// <summary> |
| | 189 | | /// Initializes a new instance of the <see cref="EventData"/> class. |
| | 190 | | /// </summary> |
| | 191 | | /// |
| | 192 | | /// <param name="eventBody">The raw data to use as the body of the event.</param> |
| | 193 | | /// <param name="properties">The set of free-form event properties to send with the event.</param> |
| | 194 | | /// <param name="systemProperties">The set of system properties received from the Event Hubs service.</param> |
| | 195 | | /// <param name="sequenceNumber">The sequence number assigned to the event when it was enqueued in the associate |
| | 196 | | /// <param name="offset">The offset of the event when it was received from the associated Event Hub partition.</ |
| | 197 | | /// <param name="enqueuedTime">The date and time, in UTC, of when the event was enqueued in the Event Hub partit |
| | 198 | | /// <param name="partitionKey">The partition hashing key applied to the batch that the associated <see cref="Eve |
| | 199 | | /// <param name="lastPartitionSequenceNumber">The sequence number that was last enqueued into the Event Hub part |
| | 200 | | /// <param name="lastPartitionOffset">The offset that was last enqueued into the Event Hub partition.</param> |
| | 201 | | /// <param name="lastPartitionEnqueuedTime">The date and time, in UTC, of the event that was last enqueued into |
| | 202 | | /// <param name="lastPartitionPropertiesRetrievalTime">The date and time, in UTC, that the last event informatio |
| | 203 | | /// |
| 16818 | 204 | | internal EventData(ReadOnlyMemory<byte> eventBody, |
| 16818 | 205 | | IDictionary<string, object> properties = null, |
| 16818 | 206 | | IReadOnlyDictionary<string, object> systemProperties = null, |
| 16818 | 207 | | long sequenceNumber = long.MinValue, |
| 16818 | 208 | | long offset = long.MinValue, |
| 16818 | 209 | | DateTimeOffset enqueuedTime = default, |
| 16818 | 210 | | string partitionKey = null, |
| 16818 | 211 | | long? lastPartitionSequenceNumber = null, |
| 16818 | 212 | | long? lastPartitionOffset = null, |
| 16818 | 213 | | DateTimeOffset? lastPartitionEnqueuedTime = null, |
| 16818 | 214 | | DateTimeOffset? lastPartitionPropertiesRetrievalTime = null) |
| | 215 | | { |
| 16818 | 216 | | Body = eventBody; |
| 16818 | 217 | | Properties = properties ?? new Dictionary<string, object>(); |
| 16818 | 218 | | SystemProperties = systemProperties ?? new Dictionary<string, object>(); |
| 16818 | 219 | | SequenceNumber = sequenceNumber; |
| 16818 | 220 | | Offset = offset; |
| 16818 | 221 | | EnqueuedTime = enqueuedTime; |
| 16818 | 222 | | PartitionKey = partitionKey; |
| 16818 | 223 | | LastPartitionSequenceNumber = lastPartitionSequenceNumber; |
| 16818 | 224 | | LastPartitionOffset = lastPartitionOffset; |
| 16818 | 225 | | LastPartitionEnqueuedTime = lastPartitionEnqueuedTime; |
| 16818 | 226 | | LastPartitionPropertiesRetrievalTime = lastPartitionPropertiesRetrievalTime; |
| 16818 | 227 | | } |
| | 228 | |
|
| | 229 | | /// <summary> |
| | 230 | | /// Initializes a new instance of the <see cref="EventData"/> class. |
| | 231 | | /// </summary> |
| | 232 | | /// |
| | 233 | | /// <param name="eventBody">The raw data to use as the body of the event.</param> |
| | 234 | | /// <param name="properties">The set of free-form event properties to send with the event.</param> |
| | 235 | | /// <param name="systemProperties">The set of system properties received from the Event Hubs service.</param> |
| | 236 | | /// <param name="sequenceNumber">The sequence number assigned to the event when it was enqueued in the associate |
| | 237 | | /// <param name="offset">The offset of the event when it was received from the associated Event Hub partition.</ |
| | 238 | | /// <param name="enqueuedTime">The date and time, in UTC, of when the event was enqueued in the Event Hub partit |
| | 239 | | /// <param name="partitionKey">The partition hashing key applied to the batch that the associated <see cref="Eve |
| | 240 | | /// |
| | 241 | | protected EventData(ReadOnlyMemory<byte> eventBody, |
| | 242 | | IDictionary<string, object> properties = null, |
| | 243 | | IReadOnlyDictionary<string, object> systemProperties = null, |
| | 244 | | long sequenceNumber = long.MinValue, |
| | 245 | | long offset = long.MinValue, |
| | 246 | | DateTimeOffset enqueuedTime = default, |
| 66 | 247 | | string partitionKey = null) : this(eventBody, properties, systemProperties, sequenceNumber, |
| | 248 | | { |
| 66 | 249 | | } |
| | 250 | |
|
| | 251 | | /// <summary> |
| | 252 | | /// Determines whether the specified <see cref="System.Object" /> is equal to this instance. |
| | 253 | | /// </summary> |
| | 254 | | /// |
| | 255 | | /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param> |
| | 256 | | /// |
| | 257 | | /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c> |
| | 258 | | /// |
| | 259 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 134510 | 260 | | public override bool Equals(object obj) => base.Equals(obj); |
| | 261 | |
|
| | 262 | | /// <summary> |
| | 263 | | /// Returns a hash code for this instance. |
| | 264 | | /// </summary> |
| | 265 | | /// |
| | 266 | | /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a ha |
| | 267 | | /// |
| | 268 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 269 | | public override int GetHashCode() => base.GetHashCode(); |
| | 270 | |
|
| | 271 | | /// <summary> |
| | 272 | | /// Converts the instance to string representation. |
| | 273 | | /// </summary> |
| | 274 | | /// |
| | 275 | | /// <returns>A <see cref="System.String" /> that represents this instance.</returns> |
| | 276 | | /// |
| | 277 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 278 | | public override string ToString() => base.ToString(); |
| | 279 | |
|
| | 280 | | /// <summary> |
| | 281 | | /// Creates a new copy of the current <see cref="EventData" />, cloning its attributes into a new instance. |
| | 282 | | /// </summary> |
| | 283 | | /// |
| | 284 | | /// <returns>A new copy of <see cref="EventData" />.</returns> |
| | 285 | | /// |
| | 286 | | internal EventData Clone() => |
| 44 | 287 | | new EventData |
| 44 | 288 | | ( |
| 44 | 289 | | Body, |
| 44 | 290 | | new Dictionary<string, object>(Properties), |
| 44 | 291 | | SystemProperties, |
| 44 | 292 | | SequenceNumber, |
| 44 | 293 | | Offset, |
| 44 | 294 | | EnqueuedTime, |
| 44 | 295 | | PartitionKey, |
| 44 | 296 | | LastPartitionSequenceNumber, |
| 44 | 297 | | LastPartitionOffset, |
| 44 | 298 | | LastPartitionEnqueuedTime, |
| 44 | 299 | | LastPartitionPropertiesRetrievalTime |
| 44 | 300 | | ); |
| | 301 | | } |
| | 302 | | } |