< Summary

Class:Azure.Storage.Blobs.ChangeFeed.Segment
Assembly:Azure.Storage.Blobs.ChangeFeed
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs.ChangeFeed\src\Segment.cs
Covered lines:44
Uncovered lines:3
Coverable lines:47
Total lines:134
Line coverage:93.6% (44 of 47)
Covered branches:18
Total branches:20
Branch coverage:90% (18 of 20)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_DateTime()-100%100%
get_ManifestPath()-100%100%
.ctor(...)-100%100%
GetCursor()-100%100%
GetPage()-86.96%87.5%
HasNext()-100%100%
.ctor()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs.ChangeFeed\src\Segment.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Threading;
 7using System.Threading.Tasks;
 8using Azure.Storage.Blobs.Models;
 9
 10namespace Azure.Storage.Blobs.ChangeFeed
 11{
 12    internal class Segment
 13    {
 14        /// <summary>
 15        /// The time (to the nearest hour) associated with this Segment.
 16        /// </summary>
 69617        public DateTimeOffset DateTime { get; private set; }
 18
 19        /// <summary>
 20        /// The path of manifest associated with this Segment.
 21        /// </summary>
 26422        public string ManifestPath { get; private set; }
 23
 24        /// <summary>
 25        /// The Shards associated with this Segment.
 26        /// </summary>
 27        private readonly List<Shard> _shards;
 28
 29        /// <summary>
 30        /// The Shards we have finished reading from.
 31        /// </summary>
 32        private readonly HashSet<int> _finishedShards;
 33
 34        /// <summary>
 35        /// The index of the Shard we will return the next event from.
 36        /// </summary>
 37        private int _shardIndex;
 38
 12839        public Segment(
 12840            List<Shard> shards,
 12841            int shardIndex,
 12842            DateTimeOffset dateTime,
 12843            string manifestPath)
 44        {
 12845            _shards = shards;
 12846            _shardIndex = shardIndex;
 12847            DateTime = dateTime;
 12848            ManifestPath = manifestPath;
 12849            _finishedShards = new HashSet<int>();
 12850        }
 51
 52        public virtual SegmentCursor GetCursor()
 53        {
 13654            List<ShardCursor> shardCursors = new List<ShardCursor>();
 68855            foreach (Shard shard in _shards)
 56            {
 20857                var shardCursor = shard.GetCursor();
 20858                if (shardCursor != null)
 59                {
 20860                    shardCursors.Add(shard.GetCursor());
 61                }
 62            }
 13663            return new SegmentCursor(
 13664                segmentPath: ManifestPath,
 13665                shardCursors: shardCursors,
 13666                currentShardPath: _shards[_shardIndex].ShardPath);
 67        }
 68
 69        public virtual async Task<List<BlobChangeFeedEvent>> GetPage(
 70            bool async,
 71            int? pageSize,
 72            CancellationToken cancellationToken = default)
 73        {
 14874            List<BlobChangeFeedEvent> changeFeedEventList = new List<BlobChangeFeedEvent>();
 75
 14876            if (!HasNext())
 77            {
 078                throw new InvalidOperationException("Segment doesn't have any more events");
 79            }
 80
 14881            int i = 0;
 2990082            while (i < pageSize && _shards.Count > 0)
 83            {
 84                // If this Shard is finished, skip it.
 2981685                if (_finishedShards.Contains(_shardIndex))
 86                {
 21687                    _shardIndex++;
 88
 21689                    if (_shardIndex == _shards.Count)
 90                    {
 091                        _shardIndex = 0;
 92                    }
 93
 094                    continue;
 95                }
 96
 2960097                Shard currentShard = _shards[_shardIndex];
 98
 2960099                BlobChangeFeedEvent changeFeedEvent = await currentShard.Next(async, cancellationToken).ConfigureAwait(f
 100
 29600101                changeFeedEventList.Add(changeFeedEvent);
 102
 103                // If the current shard is completed, remove it from _shards
 29600104                if (!currentShard.HasNext())
 105                {
 88106                    _finishedShards.Add(_shardIndex);
 107                }
 108
 29600109                i++;
 29600110                _shardIndex++;
 29600111                if (_shardIndex >= _shards.Count)
 112                {
 26776113                    _shardIndex = 0;
 114                }
 115
 116                // If all the Shards are finished, we need to break out early.
 29600117                if (_finishedShards.Count == _shards.Count)
 118                {
 119                    break;
 120                }
 29536121            }
 122
 148123            return changeFeedEventList;
 148124        }
 125
 126        public virtual bool HasNext()
 436127            => _finishedShards.Count < _shards.Count;
 128
 129        /// <summary>
 130        /// Constructor for mocking.
 131        /// </summary>
 56132        public Segment() { }
 133    }
 134}