< Summary

Class:Microsoft.Azure.EventHubs.ServiceFabricProcessor.Checkpoint
Assembly:Microsoft.Azure.EventHubs.ServiceFabricProcessor
File(s):C:\Git\azure-sdk-for-net\sdk\eventhub\Microsoft.Azure.EventHubs.ServiceFabricProcessor\src\Checkpoint.cs
Covered lines:32
Uncovered lines:11
Coverable lines:43
Total lines:151
Line coverage:74.4% (32 of 43)
Covered branches:4
Total branches:10
Branch coverage:40% (4 of 10)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
.ctor(...)-100%100%
get_Version()-100%100%
get_Valid()-100%100%
ToDictionary()-88.89%50%
CreateFromDictionary(...)-90.91%75%
InitializeV1(...)-0%0%
get_Offset()-100%100%
get_SequenceNumber()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\eventhub\Microsoft.Azure.EventHubs.ServiceFabricProcessor\src\Checkpoint.cs

#LineLine coverage
 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
 4namespace 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>
 1420        internal Checkpoint(int version)
 21        {
 1422            this.Version = version;
 1423            this.Valid = false;
 1424        }
 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>
 831        public Checkpoint(string offset, long sequenceNumber)
 32        {
 833            this.Version = 1;
 834            this.Offset = offset;
 835            this.SequenceNumber = sequenceNumber;
 836            this.Valid = true;
 837        }
 38
 39        #region AllVersions
 40        //
 41        // Methods and properties valid for all versions.
 42        //
 43
 44        /// <summary>
 45        /// Version of this checkpoint.
 46        /// </summary>
 6047        public int Version { get; protected set; }
 48
 49        /// <summary>
 50        /// True if this checkpoint contains a valid position.
 51        /// </summary>
 5252        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        {
 1460            Dictionary<string, object> converted = new Dictionary<string, object>();
 61
 1462            converted.Add(Constants.CheckpointPropertyVersion, this.Version);
 1463            converted.Add(Constants.CheckpointPropertyValid, this.Valid);
 64
 1465            switch (this.Version)
 66            {
 67                case 1:
 1468                    converted.Add(Constants.CheckpointPropertyOffsetV1, this.Offset);
 1469                    converted.Add(Constants.CheckpointPropertySequenceNumberV1, this.SequenceNumber);
 1470                    break;
 71
 72                default:
 073                    throw new NotImplementedException();
 74            }
 75
 1476            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        {
 886            int version = (int)dictionary[Constants.CheckpointPropertyVersion];
 887            bool valid = (bool)dictionary[Constants.CheckpointPropertyValid];
 88
 889            Checkpoint result = new Checkpoint(version);
 90
 891            if (valid)
 92            {
 893                result.Valid = true;
 94
 895                switch (result.Version)
 96                {
 97                    case 1:
 898                        result.Offset = (string)dictionary[Constants.CheckpointPropertyOffsetV1];
 899                        result.SequenceNumber = (long)dictionary[Constants.CheckpointPropertySequenceNumberV1];
 8100                        break;
 101
 102                    default:
 0103                        throw new NotImplementedException($"Unrecognized checkpoint version {result.Version}");
 104                }
 105            }
 106
 8107            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        {
 0123            this.Version = 1;
 124
 0125            if (string.IsNullOrEmpty(offset))
 126            {
 0127                throw new ArgumentException("offset must not be null or empty");
 128            }
 0129            if (sequenceNumber < 0)
 130            {
 0131                throw new ArgumentException("sequenceNumber must be >= 0");
 132            }
 133
 0134            this.Offset = offset;
 0135            this.SequenceNumber = sequenceNumber;
 136
 0137            this.Valid = true;
 0138        }
 139
 140        /// <summary>
 141        /// Offset of highest-processed position. Immutable after construction or initialization.
 142        /// </summary>
 52143        public string Offset { get; private set; }
 144
 145        /// <summary>
 146        /// Sequence number of highest-processed position. Immutable after construction or initialization.
 147        /// </summary>
 50148        public long SequenceNumber { get; private set; }
 149        #endregion Version1
 150    }
 151}