diff --git a/src/GameCult.Mesh/CultMesh.cs b/src/GameCult.Mesh/CultMesh.cs index e06ced87..38fb7954 100644 --- a/src/GameCult.Mesh/CultMesh.cs +++ b/src/GameCult.Mesh/CultMesh.cs @@ -285,6 +285,67 @@ namespace GameCult.Mesh } } + /// + /// Describes one typed document to bind from a CultMesh publication source. + /// + public interface ICultMeshPublicationDocumentBinding + { + /// Gets the publication source override for this document, when it differs from the catalog source. + CultMeshDocumentPublicationSource? Source { get; } + + /// Binds this document request to a publication-backed handle. + ICultMeshDocumentHandle Bind( + CultMeshDocumentPublicationSource source, + CultMeshVerseContext context, + CultMeshStoreDocumentOptions? storeOptions, + CultMeshPeerSnapshotDocumentOptions? peerOptions); + } + + /// + /// Describes one typed document to bind from a CultMesh publication source. + /// + public sealed class CultMeshPublicationDocumentBinding : ICultMeshPublicationDocumentBinding + where TDocument : class + { + internal CultMeshPublicationDocumentBinding( + CultRecordKey key, + string? documentId, + string? sourceId, + CultMeshDocumentPublicationSource? source) + { + Key = key; + DocumentId = documentId; + SourceId = sourceId; + Source = source; + } + + /// Gets the record key to read. + public CultRecordKey Key { get; } + + /// Gets the semantic document id. Defaults to the record key. + public string? DocumentId { get; } + + /// Gets the source id advertised by diagnostics. Defaults to the document id or record key. + public string? SourceId { get; } + + /// + public CultMeshDocumentPublicationSource? Source { get; } + + /// + public ICultMeshDocumentHandle Bind( + CultMeshDocumentPublicationSource source, + CultMeshVerseContext context, + CultMeshStoreDocumentOptions? storeOptions, + CultMeshPeerSnapshotDocumentOptions? peerOptions) + { + return CultMesh.DocumentFromPublication( + Source ?? source, + Key, + context, + CultMesh.WithPublicationBindingOptions(storeOptions, Key, DocumentId, SourceId), + CultMesh.WithPublicationBindingOptions(peerOptions, Key, DocumentId, SourceId)); + } + } /// /// A locally hosted CultMesh node over CultCache, CultNet transport, and the mesh database facade. /// @@ -1544,6 +1605,67 @@ namespace GameCult.Mesh return DocumentFromPublication(source, key, verse.Context, storeOptions, peerOptions); } + /// + /// Describes one typed document to bind from a CultMesh publication source. + /// + public static CultMeshPublicationDocumentBinding PublicationDocument( + CultRecordKey key, + string? documentId = null, + string? sourceId = null, + CultMeshDocumentPublicationSource? source = null) + where TDocument : class + { + return new CultMeshPublicationDocumentBinding(key, documentId, sourceId, source); + } + + /// + /// Describes one typed document to bind from a CultMesh publication source. + /// + public static CultMeshPublicationDocumentBinding PublicationDocument( + string recordKey, + string? documentId = null, + string? sourceId = null, + CultMeshDocumentPublicationSource? source = null) + where TDocument : class + { + if (string.IsNullOrWhiteSpace(recordKey)) throw new ArgumentException("Value must be non-empty.", nameof(recordKey)); + return PublicationDocument(new CultRecordKey(recordKey), documentId, sourceId, source); + } + + /// + /// Creates a schema-aware document catalog from one or more configured publication bindings. + /// + public static CultMeshDocumentCatalog DocumentsFromPublication( + CultMeshDocumentPublicationSource source, + IEnumerable bindings, + CultMeshVerseContext context, + CultMeshStoreDocumentOptions? storeOptions = null, + CultMeshPeerSnapshotDocumentOptions? peerOptions = null) + { + if (source == null) throw new ArgumentNullException(nameof(source)); + if (bindings == null) throw new ArgumentNullException(nameof(bindings)); + if (context == null) throw new ArgumentNullException(nameof(context)); + + return Documents(bindings.Select(binding => + { + if (binding == null) throw new ArgumentException("CultMesh publication bindings cannot contain null.", nameof(bindings)); + return binding.Bind(source, context, storeOptions, peerOptions); + }).ToArray()); + } + + /// + /// Creates a schema-aware document catalog from one or more configured publication bindings. + /// + public static CultMeshDocumentCatalog DocumentsFromPublication( + CultMeshDocumentPublicationSource source, + IEnumerable bindings, + CultMeshVerse verse, + CultMeshStoreDocumentOptions? storeOptions = null, + CultMeshPeerSnapshotDocumentOptions? peerOptions = null) + { + if (verse == null) throw new ArgumentNullException(nameof(verse)); + return DocumentsFromPublication(source, bindings, verse.Context, storeOptions, peerOptions); + } /// /// Reads one typed document from a configured publication source and hydrates it into a local node. /// @@ -1627,6 +1749,53 @@ namespace GameCult.Mesh peerOptions, flush); } + internal static CultMeshStoreDocumentOptions WithPublicationBindingOptions( + CultMeshStoreDocumentOptions? options, + CultRecordKey key, + string? documentId, + string? sourceId) + { + return new CultMeshStoreDocumentOptions + { + DocumentId = string.IsNullOrWhiteSpace(documentId) ? key.Value : documentId, + SourceId = string.IsNullOrWhiteSpace(sourceId) + ? (string.IsNullOrWhiteSpace(documentId) ? key.Value : documentId) + : sourceId, + RouteHint = options?.RouteHint, + PollInterval = options?.PollInterval ?? TimeSpan.FromMilliseconds(250), + Registry = options?.Registry + }; + } + + internal static CultMeshPeerSnapshotDocumentOptions WithPublicationBindingOptions( + CultMeshPeerSnapshotDocumentOptions? options, + CultRecordKey key, + string? documentId, + string? sourceId) + { + return new CultMeshPeerSnapshotDocumentOptions + { + DocumentId = string.IsNullOrWhiteSpace(documentId) ? key.Value : documentId, + SourceId = string.IsNullOrWhiteSpace(sourceId) + ? (string.IsNullOrWhiteSpace(documentId) ? key.Value : documentId) + : sourceId, + RouteHint = options?.RouteHint, + ResponseTimeout = options?.ResponseTimeout ?? TimeSpan.FromSeconds(5), + ConnectTimeout = options?.ConnectTimeout ?? TimeSpan.FromSeconds(5), + PollInterval = options?.PollInterval ?? TimeSpan.FromMilliseconds(250), + MessageIdPrefix = options?.MessageIdPrefix, + SchemaIds = options?.SchemaIds, + ShardId = options?.ShardId, + ShardEpoch = options?.ShardEpoch, + Security = options?.Security, + ConfigureClient = options?.ConfigureClient, + RudpRuntimeId = options?.RudpRuntimeId, + RudpConnectionId = options?.RudpConnectionId ?? 0x43554c54, + RudpConnectPayload = options?.RudpConnectPayload ?? "cultnet-schema-rudp", + RudpMaxFragmentBytes = options?.RudpMaxFragmentBytes ?? 1024, + RudpResendDelayMs = options?.RudpResendDelayMs ?? 25 + }; + } /// /// Creates a typed document handle directly over one distributed CultNet database record. /// diff --git a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs index 45fbf6d3..b7db4594 100644 --- a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs +++ b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs @@ -2305,6 +2305,60 @@ public sealed class CultMeshStreamingTests (await mixedAliasSchemaHandle.LatestAsync()).Text.Should().Be("compatible-record-key"); } + [Test] + public async Task PublicationHelpers_BindConfiguredDocumentsIntoCatalog() + { + using var directory = new TemporaryDirectory(); + var firstPath = Path.Combine(directory.Path, "first.ccmp"); + var secondPath = Path.Combine(directory.Path, "second.ccmp"); + var firstKey = new CultRecordKey("mesh-note:publication-catalog-first"); + var secondKey = new CultRecordKey("mesh-note:publication-catalog-second"); + var firstWriter = new CultCache(); + firstWriter.AddBackingStore(new SingleFileMessagePackBackingStore(firstPath)); + await firstWriter.UpsertAsync(typeof(MeshPublicationNoteDocument), new MeshPublicationNoteDocument + { + Schema = "tests.mesh_publication_note.v1", + Text = "first source", + Revision = 21 + }, firstKey); + firstWriter.FlushAllBackingStores(); + var secondWriter = new CultCache(); + secondWriter.AddBackingStore(new SingleFileMessagePackBackingStore(secondPath)); + await secondWriter.UpsertAsync(new MeshNoteDocument + { + Schema = "tests.mesh_note.v1", + Text = "second source", + Revision = 22 + }, new CultRecordHandle(secondKey)); + secondWriter.FlushAllBackingStores(); + + var catalog = CultMesh.DocumentsFromPublication( + CultMeshDocumentPublicationSource.SingleFile(firstPath), + new ICultMeshPublicationDocumentBinding[] + { + CultMesh.PublicationDocument( + firstKey, + documentId: "daemon:first"), + CultMesh.PublicationDocument( + secondKey, + documentId: "daemon:second", + source: CultMeshDocumentPublicationSource.SingleFile(secondPath)) + }, + CultMesh.Verse("starbridge", "unity-catalog"), + new CultMeshStoreDocumentOptions + { + RouteHint = new CultMeshRouteHint(CultMeshLocalityKind.SharedMemory, "publication catalog"), + PollInterval = TimeSpan.FromMilliseconds(10) + }); + + catalog.Documents.Select(document => document.DocumentId).Should().Equal("daemon:first", "daemon:second"); + (await catalog.LatestAsync()).Text.Should().Be("first source"); + (await catalog.LatestAsync()).Text.Should().Be("second source"); + catalog.Document().DocumentId.Should().Be("daemon:second"); + catalog.Document().RouteHint.Description.Should().Be("publication catalog"); + catalog.Document().Sources.Should().ContainSingle().Which.SourceId.Should().Be("daemon:second"); + } + [Test] public async Task PublicationHelpers_SyncConfiguredPeerSourceIntoLocalNodeAlias() {