Skip to content

Commit 8939a02

Browse files
committed
improve ctid class
1 parent 91b2f2a commit 8939a02

File tree

3 files changed

+20
-55
lines changed

3 files changed

+20
-55
lines changed

airbyte-integrations/connectors/source-postgres/src/main/kotlin/io/airbyte/integrations/source/postgres/PostgresSourceJdbcPartitionFactory.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ class PostgresSourceJdbcPartitionFactory(
322322
private fun PostgresSourceJdbcSplittableSnapshotPartition.split(
323323
splitPointValues: List<PostgresSourceJdbcStreamStateValue>
324324
): List<PostgresSourceJdbcSplittableSnapshotPartition> {
325-
val inners: List<Ctid> = splitPointValues.map { Ctid(it.ctid!!) }
325+
val inners: List<Ctid> = splitPointValues.map { Ctid.of(it.ctid!!) }
326326
val lbCtid: Ctid? =
327327
lowerBound?.let {
328328
if (it.isNotEmpty()) {
@@ -351,7 +351,7 @@ class PostgresSourceJdbcPartitionFactory(
351351
private fun PostgresSourceJdbcSplittableSnapshotWithCursorPartition.split(
352352
splitPointValues: List<PostgresSourceJdbcStreamStateValue>
353353
): List<PostgresSourceJdbcSplittableSnapshotWithCursorPartition> {
354-
val inners: List<Ctid> = splitPointValues.map { Ctid(it.ctid!!) }
354+
val inners: List<Ctid> = splitPointValues.map { Ctid.of(it.ctid!!) }
355355
val lbCtid: Ctid? =
356356
lowerBound?.let {
357357
if (it.isNotEmpty()) {

airbyte-integrations/connectors/source-postgres/src/main/kotlin/io/airbyte/integrations/source/postgres/PostgresSourceJdbcStreamState.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class PostgresSourceJdbcStreamState(val base: DefaultJdbcStreamState) :
2828
val maybeFilenode: Filenode?
2929
get() = stateValue?.let { sv -> sv.filenode }
3030
val maybeCtid: Ctid?
31-
get() = stateValue?.let { sv -> sv.ctid?.let { Ctid(it) } }
31+
get() = stateValue?.let { sv -> sv.ctid?.let { Ctid.of(it) } }
3232

3333
override fun validatePartition(
3434
partition: JdbcPartition<*>,

airbyte-integrations/connectors/source-postgres/src/main/kotlin/io/airbyte/integrations/source/postgres/ctid/Ctid.kt

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,33 @@
44

55
package io.airbyte.integrations.source.postgres.ctid
66

7-
import java.util.*
87
import java.util.regex.Pattern
98

10-
class Ctid {
11-
val page: Long
9+
data class Ctid (
10+
val page: Long,
1211
val tuple: Long
13-
14-
internal constructor(page: Long, tuple: Long) {
15-
this.page = page
16-
this.tuple = tuple
17-
}
18-
19-
internal constructor(ctid: String) {
20-
val p = Pattern.compile("\\d+")
21-
val m = p.matcher(ctid)
22-
require(m.find()) { "Invalid ctid format" }
23-
val ctidPageStr = m.group()
24-
this.page = ctidPageStr.toLong()
25-
26-
require(m.find()) { "Invalid ctid format" }
27-
val ctidTupleStr = m.group()
28-
this.tuple = ctidTupleStr.toLong()
29-
30-
Objects.requireNonNull<Long?>(this.page)
31-
Objects.requireNonNull<Long?>(this.tuple)
32-
}
33-
12+
) {
3413
override fun toString(): String = "($page,$tuple)"
3514

36-
override fun equals(other: Any?): Boolean {
37-
if (this === other) {
38-
return true
39-
}
40-
if (other == null || javaClass != other.javaClass) {
41-
return false
42-
}
43-
val ctid = other as Ctid
44-
return page == ctid.page && tuple == ctid.tuple
45-
}
15+
companion object {
16+
val ZERO: Ctid = Ctid(0, 0)
4617

47-
override fun hashCode(): Int {
48-
return Objects.hash(page, tuple)
49-
}
18+
fun of(ctid: String): Ctid {
19+
val p = Pattern.compile("\\d+")
20+
val m = p.matcher(ctid)
21+
require(m.find()) { "Invalid ctid format" }
22+
val ctidPageStr = m.group()
23+
val page: Long = ctidPageStr.toLong()
5024

51-
companion object {
52-
val ZERO: Ctid = of(0, 0)
25+
require(m.find()) { "Invalid ctid format" }
26+
val ctidTupleStr = m.group()
27+
val tuple: Long = ctidTupleStr.toLong()
5328

54-
fun of(page: Long, tuple: Long): Ctid {
5529
return Ctid(page, tuple)
5630
}
5731

58-
fun of(ctid: String): Ctid {
59-
return Ctid(ctid)
60-
}
61-
62-
fun inc(ctid: Ctid, maxTuple: Long): Ctid {
63-
return if (ctid.tuple + 1 > maxTuple) of(ctid.page + 1, 1)
64-
else
65-
of(
66-
ctid.page,
67-
ctid.tuple + 1,
68-
)
69-
}
32+
fun inc(ctid: Ctid, maxTuple: Long): Ctid =
33+
if (ctid.tuple + 1 > maxTuple) Ctid(ctid.page + 1, 1)
34+
else Ctid(ctid.page, ctid.tuple + 1)
7035
}
7136
}

0 commit comments

Comments
 (0)