From 1bbafa6386ce7b1e8c90a33a2cf25b19cbef5f82 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Wed, 13 Sep 2023 17:57:24 -0500 Subject: [PATCH] refactor!: Remove support for old cache directories Old cache directories previously included the instance name in the hash generation. Code to support migration from this old path has been removed. Relates to #202. --- src/Recyclarr.Cli/Cache/ServiceCache.cs | 10 +-- .../Console/Helpers/CacheStoragePath.cs | 49 +------------ .../Interfaces/ICacheStoragePath.cs | 1 - .../Console/Helpers/CacheStoragePathTest.cs | 68 ------------------- 4 files changed, 3 insertions(+), 125 deletions(-) diff --git a/src/Recyclarr.Cli/Cache/ServiceCache.cs b/src/Recyclarr.Cli/Cache/ServiceCache.cs index aecd8dd0..995ac063 100644 --- a/src/Recyclarr.Cli/Cache/ServiceCache.cs +++ b/src/Recyclarr.Cli/Cache/ServiceCache.cs @@ -24,7 +24,7 @@ public partial class ServiceCache : IServiceCache public T? Load(IServiceConfiguration config) where T : class { - var path = PathFromAttribute(config, true); + var path = PathFromAttribute(config); _log.Debug("Loading cache from path: {Path}", path.FullName); if (!path.Exists) { @@ -70,7 +70,7 @@ public partial class ServiceCache : IServiceCache return attribute.Name; } - private IFileInfo PathFromAttribute(IServiceConfiguration config, bool migratePath = false) + private IFileInfo PathFromAttribute(IServiceConfiguration config) { var objectName = GetCacheObjectNameAttribute(); if (!AllowedObjectNameCharactersRegex().IsMatch(objectName)) @@ -78,12 +78,6 @@ public partial class ServiceCache : IServiceCache throw new ArgumentException($"Object name '{objectName}' has unacceptable characters"); } - if (migratePath) - { - // Only do this while loading the cache. Saving should always use the direct (latest) path. - _storagePath.MigrateOldPath(config, objectName); - } - return _storagePath.CalculatePath(config, objectName); } diff --git a/src/Recyclarr.Cli/Console/Helpers/CacheStoragePath.cs b/src/Recyclarr.Cli/Console/Helpers/CacheStoragePath.cs index 971a853a..a35a184e 100644 --- a/src/Recyclarr.Cli/Console/Helpers/CacheStoragePath.cs +++ b/src/Recyclarr.Cli/Console/Helpers/CacheStoragePath.cs @@ -10,16 +10,12 @@ namespace Recyclarr.Cli.Console.Helpers; public class CacheStoragePath : ICacheStoragePath { - private readonly ILogger _log; private readonly IAppPaths _paths; - private readonly IFNV1a _hashOld; private readonly IFNV1a _hash; - public CacheStoragePath(ILogger log, IAppPaths paths) + public CacheStoragePath(IAppPaths paths) { - _log = log; _paths = paths; - _hashOld = FNV1aFactory.Instance.Create(FNVConfig.GetPredefinedConfig(32)); _hash = FNV1aFactory.Instance.Create(FNVConfig.GetPredefinedConfig(64)); } @@ -29,13 +25,6 @@ public class CacheStoragePath : ICacheStoragePath return _hash.ComputeHash(Encoding.ASCII.GetBytes(url)).AsHexString(); } - private string BuildOldUniqueServiceDir(IServiceConfiguration config) - { - var url = config.BaseUrl.OriginalString; - var hash = _hashOld.ComputeHash(Encoding.ASCII.GetBytes(url)).AsHexString(); - return $"{config.InstanceName}_{hash}"; - } - private IFileInfo CalculatePathInternal(IServiceConfiguration config, string cacheObjectName, string serviceDir) { return _paths.CacheDirectory @@ -48,40 +37,4 @@ public class CacheStoragePath : ICacheStoragePath { return CalculatePathInternal(config, cacheObjectName, BuildUniqueServiceDir(config)); } - - public IFileInfo CalculateOldPath(IServiceConfiguration config, string cacheObjectName) - { - return CalculatePathInternal(config, cacheObjectName, BuildOldUniqueServiceDir(config)); - } - - public void MigrateOldPath(IServiceConfiguration config, string cacheObjectName) - { - var oldServiceDir = CalculateOldPath(config, cacheObjectName).Directory; - var newServiceDir = CalculatePath(config, cacheObjectName).Directory; - - if (oldServiceDir is null || newServiceDir is null) - { - _log.Debug("Cache Migration: Unable to migrate cache dir due to null value for either old or new path"); - return; - } - - if (!oldServiceDir.Exists) - { - _log.Debug("Cache Migration: Old path doesn't exist; skipping"); - return; - } - - if (newServiceDir.Exists) - { - // New dir already exists, so we can't move. Delete it. - _log.Debug("Cache Migration: Deleting {OldDir}", oldServiceDir); - oldServiceDir.Delete(true); - } - else - { - // New dir doesn't exist yet; so rename old to new. - _log.Debug("Cache Migration: Moving from {OldDir} to {NewDir}", oldServiceDir, newServiceDir); - oldServiceDir.MoveTo(newServiceDir.FullName); - } - } } diff --git a/src/Recyclarr.TrashLib/Interfaces/ICacheStoragePath.cs b/src/Recyclarr.TrashLib/Interfaces/ICacheStoragePath.cs index 95f300d8..2e5592f6 100644 --- a/src/Recyclarr.TrashLib/Interfaces/ICacheStoragePath.cs +++ b/src/Recyclarr.TrashLib/Interfaces/ICacheStoragePath.cs @@ -6,5 +6,4 @@ namespace Recyclarr.TrashLib.Interfaces; public interface ICacheStoragePath { IFileInfo CalculatePath(IServiceConfiguration config, string cacheObjectName); - void MigrateOldPath(IServiceConfiguration config, string cacheObjectName); } diff --git a/src/tests/Recyclarr.Cli.Tests/Console/Helpers/CacheStoragePathTest.cs b/src/tests/Recyclarr.Cli.Tests/Console/Helpers/CacheStoragePathTest.cs index df81aa21..e4d100e1 100644 --- a/src/tests/Recyclarr.Cli.Tests/Console/Helpers/CacheStoragePathTest.cs +++ b/src/tests/Recyclarr.Cli.Tests/Console/Helpers/CacheStoragePathTest.cs @@ -20,72 +20,4 @@ public class CacheStoragePathTest result.FullName.Should().MatchRegex(@".*[/\\][a-f0-9]+[/\\]obj\.json$"); } - - [Test, AutoMockData] - public void Migration_old_path_moved_to_new_path( - [Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs, - CacheStoragePath sut) - { - var config = new SonarrConfiguration - { - BaseUrl = new Uri("http://something"), - InstanceName = "thename" - }; - - var oldPath = sut.CalculateOldPath(config, "obj"); - var newPath = sut.CalculatePath(config, "obj"); - - fs.AddEmptyFile(oldPath); - - sut.MigrateOldPath(config, "obj"); - - fs.AllFiles.Should().Contain(newPath.FullName); - fs.AllFiles.Should().NotContain(oldPath.FullName); - } - - [Test, AutoMockData] - public void Migration_old_path_deleted_when_new_path_already_exists( - [Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs, - CacheStoragePath sut) - { - var config = new SonarrConfiguration - { - BaseUrl = new Uri("http://something"), - InstanceName = "thename" - }; - - var oldPath = sut.CalculateOldPath(config, "obj"); - var newPath = sut.CalculatePath(config, "obj"); - - fs.AddEmptyFile(oldPath); - fs.AddFile(newPath, new MockFileData("something")); - - sut.MigrateOldPath(config, "obj"); - - fs.AllFiles.Should().NotContain(oldPath.FullName); - - var file = fs.GetFile(newPath); - file.Should().NotBeNull(); - file.TextContents.Should().Be("something"); - } - - [Test, AutoMockData] - public void Migration_nothing_moved_if_old_path_not_exist( - [Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs, - CacheStoragePath sut) - { - var config = new SonarrConfiguration - { - BaseUrl = new Uri("http://something"), - InstanceName = "thename" - }; - - var oldPath = sut.CalculateOldPath(config, "obj"); - var newPath = sut.CalculatePath(config, "obj"); - - sut.MigrateOldPath(config, "obj"); - - fs.AllFiles.Should().NotContain(oldPath.FullName); - fs.AllFiles.Should().NotContain(newPath.FullName); - } }