| | 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.Globalization; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Azure.Storage.Blobs.Models; |
| | 10 | |
|
| | 11 | | namespace Azure.Storage.Blobs.ChangeFeed |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// BlobChangeFeedExtensions. |
| | 15 | | /// </summary> |
| | 16 | | public static class BlobChangeFeedExtensions |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// GetChangeFeedClient. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="serviceClient"></param> |
| | 22 | | /// <returns><see cref="BlobChangeFeedClient"/>.</returns> |
| | 23 | | public static BlobChangeFeedClient GetChangeFeedClient(this BlobServiceClient serviceClient) |
| | 24 | | { |
| 32 | 25 | | return new BlobChangeFeedClient(serviceClient); |
| | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Builds a DateTimeOffset from a segment path. |
| | 30 | | /// </summary> |
| | 31 | | internal static DateTimeOffset? ToDateTimeOffset(string segmentPath) |
| | 32 | | { |
| 10268 | 33 | | if (segmentPath == null) |
| | 34 | | { |
| 4 | 35 | | return default; |
| | 36 | | } |
| 10264 | 37 | | string[] splitPath = segmentPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); |
| | 38 | |
|
| 10264 | 39 | | if (splitPath.Length < 3) |
| | 40 | | { |
| 0 | 41 | | throw new ArgumentException($"{nameof(segmentPath)} is not a valid segment path."); |
| | 42 | | } |
| | 43 | |
|
| 10264 | 44 | | return new DateTimeOffset( |
| 10264 | 45 | | year: int.Parse(splitPath[2], CultureInfo.InvariantCulture), |
| 10264 | 46 | | month: splitPath.Length >= 4 |
| 10264 | 47 | | ? int.Parse(splitPath[3], CultureInfo.InvariantCulture) |
| 10264 | 48 | | : 1, |
| 10264 | 49 | | day: splitPath.Length >= 5 |
| 10264 | 50 | | ? int.Parse(splitPath[4], CultureInfo.InvariantCulture) |
| 10264 | 51 | | : 1, |
| 10264 | 52 | | hour: splitPath.Length >= 6 |
| 10264 | 53 | | ? int.Parse(splitPath[5], CultureInfo.InvariantCulture) / 100 |
| 10264 | 54 | | : 0, |
| 10264 | 55 | | minute: 0, |
| 10264 | 56 | | second: 0, |
| 10264 | 57 | | offset: TimeSpan.Zero); |
| | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Rounds a DateTimeOffset down to the nearest hour. |
| | 62 | | /// </summary> |
| | 63 | | internal static DateTimeOffset? RoundDownToNearestHour(this DateTimeOffset? dateTimeOffset) |
| | 64 | | { |
| 100 | 65 | | if (dateTimeOffset == null) |
| | 66 | | { |
| 8 | 67 | | return null; |
| | 68 | | } |
| | 69 | |
|
| 92 | 70 | | return new DateTimeOffset( |
| 92 | 71 | | year: dateTimeOffset.Value.Year, |
| 92 | 72 | | month: dateTimeOffset.Value.Month, |
| 92 | 73 | | day: dateTimeOffset.Value.Day, |
| 92 | 74 | | hour: dateTimeOffset.Value.Hour, |
| 92 | 75 | | minute: 0, |
| 92 | 76 | | second: 0, |
| 92 | 77 | | offset: dateTimeOffset.Value.Offset); |
| | 78 | | } |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Rounds a DateTimeOffset up to the nearest hour. |
| | 82 | | /// </summary> |
| | 83 | | internal static DateTimeOffset? RoundUpToNearestHour(this DateTimeOffset? dateTimeOffset) |
| | 84 | | { |
| 68 | 85 | | if (dateTimeOffset == null) |
| | 86 | | { |
| 24 | 87 | | return null; |
| | 88 | | } |
| | 89 | |
|
| 44 | 90 | | if (dateTimeOffset.Value.Minute == 0 && dateTimeOffset.Value.Second == 0 && dateTimeOffset.Value.Millisecond |
| | 91 | | { |
| 8 | 92 | | return dateTimeOffset; |
| | 93 | | } |
| | 94 | |
|
| 36 | 95 | | DateTimeOffset? newDateTimeOffest = dateTimeOffset.RoundDownToNearestHour(); |
| | 96 | |
|
| 36 | 97 | | return newDateTimeOffest.Value.AddHours(1); |
| | 98 | | } |
| | 99 | |
|
| | 100 | | internal static DateTimeOffset? RoundDownToNearestYear(this DateTimeOffset? dateTimeOffset) |
| | 101 | | { |
| 112 | 102 | | if (dateTimeOffset == null) |
| | 103 | | { |
| 4 | 104 | | return null; |
| | 105 | | } |
| | 106 | |
|
| 108 | 107 | | return new DateTimeOffset( |
| 108 | 108 | | year: dateTimeOffset.Value.ToUniversalTime().Year, |
| 108 | 109 | | month: 1, |
| 108 | 110 | | day: 1, |
| 108 | 111 | | hour: 0, |
| 108 | 112 | | minute: 0, |
| 108 | 113 | | second: 0, |
| 108 | 114 | | offset: TimeSpan.Zero); |
| | 115 | | } |
| | 116 | |
|
| | 117 | | internal static async Task<Queue<string>> GetSegmentsInYearInternal( |
| | 118 | | BlobContainerClient containerClient, |
| | 119 | | string yearPath, |
| | 120 | | DateTimeOffset? startTime, |
| | 121 | | DateTimeOffset? endTime, |
| | 122 | | bool async, |
| | 123 | | CancellationToken cancellationToken) |
| | 124 | | { |
| 100 | 125 | | List<string> list = new List<string>(); |
| | 126 | |
|
| 100 | 127 | | if (async) |
| | 128 | | { |
| 20008 | 129 | | await foreach (BlobHierarchyItem blobHierarchyItem in containerClient.GetBlobsByHierarchyAsync( |
| 88 | 130 | | prefix: yearPath, |
| 88 | 131 | | cancellationToken: cancellationToken) |
| 88 | 132 | | .ConfigureAwait(false)) |
| | 133 | | { |
| 9916 | 134 | | if (blobHierarchyItem.IsPrefix) |
| | 135 | | continue; |
| | 136 | |
|
| 9916 | 137 | | DateTimeOffset segmentDateTime = ToDateTimeOffset(blobHierarchyItem.Blob.Name).Value; |
| 9916 | 138 | | if (startTime.HasValue && segmentDateTime < startTime |
| 9916 | 139 | | || endTime.HasValue && segmentDateTime > endTime) |
| | 140 | | continue; |
| | 141 | |
|
| 182 | 142 | | list.Add(blobHierarchyItem.Blob.Name); |
| | 143 | | } |
| | 144 | | } |
| | 145 | | else |
| | 146 | | { |
| 112 | 147 | | foreach (BlobHierarchyItem blobHierarchyItem in containerClient.GetBlobsByHierarchy( |
| 12 | 148 | | prefix: yearPath, |
| 12 | 149 | | cancellationToken: cancellationToken)) |
| | 150 | | { |
| 44 | 151 | | if (blobHierarchyItem.IsPrefix) |
| | 152 | | continue; |
| | 153 | |
|
| 44 | 154 | | DateTimeOffset segmentDateTime = ToDateTimeOffset(blobHierarchyItem.Blob.Name).Value; |
| 44 | 155 | | if (startTime.HasValue && segmentDateTime < startTime |
| 44 | 156 | | || endTime.HasValue && segmentDateTime > endTime) |
| | 157 | | continue; |
| | 158 | |
|
| 34 | 159 | | list.Add(blobHierarchyItem.Blob.Name); |
| | 160 | | } |
| | 161 | | } |
| | 162 | |
|
| 100 | 163 | | return new Queue<string>(list); |
| 100 | 164 | | } |
| | 165 | |
|
| | 166 | | internal static DateTimeOffset MinDateTime(DateTimeOffset lastConsumable, DateTimeOffset? endDate) |
| | 167 | | { |
| 92 | 168 | | if (endDate.HasValue && endDate.Value < lastConsumable) |
| | 169 | | { |
| 60 | 170 | | return endDate.Value; |
| | 171 | | } |
| | 172 | |
|
| 32 | 173 | | return lastConsumable; |
| | 174 | | } |
| | 175 | |
|
| | 176 | | internal static string ComputeMD5(string input) |
| | 177 | | { |
| | 178 | | #pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms |
| 192 | 179 | | using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) |
| | 180 | | #pragma warning restore CA5351 // Do Not Use Broken Cryptographic Algorithms |
| | 181 | | { |
| 192 | 182 | | byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input); |
| 192 | 183 | | byte[] hashBytes = md5.ComputeHash(inputBytes); |
| | 184 | |
|
| 192 | 185 | | return Convert.ToBase64String(hashBytes); |
| | 186 | | } |
| 192 | 187 | | } |
| | 188 | | } |
| | 189 | | } |