Add CultMesh document prediction submissions
This commit is contained in:
parent
b6d4ebc920
commit
a08a66a66e
5 changed files with 180 additions and 11 deletions
|
|
@ -426,6 +426,21 @@ namespace GameCult.Mesh
|
|||
return new CultMeshDocumentHandle<TDocument>(BindLiveFeed(context, feed), replace);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Binds a mutable predicted typed document live feed to a Verse context.
|
||||
/// </summary>
|
||||
public static CultMeshDocumentHandle<TDocument> BindDocument<TDocument>(
|
||||
CultMeshVerseContext context,
|
||||
CultMeshLiveFeed<CultMeshDocumentQueryParameters, TDocument> feed,
|
||||
Func<TDocument, Task> replace,
|
||||
Func<TDocument, Task> submitPrediction)
|
||||
where TDocument : class
|
||||
{
|
||||
if (replace == null) throw new ArgumentNullException(nameof(replace));
|
||||
if (submitPrediction == null) throw new ArgumentNullException(nameof(submitPrediction));
|
||||
return new CultMeshDocumentHandle<TDocument>(BindLiveFeed(context, feed), replace, submitPrediction);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Binds a typed document live feed to a Verse.
|
||||
/// </summary>
|
||||
|
|
@ -451,6 +466,20 @@ namespace GameCult.Mesh
|
|||
return BindDocument(verse.Context, feed, replace);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Binds a mutable predicted typed document live feed to a Verse.
|
||||
/// </summary>
|
||||
public static CultMeshDocumentHandle<TDocument> BindDocument<TDocument>(
|
||||
CultMeshVerse verse,
|
||||
CultMeshLiveFeed<CultMeshDocumentQueryParameters, TDocument> feed,
|
||||
Func<TDocument, Task> replace,
|
||||
Func<TDocument, Task> submitPrediction)
|
||||
where TDocument : class
|
||||
{
|
||||
if (verse == null) throw new ArgumentNullException(nameof(verse));
|
||||
return BindDocument(verse.Context, feed, replace, submitPrediction);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Binds a typed state pointer to a Verse context.
|
||||
/// </summary>
|
||||
|
|
@ -935,6 +964,32 @@ namespace GameCult.Mesh
|
|||
return BindDocument(context, feed, replace);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a mutable predicted typed document handle from snapshot/watch/replace/prediction delegates and binds it to a Verse context.
|
||||
/// </summary>
|
||||
public static CultMeshDocumentHandle<TDocument> Document<TDocument>(
|
||||
string documentId,
|
||||
CultMeshVerseContext context,
|
||||
Func<CultMeshQueryContext, Task<TDocument>> latest,
|
||||
Func<CultMeshQueryContext, R3.Observable<TDocument>> watch,
|
||||
Func<TDocument, Task> replace,
|
||||
Func<TDocument, Task> submitPrediction,
|
||||
IEnumerable<CultMeshProjectionSource>? sources = null,
|
||||
CultMeshRouteHint? routeHint = null)
|
||||
where TDocument : class
|
||||
{
|
||||
if (replace == null) throw new ArgumentNullException(nameof(replace));
|
||||
if (submitPrediction == null) throw new ArgumentNullException(nameof(submitPrediction));
|
||||
|
||||
var feed = LiveFeed<CultMeshDocumentQueryParameters, TDocument>(
|
||||
documentId,
|
||||
(_parameters, queryContext) => latest(queryContext),
|
||||
(_parameters, queryContext) => watch(queryContext),
|
||||
sources,
|
||||
routeHint);
|
||||
return BindDocument(context, feed, replace, submitPrediction);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a typed document handle from snapshot/watch delegates and binds it to a Verse.
|
||||
/// </summary>
|
||||
|
|
@ -968,6 +1023,24 @@ namespace GameCult.Mesh
|
|||
return Document(documentId, verse.Context, latest, watch, replace, sources, routeHint);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a mutable predicted typed document handle from snapshot/watch/replace/prediction delegates and binds it to a Verse.
|
||||
/// </summary>
|
||||
public static CultMeshDocumentHandle<TDocument> Document<TDocument>(
|
||||
string documentId,
|
||||
CultMeshVerse verse,
|
||||
Func<CultMeshQueryContext, Task<TDocument>> latest,
|
||||
Func<CultMeshQueryContext, R3.Observable<TDocument>> watch,
|
||||
Func<TDocument, Task> replace,
|
||||
Func<TDocument, Task> submitPrediction,
|
||||
IEnumerable<CultMeshProjectionSource>? sources = null,
|
||||
CultMeshRouteHint? routeHint = null)
|
||||
where TDocument : class
|
||||
{
|
||||
if (verse == null) throw new ArgumentNullException(nameof(verse));
|
||||
return Document(documentId, verse.Context, latest, watch, replace, submitPrediction, sources, routeHint);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a typed document handle directly over one CultCache record.
|
||||
/// </summary>
|
||||
|
|
@ -1068,6 +1141,10 @@ namespace GameCult.Mesh
|
|||
{
|
||||
await database.PutAsync(key, value).ConfigureAwait(false);
|
||||
},
|
||||
async value =>
|
||||
{
|
||||
await database.PutPredictedAsync(key, value).ConfigureAwait(false);
|
||||
},
|
||||
sourceList,
|
||||
route);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -900,6 +900,9 @@ namespace GameCult.Mesh
|
|||
/// <summary>Gets whether this handle can replace the underlying document value.</summary>
|
||||
bool CanReplace { get; }
|
||||
|
||||
/// <summary>Gets whether this handle can submit a client prediction for the underlying document value.</summary>
|
||||
bool CanSubmitPrediction { get; }
|
||||
|
||||
/// <summary>Creates a same-schema alias presentation for another CLR document type.</summary>
|
||||
CultMeshDocumentHandle<TAlias> AsSchemaAlias<TAlias>() where TAlias : class;
|
||||
}
|
||||
|
|
@ -915,14 +918,17 @@ namespace GameCult.Mesh
|
|||
|
||||
private readonly CultMeshBoundLiveFeed<CultMeshDocumentQueryParameters, TDocument> _feed;
|
||||
private readonly Func<TDocument, Task>? _replace;
|
||||
private readonly Func<TDocument, Task>? _submitPrediction;
|
||||
|
||||
/// <summary>Creates a document handle from a Verse-bound live feed.</summary>
|
||||
public CultMeshDocumentHandle(
|
||||
CultMeshBoundLiveFeed<CultMeshDocumentQueryParameters, TDocument> feed,
|
||||
Func<TDocument, Task>? replace = null)
|
||||
Func<TDocument, Task>? replace = null,
|
||||
Func<TDocument, Task>? submitPrediction = null)
|
||||
{
|
||||
_feed = feed ?? throw new ArgumentNullException(nameof(feed));
|
||||
_replace = replace;
|
||||
_submitPrediction = submitPrediction;
|
||||
}
|
||||
|
||||
/// <summary>Gets the semantic document id.</summary>
|
||||
|
|
@ -955,6 +961,9 @@ namespace GameCult.Mesh
|
|||
/// <summary>Gets whether this handle can replace the underlying document value.</summary>
|
||||
public bool CanReplace => _replace != null;
|
||||
|
||||
/// <summary>Gets whether this handle can submit a client prediction for the underlying document value.</summary>
|
||||
public bool CanSubmitPrediction => _submitPrediction != null;
|
||||
|
||||
/// <summary>Reads one coherent document snapshot.</summary>
|
||||
public Task<TDocument> LatestAsync()
|
||||
{
|
||||
|
|
@ -992,6 +1001,18 @@ namespace GameCult.Mesh
|
|||
return _replace(value);
|
||||
}
|
||||
|
||||
/// <summary>Submits a locally predicted document value when this handle is backed by client-authoritative state.</summary>
|
||||
public Task SubmitPredictionAsync(TDocument value)
|
||||
{
|
||||
if (value == null) throw new ArgumentNullException(nameof(value));
|
||||
if (_submitPrediction == null)
|
||||
{
|
||||
throw new NotSupportedException($"Document handle '{DocumentId}' does not accept client predictions.");
|
||||
}
|
||||
|
||||
return _submitPrediction(value);
|
||||
}
|
||||
|
||||
/// <summary>Creates a same-schema alias presentation for another CLR document type.</summary>
|
||||
public CultMeshDocumentHandle<TAlias> AsSchemaAlias<TAlias>() where TAlias : class
|
||||
{
|
||||
|
|
@ -1018,8 +1039,11 @@ namespace GameCult.Mesh
|
|||
Func<TAlias, Task>? replace = _replace == null
|
||||
? null
|
||||
: value => _replace(ConvertDocument<TAlias, TDocument>(value));
|
||||
Func<TAlias, Task>? submitPrediction = _submitPrediction == null
|
||||
? null
|
||||
: value => _submitPrediction(ConvertDocument<TAlias, TDocument>(value));
|
||||
|
||||
return new CultMeshDocumentHandle<TAlias>(aliasFeed.Bind(Context), replace);
|
||||
return new CultMeshDocumentHandle<TAlias>(aliasFeed.Bind(Context), replace, submitPrediction);
|
||||
}
|
||||
|
||||
private static TTarget ConvertDocument<TSource, TTarget>(TSource document)
|
||||
|
|
|
|||
|
|
@ -155,13 +155,14 @@ the record.
|
|||
|
||||
```csharp
|
||||
var db = node.Database;
|
||||
var input = CultMesh.Document<PlayerInput>(db, inputKey, verse);
|
||||
|
||||
await db.PutPredictedAsync(inputKey, inputState);
|
||||
await input.SubmitPredictionAsync(inputState);
|
||||
|
||||
using var sub = db.WatchRecord<PlayerInput>(inputKey)
|
||||
.Subscribe(change =>
|
||||
using var sub = input.Watch()
|
||||
.Subscribe(latest =>
|
||||
{
|
||||
ReconcilePrediction(change);
|
||||
ReconcilePrediction(latest);
|
||||
});
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -163,13 +163,16 @@ portable machinery that makes it feel native.
|
|||
- `CultMeshDocumentHandle<TDocument>` is the typed reactive document edge for
|
||||
CultCache/CultNet state. It exposes document metadata, one coherent
|
||||
`LatestAsync()` read, a `Watch()` stream, and `ReplaceAsync(...)` when the
|
||||
handle is backed by mutable state. Callers can bind a projected live feed, a
|
||||
local `CultCache` record, a distributed `CultNetDatabase` record, or a
|
||||
handle is backed by mutable state. Distributed `CultNetDatabase` handles also
|
||||
expose `SubmitPredictionAsync(...)`, which applies a local prediction through
|
||||
the database's configured client-authority scope and later reconciles through
|
||||
the normal shard log. Callers can bind a projected live feed, a local
|
||||
`CultCache` record, a distributed `CultNetDatabase` record, or a
|
||||
`CultMeshNode` record through the same surface instead of walking transport
|
||||
and projection layers themselves. Same-schema CLR aliases can be requested
|
||||
with `AsSchemaAlias<TAlias>()`; CultMesh verifies the shared
|
||||
`[CultDocument(schemaName, schemaVersion)]` identity and uses the shared
|
||||
CultCache MessagePack codec for read/watch/replace conversion.
|
||||
CultCache MessagePack codec for read/watch/replace/prediction conversion.
|
||||
- `CultMeshDocumentCatalog` is the schema-aware lookup edge for a set of
|
||||
document handles. It indexes handles by CLR document type, schema name, and
|
||||
schema version, and resolves same-schema CLR aliases by delegating to
|
||||
|
|
@ -487,8 +490,10 @@ methods. Subscriptions receive domain changes rather than storage envelopes.
|
|||
### Client Prediction
|
||||
|
||||
`CultNetClientAuthorityScope` declares input documents a runtime may predict.
|
||||
`PutPredictedAsync` writes local state and emits `Predicted`. When the
|
||||
authoritative log arrives, the database emits `Reconciled`.
|
||||
CultMesh document handles opened over a `CultNetDatabase` expose
|
||||
`SubmitPredictionAsync(...)`, which writes local state and emits `Predicted`
|
||||
through the configured database authority policy. When the authoritative log
|
||||
arrives, the database emits `Reconciled`.
|
||||
|
||||
### Replica Catch-Up
|
||||
|
||||
|
|
|
|||
|
|
@ -754,6 +754,8 @@ public sealed class CultMeshStreamingTests
|
|||
|
||||
handle.CanReplace.Should().BeTrue();
|
||||
alias.CanReplace.Should().BeTrue();
|
||||
handle.CanSubmitPrediction.Should().BeFalse();
|
||||
alias.CanSubmitPrediction.Should().BeFalse();
|
||||
handle.DocumentId.Should().Be(key.Value);
|
||||
handle.Sources.Should().ContainSingle().Which.SchemaId.Should().Be(handle.SchemaId);
|
||||
|
||||
|
|
@ -772,6 +774,66 @@ public sealed class CultMeshStreamingTests
|
|||
cache.Get<MeshNoteDocument>(key)!.Text.Should().Be("cache-replaced");
|
||||
observed.Text.Should().Be("cache-replaced");
|
||||
observed.Revision.Should().Be(2);
|
||||
|
||||
Func<Task> act = () => alias.SubmitPredictionAsync(new MeshNoteAliasDocument
|
||||
{
|
||||
Schema = "tests.mesh_note.v1",
|
||||
Text = "cache-predicted",
|
||||
Revision = 3
|
||||
});
|
||||
await act.Should().ThrowAsync<NotSupportedException>();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DocumentHandle_SubmitsPredictionsThroughCultNetDatabase()
|
||||
{
|
||||
var cache = new CultCache();
|
||||
var schemaId = CultDocumentRegistry.Shared.GetRequired<MeshNoteDocument>().SchemaId;
|
||||
var database = new CultNetDatabase(cache, new CultNetDatabaseOptions
|
||||
{
|
||||
RuntimeId = "pilot-a",
|
||||
Shards = new[]
|
||||
{
|
||||
new CultNetShardDescriptor(
|
||||
"pilot-inputs",
|
||||
"server",
|
||||
epoch: 1,
|
||||
isPrimary: false,
|
||||
schemaIds: new[] { schemaId },
|
||||
keyPrefix: "input:")
|
||||
},
|
||||
ClientAuthorityScopes = new[]
|
||||
{
|
||||
new CultNetClientAuthorityScope(
|
||||
"pilot-a",
|
||||
schemaIds: new[] { schemaId },
|
||||
keyPrefix: "input:pilot-a")
|
||||
}
|
||||
});
|
||||
var key = new CultRecordKey("input:pilot-a:thermal");
|
||||
|
||||
var handle = CultMesh.Document<MeshNoteDocument>(
|
||||
database,
|
||||
key,
|
||||
CultMesh.Verse("starbridge", "pilot-a"));
|
||||
var alias = handle.AsSchemaAlias<MeshNoteAliasDocument>();
|
||||
|
||||
handle.CanReplace.Should().BeTrue();
|
||||
alias.CanSubmitPrediction.Should().BeTrue();
|
||||
|
||||
MeshNoteDocument observed = null!;
|
||||
using var subscription = handle.Watch(value => observed = value);
|
||||
await alias.SubmitPredictionAsync(new MeshNoteAliasDocument
|
||||
{
|
||||
Schema = "tests.mesh_note.v1",
|
||||
Text = "predicted-thermal",
|
||||
Revision = 5
|
||||
});
|
||||
|
||||
cache.Get<MeshNoteDocument>(key)!.Text.Should().Be("predicted-thermal");
|
||||
observed.Text.Should().Be("predicted-thermal");
|
||||
observed.Revision.Should().Be(5);
|
||||
(await handle.LatestAsync()).Text.Should().Be("predicted-thermal");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue