I also had a problem similar to yours
which I have solved in the following way and with a test software I have tested up to 50 million texts, all of which were in English, and there was no problem in testing them, but surely, considering the statistical population of the input data, it is completely logical that it cannot be For all cases of a string with any string length, it will produce a unique long numeric value, but for strings with special and meaningful conditions, such as the names of tables and columns, this method can be used.
If there is a better solution, please guide me
public class ContextIdGenerator
{
public long CreateTableId(string schemaName, string tableName) => CreateId($"{schemaName}.{tableName}", false, Encoding.ASCII);
public long CreateTableColumnId(string schemaName, string tableName, string columnName) => CreateId($"{schemaName}.{tableName}.{columnName}", false, Encoding.ASCII);
public long CreateId(string context, bool sensitive, Encoding encoding)
{
if (string.IsNullOrEmpty(context)) return 0;
if (!sensitive) context = context.ToUpper();
using var hasher = MD5.Create();
var bytes = encoding.GetBytes(context.ToUpper());
var hBytes = hasher.ComputeHash(bytes);
return hBytes.Select((q, i) => Convert.ToInt64(q * Math.Pow(10, i + 1))).Sum();
}
}
The test source is mentioned below (ASCII state)
private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = ".ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
private void TestButton_Click(object sender, EventArgs e)
{
Enabled = false;
Dictionary<long, string> db = new();
var A = new ContextIdGenerator();
var errorCounter = 0;
var counter = 0;
while (counter <= 50000000)
{
Application.DoEvents();
var len = random.Next(20, 30);
var text = RandomString(len);
var keyId = A.CreateId(text, false, Encoding.ASCII);
counter++;
if (keyId == 0) continue;
try
{
db.Add(keyId, text);
}
catch
{
if (db.Values.Contains(text, StringComparer.OrdinalIgnoreCase))
continue;
errorCounter++;
}
}
Enabled = true;
MessageBox.Show($"ErrorCount : {errorCounter}");
}