| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information.using System; |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.EventHubs.ServiceFabricProcessor |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// A persistable representation of what events in the stream have been processed. |
| | 11 | | /// Version 1 checkpoint is just a high-water mark, containing an offset and sequence number. All events at or lower |
| | 12 | | /// have been processed. Any events higher than the given position are unprocessed. |
| | 13 | | /// </summary> |
| | 14 | | public class Checkpoint |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Create an uninitialized checkpoint of the given version. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="version"></param> |
| 14 | 20 | | internal Checkpoint(int version) |
| | 21 | | { |
| 14 | 22 | | this.Version = version; |
| 14 | 23 | | this.Valid = false; |
| 14 | 24 | | } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Create an initialized version 1 checkpoint. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="offset">Offset of highest-processed position.</param> |
| | 30 | | /// <param name="sequenceNumber">Sequence number of highest-processed position.</param> |
| 8 | 31 | | public Checkpoint(string offset, long sequenceNumber) |
| | 32 | | { |
| 8 | 33 | | this.Version = 1; |
| 8 | 34 | | this.Offset = offset; |
| 8 | 35 | | this.SequenceNumber = sequenceNumber; |
| 8 | 36 | | this.Valid = true; |
| 8 | 37 | | } |
| | 38 | |
|
| | 39 | | #region AllVersions |
| | 40 | | // |
| | 41 | | // Methods and properties valid for all versions. |
| | 42 | | // |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Version of this checkpoint. |
| | 46 | | /// </summary> |
| 60 | 47 | | public int Version { get; protected set; } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// True if this checkpoint contains a valid position. |
| | 51 | | /// </summary> |
| 52 | 52 | | public bool Valid { get; protected set; } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Serialize this instance to a persistable representation as a name-value dictionary. |
| | 56 | | /// </summary> |
| | 57 | | /// <returns>Serialized dictionary representation.</returns> |
| | 58 | | public Dictionary<string, object> ToDictionary() |
| | 59 | | { |
| 14 | 60 | | Dictionary<string, object> converted = new Dictionary<string, object>(); |
| | 61 | |
|
| 14 | 62 | | converted.Add(Constants.CheckpointPropertyVersion, this.Version); |
| 14 | 63 | | converted.Add(Constants.CheckpointPropertyValid, this.Valid); |
| | 64 | |
|
| 14 | 65 | | switch (this.Version) |
| | 66 | | { |
| | 67 | | case 1: |
| 14 | 68 | | converted.Add(Constants.CheckpointPropertyOffsetV1, this.Offset); |
| 14 | 69 | | converted.Add(Constants.CheckpointPropertySequenceNumberV1, this.SequenceNumber); |
| 14 | 70 | | break; |
| | 71 | |
|
| | 72 | | default: |
| 0 | 73 | | throw new NotImplementedException(); |
| | 74 | | } |
| | 75 | |
|
| 14 | 76 | | return converted; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Deserialize from a name-value dictionary. |
| | 81 | | /// </summary> |
| | 82 | | /// <param name="dictionary">Serialized representation.</param> |
| | 83 | | /// <returns>Deserialized instance.</returns> |
| | 84 | | static public Checkpoint CreateFromDictionary(Dictionary<string, object> dictionary) |
| | 85 | | { |
| 8 | 86 | | int version = (int)dictionary[Constants.CheckpointPropertyVersion]; |
| 8 | 87 | | bool valid = (bool)dictionary[Constants.CheckpointPropertyValid]; |
| | 88 | |
|
| 8 | 89 | | Checkpoint result = new Checkpoint(version); |
| | 90 | |
|
| 8 | 91 | | if (valid) |
| | 92 | | { |
| 8 | 93 | | result.Valid = true; |
| | 94 | |
|
| 8 | 95 | | switch (result.Version) |
| | 96 | | { |
| | 97 | | case 1: |
| 8 | 98 | | result.Offset = (string)dictionary[Constants.CheckpointPropertyOffsetV1]; |
| 8 | 99 | | result.SequenceNumber = (long)dictionary[Constants.CheckpointPropertySequenceNumberV1]; |
| 8 | 100 | | break; |
| | 101 | |
|
| | 102 | | default: |
| 0 | 103 | | throw new NotImplementedException($"Unrecognized checkpoint version {result.Version}"); |
| | 104 | | } |
| | 105 | | } |
| | 106 | |
|
| 8 | 107 | | return result; |
| | 108 | | } |
| | 109 | | #endregion AllVersions |
| | 110 | |
|
| | 111 | | #region Version1 |
| | 112 | | // |
| | 113 | | // Methods and properties for Version==1 |
| | 114 | | // |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Initialize an uninitialized instance as a version 1 checkpoint. |
| | 118 | | /// </summary> |
| | 119 | | /// <param name="offset">Offset of highest-processed position.</param> |
| | 120 | | /// <param name="sequenceNumber">Sequence number of highest-processed position.</param> |
| | 121 | | public void InitializeV1(string offset, long sequenceNumber) |
| | 122 | | { |
| 0 | 123 | | this.Version = 1; |
| | 124 | |
|
| 0 | 125 | | if (string.IsNullOrEmpty(offset)) |
| | 126 | | { |
| 0 | 127 | | throw new ArgumentException("offset must not be null or empty"); |
| | 128 | | } |
| 0 | 129 | | if (sequenceNumber < 0) |
| | 130 | | { |
| 0 | 131 | | throw new ArgumentException("sequenceNumber must be >= 0"); |
| | 132 | | } |
| | 133 | |
|
| 0 | 134 | | this.Offset = offset; |
| 0 | 135 | | this.SequenceNumber = sequenceNumber; |
| | 136 | |
|
| 0 | 137 | | this.Valid = true; |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | /// <summary> |
| | 141 | | /// Offset of highest-processed position. Immutable after construction or initialization. |
| | 142 | | /// </summary> |
| 52 | 143 | | public string Offset { get; private set; } |
| | 144 | |
|
| | 145 | | /// <summary> |
| | 146 | | /// Sequence number of highest-processed position. Immutable after construction or initialization. |
| | 147 | | /// </summary> |
| 50 | 148 | | public long SequenceNumber { get; private set; } |
| | 149 | | #endregion Version1 |
| | 150 | | } |
| | 151 | | } |