diff --git a/SabreTools.DatFiles/ItemDictionary.cs b/SabreTools.DatFiles/ItemDictionary.cs index 11a0acfb..29074d3c 100644 --- a/SabreTools.DatFiles/ItemDictionary.cs +++ b/SabreTools.DatFiles/ItemDictionary.cs @@ -21,10 +21,6 @@ namespace SabreTools.DatFiles /// /// Item dictionary with statistics, bucketing, and sorting /// - /// - /// TODO: Make this into a database model instead of just an in-memory object - /// This will help handle extremely large sets - /// [JsonObject("items"), XmlRoot("items")] public class ItemDictionary : IDictionary?> { diff --git a/SabreTools.DatFiles/ItemDictionaryDB.cs b/SabreTools.DatFiles/ItemDictionaryDB.cs index a80fd560..ecf0c465 100644 --- a/SabreTools.DatFiles/ItemDictionaryDB.cs +++ b/SabreTools.DatFiles/ItemDictionaryDB.cs @@ -23,7 +23,6 @@ using SabreTools.Matching; * - Feature parity with all existing item dictionary operations * - A way to transition between the two item dictionaries (a flag?) * - Helper methods that target the "database" version instead of assuming the standard dictionary - * - Automatically add to default buckets based on... [Machine name? Item type?] * * Notable changes include: * - Separation of Machine from DatItem, leading to a mapping instead @@ -119,9 +118,9 @@ namespace SabreTools.DatFiles /// /// Item data to validate /// Index of the machine related to the item - /// True to only add item statistics while parsing, false otherwise + /// True to only add item statistics while parsing, false otherwise (default) /// The index for the added item, -1 on error - public long AddItemAndValidate(DatItem item, long machineIndex, bool statsOnly) + public long AddItemAndValidate(DatItem item, long machineIndex, bool statsOnly = false) { // If we have a Disk, Media, or Rom, clean the hash data if (item is Disk disk) @@ -206,19 +205,6 @@ namespace SabreTools.DatFiles } } - /// - /// Add an item, returning the insert index - /// - public long AddItem(DatItem item, long machineIndex) - { - // TODO: Add to buckets based on current sorting - - _items[_itemIndex++] = item; - _itemToMachineMapping[_itemIndex - 1] = machineIndex; - DatStatistics.AddItemStatistics(item); - return _itemIndex - 1; - } - /// /// Add a machine, returning the insert index /// @@ -379,6 +365,27 @@ namespace SabreTools.DatFiles return true; } + /// + /// Add an item, returning the insert index + /// + private long AddItem(DatItem item, long machineIndex) + { + // Add the item with a new index + _items[_itemIndex++] = item; + + // Add the machine mapping + _itemToMachineMapping[_itemIndex - 1] = machineIndex; + + // Add the item statistics + DatStatistics.AddItemStatistics(item); + + // Add the item to the default bucket + PerformItemBucketing(_itemIndex - 1, _bucketedBy, lower: true, norename: true); + + // Return the used index + return _itemIndex - 1; + } + #endregion #region Bucketing @@ -662,9 +669,7 @@ namespace SabreTools.DatFiles for (int i = 0; i < itemIndicies.Length; i++) #endif { - string? bucketKey = GetBucketKey(i, bucketBy, lower, norename); - EnsureBucketingKey(bucketKey); - _buckets[bucketKey].Add(i); + PerformItemBucketing(i, bucketBy, lower, norename); #if NET40_OR_GREATER || NETCOREAPP }); #else @@ -672,6 +677,20 @@ namespace SabreTools.DatFiles #endif } + /// + /// Bucket a single DatItem + /// + /// Index of the item to bucket + /// ItemKey enum representing how to bucket the individual items + /// True if the key should be lowercased, false otherwise + /// True if games should only be compared on game and file name, false if system and source are counted + private void PerformItemBucketing(long itemIndex, ItemKey bucketBy, bool lower, bool norename) + { + string? bucketKey = GetBucketKey(itemIndex, bucketBy, lower, norename); + EnsureBucketingKey(bucketKey); + _buckets[bucketKey].Add(itemIndex); + } + /// /// Perform deduplication based on the deduplication type provided ///