|
| 1 | +package no.nav.paw.kafkakeymaintenance.kafka |
| 2 | + |
| 3 | +import no.nav.paw.kafkakeymaintenance.ApplicationContext |
| 4 | +import org.jetbrains.exposed.sql.* |
| 5 | +import org.jetbrains.exposed.sql.SqlExpressionBuilder.wrap |
| 6 | +import java.time.Instant |
| 7 | + |
| 8 | +data class TransactionContext( |
| 9 | + val appContext: ApplicationContext, |
| 10 | + val transaction: Transaction |
| 11 | +) |
| 12 | + |
| 13 | +fun txContext(applicationContext: ApplicationContext): Transaction.() -> TransactionContext = { |
| 14 | + TransactionContext(applicationContext, this) |
| 15 | +} |
| 16 | + |
| 17 | +fun TransactionContext.initHwm(topic: Topic, partitionCount: Int) { |
| 18 | + (0 until partitionCount) |
| 19 | + .filter { getHwm(topic, it) == null } |
| 20 | + .forEach { insertHwm(topic, it, -1) } |
| 21 | +} |
| 22 | + |
| 23 | +fun TransactionContext.getHwm(topic: Topic, partition: Int): Long? = |
| 24 | + HwmTable |
| 25 | + .selectAll() |
| 26 | + .where { |
| 27 | + (HwmTable.topic eq topic.value) and |
| 28 | + (HwmTable.partition eq partition) and |
| 29 | + (HwmTable.version eq appContext.consumerVersion) |
| 30 | + } |
| 31 | + .singleOrNull()?.get(HwmTable.offset) |
| 32 | + |
| 33 | +fun TransactionContext.getTopicPartitionMetadata(topic: Topic, partition: Int): TopicPartitionMetadata? = |
| 34 | + HwmTable |
| 35 | + .selectAll() |
| 36 | + .where { |
| 37 | + (HwmTable.topic eq topic.value) and |
| 38 | + (HwmTable.partition eq partition) and |
| 39 | + (HwmTable.version eq appContext.consumerVersion) |
| 40 | + } |
| 41 | + .singleOrNull() |
| 42 | + ?.let { |
| 43 | + TopicPartitionMetadata( |
| 44 | + topic = Topic(it[HwmTable.topic]), |
| 45 | + partition = it[HwmTable.partition], |
| 46 | + offset = it[HwmTable.offset], |
| 47 | + time = partitionTime(it[HwmTable.time]), |
| 48 | + lastUpdated = partitionLastUpdated(it[HwmTable.lastUpdated]) |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | +fun TransactionContext.getAllHwms(): List<HwmInfo> = |
| 53 | + HwmTable |
| 54 | + .selectAll() |
| 55 | + .where { HwmTable.version eq appContext.consumerVersion } |
| 56 | + .map { |
| 57 | + hwmInfo( |
| 58 | + topic = Topic(it[HwmTable.topic]), |
| 59 | + partition = it[HwmTable.partition], |
| 60 | + offset = it[HwmTable.offset] |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | +fun TransactionContext.insertHwm( |
| 65 | + topic: Topic, |
| 66 | + partition: Int, |
| 67 | + offset: Long, |
| 68 | + time: Instant = Instant.EPOCH, |
| 69 | + lastUpdated: Instant = Instant.EPOCH |
| 70 | +) { |
| 71 | + HwmTable.insert { |
| 72 | + it[HwmTable.topic] = topic.value |
| 73 | + it[version] = appContext.consumerVersion |
| 74 | + it[HwmTable.partition] = partition |
| 75 | + it[HwmTable.offset] = offset |
| 76 | + it[HwmTable.time] = time |
| 77 | + it[HwmTable.lastUpdated] = lastUpdated |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +fun TransactionContext.updateHwm( |
| 82 | + topic: Topic, |
| 83 | + partition: Int, |
| 84 | + offset: Long, |
| 85 | + time: Instant, |
| 86 | + lastUpdated: Instant |
| 87 | +): Boolean = |
| 88 | + HwmTable |
| 89 | + .update({ |
| 90 | + (HwmTable.topic eq topic.value) and |
| 91 | + (HwmTable.partition eq partition) and |
| 92 | + (HwmTable.offset less offset) and |
| 93 | + (HwmTable.version eq appContext.consumerVersion) |
| 94 | + } |
| 95 | + ) { |
| 96 | + it[HwmTable.offset] = offset |
| 97 | + it[HwmTable.time] = HwmTable.time.max(time) |
| 98 | + it[HwmTable.lastUpdated] = lastUpdated |
| 99 | + } == 1 |
| 100 | + |
| 101 | +infix fun <T> ExpressionWithColumnType<T>.max(t: T): Greatest<T> = Greatest( |
| 102 | + expr1 = this, |
| 103 | + expr2 = wrap(t), |
| 104 | + columnType = this.columnType |
| 105 | +) |
| 106 | + |
| 107 | +class Greatest<T>( |
| 108 | + expr1: Expression<T>, |
| 109 | + expr2: Expression<T>, |
| 110 | + columnType: IColumnType<T & Any> |
| 111 | +) : CustomFunction<T>( |
| 112 | + functionName = "greatest", |
| 113 | + columnType = columnType, |
| 114 | + expr1, |
| 115 | + expr2 |
| 116 | +) |
0 commit comments