Changed the default way to modify values in store. Added basic validation to stores. Using interfaces to DI store objects.

This commit is contained in:
Tom
2025-01-16 19:49:02 +00:00
parent b00c72ec2a
commit ee3f128a9f
24 changed files with 173 additions and 61 deletions

View File

@@ -48,7 +48,7 @@ namespace HermesSocketServer.Store.Internal
async (query, list) => await _generator.DoPreparedStatement(_database, query, list, _table.KeyColumns));
}
private async Task GenerateQuery(IList<K> keys, Func<int, string> generate, Func<string, IEnumerable<K>, IEnumerable<V>, Task<int>> execute)
private async Task GenerateQuery(IList<K> keys, Func<int, string> generate, Func<string, IEnumerable<K>, IEnumerable<V?>, Task<int>> execute)
{
ImmutableList<K>? list = null;
lock (_lock)

View File

@@ -24,7 +24,7 @@ namespace HermesSocketServer.Store.Internal
public abstract Task Load();
protected abstract void OnInitialAdd(K key, V value);
protected abstract void OnInitialModify(K key, V value);
protected abstract void OnInitialModify(K key, V value, V newValue);
protected abstract void OnPostRemove(K key, V value);
public abstract Task Save();
@@ -46,7 +46,28 @@ namespace HermesSocketServer.Store.Internal
}
}
public bool Modify(K? key, Action<V> action)
public bool Modify(K? key, V value)
{
if (key == null)
return false;
lock (_lock)
{
if (_store.TryGetValue(key, out V? oldValue))
{
OnInitialModify(key, oldValue, value);
_store[key] = value;
if (!_added.Contains(key) && !_modified.Contains(key))
{
_modified.Add(key);
}
return true;
}
}
return false;
}
public bool Modify(K? key, Action<V> modify)
{
if (key == null)
return false;
@@ -55,11 +76,7 @@ namespace HermesSocketServer.Store.Internal
{
if (_store.TryGetValue(key, out V? value))
{
if (value == null)
return false;
OnInitialModify(key, value);
action(value);
modify(value);
if (!_added.Contains(key) && !_modified.Contains(key))
{
_modified.Add(key);
@@ -97,9 +114,9 @@ namespace HermesSocketServer.Store.Internal
return false;
}
public bool Set(K? key, V? value)
public bool Set(K? key, V value)
{
if (key == null || value == null)
if (key == null)
return false;
lock (_lock)
@@ -108,7 +125,7 @@ namespace HermesSocketServer.Store.Internal
{
if (fetched != value)
{
OnInitialModify(key, value);
OnInitialModify(key, fetched, value);
_store[key] = value;
if (!_added.Contains(key) && !_modified.Contains(key))
{