Added database table data into configuration. Store saves is auto-handled. Added Action & Redemption stores.
This commit is contained in:
139
Store/Internal/GroupedSaveStore.cs
Normal file
139
Store/Internal/GroupedSaveStore.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
|
||||
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace HermesSocketServer.Store.Internal
|
||||
{
|
||||
public abstract class GroupSaveStore<K, V> : IStore<K, V> where K : class where V : class
|
||||
{
|
||||
private readonly Serilog.ILogger _logger;
|
||||
protected readonly IDictionary<K, V> _store;
|
||||
protected readonly IList<K> _added;
|
||||
protected readonly IList<K> _modified;
|
||||
protected readonly IList<K> _deleted;
|
||||
protected readonly object _lock;
|
||||
|
||||
|
||||
public GroupSaveStore(Serilog.ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_store = new Dictionary<K, V>();
|
||||
_added = new List<K>();
|
||||
_modified = new List<K>();
|
||||
_deleted = new List<K>();
|
||||
_lock = new object();
|
||||
}
|
||||
|
||||
public abstract Task Load();
|
||||
protected abstract void OnInitialAdd(K key, V value);
|
||||
protected abstract void OnInitialModify(K key, V value);
|
||||
protected abstract void OnInitialRemove(K key);
|
||||
public abstract Task Save();
|
||||
|
||||
public V? Get(K key)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_store.TryGetValue(key, out var value))
|
||||
return value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public IDictionary<K, V> Get()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return _store.ToImmutableDictionary();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Modify(K? key, Action<V> action)
|
||||
{
|
||||
if (key == null)
|
||||
return false;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (_store.TryGetValue(key, out V? value))
|
||||
{
|
||||
if (value == null)
|
||||
return false;
|
||||
|
||||
OnInitialModify(key, value);
|
||||
action(value);
|
||||
if (!_added.Contains(key) && !_modified.Contains(key))
|
||||
{
|
||||
_modified.Add(key);
|
||||
_logger.Information($"added key to _modified {key}");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Remove(K? key)
|
||||
{
|
||||
if (key == null)
|
||||
return false;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
OnInitialRemove(key);
|
||||
if (_store.Remove(key))
|
||||
{
|
||||
_logger.Information($"removed key from _deleted {key}");
|
||||
if (!_added.Remove(key))
|
||||
{
|
||||
_modified.Remove(key);
|
||||
_logger.Information($"removed key from _added & _modified {key}");
|
||||
if (!_deleted.Contains(key))
|
||||
{
|
||||
_deleted.Add(key);
|
||||
_logger.Information($"added key to _deleted {key}");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Set(K? key, V? value)
|
||||
{
|
||||
if (key == null || value == null)
|
||||
return false;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (_store.TryGetValue(key, out V? fetched))
|
||||
{
|
||||
if (fetched != value)
|
||||
{
|
||||
OnInitialModify(key, value);
|
||||
_store[key] = value;
|
||||
if (!_added.Contains(key) && !_modified.Contains(key))
|
||||
{
|
||||
_modified.Add(key);
|
||||
_logger.Information($"added key to _modified {key}");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
OnInitialAdd(key, value);
|
||||
_store.Add(key, value);
|
||||
if (!_deleted.Remove(key) && !_added.Contains(key))
|
||||
{
|
||||
_added.Add(key);
|
||||
_logger.Information($"added key to _added {key}");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user