Add new methods to DB implementation

This commit is contained in:
Matt Nadareski
2025-01-14 21:02:37 -05:00
parent bea43d38f4
commit d52f139e73
3 changed files with 42 additions and 32 deletions

View File

@@ -609,7 +609,7 @@ namespace SabreTools.DatFiles
return;
// Reassign the item to the new machine
ItemsDB._itemToMachineMapping[datItem.Key] = newMachineIndex;
ItemsDB.RemapDatItemToMachine(datItem.Key, newMachineIndex);
// Remove extensions from File and Rom items
if (datItem.Value is DatItems.Formats.File || datItem.Value is Rom)

View File

@@ -395,6 +395,9 @@ namespace SabreTools.DatFiles
items = GetItemsForBucketDB(bucket);
foreach (var item in items)
{
// Get the source for the current item
var source = ItemsDB.GetSourceForItem(item.Key);
// Get the parent items and current machine name
Dictionary<long, DatItem> parentItems = GetItemsForBucketDB(cloneOf);
if (parentItems.Count == 0)
@@ -423,15 +426,15 @@ namespace SabreTools.DatFiles
.Select(i => (i as Disk)!.GetName())
.Contains(mergeTag))
{
ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key;
ItemsDB._buckets[cloneOf!].Add(item.Key);
ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key);
ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
}
// If there is no merge tag, add to parent
else if (mergeTag == null)
{
ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key;
ItemsDB._buckets[cloneOf!].Add(item.Key);
ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key);
ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
}
}
@@ -456,8 +459,8 @@ namespace SabreTools.DatFiles
if (subfolder)
rom.SetName($"{machineName}\\{rom.GetName()}");
ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key;
ItemsDB._buckets[cloneOf!].Add(item.Key);
ItemsDB.RemapDatItemToMachine(item.Key, machineIndex: cloneOfMachine.Key);
ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
}
// If the parent doesn't already contain this item, add to subfolder of parent
@@ -466,8 +469,8 @@ namespace SabreTools.DatFiles
if (subfolder)
rom.SetName($"{machineName}\\{rom.GetName()}");
ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key;
ItemsDB._buckets[cloneOf!].Add(item.Key);
ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key);
ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
}
}
@@ -477,17 +480,13 @@ namespace SabreTools.DatFiles
if (subfolder)
item.Value.SetName($"{machineName}\\{item.Value.GetName()}");
ItemsDB._itemToMachineMapping[item.Key] = cloneOfMachine.Key;
ItemsDB._buckets[cloneOf!].Add(item.Key);
}
ItemsDB.RemapDatItemToMachine(item.Key, cloneOfMachine.Key);
ItemsDB.AddItem(item.Value, cloneOfMachine.Key, source.Key);
}
// Then, remove the old game so it's not picked up by the writer
#if NET40_OR_GREATER || NETCOREAPP
ItemsDB._buckets.TryRemove(bucket, out _);
#else
ItemsDB._buckets.Remove(bucket);
#endif
// Remove the current item
ItemsDB.RemoveItem(item.Key);
}
}
}

View File

@@ -95,12 +95,11 @@ namespace SabreTools.DatFiles
/// <summary>
/// Internal dictionary for item to machine mappings
/// </summary>
/// TODO: Make private when access issues are figured out
[JsonIgnore, XmlIgnore]
#if NET40_OR_GREATER || NETCOREAPP
internal readonly ConcurrentDictionary<long, long> _itemToMachineMapping = [];
private readonly ConcurrentDictionary<long, long> _itemToMachineMapping = [];
#else
internal readonly Dictionary<long, long> _itemToMachineMapping = [];
private readonly Dictionary<long, long> _itemToMachineMapping = [];
#endif
/// <summary>
@@ -116,12 +115,11 @@ namespace SabreTools.DatFiles
/// <summary>
/// Internal dictionary representing the current buckets
/// </summary>
/// TODO: Make private when access issues are figured out
[JsonIgnore, XmlIgnore]
#if NET40_OR_GREATER || NETCOREAPP
internal readonly ConcurrentDictionary<string, List<long>> _buckets = [];
private readonly ConcurrentDictionary<string, List<long>> _buckets = [];
#else
internal readonly Dictionary<string, List<long>> _buckets = [];
private readonly Dictionary<string, List<long>> _buckets = [];
#endif
/// <summary>
@@ -572,6 +570,19 @@ namespace SabreTools.DatFiles
/// </summary>
public IDictionary<long, Source> GetSources() => _sources;
/// <summary>
/// Remap an item to a new machine index without validation
/// </summary>
/// <param name="itemIndex">Current item index</param>
/// <param name="machineIndex">New machine index</param>
public void RemapDatItemToMachine(long itemIndex, long machineIndex)
{
lock (_itemToMachineMapping)
{
_itemToMachineMapping[itemIndex] = machineIndex;
}
}
/// <summary>
/// Remove an item, returning if it could be removed
/// </summary>