Resolve C# snapshot filters by schema alias
This commit is contained in:
parent
12f819bffe
commit
4dfa9a6162
3 changed files with 128 additions and 6 deletions
|
|
@ -529,7 +529,7 @@ namespace GameCult.Networking
|
|||
var key = GetTrackedKey(document, documentType);
|
||||
if (string.IsNullOrWhiteSpace(key.Value) ||
|
||||
!shard.Matches(descriptor.SchemaId, key) ||
|
||||
(requestedSchemaIds != null && !requestedSchemaIds.Contains(descriptor.SchemaId)) ||
|
||||
(requestedSchemaIds != null && !MatchesRequestedSchema(descriptor, requestedSchemaIds)) ||
|
||||
(requestedRecordKeys != null && !requestedRecordKeys.Contains(key.Value)))
|
||||
{
|
||||
continue;
|
||||
|
|
@ -1525,6 +1525,37 @@ namespace GameCult.Networking
|
|||
return schemaMatches && keyMatches;
|
||||
}
|
||||
|
||||
private static bool MatchesRequestedSchema(
|
||||
CultDocumentDescriptor descriptor,
|
||||
ISet<string> requestedSchemaIds)
|
||||
{
|
||||
if (requestedSchemaIds.Count == 0)
|
||||
return true;
|
||||
|
||||
if (requestedSchemaIds.Contains(descriptor.SchemaId))
|
||||
return true;
|
||||
|
||||
if (descriptor.ToCatalogEntry().CompatibleSchemaIds.Any(requestedSchemaIds.Contains))
|
||||
return true;
|
||||
|
||||
return requestedSchemaIds
|
||||
.Select(InferSchemaName)
|
||||
.Where(schemaName => !string.IsNullOrWhiteSpace(schemaName))
|
||||
.Any(schemaName => string.Equals(schemaName, descriptor.SchemaName, StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
private static string? InferSchemaName(string schemaVersion)
|
||||
{
|
||||
var marker = schemaVersion.LastIndexOf(".v", StringComparison.Ordinal);
|
||||
if (marker <= 0 || marker + 2 >= schemaVersion.Length)
|
||||
return null;
|
||||
|
||||
var version = schemaVersion.Substring(marker + 2);
|
||||
return version.All(char.IsDigit)
|
||||
? schemaVersion.Substring(0, marker)
|
||||
: null;
|
||||
}
|
||||
|
||||
internal static CultNetShardDescriptorMessage ToMessage(CultNetShardDescriptor shard)
|
||||
{
|
||||
return new CultNetShardDescriptorMessage
|
||||
|
|
|
|||
|
|
@ -280,12 +280,9 @@ namespace GameCult.Networking
|
|||
value => CultDocumentMessagePackSerialization.SerializeUntyped(value, value.GetType()),
|
||||
payload => CultDocumentMessagePackSerialization.DeserializeUntyped(document.GetType(), payload));
|
||||
|
||||
if (requestedSchemaIds != null && !requestedSchemaIds.Contains(descriptor.SchemaId))
|
||||
if (requestedSchemaIds != null && !MatchesRequestedSchema(descriptor, binding, requestedSchemaIds))
|
||||
{
|
||||
if (!requestedSchemaIds.Contains(binding.SchemaId))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
var handleMethod = typeof(CultCache)
|
||||
|
|
@ -438,6 +435,27 @@ namespace GameCult.Networking
|
|||
.Select(group => group.First());
|
||||
}
|
||||
|
||||
private static bool MatchesRequestedSchema(
|
||||
CultDocumentDescriptor descriptor,
|
||||
CultNetDocumentBinding binding,
|
||||
ISet<string> requestedSchemaIds)
|
||||
{
|
||||
if (requestedSchemaIds.Count == 0)
|
||||
return true;
|
||||
|
||||
if (requestedSchemaIds.Contains(descriptor.SchemaId) ||
|
||||
requestedSchemaIds.Contains(binding.SchemaId))
|
||||
return true;
|
||||
|
||||
if (descriptor.ToCatalogEntry().CompatibleSchemaIds.Any(requestedSchemaIds.Contains))
|
||||
return true;
|
||||
|
||||
return requestedSchemaIds
|
||||
.Select(InferSchemaName)
|
||||
.Where(schemaName => !string.IsNullOrWhiteSpace(schemaName))
|
||||
.Any(schemaName => string.Equals(schemaName, descriptor.SchemaName, StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
private static string? TryReadSchemaVersion(byte[] payload)
|
||||
{
|
||||
try
|
||||
|
|
|
|||
|
|
@ -1837,6 +1837,79 @@ namespace GameCult.Networking.Tests
|
|||
Assert.That(response.Documents[0].Payload, Is.EqualTo(SerializePlayerDataPayload(player)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CultNetDatabaseServer_Creates_SnapshotResponse_ForCompatibleSchemaAlias()
|
||||
{
|
||||
var cache = new CultCache();
|
||||
var registry = new CultNetDocumentRegistry(cache.Registry)
|
||||
.Register(CultNetDocumentBinding.ForDocument<NetworkSchemaNote>(cache.Registry));
|
||||
var database = new CultNetDatabase(cache, new CultNetDatabaseOptions
|
||||
{
|
||||
DocumentRegistry = registry
|
||||
});
|
||||
using var server = new Server(cache, DevelopmentServerSecurity);
|
||||
using var databaseServer = new CultNetDatabaseServer(server, database);
|
||||
var key = new CultRecordKey("network-note:alias-snapshot");
|
||||
var note = new NetworkSchemaNote
|
||||
{
|
||||
Schema = "tests.networking_note.v1",
|
||||
Text = "alias snapshot",
|
||||
Revision = 7
|
||||
};
|
||||
await database.PutAsync(key, note);
|
||||
|
||||
var response = databaseServer.CreateSnapshotResponse(registry.CreateSnapshotRequest(
|
||||
"snapshot-alias-request",
|
||||
schemaIds: ["tests.networking_note.v1"],
|
||||
recordKeys: [key.Value]));
|
||||
|
||||
Assert.That(response.Documents, Has.Length.EqualTo(1));
|
||||
Assert.That(response.Documents[0].RecordKey, Is.EqualTo(key.Value));
|
||||
Assert.That(response.Documents[0].Payload, Is.EqualTo(CultDocumentMessagePackSerialization.Serialize(note)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CultNetDatabase_Creates_ShardSnapshotResponse_ForCompatibleSchemaAlias()
|
||||
{
|
||||
var cache = new CultCache();
|
||||
var registry = new CultNetDocumentRegistry(cache.Registry)
|
||||
.Register(CultNetDocumentBinding.ForDocument<NetworkSchemaNote>(cache.Registry));
|
||||
var descriptor = cache.Registry.GetRequired<NetworkSchemaNote>();
|
||||
var shard = new CultNetShardDescriptor(
|
||||
"network-notes",
|
||||
"runtime-a",
|
||||
epoch: 4,
|
||||
isPrimary: true,
|
||||
schemaIds: [descriptor.SchemaId],
|
||||
keyPrefix: "network-note:",
|
||||
primaryEndpoints: ["cultnet://runtime-a:3075"]);
|
||||
var database = new CultNetDatabase(cache, new CultNetDatabaseOptions
|
||||
{
|
||||
DocumentRegistry = registry,
|
||||
Shards = [shard]
|
||||
});
|
||||
var key = new CultRecordKey("network-note:alias-shard-snapshot");
|
||||
var note = new NetworkSchemaNote
|
||||
{
|
||||
Schema = "tests.networking_note.v1",
|
||||
Text = "alias shard snapshot",
|
||||
Revision = 8
|
||||
};
|
||||
await database.PutAsync(key, note);
|
||||
|
||||
var response = database.CreateShardSnapshotResponse(
|
||||
shard,
|
||||
"snapshot-shard-alias-request",
|
||||
registry.CreateSnapshotRequest(
|
||||
"snapshot-shard-alias-request",
|
||||
schemaIds: ["tests.networking_note.v1"],
|
||||
recordKeys: [key.Value]));
|
||||
|
||||
Assert.That(response.Documents, Has.Length.EqualTo(1));
|
||||
Assert.That(response.Documents[0].RecordKey, Is.EqualTo(key.Value));
|
||||
Assert.That(response.Documents[0].Payload, Is.EqualTo(CultDocumentMessagePackSerialization.Serialize(note)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CultNetDatabaseServer_Creates_Filtered_ShardCatalogResponse()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue