@@ -4,10 +4,8 @@ package io.airbyte.cdk.discover
4
4
import io.airbyte.cdk.Operation
5
5
import io.airbyte.cdk.command.SourceConfiguration
6
6
import io.airbyte.cdk.output.OutputConsumer
7
- import io.airbyte.protocol.models.Field as AirbyteField
8
7
import io.airbyte.protocol.models.v0.AirbyteCatalog
9
8
import io.airbyte.protocol.models.v0.AirbyteStream
10
- import io.airbyte.protocol.models.v0.CatalogHelpers
11
9
import io.github.oshai.kotlinlogging.KotlinLogging
12
10
import io.micronaut.context.annotation.Requires
13
11
import jakarta.inject.Singleton
@@ -18,7 +16,7 @@ import jakarta.inject.Singleton
18
16
class DiscoverOperation (
19
17
val config : SourceConfiguration ,
20
18
val metadataQuerierFactory : MetadataQuerier .Factory <SourceConfiguration >,
21
- val airbyteStreamDecorator : AirbyteStreamDecorator ,
19
+ val airbyteStreamFactory : AirbyteStreamFactory ,
22
20
val outputConsumer : OutputConsumer ,
23
21
) : Operation {
24
22
private val log = KotlinLogging .logger {}
@@ -39,50 +37,16 @@ class DiscoverOperation(
39
37
}
40
38
val primaryKey: List <List <String >> = metadataQuerier.primaryKey(name, namespace)
41
39
val discoveredStream = DiscoveredStream (name, namespace, fields, primaryKey)
42
- airbyteStreams.add(toAirbyteStream(discoveredStream))
40
+ val airbyteStream: AirbyteStream =
41
+ if (config.global) {
42
+ airbyteStreamFactory.createGlobal(discoveredStream)
43
+ } else {
44
+ airbyteStreamFactory.createNonGlobal(discoveredStream)
45
+ }
46
+ airbyteStreams.add(airbyteStream)
43
47
}
44
48
}
45
49
}
46
50
outputConsumer.accept(AirbyteCatalog ().withStreams(airbyteStreams))
47
51
}
48
-
49
- fun toAirbyteStream (discoveredStream : DiscoveredStream ): AirbyteStream {
50
- val allColumnsByID: Map <String , Field > = discoveredStream.columns.associateBy { it.id }
51
- val airbyteStream: AirbyteStream =
52
- CatalogHelpers .createAirbyteStream(
53
- discoveredStream.name,
54
- discoveredStream.namespace,
55
- discoveredStream.columns.map {
56
- AirbyteField .of(it.id, it.type.airbyteType.asJsonSchemaType())
57
- },
58
- )
59
- val isValidPK: Boolean =
60
- discoveredStream.primaryKeyColumnIDs.all { idComponents: List <String > ->
61
- val id: String = idComponents.joinToString(separator = " ." )
62
- val field: Field ? = allColumnsByID[id]
63
- field != null && airbyteStreamDecorator.isPossiblePrimaryKeyElement(field)
64
- }
65
- airbyteStream.withSourceDefinedPrimaryKey(
66
- if (isValidPK) discoveredStream.primaryKeyColumnIDs else listOf (),
67
- )
68
- airbyteStream.isResumable = airbyteStream.sourceDefinedPrimaryKey.isNotEmpty()
69
- if (config.global) {
70
- // There is a global feed of incremental records, like CDC.
71
- airbyteStreamDecorator.decorateGlobal(airbyteStream)
72
- } else if (discoveredStream.columns.any { airbyteStreamDecorator.isPossibleCursor(it) }) {
73
- // There is one field whose values can be round-tripped and aggregated by MAX.
74
- airbyteStreamDecorator.decorateNonGlobal(airbyteStream)
75
- } else {
76
- // There is no such field.
77
- airbyteStreamDecorator.decorateNonGlobalNoCursor(airbyteStream)
78
- }
79
- return airbyteStream
80
- }
81
-
82
- data class DiscoveredStream (
83
- val name : String ,
84
- val namespace : String? ,
85
- val columns : List <Field >,
86
- val primaryKeyColumnIDs : List <List <String >>,
87
- )
88
52
}
0 commit comments