Move bucketing away from Field

This commit is contained in:
Matt Nadareski
2020-12-14 15:31:28 -08:00
parent 7f7c3ec314
commit 52d1673cb8
8 changed files with 63 additions and 63 deletions

View File

@@ -277,7 +277,7 @@ namespace SabreTools.DatFiles
} }
// Get the key and add the file // Get the key and add the file
key = item.GetKey(Field.Machine_Name); key = item.GetKey(ItemKey.Machine);
Items.Add(key, item); Items.Add(key, item);
return key; return key;

View File

@@ -24,7 +24,7 @@ namespace SabreTools.DatFiles
/// <summary> /// <summary>
/// Determine the bucketing key for all items /// Determine the bucketing key for all items
/// </summary> /// </summary>
private Field bucketedBy; private ItemKey bucketedBy;
/// <summary> /// <summary>
/// Determine merging type for all items /// Determine merging type for all items
@@ -616,10 +616,10 @@ namespace SabreTools.DatFiles
} }
/// <summary> /// <summary>
/// Override the internal Field value /// Override the internal ItemKey value
/// </summary> /// </summary>
/// <param name="newBucket"></param> /// <param name="newBucket"></param>
public void SetBucketedBy(Field newBucket) public void SetBucketedBy(ItemKey newBucket)
{ {
bucketedBy = newBucket; bucketedBy = newBucket;
} }
@@ -930,7 +930,7 @@ namespace SabreTools.DatFiles
/// </summary> /// </summary>
public ItemDictionary() public ItemDictionary()
{ {
bucketedBy = Field.NULL; bucketedBy = ItemKey.NULL;
mergedBy = DedupeType.None; mergedBy = DedupeType.None;
items = new ConcurrentDictionary<string, List<DatItem>>(); items = new ConcurrentDictionary<string, List<DatItem>>();
logger = new Logger(this); logger = new Logger(this);
@@ -943,11 +943,11 @@ namespace SabreTools.DatFiles
/// <summary> /// <summary>
/// Take the arbitrarily bucketed Files Dictionary and convert to one bucketed by a user-defined method /// Take the arbitrarily bucketed Files Dictionary and convert to one bucketed by a user-defined method
/// </summary> /// </summary>
/// <param name="bucketBy">Field enum representing how to bucket the individual items</param> /// <param name="bucketBy">ItemKey enum representing how to bucket the individual items</param>
/// <param name="dedupeType">Dedupe type that should be used</param> /// <param name="dedupeType">Dedupe type that should be used</param>
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param> /// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param> /// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
public void BucketBy(Field bucketBy, DedupeType dedupeType, bool lower = true, bool norename = true) public void BucketBy(ItemKey bucketBy, DedupeType dedupeType, bool lower = true, bool norename = true)
{ {
// If we have a situation where there's no dictionary or no keys at all, we skip // If we have a situation where there's no dictionary or no keys at all, we skip
if (items == null || items.Count == 0) if (items == null || items.Count == 0)
@@ -1013,7 +1013,7 @@ namespace SabreTools.DatFiles
DatItem.Sort(ref sortedlist, false); DatItem.Sort(ref sortedlist, false);
// If we're merging the roms, do so // If we're merging the roms, do so
if (dedupeType == DedupeType.Full || (dedupeType == DedupeType.Game && bucketBy == Field.Machine_Name)) if (dedupeType == DedupeType.Full || (dedupeType == DedupeType.Game && bucketBy == ItemKey.Machine))
sortedlist = DatItem.Merge(sortedlist); sortedlist = DatItem.Merge(sortedlist);
// Add the list back to the dictionary // Add the list back to the dictionary
@@ -1208,31 +1208,31 @@ namespace SabreTools.DatFiles
/// <summary> /// <summary>
/// Get the highest-order Field value that represents the statistics /// Get the highest-order Field value that represents the statistics
/// </summary> /// </summary>
private Field GetBestAvailable() private ItemKey GetBestAvailable()
{ {
// If all items are supposed to have a SHA-512, we bucket by that // If all items are supposed to have a SHA-512, we bucket by that
if (DiskCount + MediaCount + RomCount - NodumpCount == SHA512Count) if (DiskCount + MediaCount + RomCount - NodumpCount == SHA512Count)
return Field.DatItem_SHA512; return ItemKey.SHA512;
// If all items are supposed to have a SHA-384, we bucket by that // If all items are supposed to have a SHA-384, we bucket by that
else if (DiskCount + MediaCount + RomCount - NodumpCount == SHA384Count) else if (DiskCount + MediaCount + RomCount - NodumpCount == SHA384Count)
return Field.DatItem_SHA384; return ItemKey.SHA384;
// If all items are supposed to have a SHA-256, we bucket by that // If all items are supposed to have a SHA-256, we bucket by that
else if (DiskCount + MediaCount + RomCount - NodumpCount == SHA256Count) else if (DiskCount + MediaCount + RomCount - NodumpCount == SHA256Count)
return Field.DatItem_SHA256; return ItemKey.SHA256;
// If all items are supposed to have a SHA-1, we bucket by that // If all items are supposed to have a SHA-1, we bucket by that
else if (DiskCount + MediaCount + RomCount - NodumpCount == SHA1Count) else if (DiskCount + MediaCount + RomCount - NodumpCount == SHA1Count)
return Field.DatItem_SHA1; return ItemKey.SHA1;
// If all items are supposed to have a MD5, we bucket by that // If all items are supposed to have a MD5, we bucket by that
else if (DiskCount + MediaCount + RomCount - NodumpCount == MD5Count) else if (DiskCount + MediaCount + RomCount - NodumpCount == MD5Count)
return Field.DatItem_MD5; return ItemKey.MD5;
// Otherwise, we bucket by CRC // Otherwise, we bucket by CRC
else else
return Field.DatItem_CRC; return ItemKey.CRC;
} }
/// <summary> /// <summary>

View File

@@ -429,12 +429,12 @@ namespace SabreTools.DatItems
/// <summary> /// <summary>
/// Get the dictionary key that should be used for a given item and bucketing type /// Get the dictionary key that should be used for a given item and bucketing type
/// </summary> /// </summary>
/// <param name="bucketedBy">Field value representing what key to get</param> /// <param name="bucketedBy">ItemKey value representing what key to get</param>
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param> /// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param> /// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
/// <returns>String representing the key to be used for the DatItem</returns> /// <returns>String representing the key to be used for the DatItem</returns>
/// TODO: What other fields can we reasonably allow bucketing on? /// TODO: What other fields can we reasonably allow bucketing on?
public virtual string GetKey(Field bucketedBy, bool lower = true, bool norename = true) public virtual string GetKey(ItemKey bucketedBy, bool lower = true, bool norename = true)
{ {
// Set the output key as the default blank string // Set the output key as the default blank string
string key = string.Empty; string key = string.Empty;
@@ -442,11 +442,11 @@ namespace SabreTools.DatItems
// Now determine what the key should be based on the bucketedBy value // Now determine what the key should be based on the bucketedBy value
switch (bucketedBy) switch (bucketedBy)
{ {
case Field.DatItem_CRC: case ItemKey.CRC:
key = Constants.CRCZero; key = Constants.CRCZero;
break; break;
case Field.Machine_Name: case ItemKey.Machine:
key = (norename ? string.Empty key = (norename ? string.Empty
: Source.Index.ToString().PadLeft(10, '0') : Source.Index.ToString().PadLeft(10, '0')
+ "-") + "-")
@@ -461,27 +461,27 @@ namespace SabreTools.DatItems
break; break;
case Field.DatItem_MD5: case ItemKey.MD5:
key = Constants.MD5Zero; key = Constants.MD5Zero;
break; break;
case Field.DatItem_SHA1: case ItemKey.SHA1:
key = Constants.SHA1Zero; key = Constants.SHA1Zero;
break; break;
case Field.DatItem_SHA256: case ItemKey.SHA256:
key = Constants.SHA256Zero; key = Constants.SHA256Zero;
break; break;
case Field.DatItem_SHA384: case ItemKey.SHA384:
key = Constants.SHA384Zero; key = Constants.SHA384Zero;
break; break;
case Field.DatItem_SHA512: case ItemKey.SHA512:
key = Constants.SHA512Zero; key = Constants.SHA512Zero;
break; break;
case Field.DatItem_SpamSum: case ItemKey.SpamSum:
key = Constants.SpamSumZero; key = Constants.SpamSumZero;
break; break;
} }

View File

@@ -366,14 +366,8 @@ namespace SabreTools.DatItems
#region Sorting and Merging #region Sorting and Merging
/// <summary> /// <inheritdoc/>
/// Get the dictionary key that should be used for a given item and bucketing type public override string GetKey(ItemKey bucketedBy, bool lower = true, bool norename = true)
/// </summary>
/// <param name="bucketedBy">Field enum representing what key to get</param>
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
/// <returns>String representing the key to be used for the DatItem</returns>
public override string GetKey(Field bucketedBy, bool lower = true, bool norename = true)
{ {
// Set the output key as the default blank string // Set the output key as the default blank string
string key = string.Empty; string key = string.Empty;
@@ -381,11 +375,11 @@ namespace SabreTools.DatItems
// Now determine what the key should be based on the bucketedBy value // Now determine what the key should be based on the bucketedBy value
switch (bucketedBy) switch (bucketedBy)
{ {
case Field.DatItem_MD5: case ItemKey.MD5:
key = MD5; key = MD5;
break; break;
case Field.DatItem_SHA1: case ItemKey.SHA1:
key = SHA1; key = SHA1;
break; break;

View File

@@ -16,4 +16,22 @@ namespace SabreTools.DatItems
Internal = 1 << 2, Internal = 1 << 2,
External = 1 << 3, External = 1 << 3,
} }
/// <summary>
/// A subset of fields that can be used as keys
/// </summary>
public enum ItemKey
{
NULL = 0,
Machine,
CRC,
MD5,
SHA1,
SHA256,
SHA384,
SHA512,
SpamSum,
}
} }

View File

@@ -289,14 +289,8 @@ namespace SabreTools.DatItems
#region Sorting and Merging #region Sorting and Merging
/// <summary> /// <inheritdoc/>
/// Get the dictionary key that should be used for a given item and bucketing type public override string GetKey(ItemKey bucketedBy, bool lower = true, bool norename = true)
/// </summary>
/// <param name="bucketedBy">Field enum representing what key to get</param>
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
/// <returns>String representing the key to be used for the DatItem</returns>
public override string GetKey(Field bucketedBy, bool lower = true, bool norename = true)
{ {
// Set the output key as the default blank string // Set the output key as the default blank string
string key = string.Empty; string key = string.Empty;
@@ -304,19 +298,19 @@ namespace SabreTools.DatItems
// Now determine what the key should be based on the bucketedBy value // Now determine what the key should be based on the bucketedBy value
switch (bucketedBy) switch (bucketedBy)
{ {
case Field.DatItem_MD5: case ItemKey.MD5:
key = MD5; key = MD5;
break; break;
case Field.DatItem_SHA1: case ItemKey.SHA1:
key = SHA1; key = SHA1;
break; break;
case Field.DatItem_SHA256: case ItemKey.SHA256:
key = SHA256; key = SHA256;
break; break;
case Field.DatItem_SpamSum: case ItemKey.SpamSum:
key = SpamSum; key = SpamSum;
break; break;

View File

@@ -625,14 +625,8 @@ namespace SabreTools.DatItems
#region Sorting and Merging #region Sorting and Merging
/// <summary> /// <inheritdoc/>
/// Get the dictionary key that should be used for a given item and bucketing type public override string GetKey(ItemKey bucketedBy, bool lower = true, bool norename = true)
/// </summary>
/// <param name="bucketedBy">Field enum representing what key to get</param>
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
/// <returns>String representing the key to be used for the DatItem</returns>
public override string GetKey(Field bucketedBy, bool lower = true, bool norename = true)
{ {
// Set the output key as the default blank string // Set the output key as the default blank string
string key = string.Empty; string key = string.Empty;
@@ -640,31 +634,31 @@ namespace SabreTools.DatItems
// Now determine what the key should be based on the bucketedBy value // Now determine what the key should be based on the bucketedBy value
switch (bucketedBy) switch (bucketedBy)
{ {
case Field.DatItem_CRC: case ItemKey.CRC:
key = CRC; key = CRC;
break; break;
case Field.DatItem_MD5: case ItemKey.MD5:
key = MD5; key = MD5;
break; break;
case Field.DatItem_SHA1: case ItemKey.SHA1:
key = SHA1; key = SHA1;
break; break;
case Field.DatItem_SHA256: case ItemKey.SHA256:
key = SHA256; key = SHA256;
break; break;
case Field.DatItem_SHA384: case ItemKey.SHA384:
key = SHA384; key = SHA384;
break; break;
case Field.DatItem_SHA512: case ItemKey.SHA512:
key = SHA512; key = SHA512;
break; break;
case Field.DatItem_SpamSum: case ItemKey.SpamSum:
key = SpamSum; key = SpamSum;
break; break;

View File

@@ -337,7 +337,7 @@ namespace SabreTools.DatTools
SetDatItemInfo(datFile, datItem, item, parent, basepath); SetDatItemInfo(datFile, datItem, item, parent, basepath);
// Add the file information to the DAT // Add the file information to the DAT
string key = datItem.GetKey(Field.DatItem_CRC); string key = datItem.GetKey(ItemKey.CRC);
datFile.Items.Add(key, datItem); datFile.Items.Add(key, datItem);
logger.Verbose($"File added: {datItem.GetName() ?? string.Empty}"); logger.Verbose($"File added: {datItem.GetName() ?? string.Empty}");