Remove alternate CultCache backing stores
This commit is contained in:
parent
ef1f4c16ee
commit
08208b9722
11 changed files with 32 additions and 467 deletions
|
|
@ -904,11 +904,6 @@ namespace GameCult.Caching
|
|||
}
|
||||
}
|
||||
|
||||
public interface IRealtimeBackingStore
|
||||
{
|
||||
void ObserveChanges();
|
||||
}
|
||||
|
||||
public abstract class CacheBackingStore : IDisposable
|
||||
{
|
||||
private CultDocumentRegistry? _registry;
|
||||
|
|
@ -972,135 +967,6 @@ namespace GameCult.Caching
|
|||
}
|
||||
}
|
||||
|
||||
public abstract class MultiFileBackingStore : CacheBackingStore, IRealtimeBackingStore
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, string> _pathsByKey = new(StringComparer.Ordinal);
|
||||
|
||||
protected MultiFileBackingStore(string rootPath)
|
||||
{
|
||||
RootDirectory = new DirectoryInfo(rootPath);
|
||||
}
|
||||
|
||||
protected DirectoryInfo RootDirectory { get; }
|
||||
protected abstract byte[] SerializeRecord(CultPersistedRecord record);
|
||||
protected abstract CultPersistedRecord DeserializeRecord(byte[] data);
|
||||
protected abstract byte[] SerializeCatalog(CultSchemaCatalogEntry[] catalog);
|
||||
protected abstract CultSchemaCatalogEntry[] DeserializeCatalog(byte[] data);
|
||||
protected abstract byte[] SerializePayload(object document);
|
||||
protected abstract object DeserializePayload(Type documentType, byte[] payload);
|
||||
public abstract string Extension { get; }
|
||||
protected virtual string CatalogFileName => $"__cultcache.catalog.{Extension}";
|
||||
|
||||
public override void PullAll()
|
||||
{
|
||||
if (!RootDirectory.Exists)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var catalog = LoadCatalog();
|
||||
foreach (var file in RootDirectory.EnumerateFiles($"*.{Extension}", SearchOption.AllDirectories)
|
||||
.Where(file => !string.Equals(file.Name, CatalogFileName, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
var stored = ToStoredDocument(
|
||||
DeserializeRecord(File.ReadAllBytes(file.FullName)),
|
||||
catalog,
|
||||
DeserializePayload);
|
||||
Entries[stored.Key.Value] = stored;
|
||||
_pathsByKey[stored.Key.Value] = file.FullName;
|
||||
EntryAdded.OnNext(stored);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Push(CultStoredDocument entry)
|
||||
{
|
||||
Entries[entry.Key.Value] = entry;
|
||||
var filePath = GetFilePath(entry);
|
||||
if (_pathsByKey.TryGetValue(entry.Key.Value, out var existingPath) &&
|
||||
!string.Equals(existingPath, filePath, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
TryDeleteFile(existingPath);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(filePath)!);
|
||||
File.WriteAllBytes(filePath, SerializeRecord(ToPersistedRecord(entry, SerializePayload)));
|
||||
_pathsByKey[entry.Key.Value] = filePath;
|
||||
PersistCatalog();
|
||||
}
|
||||
|
||||
public override void Delete(CultStoredDocument entry)
|
||||
{
|
||||
Entries.TryRemove(entry.Key.Value, out _);
|
||||
if (_pathsByKey.TryRemove(entry.Key.Value, out var existingPath))
|
||||
{
|
||||
TryDeleteFile(existingPath);
|
||||
}
|
||||
|
||||
PersistCatalog();
|
||||
}
|
||||
|
||||
public override void PushAll(bool soft = false)
|
||||
{
|
||||
foreach (var entry in Entries.Values.ToArray())
|
||||
{
|
||||
Push(entry);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void ObserveChanges()
|
||||
{
|
||||
}
|
||||
|
||||
private CultSchemaCatalogEntry[] LoadCatalog()
|
||||
{
|
||||
var catalogFile = Path.Combine(RootDirectory.FullName, CatalogFileName);
|
||||
if (!File.Exists(catalogFile))
|
||||
{
|
||||
return Array.Empty<CultSchemaCatalogEntry>();
|
||||
}
|
||||
|
||||
return DeserializeCatalog(File.ReadAllBytes(catalogFile));
|
||||
}
|
||||
|
||||
private void PersistCatalog()
|
||||
{
|
||||
Directory.CreateDirectory(RootDirectory.FullName);
|
||||
var catalogFile = Path.Combine(RootDirectory.FullName, CatalogFileName);
|
||||
var catalog = Entries.Values
|
||||
.Select(entry => entry.Descriptor.ToCatalogEntry())
|
||||
.GroupBy(entry => entry.SchemaId, StringComparer.Ordinal)
|
||||
.Select(group => group.First())
|
||||
.OrderBy(entry => entry.SchemaName, StringComparer.Ordinal)
|
||||
.ToArray();
|
||||
File.WriteAllBytes(catalogFile, SerializeCatalog(catalog));
|
||||
}
|
||||
|
||||
private string GetFilePath(CultStoredDocument entry)
|
||||
{
|
||||
var directory = Path.Combine(RootDirectory.FullName, entry.Descriptor.SchemaName);
|
||||
var fileName = SanitizeFileName(entry.Descriptor.GetPreferredFileStem(entry.Document));
|
||||
return Path.Combine(directory, $"{fileName}.{Extension}");
|
||||
}
|
||||
|
||||
private static string SanitizeFileName(string value)
|
||||
{
|
||||
foreach (var invalid in Path.GetInvalidFileNameChars())
|
||||
{
|
||||
value = value.Replace(invalid, '_');
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private static void TryDeleteFile(string path)
|
||||
{
|
||||
if (File.Exists(path))
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class SingleFileBackingStore : CacheBackingStore
|
||||
{
|
||||
protected SingleFileBackingStore(string filePath)
|
||||
|
|
|
|||
Loading…
Reference in a new issue