Skip to content

Commit

Permalink
[Bugfix] A smartlist could be set as the source of another list refer…
Browse files Browse the repository at this point in the history
…encing itself.

Happens when changing source, if the previous one was already cached. Will remove all reference to a list in the recursion cache when changing the source.
  • Loading branch information
maforget committed Jul 12, 2024
1 parent 8295041 commit 704da79
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 39 deletions.
1 change: 1 addition & 0 deletions ComicRack.Engine/ComicRack.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="BookPageRetrievalCompletedEventArgs.cs" />
<Compile Include="CacheManager.cs" />
<Compile Include="ComicBook.cs" />
<Compile Include="Database\RecursionCache.cs" />
<Compile Include="IO\Provider\Pdfium.cs" />
<Compile Include="IO\Provider\Readers\Pdf\PdfiumReaderEngine.cs" />
<Compile Include="Metadata\ComicBook\Comparer\ComicBookAddedComparer.cs" />
Expand Down
37 changes: 0 additions & 37 deletions ComicRack.Engine/Database/ComicListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -816,41 +816,4 @@ public ComicBookSeriesStatistics GetSeriesStats(ComicBook book)
}
}
}

public class RecursionCache: Dictionary<Guid, RecursionCacheItem>
{
private static Lazy<RecursionCache> instance = new Lazy<RecursionCache>(() => new RecursionCache());

public static RecursionCache Items => instance.Value;

private RecursionCache() : base()
{

}

public RecursionCacheItem GetValue(Guid guid)
{
if (Items.TryGetValue(guid, out RecursionCacheItem cachedResult))
{
return cachedResult;
}

RecursionCacheItem item = RecursionCacheItem.Empty();
this[guid] = item;
return item;
}
}

public class RecursionCacheItem : Dictionary<Guid, bool>
{
private RecursionCacheItem() : base()
{

}

public static RecursionCacheItem Empty()
{
return new RecursionCacheItem();
}
}
}
52 changes: 52 additions & 0 deletions ComicRack.Engine/Database/RecursionCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using cYo.Common.Collections;
using System;
using System.Collections.Generic;
using System.Linq;

namespace cYo.Projects.ComicRack.Engine.Database
{
public class RecursionCache: Dictionary<Guid, RecursionCacheItem>
{
private static Lazy<RecursionCache> instance = new Lazy<RecursionCache>(() => new RecursionCache());

public static RecursionCache Items => instance.Value;

private RecursionCache() : base()
{

}

public RecursionCacheItem GetValue(Guid guid)
{
if (Items.TryGetValue(guid, out RecursionCacheItem cachedResult))
{
return cachedResult;
}

RecursionCacheItem item = RecursionCacheItem.Empty();
this[guid] = item;
return item;
}

public void RemoveReference(Guid guid)
{
//Remove all the cache for this list
Items.Remove(guid);
//And all it's reference
Items.Where(cacheItem => cacheItem.Value.ContainsKey(guid)).SafeForEach(x => x.Value.Remove(guid));
}
}

public class RecursionCacheItem : Dictionary<Guid, bool>
{
private RecursionCacheItem() : base()
{

}

public static RecursionCacheItem Empty()
{
return new RecursionCacheItem();
}
}
}
2 changes: 1 addition & 1 deletion ComicRack/ComicRack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ del /Q *.pdb
<PreBuildEvent>
</PreBuildEvent>
</PropertyGroup>
<Target Name = "Clean">
<Target Name="Clean">
<RemoveDir Directories="$(TargetDir)\Resources\Icons" />
</Target>
<Target Name="AfterBuild">
Expand Down
6 changes: 5 additions & 1 deletion ComicRack/Views/ComicListLibraryBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@ private bool EditSmartListItem(TreeNode tn, ComicSmartListItem csli)
sld.Apply += delegate
{
csli.SetList(sld.SmartComicList);

//Remove recursionCache if we changed the source
if (csli.BaseListId != oldList.BaseListId)
RecursionCache.Items.RemoveReference(csli.Id);
};
sld.Next += delegate
{
Expand Down Expand Up @@ -1110,7 +1114,7 @@ private void tvQueries_DragDrop(object sender, DragEventArgs e)
}
ComicListItemCollection comicListItemCollection = ((dragNode.Parent == null) ? Library.ComicLists : ((ComicListItemFolder)dragNode.Parent.Tag).Items);
ComicListItem comicListItem = dragNode.Tag as ComicListItem;
RecursionCache.Items.Remove(comicListItem.Id);
RecursionCache.Items.RemoveReference(comicListItem.Id);
if (e.Effect == DragDropEffects.Copy && comicListItem is ShareableComicListItem)
{
comicListItem = ((ICloneable)(ShareableComicListItem)comicListItem).Clone<ShareableComicListItem>();
Expand Down

0 comments on commit 704da79

Please sign in to comment.