IngestContracts.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Text.Json.Serialization;
  2. namespace Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  3. /// <summary>Scope resolved by the host from an authenticated source and its bound dataset.</summary>
  4. public sealed record IngestDatasetIdentity(
  5. long TenantId, long FactoryId, long SourceId, long DatasetId, string SourceCode, string DatasetKey);
  6. public sealed record IngestLimits(int MaxBodyBytes, int MaxRecords, int MaxJsonDepth);
  7. public sealed record SchemaStamp(string Version, string Sha256);
  8. public sealed record SchemaDefinition(SchemaStamp Stamp, string Json);
  9. public sealed record IngestVersionSnapshot(
  10. SchemaStamp SourceSchema,
  11. SchemaStamp RawSchema,
  12. SchemaStamp HandoffSchema,
  13. string MappingVersion,
  14. string RuleVersion,
  15. string ConfigVersion)
  16. {
  17. [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
  18. public string? FieldMapJson { get; init; }
  19. }
  20. public sealed record IngestContract(
  21. string DatasetKey,
  22. string ObjectCode,
  23. SchemaDefinition SourceSchema,
  24. SchemaDefinition RawSchema,
  25. SchemaDefinition HandoffSchema,
  26. string MappingVersion,
  27. string RuleVersion,
  28. string ConfigVersion)
  29. {
  30. [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
  31. public string? FieldMapJson { get; init; }
  32. public IngestVersionSnapshot Snapshot() => new(
  33. SourceSchema.Stamp, RawSchema.Stamp, HandoffSchema.Stamp, MappingVersion, RuleVersion, ConfigVersion) { FieldMapJson = FieldMapJson };
  34. }
  35. public sealed record IngestSourceWindow(DateTimeOffset? From, DateTimeOffset? To, string? Watermark);
  36. public sealed record RawIngestBatch(
  37. Guid BatchId,
  38. IngestDatasetIdentity Identity,
  39. DateTimeOffset ReceivedAt,
  40. IngestVersionSnapshot Versions,
  41. int AcceptedRowCount,
  42. ReadOnlyMemory<byte> Payload,
  43. string PayloadSha256,
  44. IngestSourceWindow? SourceWindow);
  45. public sealed record RawIngestEvent(
  46. Guid RawEventId,
  47. Guid BatchId,
  48. int RecordOrdinal,
  49. string ObjectCode,
  50. string SourcePosition,
  51. int PayloadOffset,
  52. int PayloadLength,
  53. string PayloadSha256,
  54. string? SourceBusinessKey,
  55. string? SourceUpdatedAt);
  56. public sealed record PreparedIngestBatch(RawIngestBatch Batch, IReadOnlyList<RawIngestEvent> Events);
  57. public enum IngestContractError
  58. {
  59. DatasetUnavailable,
  60. UnknownSchema,
  61. BodyTooLarge,
  62. InvalidJson,
  63. JsonTooDeep,
  64. InvalidEnvelope,
  65. TooManyRecords
  66. }
  67. public sealed class IngestContractException(IngestContractError code) : Exception(code.ToString())
  68. {
  69. public IngestContractError Code { get; } = code;
  70. }
  71. public interface ISchemaRegistry
  72. {
  73. /// <summary>Returns the exact archived contract. Unknown versions never resolve to the active version.</summary>
  74. IngestContract? Find(string datasetKey, string sourceSchemaVersion);
  75. }
  76. public interface IRawStorage
  77. {
  78. /// <summary>Appends one batch and its events in the caller's transaction. The caller commits it with the receive task.</summary>
  79. Task AppendAsync(PreparedIngestBatch prepared, CancellationToken cancellationToken);
  80. /// <summary>Returns a batch only within the trusted scope supplied by the caller.</summary>
  81. Task<PreparedIngestBatch?> FindAsync(
  82. IngestDatasetIdentity authorizedScope, Guid batchId, CancellationToken cancellationToken);
  83. }