|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.cdk.message |
| 6 | + |
| 7 | +import com.google.common.collect.Range |
| 8 | +import com.google.common.collect.RangeSet |
| 9 | +import com.google.common.collect.TreeRangeSet |
| 10 | +import java.nio.file.Path |
| 11 | + |
| 12 | +/** |
| 13 | + * Represents an accumulated batch of records in some stage of processing. |
| 14 | + * |
| 15 | + * Emitted by @[io.airbyte.cdk.write.StreamLoader.processRecords] to describe the batch accumulated. |
| 16 | + * Non-[State.COMPLETE] batches are routed to @[io.airbyte.cdk.write.StreamLoader.processBatch] |
| 17 | + * re-entrantly until completion. |
| 18 | + * |
| 19 | + * The framework will track the association between the Batch and the range of records it |
| 20 | + * represents, by [Batch.State]s. The [State.PERSISTED] state has special meaning: it indicates that |
| 21 | + * the associated ranges have been persisted remotely, and that platform checkpoint messages can be |
| 22 | + * emitted. |
| 23 | + * |
| 24 | + * [State.SPOOLED] is used internally to indicate that records have been spooled to disk for |
| 25 | + * processing and should not be used by implementors. |
| 26 | + * |
| 27 | + * When a stream has been read to End-of-stream, and all ranges between 0 and End-of-stream are |
| 28 | + * [State.COMPLETE], then all records are considered to have been processed. |
| 29 | + * |
| 30 | + * The intended usage for implementors is to implement the provided interfaces in case classes that |
| 31 | + * contain the necessary metadata for processing, using them in @ |
| 32 | + * [io.airbyte.cdk.write.StreamLoader.processBatch] to route to the appropriate handler(s). |
| 33 | + * |
| 34 | + * For example: |
| 35 | + * |
| 36 | + * ```kotlin |
| 37 | + * sealed class MyBatch: Batch |
| 38 | + * data class MyLocalFile( |
| 39 | + * override val path: Path, |
| 40 | + * override val totalSizeBytes: Long |
| 41 | + * ): StagedLocalFile |
| 42 | + * data class MyRemoteObject( |
| 43 | + * override val key: String |
| 44 | + * ): RemoteObject |
| 45 | + * // etc... |
| 46 | + * ``` |
| 47 | + */ |
| 48 | +interface Batch { |
| 49 | + enum class State { |
| 50 | + SPOOLED, |
| 51 | + LOCAL, |
| 52 | + PERSISTED, |
| 53 | + COMPLETE |
| 54 | + } |
| 55 | + |
| 56 | + val state: State |
| 57 | +} |
| 58 | + |
| 59 | +/** Simple batch: use if you need no other metadata for processing. */ |
| 60 | +data class SimpleBatch(override val state: Batch.State) : Batch |
| 61 | + |
| 62 | +/** Represents a file of records locally staged. */ |
| 63 | +abstract class StagedLocalFile() : Batch { |
| 64 | + override val state: Batch.State = Batch.State.LOCAL |
| 65 | + abstract val localPath: Path |
| 66 | + abstract val totalSizeBytes: Long |
| 67 | +} |
| 68 | + |
| 69 | +/** Represents a remote object containing persisted records. */ |
| 70 | +abstract class RemoteObject() : Batch { |
| 71 | + override val state: Batch.State = Batch.State.PERSISTED |
| 72 | + abstract val key: String |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Represents a file of raw records staged to disk for pre-processing. Used internally by the |
| 77 | + * framework |
| 78 | + */ |
| 79 | +data class SpooledRawMessagesLocalFile( |
| 80 | + override val localPath: Path, |
| 81 | + override val totalSizeBytes: Long, |
| 82 | + override val state: Batch.State = Batch.State.SPOOLED |
| 83 | +) : StagedLocalFile() |
| 84 | + |
| 85 | +/** |
| 86 | + * Internally-used wrapper for tracking the association between a batch and the range of records it |
| 87 | + * contains. |
| 88 | + */ |
| 89 | +data class BatchEnvelope<B : Batch>( |
| 90 | + val batch: B, |
| 91 | + val ranges: RangeSet<Long> = TreeRangeSet.create() |
| 92 | +) { |
| 93 | + constructor( |
| 94 | + batch: B, |
| 95 | + range: Range<Long> |
| 96 | + ) : this(batch = batch, ranges = TreeRangeSet.create(listOf(range))) |
| 97 | + |
| 98 | + fun <C : Batch> withBatch(newBatch: C): BatchEnvelope<C> { |
| 99 | + return BatchEnvelope(newBatch, ranges) |
| 100 | + } |
| 101 | +} |
0 commit comments