|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Newtonsoft.Json; |
| 12 | +using NuGet.Services.Metadata.Catalog.Persistence; |
| 13 | + |
| 14 | +namespace NuGet.Services.Metadata.Catalog |
| 15 | +{ |
| 16 | + public class DurableCursorWithUpdates : DurableCursor |
| 17 | + { |
| 18 | + private readonly ILogger _logger; |
| 19 | + |
| 20 | + private readonly int _maxNumberOfUpdatesToKeep; |
| 21 | + private readonly TimeSpan _minIntervalBetweenTwoUpdates; |
| 22 | + |
| 23 | + public DurableCursorWithUpdates(Uri address, Persistence.Storage storage, DateTime defaultValue, ILogger logger, |
| 24 | + int maxNumberOfUpdatesToKeep, TimeSpan minIntervalBetweenTwoUpdates) |
| 25 | + : base(address, storage, defaultValue) |
| 26 | + { |
| 27 | + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 28 | + |
| 29 | + if (maxNumberOfUpdatesToKeep < 0) |
| 30 | + { |
| 31 | + throw new ArgumentOutOfRangeException(nameof(maxNumberOfUpdatesToKeep), $"{nameof(maxNumberOfUpdatesToKeep)} must be equal or larger than 0."); |
| 32 | + } |
| 33 | + |
| 34 | + if (minIntervalBetweenTwoUpdates < TimeSpan.Zero) |
| 35 | + { |
| 36 | + throw new ArgumentOutOfRangeException(nameof(minIntervalBetweenTwoUpdates), $"{nameof(minIntervalBetweenTwoUpdates)} must be equal or larger than 0."); |
| 37 | + } |
| 38 | + |
| 39 | + _maxNumberOfUpdatesToKeep = maxNumberOfUpdatesToKeep; |
| 40 | + _minIntervalBetweenTwoUpdates = minIntervalBetweenTwoUpdates; |
| 41 | + } |
| 42 | + |
| 43 | + public override async Task SaveAsync(CancellationToken cancellationToken) |
| 44 | + { |
| 45 | + var cursorValueWithUpdates = new CursorValueWithUpdates(); |
| 46 | + |
| 47 | + var storageContent = await _storage.LoadStringStorageContentAsync(_address, cancellationToken); |
| 48 | + if (storageContent != null && storageContent.Content != null) |
| 49 | + { |
| 50 | + cursorValueWithUpdates = JsonConvert.DeserializeObject<CursorValueWithUpdates>(storageContent.Content, CursorValueWithUpdates.SerializerSettings); |
| 51 | + |
| 52 | + _logger.LogInformation("Read the cursor value: {CursorValue} with the count of updates: {CursorUpdatesCount}, at {Address}.", |
| 53 | + cursorValueWithUpdates.Value, cursorValueWithUpdates.Updates.Count, _address.AbsoluteUri); |
| 54 | + } |
| 55 | + |
| 56 | + cursorValueWithUpdates.Value = Value.ToString("O"); |
| 57 | + if (storageContent != null) |
| 58 | + { |
| 59 | + cursorValueWithUpdates.Updates = GetUpdates(cursorValueWithUpdates, storageContent.StorageDateTimeInUtc); |
| 60 | + } |
| 61 | + |
| 62 | + var content = new StringStorageContent(JsonConvert.SerializeObject(cursorValueWithUpdates, CursorValueWithUpdates.SerializerSettings), |
| 63 | + "application/json", Constants.NoStoreCacheControl); |
| 64 | + |
| 65 | + await _storage.SaveAsync(_address, content, cancellationToken); |
| 66 | + |
| 67 | + _logger.LogInformation("Updated the cursor value: {CursorValue} with the count of updates: {CursorUpdatesCount}, at {Address}.", |
| 68 | + cursorValueWithUpdates.Value, cursorValueWithUpdates.Updates.Count, _address.AbsoluteUri); |
| 69 | + } |
| 70 | + |
| 71 | + private IList<CursorValueUpdate> GetUpdates(CursorValueWithUpdates cursorValueWithUpdates, DateTime? storageDateTimeInUtc) |
| 72 | + { |
| 73 | + if (!storageDateTimeInUtc.HasValue) |
| 74 | + { |
| 75 | + throw new ArgumentNullException(nameof(storageDateTimeInUtc)); |
| 76 | + } |
| 77 | + |
| 78 | + var updates = cursorValueWithUpdates.Updates.OrderByDescending(u => u.TimeStamp).ToList(); |
| 79 | + if (updates.Count == 0 || (updates.Count > 0 && (storageDateTimeInUtc.Value - updates.First().TimeStamp >= _minIntervalBetweenTwoUpdates))) |
| 80 | + { |
| 81 | + var update = new CursorValueUpdate(storageDateTimeInUtc.Value, cursorValueWithUpdates.Value); |
| 82 | + updates.Insert(0, update); |
| 83 | + |
| 84 | + _logger.LogInformation("Added the cursor update with timeStamp: {TimeStamp} and value: {UpdateValue}.", |
| 85 | + update.TimeStamp.ToString(CursorValueWithUpdates.SerializerSettings.DateFormatString), update.Value); |
| 86 | + } |
| 87 | + |
| 88 | + while (updates.Count > 0 && updates.Count > _maxNumberOfUpdatesToKeep) |
| 89 | + { |
| 90 | + var update = updates[updates.Count - 1]; |
| 91 | + |
| 92 | + _logger.LogInformation("Deleted the cursor update with timeStamp: {TimeStamp} and value: {UpdateValue}.", |
| 93 | + update.TimeStamp.ToString(CursorValueWithUpdates.SerializerSettings.DateFormatString), update.Value); |
| 94 | + |
| 95 | + updates.RemoveAt(updates.Count - 1); |
| 96 | + } |
| 97 | + |
| 98 | + return updates; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments