Add C# typed peer snapshot node sync
This commit is contained in:
parent
743979eca7
commit
76a38ec36e
3 changed files with 143 additions and 0 deletions
|
|
@ -510,6 +510,80 @@ namespace GameCult.Mesh
|
|||
return new CultMeshSnapshotDocumentBinding<TDocument>(recordKey, documentId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches one typed snapshot document and syncs it into a local node in one call.
|
||||
/// </summary>
|
||||
public static Task<TDocument> SyncDocumentFromPeerSnapshotAsync<TDocument>(
|
||||
CultMeshNode node,
|
||||
string endpoint,
|
||||
string recordKey,
|
||||
CultMeshSnapshotEndpointOptions? options = null,
|
||||
bool flush = false)
|
||||
where TDocument : class
|
||||
{
|
||||
if (node == null) throw new ArgumentNullException(nameof(node));
|
||||
if (string.IsNullOrWhiteSpace(endpoint)) throw new ArgumentException("Value must be non-empty.", nameof(endpoint));
|
||||
if (string.IsNullOrWhiteSpace(recordKey)) throw new ArgumentException("Value must be non-empty.", nameof(recordKey));
|
||||
|
||||
return SnapshotEndpoint(endpoint, options).SyncDocumentAsync<TDocument>(node, recordKey, flush);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches one typed snapshot document through a caller-provided schema client and syncs it into a local node in one call.
|
||||
/// </summary>
|
||||
public static Task<TDocument> SyncDocumentFromPeerSnapshotAsync<TDocument>(
|
||||
CultMeshNode node,
|
||||
Func<ICultNetSchemaClient> createClient,
|
||||
string endpoint,
|
||||
string recordKey,
|
||||
CultMeshSnapshotEndpointOptions? options = null,
|
||||
bool flush = false)
|
||||
where TDocument : class
|
||||
{
|
||||
if (node == null) throw new ArgumentNullException(nameof(node));
|
||||
if (createClient == null) throw new ArgumentNullException(nameof(createClient));
|
||||
if (string.IsNullOrWhiteSpace(endpoint)) throw new ArgumentException("Value must be non-empty.", nameof(endpoint));
|
||||
if (string.IsNullOrWhiteSpace(recordKey)) throw new ArgumentException("Value must be non-empty.", nameof(recordKey));
|
||||
|
||||
var request = CloneSnapshotFacadeRequestOptions(options?.Request);
|
||||
request.CreateClient = createClient;
|
||||
var resolvedOptions = new CultMeshSnapshotEndpointOptions
|
||||
{
|
||||
Context = options?.Context,
|
||||
DocumentRegistry = options?.DocumentRegistry,
|
||||
Request = request,
|
||||
SourceId = options?.SourceId,
|
||||
RouteHint = options?.RouteHint,
|
||||
PollInterval = options?.PollInterval ?? TimeSpan.FromMilliseconds(250)
|
||||
};
|
||||
return SnapshotEndpoint(endpoint, resolvedOptions).SyncDocumentAsync<TDocument>(node, recordKey, flush);
|
||||
}
|
||||
|
||||
private static CultMeshSnapshotRequestOptions CloneSnapshotFacadeRequestOptions(CultMeshSnapshotRequestOptions? source)
|
||||
{
|
||||
if (source == null)
|
||||
return new CultMeshSnapshotRequestOptions();
|
||||
|
||||
return new CultMeshSnapshotRequestOptions
|
||||
{
|
||||
SchemaIds = source.SchemaIds,
|
||||
RecordKeys = source.RecordKeys,
|
||||
ShardId = source.ShardId,
|
||||
ShardEpoch = source.ShardEpoch,
|
||||
ResponseTimeout = source.ResponseTimeout,
|
||||
ConnectTimeout = source.ConnectTimeout,
|
||||
MessageIdPrefix = source.MessageIdPrefix,
|
||||
Security = source.Security,
|
||||
ConfigureClient = source.ConfigureClient,
|
||||
CreateClient = source.CreateClient,
|
||||
RudpRuntimeId = source.RudpRuntimeId,
|
||||
RudpConnectionId = source.RudpConnectionId,
|
||||
RudpConnectPayload = source.RudpConnectPayload,
|
||||
RudpMaxFragmentBytes = source.RudpMaxFragmentBytes,
|
||||
RudpResendDelayMs = source.RudpResendDelayMs
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches one scoped raw CultNet snapshot from an endpoint.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -186,6 +186,21 @@ Use `DocumentFromPublication<T>()` when the caller should provide a configured
|
|||
source instead of branching between local files, backing stores, and remote
|
||||
CultNet snapshots itself.
|
||||
|
||||
When the remote snapshot should hydrate a local node, use the direct sync
|
||||
facade and keep the rest of the code on local typed handles:
|
||||
|
||||
```csharp
|
||||
var syncedHealth = await CultMesh.SyncDocumentFromPeerSnapshotAsync<DaemonHealthUi>(
|
||||
node,
|
||||
"cultnet://daemon.local:3075",
|
||||
"daemon:aetheria.health.v1",
|
||||
new CultMeshSnapshotEndpointOptions { DocumentRegistry = daemonRegistry });
|
||||
|
||||
var localHealth = await node
|
||||
.Document<DaemonHealthUi>("daemon:aetheria.health.v1", verse)
|
||||
.LatestAsync();
|
||||
```
|
||||
|
||||
## Game Session
|
||||
|
||||
`CultMeshGameSession` wires the common gameplay loop: local catalogs,
|
||||
|
|
|
|||
|
|
@ -2480,6 +2480,60 @@ public sealed class CultMeshStreamingTests
|
|||
node.Cache.Get<MeshNoteDocument>(key)!.Revision.Should().Be(45);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SnapshotFacade_SyncsRemoteDocumentIntoLocalNodeWithSameSchemaAlias()
|
||||
{
|
||||
var sourceCache = new CultCache();
|
||||
var registry = new CultNetDocumentRegistry(CultDocumentRegistry.Shared);
|
||||
registry.Register(CultNetDocumentBinding.ForDocument<MeshNoteDocument>(sourceCache.Registry));
|
||||
var key = new CultRecordKey("mesh-note:facade-sync-alias");
|
||||
await sourceCache.UpsertAsync(new MeshNoteDocument
|
||||
{
|
||||
Schema = "tests.mesh_note.v1",
|
||||
Text = "facade-synced",
|
||||
Revision = 46
|
||||
}, new CultRecordHandle<MeshNoteDocument>(key));
|
||||
var requests = new List<CultNetSnapshotRequestMessage>();
|
||||
using var node = await CultMesh.CreateNodeAsync(
|
||||
Path.Combine(Path.GetTempPath(), $"cultmesh-facade-sync-alias-{Guid.NewGuid():N}.ccmp"),
|
||||
new CultMeshNodeOptions
|
||||
{
|
||||
StartServer = false,
|
||||
CacheOptions = new CultCacheOpenOptions
|
||||
{
|
||||
Registry = sourceCache.Registry,
|
||||
PullOnOpen = false
|
||||
},
|
||||
DatabaseOptions = new CultNetDatabaseOptions
|
||||
{
|
||||
DocumentRegistry = registry
|
||||
}
|
||||
});
|
||||
|
||||
var synced = await CultMesh.SyncDocumentFromPeerSnapshotAsync<MeshNoteAliasDocument>(
|
||||
node,
|
||||
() => new MeshSnapshotSchemaClient(request =>
|
||||
{
|
||||
requests.Add(request);
|
||||
return registry.CreateRawSnapshotResponse(sourceCache, request.MessageId, request);
|
||||
}),
|
||||
"cultnet://facade-sync.test:3075",
|
||||
key.Value,
|
||||
new CultMeshSnapshotEndpointOptions
|
||||
{
|
||||
DocumentRegistry = registry,
|
||||
Context = CultMesh.Verse("starbridge", "unity-raven").Context
|
||||
});
|
||||
var localAlias = await node.Document<MeshNoteAliasDocument>(key.Value, CultMesh.Verse("starbridge", "unity-raven"))
|
||||
.LatestAsync();
|
||||
|
||||
synced.Text.Should().Be("facade-synced");
|
||||
synced.Revision.Should().Be(46);
|
||||
localAlias.Text.Should().Be("facade-synced");
|
||||
node.Cache.Get<MeshNoteDocument>(key)!.Revision.Should().Be(46);
|
||||
requests.Should().ContainSingle().Which.RecordKeys.Should().Equal(key.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DocumentRegistryHelpers_CreateTypedNetworkRegistriesForAliases()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue