Extract JSON snapshot persistence helpers
This commit is contained in:
@@ -18,7 +18,7 @@ public sealed class InMemoryCloudStateStore : ICloudStateStore
|
||||
private readonly ConcurrentDictionary<string, CloudSession> _sessionsByToken = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly ConcurrentDictionary<string, string> _symmetricKeys = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly ConcurrentDictionary<string, KeyRequestRecord> _keyRequests = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly string? _persistencePath;
|
||||
private readonly JsonSnapshotStore _snapshotStore;
|
||||
private readonly Lock _syncRoot = new();
|
||||
private readonly List<UpdateManifest> _updates;
|
||||
private readonly List<MediaRecord> _media = [];
|
||||
@@ -33,7 +33,7 @@ public sealed class InMemoryCloudStateStore : ICloudStateStore
|
||||
|
||||
public InMemoryCloudStateStore(string? persistencePath = null)
|
||||
{
|
||||
_persistencePath = persistencePath;
|
||||
_snapshotStore = new JsonSnapshotStore(persistencePath, PersistenceJsonOptions);
|
||||
_robot = new DeviceRegistration
|
||||
{
|
||||
HostMappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
||||
@@ -104,14 +104,7 @@ public sealed class InMemoryCloudStateStore : ICloudStateStore
|
||||
|
||||
public void LoadPersistedState()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_persistencePath) || !File.Exists(_persistencePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var snapshot = JsonSerializer.Deserialize<PersistentStateSnapshot>(File.ReadAllText(_persistencePath), PersistenceJsonOptions);
|
||||
var snapshot = _snapshotStore.Load<PersistentStateSnapshot>();
|
||||
if (snapshot is null)
|
||||
{
|
||||
return;
|
||||
@@ -188,27 +181,11 @@ public sealed class InMemoryCloudStateStore : ICloudStateStore
|
||||
_lastLoadedUtc = snapshot.LastLoadedUtc ?? DateTimeOffset.UtcNow;
|
||||
_lastSavedUtc = snapshot.LastSavedUtc;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore corrupt state and continue with the in-memory defaults.
|
||||
}
|
||||
}
|
||||
|
||||
public void SavePersistedState()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_persistencePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (_syncRoot)
|
||||
{
|
||||
var directory = Path.GetDirectoryName(_persistencePath);
|
||||
if (!string.IsNullOrWhiteSpace(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var snapshot = new PersistentStateSnapshot
|
||||
{
|
||||
@@ -229,8 +206,7 @@ public sealed class InMemoryCloudStateStore : ICloudStateStore
|
||||
Loops = _loops.ToArray(),
|
||||
People = _people.ToArray()
|
||||
};
|
||||
|
||||
File.WriteAllText(_persistencePath, JsonSerializer.Serialize(snapshot, PersistenceJsonOptions));
|
||||
_snapshotStore.Save(snapshot);
|
||||
_lastSavedUtc = now;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ public sealed class InMemoryPersonalMemoryStore : IPersonalMemoryStore
|
||||
};
|
||||
|
||||
private readonly ConcurrentDictionary<string, TenantMemoryRecord> _tenantMemory = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly string? _persistencePath;
|
||||
private readonly JsonSnapshotStore _snapshotStore;
|
||||
private readonly Lock _syncRoot = new();
|
||||
private long _revision;
|
||||
private DateTimeOffset? _lastLoadedUtc;
|
||||
@@ -21,7 +21,7 @@ public sealed class InMemoryPersonalMemoryStore : IPersonalMemoryStore
|
||||
|
||||
public InMemoryPersonalMemoryStore(string? persistencePath = null)
|
||||
{
|
||||
_persistencePath = persistencePath;
|
||||
_snapshotStore = new JsonSnapshotStore(persistencePath, PersistenceJsonOptions);
|
||||
LoadPersistedState();
|
||||
}
|
||||
|
||||
@@ -36,14 +36,7 @@ public sealed class InMemoryPersonalMemoryStore : IPersonalMemoryStore
|
||||
|
||||
public void LoadPersistedState()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_persistencePath) || !File.Exists(_persistencePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var snapshot = JsonSerializer.Deserialize<PersistentStateSnapshot>(File.ReadAllText(_persistencePath), PersistenceJsonOptions);
|
||||
var snapshot = _snapshotStore.Load<PersistentStateSnapshot>();
|
||||
if (snapshot is null)
|
||||
{
|
||||
return;
|
||||
@@ -59,27 +52,11 @@ public sealed class InMemoryPersonalMemoryStore : IPersonalMemoryStore
|
||||
_lastLoadedUtc = snapshot.LastLoadedUtc ?? DateTimeOffset.UtcNow;
|
||||
_lastSavedUtc = snapshot.LastSavedUtc;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore corrupt state and continue with the in-memory defaults.
|
||||
}
|
||||
}
|
||||
|
||||
public void SavePersistedState()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_persistencePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (_syncRoot)
|
||||
{
|
||||
var directory = Path.GetDirectoryName(_persistencePath);
|
||||
if (!string.IsNullOrWhiteSpace(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var snapshot = new PersistentStateSnapshot
|
||||
{
|
||||
@@ -103,8 +80,7 @@ public sealed class InMemoryPersonalMemoryStore : IPersonalMemoryStore
|
||||
})
|
||||
.ToArray()
|
||||
};
|
||||
|
||||
File.WriteAllText(_persistencePath, JsonSerializer.Serialize(snapshot, PersistenceJsonOptions));
|
||||
_snapshotStore.Save(snapshot);
|
||||
_lastSavedUtc = now;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Jibo.Cloud.Infrastructure.Persistence;
|
||||
|
||||
internal sealed class JsonSnapshotStore
|
||||
{
|
||||
private readonly string? _persistencePath;
|
||||
private readonly JsonSerializerOptions _options;
|
||||
|
||||
public JsonSnapshotStore(string? persistencePath, JsonSerializerOptions options)
|
||||
{
|
||||
_persistencePath = persistencePath;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public TSnapshot? Load<TSnapshot>()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_persistencePath) || !File.Exists(_persistencePath))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize<TSnapshot>(File.ReadAllText(_persistencePath), _options);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public void Save<TSnapshot>(TSnapshot snapshot)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_persistencePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var directory = Path.GetDirectoryName(_persistencePath);
|
||||
if (!string.IsNullOrWhiteSpace(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
File.WriteAllText(_persistencePath, JsonSerializer.Serialize(snapshot, _options));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user