using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using R3;
namespace GameCult.Caching
{
///
/// Describes how a CultCache-managed document changed.
///
public enum CultCacheDocumentChangeKind
{
///
/// A document was added.
///
Added,
///
/// A document was updated.
///
Updated,
///
/// A document was removed.
///
Removed
}
///
/// One reactive CultCache document change.
///
public sealed class CultCacheDocumentChange where T : class
{
///
/// Creates a document change.
///
public CultCacheDocumentChange(
CultCacheDocumentChangeKind kind,
CultRecordKey key,
T? document,
T? previousDocument)
{
Kind = kind;
Key = key;
Document = document;
PreviousDocument = previousDocument;
}
///
/// Gets the change kind.
///
public CultCacheDocumentChangeKind Kind { get; }
///
/// Gets the document record key.
///
public CultRecordKey Key { get; }
///
/// Gets the current POCO presentation, when present.
///
public T? Document { get; }
///
/// Gets the previous POCO presentation, when present.
///
public T? PreviousDocument { get; }
}
///
/// Reactive POCO presentation for one CultCache-managed document.
///
public sealed class CultManagedDocument where T : class
{
private readonly Func _read;
private readonly Func _commit;
private readonly Observable _watch;
///
/// Creates a managed document presentation.
///
public CultManagedDocument(
CultRecordKey key,
Func read,
Func commit,
Observable watch)
{
Key = key;
_read = read ?? throw new ArgumentNullException(nameof(read));
_commit = commit ?? throw new ArgumentNullException(nameof(commit));
_watch = watch ?? throw new ArgumentNullException(nameof(watch));
}
///
/// Gets the document record key.
///
public CultRecordKey Key { get; }
///
/// Gets the current POCO presentation.
///
public T? Value => _read();
///
/// Watches committed values for this document.
///
public Observable Watch()
{
return _watch;
}
///
/// Commits the current POCO value through the owner-supplied commit path.
///
public Task CommitAsync()
{
var value = Value ?? throw new InvalidOperationException($"Document '{Key.Value}' is not present.");
return _commit(value);
}
///
/// Replaces the current value through the owner-supplied commit path.
///
public Task ReplaceAsync(T value)
{
if (value == null) throw new ArgumentNullException(nameof(value));
return _commit(value);
}
}
///
/// CPU-local structure-of-arrays table owned by CultCache for one document type.
///
public sealed class CultSoaTable where TDocument : class
{
private readonly Dictionary _columns;
internal CultSoaTable(
CultRecordKey[] keys,
TDocument[] documents,
Dictionary columns)
{
Keys = keys;
Documents = documents;
_columns = columns;
}
///
/// Gets record keys in row order.
///
public IReadOnlyList Keys { get; }
///
/// Gets POCO presentations in row order.
///
public IReadOnlyList Documents { get; }
///
/// Gets the row count.
///
public int Count => Keys.Count;
///
/// Gets a contiguous unmanaged field/property column.
///
public CultSoaColumn Column(string name) where TValue : unmanaged
{
if (!_columns.TryGetValue(name, out var column))
{
throw new KeyNotFoundException($"SoA table for {typeof(TDocument).Name} has no '{name}' column.");
}
if (column is not TValue[] values)
{
throw new InvalidOperationException(
$"SoA table column '{name}' stores {column.GetType().GetElementType()?.FullName}, not {typeof(TValue).FullName}.");
}
return new CultSoaColumn(name, values);
}
}
///
/// Contiguous typed SoA column.
///
public readonly struct CultSoaColumn where TValue : unmanaged
{
internal CultSoaColumn(string name, TValue[] values)
{
Name = name;
Values = values;
}
///
/// Gets the column name.
///
public string Name { get; }
///
/// Gets the backing array.
///
public TValue[] Values { get; }
///
/// Gets the backing array as a span.
///
public Span Span => Values.AsSpan();
}
internal sealed class CultCacheSoaStore
{
private readonly Dictionary _tables = new();
public void Upsert(CultStoredDocument stored)
{
Table(stored.Descriptor.DocumentType).Upsert(stored.Key, stored.Document);
}
public void Remove(CultStoredDocument stored)
{
if (_tables.TryGetValue(stored.Descriptor.DocumentType, out var table))
{
table.Remove(stored.Key);
}
}
public CultSoaTable Snapshot() where TDocument : class
{
return Table(typeof(TDocument)).Snapshot();
}
private CultCacheSoaTypeTable Table(Type documentType)
{
if (!_tables.TryGetValue(documentType, out var table))
{
table = new CultCacheSoaTypeTable(documentType);
_tables[documentType] = table;
}
return table;
}
}
internal sealed class CultCacheSoaTypeTable
{
private readonly Type _documentType;
private readonly List _keys = new();
private readonly List