Skip to content

Commit

Permalink
SongCategory Enhancements And Refactorings To Static Functions (YARC-…
Browse files Browse the repository at this point in the history
…Official#89)

* Remove InstrumentCategory multithreading variable

+ Defaults to Parallel.ForEach

* Hold instrument string in InstrumentComparer

* Remove SongCategory GetEnumerator()

+ Swap _elements to protected

* CacheHandler SortCategories() dedupe

* Redo Instrument Category to not be a subtype of SongCategory

Alters SongCache to directly hold the dictionaries of elements sorted by instrument.

+ Requires main repo update

* Refactor main SongCategory types into static functions

Uses an interface pattern to create a generic static class that takes the dictionary as a parameter instead of storing one.  Allows explicitly limiting configurations of certain attributes without relying on a Debug.Assert.

+ Requires main repo update.

* Capitalize SongCache variables

* Use for loop vs explicit InstrumentCategory[] generation
  • Loading branch information
sonicfind authored Oct 8, 2023
1 parent 0f9adda commit 914fd52
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 248 deletions.
16 changes: 8 additions & 8 deletions YARG.Core/Song/Cache/CacheHandler.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ private void Serialize()

writer.Write(CACHE_VERSION);

cache.titles.WriteToCache(writer, ref nodes);
cache.artists.WriteToCache(writer, ref nodes);
cache.albums.WriteToCache(writer, ref nodes);
cache.genres.WriteToCache(writer, ref nodes);
cache.years.WriteToCache(writer, ref nodes);
cache.charters.WriteToCache(writer, ref nodes);
cache.playlists.WriteToCache(writer, ref nodes);
cache.sources.WriteToCache(writer, ref nodes);
CategoryWriter.WriteToCache(writer, cache.Titles , SongAttribute.Name, ref nodes);
CategoryWriter.WriteToCache(writer, cache.Artists , SongAttribute.Artist, ref nodes);
CategoryWriter.WriteToCache(writer, cache.Albums , SongAttribute.Album, ref nodes);
CategoryWriter.WriteToCache(writer, cache.Genres , SongAttribute.Genre, ref nodes);
CategoryWriter.WriteToCache(writer, cache.Years , SongAttribute.Year, ref nodes);
CategoryWriter.WriteToCache(writer, cache.Charters , SongAttribute.Charter, ref nodes);
CategoryWriter.WriteToCache(writer, cache.Playlists, SongAttribute.Playlist, ref nodes);
CategoryWriter.WriteToCache(writer, cache.Sources , SongAttribute.Source, ref nodes);

List<KeyValuePair<string, PackedCONGroup>> upgradeCons = new();
List<KeyValuePair<string, PackedCONGroup>> entryCons = new();
Expand Down
73 changes: 34 additions & 39 deletions YARG.Core/Song/Cache/CacheHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public CacheHandler(string cacheLocation, string badSongsLocation, bool multithr
iniGroups = new(baseDirectories.Length);
for (int i = 0; i < baseDirectories.Length; ++i)
iniGroups.Add(baseDirectories[i], new());
cache = new(multithreading);
}

public SongCache RunScan(bool fast)
Expand Down Expand Up @@ -66,7 +65,7 @@ public SongCache RunScan(bool fast)
static CacheHandler() { }


private readonly SongCache cache;
private readonly SongCache cache = new();
private int _count;

private readonly LockedCacheDictionary<UpdateGroup> updateGroups = new();
Expand Down Expand Up @@ -161,47 +160,43 @@ private void FullScan(bool loadCache)

private void SortCategories()
{
Progress = ScanProgress.Sorting;
if (multithreading)
var enums = (Instrument[])Enum.GetValues(typeof(Instrument));
var instruments = new InstrumentCategory[enums.Length];
for (int i = 0; i < instruments.Length; ++i)
instruments[i] = new InstrumentCategory(enums[i]);

void SortEntries(List<SongMetadata> entries)
{
Parallel.ForEach(cache.entries, entryList =>
foreach (var entry in entries)
{
foreach (var entry in entryList.Value)
{
cache.titles.Add(entry);
cache.artists.Add(entry);
cache.albums.Add(entry);
cache.genres.Add(entry);
cache.years.Add(entry);
cache.charters.Add(entry);
cache.playlists.Add(entry);
cache.sources.Add(entry);
cache.artistAlbums.Add(entry);
cache.songLengths.Add(entry);
cache.instruments.Add(entry);
}
});
CategorySorter<string, TitleConfig>. Add(entry, cache.Titles);
CategorySorter<SortString, ArtistConfig>. Add(entry, cache.Artists);
CategorySorter<SortString, AlbumConfig>. Add(entry, cache.Albums);
CategorySorter<SortString, GenreConfig>. Add(entry, cache.Genres);
CategorySorter<string, YearConfig>. Add(entry, cache.Years);
CategorySorter<SortString, CharterConfig>. Add(entry, cache.Charters);
CategorySorter<SortString, PlaylistConfig>. Add(entry, cache.Playlists);
CategorySorter<SortString, SourceConfig>. Add(entry, cache.Sources);
CategorySorter<string, ArtistAlbumConfig>.Add(entry, cache.ArtistAlbums);
CategorySorter<string, SongLengthConfig>. Add(entry, cache.SongLengths);

foreach (var instrument in instruments)
instrument.Add(entry);
}
}

Progress = ScanProgress.Sorting;
if (multithreading)
Parallel.ForEach(cache.Entries, node => SortEntries(node.Value));
else
{
foreach (var entryList in cache.entries)
{
foreach (var entry in entryList.Value)
{
cache.titles.Add(entry);
cache.artists.Add(entry);
cache.albums.Add(entry);
cache.genres.Add(entry);
cache.years.Add(entry);
cache.charters.Add(entry);
cache.playlists.Add(entry);
cache.sources.Add(entry);
cache.artistAlbums.Add(entry);
cache.songLengths.Add(entry);
cache.instruments.Add(entry);
}
}
foreach (var node in cache.Entries)
SortEntries(node.Value);
}

foreach (var instrument in instruments)
if (instrument.Entries.Count > 0)
cache.Instruments.Add(instrument.Key, instrument.Entries);
}

private void WriteBadSongs()
Expand Down Expand Up @@ -340,10 +335,10 @@ private bool AddEntry(SongMetadata entry)
var hash = entry.Hash;
lock (entryLock)
{
if (cache.entries.TryGetValue(hash, out var list))
if (cache.Entries.TryGetValue(hash, out var list))
list.Add(entry);
else
cache.entries.Add(hash, new() { entry });
cache.Entries.Add(hash, new() { entry });
++_count;
}
return true;
Expand Down
31 changes: 12 additions & 19 deletions YARG.Core/Song/Cache/SongCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,21 @@ namespace YARG.Core.Song.Cache
public sealed class SongCache
{
[NonSerialized]
public readonly Dictionary<HashWrapper, List<SongMetadata>> entries = new();

public readonly TitleCategory titles = new();
public readonly YearCategory years = new();
public readonly Dictionary<HashWrapper, List<SongMetadata>> Entries = new();

[NonSerialized]
public readonly ArtistAlbumCategory artistAlbums = new();
public readonly SortedDictionary<string, List<SongMetadata>> ArtistAlbums = new();
[NonSerialized]
public readonly SongLengthCategory songLengths = new();
public readonly SortedDictionary<string, List<SongMetadata>> SongLengths = new();
[NonSerialized]
public readonly InstrumentCategory instruments;

public readonly NormalCategory artists = new(SongAttribute.Artist);
public readonly NormalCategory albums = new(SongAttribute.Album);
public readonly NormalCategory genres = new(SongAttribute.Genre);
public readonly NormalCategory charters = new(SongAttribute.Charter);
public readonly NormalCategory playlists = new(SongAttribute.Playlist);
public readonly NormalCategory sources = new(SongAttribute.Source);

public SongCache(bool multithreading)
{
instruments = new(multithreading);
}
public readonly SortedDictionary<string, List<SongMetadata>> Instruments = new();
public readonly SortedDictionary<string, List<SongMetadata>> Titles = new();
public readonly SortedDictionary<string, List<SongMetadata>> Years = new();
public readonly SortedDictionary<SortString, List<SongMetadata>> Artists = new();
public readonly SortedDictionary<SortString, List<SongMetadata>> Albums = new();
public readonly SortedDictionary<SortString, List<SongMetadata>> Genres = new();
public readonly SortedDictionary<SortString, List<SongMetadata>> Charters = new();
public readonly SortedDictionary<SortString, List<SongMetadata>> Playlists = new();
public readonly SortedDictionary<SortString, List<SongMetadata>> Sources = new();
}
}
Loading

0 comments on commit 914fd52

Please sign in to comment.