diff --git a/README.md b/README.md index 1123701e..ae2887ee 100644 --- a/README.md +++ b/README.md @@ -34,11 +34,12 @@ the stack. If a design needs another peer-to-peer category, first check whether it is really Verse policy, peer exchange, or shard authority wearing a fake mustache. -If you want one concrete path for "typed local document -> raw wire document -> -mesh/runtime watch surface", start with +If you want the smallest durable "open node -> put typed doc -> get typed doc" +path, start with +[`src/GameCult.Mesh/docs/durable-node-quickstart.md`](src/GameCult.Mesh/docs/durable-node-quickstart.md). +If you want the lower-level "typed local document -> raw wire document -> +mesh/runtime watch surface" handoff, follow it with [`src/GameCult.Mesh/docs/typed-document-path.md`](src/GameCult.Mesh/docs/typed-document-path.md). -That sample shows which package owns each step and where wire provenance stays -visible. ## Repository Scope @@ -256,6 +257,7 @@ Each subproject has a local README with package-specific detail: - [GameCult.Caching.MessagePack.Analyzers](src/GameCult.Caching.MessagePack.Analyzers/README.md) - [GameCult.Networking](src/GameCult.Networking/README.md) - [GameCult.Mesh](src/GameCult.Mesh/README.md) +- [Durable Node Quickstart](src/GameCult.Mesh/docs/durable-node-quickstart.md) - [Typed Document Path](src/GameCult.Mesh/docs/typed-document-path.md) - [GameCult.Unity](src/GameCult.Unity/README.md) - [GameCult.Caching.Tests](tests/GameCult.Caching.Tests/README.md) diff --git a/src/GameCult.Mesh/README.md b/src/GameCult.Mesh/README.md index 04196ff7..22170b89 100644 --- a/src/GameCult.Mesh/README.md +++ b/src/GameCult.Mesh/README.md @@ -26,6 +26,12 @@ var player = await node.Database.GetAsync(playerKey); await node.Database.PutAsync(playerKey, player); ``` +If you want one inspectable path that starts with a durable local cache file and +ends with a typed read/write through the public CultMesh surface, see +[docs/durable-node-quickstart.md](docs/durable-node-quickstart.md). +If you want the lower-level typed-to-raw handoff that CultNet moves across the +wire, see [docs/typed-document-path.md](docs/typed-document-path.md). + Enable durable authoritative shard logs when a node should serve replica catch-up after restart: @@ -200,6 +206,7 @@ snapshot's represented log sequence. ## Documentation - [Public API](docs/public-api.md) +- [Durable Node Quickstart](docs/durable-node-quickstart.md) - [Typed Document Path](docs/typed-document-path.md) - [Research Notes](docs/research.md) - [Verse Model](docs/verses.md) diff --git a/src/GameCult.Mesh/docs/durable-node-quickstart.md b/src/GameCult.Mesh/docs/durable-node-quickstart.md new file mode 100644 index 00000000..3d08ae0f --- /dev/null +++ b/src/GameCult.Mesh/docs/durable-node-quickstart.md @@ -0,0 +1,116 @@ +# Durable Node Quickstart + +This is the smallest honest path from a durable local typed document to a +running CultMesh node. + +It uses the public surface only: + +- `CultMesh.CreateNodeAsync(...)` to open or create the durable node +- `node.Database.PutAsync(...)` and `node.Database.GetAsync(...)` for typed reads + and writes +- `node.FlushAsync()` to persist the current local state before shutdown + +## Ownership Split + +- CultCache owns the local durable state file, typed document registry, record + keys, and persistence metadata. +- CultNet owns the document registry that turns typed CultCache documents into + schema-v0 wire payloads when the node has to replicate them. +- CultMesh owns the newcomer-facing node and database illusion your game code + talks to. + +That means the game writes to `node.Database`, CultMesh routes through CultNet, +and the durable local snapshot still belongs to CultCache. + +## Sample + +Define one typed document. CultCache owns this model and its schema identity. + +```csharp +using GameCult.Caching; +using MessagePack; + +[CultDocument("sample.mesh_note", "sample.mesh_note.v0")] +[MessagePackObject] +public sealed class MeshNote +{ + [Key(0)] + [CultName] + public string NoteId { get; set; } = string.Empty; + + [Key(1)] + public string Body { get; set; } = string.Empty; +} +``` + +Open a durable node, write the document through CultMesh, flush it, then reopen +the same node and read the document back. + +```csharp +using GameCult.Caching; +using GameCult.Mesh; +using GameCult.Networking; + +var key = new CultRecordKey("note:intro"); +var documents = new CultNetDocumentRegistry() + .Register(CultNetDocumentBinding.ForDocument()); + +using (var node = await CultMesh.CreateNodeAsync("world.ccmp", new CultMeshNodeOptions +{ + DatabaseOptions = new CultNetDatabaseOptions + { + RuntimeId = "quickstart-local", + DocumentRegistry = documents + } +})) +{ + await node.Database.PutAsync(key, new MeshNote + { + NoteId = key.Value, + Body = "hello from a durable CultMesh node" + }); + + var live = await node.Database.GetAsync(key); + Console.WriteLine(live?.Body); + + await node.FlushAsync(); +} + +using (var reopened = await CultMesh.CreateNodeAsync("world.ccmp", new CultMeshNodeOptions +{ + DatabaseOptions = new CultNetDatabaseOptions + { + RuntimeId = "quickstart-local", + DocumentRegistry = documents + } +})) +{ + var stored = await reopened.Database.GetAsync(key); + Console.WriteLine(stored?.Body); +} +``` + +What happened: + +1. `CultMesh.CreateNodeAsync("world.ccmp", ...)` opened a durable CultCache + snapshot file and wrapped it in a CultMesh node. +2. `node.Database.PutAsync(...)` wrote a typed document through the public + CultMesh database surface. +3. CultNet kept the document registry needed for raw replication and + subscriptions, but the sample did not need to drop into wire messages. +4. `node.FlushAsync()` persisted the local CultCache snapshot so the reopened + node could read the same typed document back. + +## Which Layer Owns What? + +If you peel this path apart, the authority split stays simple: + +- CultCache: `world.ccmp`, schema ids, record keys, typed document payloads, + local indexes, flush/load +- CultNet: `CultNetDocumentRegistry`, raw document puts/snapshots, + subscriptions, shard routing, transport-facing replication +- CultMesh: `CultMeshNode`, `node.Database`, and the "treat this as one + reactive database" surface + +If you need the explicit raw-envelope version of the same handoff, see +[typed-document-path.md](typed-document-path.md). diff --git a/src/GameCult.Networking/README.md b/src/GameCult.Networking/README.md index 44592186..9b4100db 100644 --- a/src/GameCult.Networking/README.md +++ b/src/GameCult.Networking/README.md @@ -103,6 +103,8 @@ contracts. CultMesh owns the developer-facing mesh entrypoints and higher-level runtime documentation. See `src/GameCult.Mesh/README.md` for the public-facing CultMesh API. +For one small end-to-end path from durable local typed state into `node.Database`, +see `src/GameCult.Mesh/docs/durable-node-quickstart.md`. ## Distributed CultCache diff --git a/tests/GameCult.Networking.Tests/NetworkingTests.cs b/tests/GameCult.Networking.Tests/NetworkingTests.cs index 25a5d4b6..65831af5 100644 --- a/tests/GameCult.Networking.Tests/NetworkingTests.cs +++ b/tests/GameCult.Networking.Tests/NetworkingTests.cs @@ -1107,6 +1107,57 @@ namespace GameCult.Networking.Tests Assert.That(Directory.Exists(Path.Combine(rootPath, "world.cultmesh", "shard-logs")), Is.True); } + [Test] + public async Task CultMeshNode_CanRoundTrip_DurableTypedDocument_Through_PublicDatabaseSurface() + { + var rootPath = Path.Combine(TestContext.CurrentContext.WorkDirectory, "cultmesh-quickstart", Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(rootPath); + var cachePath = Path.Combine(rootPath, "world.ccmp"); + var documents = new CultNetDocumentRegistry() + .Register(CultNetDocumentBinding.ForDocument()); + var key = new CultRecordKey("note:intro"); + + using (var node = await CultMesh.CreateNodeAsync(cachePath, new CultMeshNodeOptions + { + StartServer = false, + DatabaseOptions = new CultNetDatabaseOptions + { + RuntimeId = "quickstart-local", + DocumentRegistry = documents + } + })) + { + await node.Database.PutAsync(key, new MeshQuickstartNote + { + NoteId = key.Value, + Body = "hello from a durable CultMesh node" + }); + + var live = await node.Database.GetAsync(key); + + Assert.That(node.Store, Is.Not.Null); + Assert.That(live, Is.Not.Null); + Assert.That(live!.Body, Is.EqualTo("hello from a durable CultMesh node")); + + await node.FlushAsync(); + } + + using var reopened = await CultMesh.CreateNodeAsync(cachePath, new CultMeshNodeOptions + { + StartServer = false, + DatabaseOptions = new CultNetDatabaseOptions + { + RuntimeId = "quickstart-local", + DocumentRegistry = documents + } + }); + var stored = await reopened.Database.GetAsync(key); + + Assert.That(stored, Is.Not.Null); + Assert.That(stored!.NoteId, Is.EqualTo(key.Value)); + Assert.That(stored.Body, Is.EqualTo("hello from a durable CultMesh node")); + } + [Test] public async Task CultNetDatabase_Appends_PerShardMutationLog() { @@ -2421,6 +2472,18 @@ namespace GameCult.Networking.Tests Assert.That(client.ReconnectState, Is.EqualTo(ClientReconnectState.Idle)); } + [CultDocument("sample.mesh_note", "sample.mesh_note.v0")] + [MessagePack.MessagePackObject] + public sealed class MeshQuickstartNote + { + [MessagePack.Key(0)] + [CultName] + public string NoteId { get; set; } = string.Empty; + + [MessagePack.Key(1)] + public string Body { get; set; } = string.Empty; + } + private sealed class EnvironmentVariableScope : IDisposable { private readonly (string Name, string? Value)[] _originalValues;