| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="RecentFilesService.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Concurrent; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.IO; |
| | | 11 | | using System.Linq; |
| | | 12 | | using System.Threading; |
| | | 13 | | using System.Threading.Tasks; |
| | | 14 | | |
| | | 15 | | namespace MyNet.IO.FileHistory; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Service responsible for managing recent files, including adding, retrieving, pinning, and removing entries. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="repository">The repository used for storing and retrieving recent files.</param> |
| | | 21 | | public sealed class RecentFilesService(IRecentFileRepository repository) : IRecentFilesService, IDisposable |
| | | 22 | | { |
| | 21 | 23 | | private readonly ConcurrentDictionary<string, RecentFile> _cache = new(StringComparer.OrdinalIgnoreCase); |
| | 21 | 24 | | private readonly SemaphoreSlim _initializationLock = new(1, 1); |
| | | 25 | | private volatile bool _initialized; |
| | | 26 | | |
| | | 27 | | /// <inheritdoc/> |
| | | 28 | | public IReadOnlyList<RecentFile> GetAll() |
| | | 29 | | { |
| | 0 | 30 | | EnsureInitializedAsync() |
| | 0 | 31 | | .GetAwaiter() |
| | 0 | 32 | | .GetResult(); |
| | | 33 | | |
| | 0 | 34 | | return GetOrderedFiles(); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc/> |
| | | 38 | | public async Task<IReadOnlyList<RecentFile>> GetAllAsync() |
| | | 39 | | { |
| | 9 | 40 | | await EnsureInitializedAsync().ConfigureAwait(false); |
| | | 41 | | |
| | 9 | 42 | | return GetOrderedFiles(); |
| | 9 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc/> |
| | | 46 | | public RecentFile? GetLast() |
| | | 47 | | { |
| | 0 | 48 | | EnsureInitializedAsync() |
| | 0 | 49 | | .GetAwaiter() |
| | 0 | 50 | | .GetResult(); |
| | | 51 | | |
| | 0 | 52 | | return GetLastCore(); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc/> |
| | | 56 | | public async Task<RecentFile?> GetLastAsync() |
| | | 57 | | { |
| | 3 | 58 | | await EnsureInitializedAsync().ConfigureAwait(false); |
| | | 59 | | |
| | 3 | 60 | | return GetLastCore(); |
| | 3 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc/> |
| | 0 | 64 | | public RecentFile? Add(string name, string path, bool isRecovered = false) => AddAsync(name, path, isRecovered) |
| | 0 | 65 | | .GetAwaiter() |
| | 0 | 66 | | .GetResult(); |
| | | 67 | | |
| | | 68 | | /// <inheritdoc/> |
| | | 69 | | public async Task<RecentFile?> AddAsync(string name, string path, bool isRecovered = false) |
| | | 70 | | { |
| | 6 | 71 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | 6 | 72 | | ArgumentException.ThrowIfNullOrWhiteSpace(path); |
| | | 73 | | |
| | 6 | 74 | | await EnsureInitializedAsync().ConfigureAwait(false); |
| | | 75 | | |
| | 6 | 76 | | if (_cache.TryGetValue(path, out var existing)) |
| | | 77 | | { |
| | 3 | 78 | | var refreshed = existing with |
| | 3 | 79 | | { |
| | 3 | 80 | | LastAccessedAt = DateTimeOffset.UtcNow, |
| | 3 | 81 | | LastModifiedAt = GetLastModified(path) |
| | 3 | 82 | | }; |
| | | 83 | | |
| | 3 | 84 | | var updated = await repository |
| | 3 | 85 | | .UpdateAsync(refreshed) |
| | 3 | 86 | | .ConfigureAwait(false); |
| | | 87 | | |
| | 3 | 88 | | if (updated is not null) |
| | 3 | 89 | | _cache[path] = updated; |
| | | 90 | | |
| | 3 | 91 | | return updated; |
| | | 92 | | } |
| | | 93 | | |
| | 3 | 94 | | var recentFile = new RecentFile |
| | 3 | 95 | | { |
| | 3 | 96 | | Name = name, |
| | 3 | 97 | | Path = path, |
| | 3 | 98 | | LastAccessedAt = DateTimeOffset.UtcNow, |
| | 3 | 99 | | LastModifiedAt = GetLastModified(path), |
| | 3 | 100 | | IsRecovered = isRecovered |
| | 3 | 101 | | }; |
| | | 102 | | |
| | 3 | 103 | | var created = await repository |
| | 3 | 104 | | .AddAsync(recentFile) |
| | 3 | 105 | | .ConfigureAwait(false); |
| | | 106 | | |
| | 3 | 107 | | if (created is not null) |
| | 3 | 108 | | _cache[path] = created; |
| | | 109 | | |
| | 3 | 110 | | return created; |
| | 6 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <inheritdoc/> |
| | 0 | 114 | | public bool Remove(string path) => RemoveAsync(path) |
| | 0 | 115 | | .GetAwaiter() |
| | 0 | 116 | | .GetResult(); |
| | | 117 | | |
| | | 118 | | /// <inheritdoc/> |
| | | 119 | | public async Task<bool> RemoveAsync(string path) |
| | | 120 | | { |
| | 3 | 121 | | ArgumentException.ThrowIfNullOrWhiteSpace(path); |
| | | 122 | | |
| | 3 | 123 | | await EnsureInitializedAsync().ConfigureAwait(false); |
| | | 124 | | |
| | 3 | 125 | | var removed = await repository |
| | 3 | 126 | | .RemoveAsync(path) |
| | 3 | 127 | | .ConfigureAwait(false); |
| | | 128 | | |
| | 3 | 129 | | if (removed) |
| | 3 | 130 | | _cache.TryRemove(path, out _); |
| | | 131 | | |
| | 3 | 132 | | return removed; |
| | 3 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <inheritdoc/> |
| | | 136 | | public RecentFile? Pin(string path, bool isPinned) |
| | 0 | 137 | | => PinAsync(path, isPinned) |
| | 0 | 138 | | .GetAwaiter() |
| | 0 | 139 | | .GetResult(); |
| | | 140 | | |
| | | 141 | | /// <inheritdoc/> |
| | | 142 | | public async Task<RecentFile?> PinAsync(string path, bool isPinned) |
| | | 143 | | { |
| | 3 | 144 | | ArgumentException.ThrowIfNullOrWhiteSpace(path); |
| | | 145 | | |
| | 3 | 146 | | await EnsureInitializedAsync().ConfigureAwait(false); |
| | | 147 | | |
| | 3 | 148 | | if (!_cache.TryGetValue(path, out var existing)) |
| | 0 | 149 | | return null; |
| | | 150 | | |
| | 3 | 151 | | var updatedFile = existing with |
| | 3 | 152 | | { |
| | 3 | 153 | | IsPinned = isPinned |
| | 3 | 154 | | }; |
| | | 155 | | |
| | 3 | 156 | | var updated = await repository |
| | 3 | 157 | | .UpdateAsync(updatedFile) |
| | 3 | 158 | | .ConfigureAwait(false); |
| | | 159 | | |
| | 3 | 160 | | if (updated is not null) |
| | 3 | 161 | | _cache[path] = updated; |
| | | 162 | | |
| | 3 | 163 | | return updated; |
| | 3 | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <inheritdoc/> |
| | | 167 | | public bool Contains(string path) |
| | | 168 | | { |
| | 0 | 169 | | EnsureInitializedAsync() |
| | 0 | 170 | | .GetAwaiter() |
| | 0 | 171 | | .GetResult(); |
| | | 172 | | |
| | 0 | 173 | | return _cache.ContainsKey(path); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <inheritdoc/> |
| | | 177 | | public async Task<bool> ContainsAsync(string path) |
| | | 178 | | { |
| | 6 | 179 | | await EnsureInitializedAsync().ConfigureAwait(false); |
| | | 180 | | |
| | 6 | 181 | | return _cache.ContainsKey(path); |
| | 6 | 182 | | } |
| | | 183 | | |
| | | 184 | | /// <inheritdoc/> |
| | 0 | 185 | | public void Clear() => ClearAsync() |
| | 0 | 186 | | .GetAwaiter() |
| | 0 | 187 | | .GetResult(); |
| | | 188 | | |
| | | 189 | | /// <inheritdoc/> |
| | | 190 | | public async Task ClearAsync() |
| | | 191 | | { |
| | 3 | 192 | | await EnsureInitializedAsync().ConfigureAwait(false); |
| | | 193 | | |
| | 18 | 194 | | foreach (var file in _cache.Keys.ToArray()) |
| | 6 | 195 | | await repository.RemoveAsync(file).ConfigureAwait(false); |
| | | 196 | | |
| | 3 | 197 | | _cache.Clear(); |
| | 3 | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Gets the last modified time of the file at the specified path. If the file does not exist, returns null. |
| | | 202 | | /// </summary> |
| | | 203 | | /// <param name="path">The path of the file.</param> |
| | | 204 | | /// <returns>The last modified time of the file, or null if the file does not exist.</returns> |
| | 6 | 205 | | private static DateTimeOffset? GetLastModified(string path) => File.Exists(path) |
| | 6 | 206 | | ? File.GetLastWriteTimeUtc(path) |
| | 6 | 207 | | : null; |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Ensures that the recent files data is loaded into the in-memory cache. This method checks if the cache has alrea |
| | | 211 | | /// </summary> |
| | | 212 | | private async Task EnsureInitializedAsync() |
| | | 213 | | { |
| | 33 | 214 | | if (_initialized) |
| | 12 | 215 | | return; |
| | | 216 | | |
| | 21 | 217 | | await _initializationLock |
| | 21 | 218 | | .WaitAsync() |
| | 21 | 219 | | .ConfigureAwait(false); |
| | | 220 | | |
| | | 221 | | try |
| | | 222 | | { |
| | 21 | 223 | | if (_initialized) |
| | 0 | 224 | | return; |
| | | 225 | | |
| | 21 | 226 | | var files = await repository |
| | 21 | 227 | | .GetAllAsync() |
| | 21 | 228 | | .ConfigureAwait(false); |
| | | 229 | | |
| | 102 | 230 | | foreach (var file in files) |
| | 30 | 231 | | _cache[file.Path] = file; |
| | | 232 | | |
| | 21 | 233 | | _initialized = true; |
| | 21 | 234 | | } |
| | | 235 | | finally |
| | | 236 | | { |
| | 21 | 237 | | _initializationLock.Release(); |
| | | 238 | | } |
| | 33 | 239 | | } |
| | | 240 | | |
| | | 241 | | /// <summary> |
| | | 242 | | /// Retrieves the list of recent files from the in-memory cache, ordered first by pinned status (pinned files appear |
| | | 243 | | /// </summary> |
| | | 244 | | /// <returns>A read-only list of recent files, ordered by pinned status and last accessed time.</returns> |
| | 9 | 245 | | private IReadOnlyList<RecentFile> GetOrderedFiles() => [.. _cache.Values |
| | 9 | 246 | | .OrderByDescending(x => x.IsPinned) |
| | 9 | 247 | | .ThenByDescending(x => x.LastAccessedAt)]; |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// Retrieves the most recently accessed file from the in-memory cache that is not marked as recovered. The method f |
| | | 251 | | /// </summary> |
| | | 252 | | /// <returns>The most recently accessed non-recovered file, or null if none exist.</returns> |
| | 3 | 253 | | private RecentFile? GetLastCore() => _cache.Values |
| | 3 | 254 | | .Where(x => !x.IsRecovered) |
| | 3 | 255 | | .OrderByDescending(x => x.LastAccessedAt) |
| | 3 | 256 | | .FirstOrDefault(); |
| | | 257 | | |
| | | 258 | | /// <summary> |
| | | 259 | | /// Disposes of the resources used by the <see cref="RecentFilesService"/>. This method is responsible for releasing |
| | | 260 | | /// </summary> |
| | 21 | 261 | | public void Dispose() => _initializationLock.Dispose(); |
| | | 262 | | } |
| | | 263 | | |