-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve encapsulation, clear up some Javadoc warnings
- Loading branch information
Showing
5 changed files
with
189 additions
and
108 deletions.
There are no files selected for viewing
102 changes: 47 additions & 55 deletions
102
postgres-cdc/src/main/java/io/github/rieske/cdc/DatabaseChange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,57 @@ | ||
package io.github.rieske.cdc; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DatabaseChange { | ||
public final Action action; | ||
public final String schema; | ||
public final String table; | ||
public final Map<String, String> columns; | ||
|
||
@JsonCreator | ||
DatabaseChange( | ||
@JsonProperty("action") Action action, | ||
@JsonProperty("schema") String schema, | ||
@JsonProperty("table") String table, | ||
@JsonProperty("columns") List<Column> columns | ||
) { | ||
this.action = action; | ||
this.schema = schema; | ||
this.table = table; | ||
Map<String, String> mutableColumns = new HashMap<>(); | ||
for (Column column : columns) { | ||
mutableColumns.put(column.name, column.value); | ||
} | ||
this.columns = Collections.unmodifiableMap(mutableColumns); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "DatabaseChange{" + | ||
"action='" + action + '\'' + | ||
", schema='" + schema + '\'' + | ||
", table='" + table + '\'' + | ||
", columns=" + columns + | ||
'}'; | ||
} | ||
|
||
public enum Action { | ||
@JsonProperty("I") | ||
/** | ||
* A record, representing a change in the database. | ||
* Exposes the action (INSERT/UPDATE/DELETE/TRUNCATE), schema, table, and a map of column names and their values, all as Strings. | ||
*/ | ||
public interface DatabaseChange { | ||
|
||
/** | ||
* @return the INSERT/UPDATE/DELETE/TRUNCATE action that yielded this change. | ||
*/ | ||
Action action(); | ||
|
||
/** | ||
* @return the schema where this change originated. | ||
*/ | ||
String schema(); | ||
|
||
/** | ||
* @return the table where this change originated. | ||
*/ | ||
String table(); | ||
|
||
/** | ||
* @return a Map of column names and their values as Strings from the database change. | ||
* Contains all columns from the changed table - both changed and unchanged. | ||
*/ | ||
Map<String, String> columns(); | ||
|
||
/** | ||
* An action that was performed on the database to cause a change. | ||
*/ | ||
enum Action { | ||
|
||
/** | ||
* Indicates that the database change was created using INSERT command | ||
*/ | ||
INSERT, | ||
@JsonProperty("U") | ||
|
||
/** | ||
* Indicates that the database change was created using UPDATE command | ||
*/ | ||
UPDATE, | ||
@JsonProperty("D") | ||
DELETE, | ||
@JsonProperty("T") | ||
TRUNCATE | ||
} | ||
|
||
static class Column { | ||
private final String name; | ||
private final String value; | ||
/** | ||
* Indicates that the database change was created using DELETE command | ||
*/ | ||
DELETE, | ||
|
||
@JsonCreator | ||
Column(@JsonProperty("name") String name, @JsonProperty("value") String value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
/** | ||
* Indicates that the database change was created using TRUNCATE command | ||
*/ | ||
TRUNCATE | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
postgres-cdc/src/main/java/io/github/rieske/cdc/JsonDeserializedDatabaseChange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.github.rieske.cdc; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
class JsonDeserializedDatabaseChange implements DatabaseChange { | ||
private final Action action; | ||
private final String schema; | ||
private final String table; | ||
private final Map<String, String> columns; | ||
|
||
@JsonCreator | ||
JsonDeserializedDatabaseChange( | ||
@JsonProperty("action") String action, | ||
@JsonProperty("schema") String schema, | ||
@JsonProperty("table") String table, | ||
@JsonProperty("columns") List<Column> columns | ||
) { | ||
switch (action) { | ||
case "I": | ||
this.action = Action.INSERT; | ||
break; | ||
case "U": | ||
this.action = Action.UPDATE; | ||
break; | ||
case "D": | ||
this.action = Action.DELETE; | ||
break; | ||
case "T": | ||
this.action = Action.TRUNCATE; | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Unrecognized database change action: " + action); | ||
} | ||
this.schema = schema; | ||
this.table = table; | ||
Map<String, String> mutableColumns = new HashMap<>(); | ||
for (Column column : columns) { | ||
mutableColumns.put(column.name, column.value); | ||
} | ||
this.columns = Collections.unmodifiableMap(mutableColumns); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "DatabaseChange{" + | ||
"action='" + action + '\'' + | ||
", schema='" + schema + '\'' + | ||
", table='" + table + '\'' + | ||
", columns=" + columns + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public Action action() { | ||
return action; | ||
} | ||
|
||
@Override | ||
public String schema() { | ||
return schema; | ||
} | ||
|
||
@Override | ||
public String table() { | ||
return table; | ||
} | ||
|
||
@Override | ||
public Map<String, String> columns() { | ||
return columns; | ||
} | ||
|
||
static class Column { | ||
private final String name; | ||
private final String value; | ||
|
||
@JsonCreator | ||
Column(@JsonProperty("name") String name, @JsonProperty("value") String value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters