ramdb/extension/src/HashStore.cs
2025-01-01 09:49:15 -06:00

86 lines
3.0 KiB
C#

using System.Collections.Concurrent;
namespace ArmaRAMDb
{
internal class HashStore
{
private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, string>> _hashTables = RAMDb._hashTables;
public static bool HashDelete(string tableName)
{
var result = _hashTables.TryRemove(tableName, out _);
Main.Log($"HashDelete: {tableName} - Success: {result}", "debug");
return result;
}
public static string HashGet(string tableName, string key)
{
if (_hashTables.TryGetValue(tableName, out var table))
{
string data = table.TryGetValue(key, out string value) ? value : string.Empty;
Main.Log($"HashGet: {tableName} - {key}", "debug");
Main.Log($"Value: {data}", "debug");
return data;
}
Main.Log($"Table not found: {tableName}", "debug");
return string.Empty;
}
public static Dictionary<string, string> HashGetAll(string tableName)
{
Main.Log($"Getting contents for table: {tableName}", "debug");
if (_hashTables.TryGetValue(tableName, out var table))
{
var contents = table.ToDictionary(kv => kv.Key, kv => kv.Value);
Main.Log($"Found {contents.Count} entries in table {tableName}", "debug");
foreach (var entry in contents)
{
Main.Log($"Key: {entry.Key}, Value: {entry.Value}", "debug");
}
return contents;
}
Main.Log($"Table {tableName} not found", "debug");
return [];
}
public static bool HashRemove(string tableName, string key)
{
if (_hashTables.TryGetValue(tableName, out var table))
{
var result = table.TryRemove(key, out _);
Main.Log($"HashRemove: {tableName} - {key} - Success: {result}", "debug");
return result;
}
Main.Log($"Table {tableName} or Key {key} not found", "debug");
return false;
}
public static bool HashSet(string tableName, string key, string value)
{
var table = _hashTables.GetOrAdd(tableName, _ => new ConcurrentDictionary<string, string>());
table.AddOrUpdate(key, value, (_, _) => value);
Main.Log($"HashSet: {tableName} - {key} - {value}", "debug");
return true;
}
public static bool HashSetBulk(string tableName, Dictionary<string, string> values)
{
var table = _hashTables.GetOrAdd(tableName, _ => new ConcurrentDictionary<string, string>());
foreach (var kv in values)
{
table.AddOrUpdate(kv.Key, kv.Value, (_, _) => kv.Value);
Main.Log($"HashSetBulk: {tableName} - {kv.Key} - {kv.Value}", "debug");
}
return true;
}
}
}