Skip to content

Commit

Permalink
Improve performance by avoiding repeated operations
Browse files Browse the repository at this point in the history
  • Loading branch information
twthorn committed Mar 5, 2025
1 parent 10d8e6c commit b99788f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/main/java/io/debezium/connector/vitess/Vgtid.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
Expand Down Expand Up @@ -36,6 +37,8 @@ public class Vgtid {
private final Binlogdata.VGtid rawVgtid;
private final List<ShardGtid> shardGtids = new ArrayList<>();
private final HashMap<String, ShardGtid> shardNameToShardGtid = new HashMap<>();
// Vgtid cannot be modified, so we can cache the string representation to improve performance
private Optional<String> cachedVgtidString = Optional.empty();

private Vgtid(Binlogdata.VGtid rawVgtid) {
this.rawVgtid = rawVgtid;
Expand Down Expand Up @@ -119,8 +122,12 @@ public boolean isSingleShard() {

@Override
public String toString() {
if (cachedVgtidString.isPresent()) {
return cachedVgtidString.get();
}
try {
return MAPPER.writeValueAsString(shardGtids);
cachedVgtidString = Optional.of(MAPPER.writeValueAsString(shardGtids));
return cachedVgtidString.get();
}
catch (JsonProcessingException e) {
throw new IllegalStateException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class VitessChangeRecordEmitter extends RelationalChangeRecordEmitter<VitessPart
private final VitessDatabaseSchema schema;
private final VitessConnectorConfig connectorConfig;
private final TableId tableId;
private final Table table;
private final boolean includeUnknownDatatypes;

VitessChangeRecordEmitter(
VitessPartition partition,
Expand All @@ -51,6 +53,9 @@ class VitessChangeRecordEmitter extends RelationalChangeRecordEmitter<VitessPart
this.connectorConfig = connectorConfig;
this.tableId = VitessDatabaseSchema.parse(message.getTable());
Objects.requireNonNull(tableId);
this.table = schema.tableFor(tableId);
Objects.requireNonNull(table);
this.includeUnknownDatatypes = connectorConfig.includeUnknownDatatypes();
}

@Override
Expand Down Expand Up @@ -95,15 +100,12 @@ private Object[] columnValues(List<ReplicationMessage.Column> columns, TableId t
if (columns == null || columns.isEmpty()) {
return null;
}
final Table table = schema.tableFor(tableId);
Objects.requireNonNull(table);

Object[] values = new Object[columns.size()];
for (ReplicationMessage.Column column : columns) {
final String columnName = Strings.unquoteIdentifierPart(column.getName());
int position = getPosition(columnName, table, values.length);
if (position != -1) {
Object value = column.getValue(connectorConfig.includeUnknownDatatypes());
Object value = column.getValue(includeUnknownDatatypes);
values[position] = value;
}
else {
Expand Down

0 comments on commit b99788f

Please sign in to comment.