using System.Text.Json.Serialization; namespace Admin.NET.Plugin.AiDOP.DataPlatform.Core; /// Scope resolved by the host from an authenticated source and its bound dataset. public sealed record IngestDatasetIdentity( long TenantId, long FactoryId, long SourceId, long DatasetId, string SourceCode, string DatasetKey); public sealed record IngestLimits(int MaxBodyBytes, int MaxRecords, int MaxJsonDepth); public sealed record SchemaStamp(string Version, string Sha256); public sealed record SchemaDefinition(SchemaStamp Stamp, string Json); public sealed record IngestVersionSnapshot( SchemaStamp SourceSchema, SchemaStamp RawSchema, SchemaStamp HandoffSchema, string MappingVersion, string RuleVersion, string ConfigVersion) { [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? FieldMapJson { get; init; } } public sealed record IngestContract( string DatasetKey, string ObjectCode, SchemaDefinition SourceSchema, SchemaDefinition RawSchema, SchemaDefinition HandoffSchema, string MappingVersion, string RuleVersion, string ConfigVersion) { [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? FieldMapJson { get; init; } public IngestVersionSnapshot Snapshot() => new( SourceSchema.Stamp, RawSchema.Stamp, HandoffSchema.Stamp, MappingVersion, RuleVersion, ConfigVersion) { FieldMapJson = FieldMapJson }; } public sealed record IngestSourceWindow(DateTimeOffset? From, DateTimeOffset? To, string? Watermark); public sealed record RawIngestBatch( Guid BatchId, IngestDatasetIdentity Identity, DateTimeOffset ReceivedAt, IngestVersionSnapshot Versions, int AcceptedRowCount, ReadOnlyMemory Payload, string PayloadSha256, IngestSourceWindow? SourceWindow); public sealed record RawIngestEvent( Guid RawEventId, Guid BatchId, int RecordOrdinal, string ObjectCode, string SourcePosition, int PayloadOffset, int PayloadLength, string PayloadSha256, string? SourceBusinessKey, string? SourceUpdatedAt); public sealed record PreparedIngestBatch(RawIngestBatch Batch, IReadOnlyList Events); public enum IngestContractError { DatasetUnavailable, UnknownSchema, BodyTooLarge, InvalidJson, JsonTooDeep, InvalidEnvelope, TooManyRecords } public sealed class IngestContractException(IngestContractError code) : Exception(code.ToString()) { public IngestContractError Code { get; } = code; } public interface ISchemaRegistry { /// Returns the exact archived contract. Unknown versions never resolve to the active version. IngestContract? Find(string datasetKey, string sourceSchemaVersion); } public interface IRawStorage { /// Appends one batch and its events in the caller's transaction. The caller commits it with the receive task. Task AppendAsync(PreparedIngestBatch prepared, CancellationToken cancellationToken); /// Returns a batch only within the trusted scope supplied by the caller. Task FindAsync( IngestDatasetIdentity authorizedScope, Guid batchId, CancellationToken cancellationToken); }