MdpBatchFactory.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Globalization;
  2. using System.Text.Json;
  3. using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  4. namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  5. internal static class MdpBatchFactory
  6. {
  7. // Identity fields are part of the signed JSON. Accept string IDs so JavaScript senders retain precision.
  8. internal static bool TryRead(byte[] payload, long tenant, long configuredFactory, bool fromBatch, out long factory)
  9. {
  10. factory = configuredFactory;
  11. try
  12. {
  13. using var document = JsonDocument.Parse(payload, new JsonDocumentOptions { MaxDepth = MdpIngestProtocol.Limits.MaxJsonDepth });
  14. var root = document.RootElement;
  15. if (root.ValueKind != JsonValueKind.Object || !TryId(root, "factoryId", out var requested)
  16. || !TryId(root, "tenantId", out var claimedTenant) || claimedTenant.HasValue && claimedTenant != tenant)
  17. return false;
  18. if (fromBatch && !requested.HasValue || !fromBatch && requested.HasValue && requested != configuredFactory)
  19. return false;
  20. factory = requested ?? configuredFactory;
  21. if (fromBatch && root.TryGetProperty("data", out var data) && data.ValueKind == JsonValueKind.Object
  22. && data.TryGetProperty("list", out var list) && list.ValueKind == JsonValueKind.Array)
  23. {
  24. foreach (var row in list.EnumerateArray())
  25. {
  26. if (!RowMatches(row, tenant, factory)) return false;
  27. }
  28. }
  29. return factory > 0;
  30. }
  31. catch (JsonException)
  32. {
  33. return false;
  34. }
  35. }
  36. internal static bool TryResolve(long configuredFactory, bool fromBatch, long? requested, out long factory)
  37. {
  38. factory = requested ?? configuredFactory;
  39. return factory > 0 && (fromBatch ? requested.HasValue : factory == configuredFactory);
  40. }
  41. internal static bool RowsMatch(PreparedIngestBatch prepared)
  42. {
  43. var identity = prepared.Batch.Identity;
  44. foreach (var row in prepared.Events)
  45. {
  46. using var document = JsonDocument.Parse(prepared.Batch.Payload.Slice(row.PayloadOffset, row.PayloadLength));
  47. if (!RowMatches(document.RootElement, identity.TenantId, identity.FactoryId))
  48. return false;
  49. }
  50. return true;
  51. }
  52. private static bool RowMatches(JsonElement row, long tenant, long factory) =>
  53. row.ValueKind == JsonValueKind.Object && TryId(row, "factoryId", out var rowFactory)
  54. && (!rowFactory.HasValue || rowFactory == factory) && TryId(row, "tenantId", out var rowTenant)
  55. && (!rowTenant.HasValue || rowTenant == tenant);
  56. private static bool TryId(JsonElement root, string field, out long? value)
  57. {
  58. value = null;
  59. foreach (var property in root.EnumerateObject())
  60. {
  61. if (!property.NameEquals(field)) continue;
  62. if (value.HasValue || property.Value.ValueKind is not (JsonValueKind.String or JsonValueKind.Number)
  63. || !long.TryParse(property.Value.ToString(), NumberStyles.None, CultureInfo.InvariantCulture, out var id) || id <= 0)
  64. return false;
  65. value = id;
  66. }
  67. return true;
  68. }
  69. }