Skip to content

Commit

Permalink
Add feature to reverse the direction of self-importing
Browse files Browse the repository at this point in the history
This allows to, for example, export a remote database to HSQLDB
  • Loading branch information
A248 committed Apr 12, 2024
1 parent 09c9ffe commit 32b1fc1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ default ConnectionSource toConnectionSource() {
"The settings here are used exactly as in sql.yml - in fact, they are the same settings."})
interface SelfSettings extends DatabaseSettingsConfig {

@ConfKey("reverse-direction")
@ConfComments({
"Whether to reverse the direction of the self-import. If enabled, this will import TO the database",
"configured here, rather than FROM it.",
"",
"Normally, LibertyBans will import from this database, to the current database defined in sql.yml.",
"If this option is enabled, LibertyBans will import to the current database in sql.yml, from this database."
})
@ConfDefault.DefaultBoolean(false)
boolean reverseDirection();

}

@ConfKey("ban-manager")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,26 @@ public CentralisedFuture<Void> transferAllData() {
public CentralisedFuture<Void> transferAllData(Path folder) {
logger.info("Beginning self-import process");

return queryExecutor.get().execute((target) -> {
return queryExecutor.get().execute((currentDbCtx) -> {
ImportConfig importConfig = configs.getImportConfig();
DatabaseResult dbResult = new DatabaseSettings(folder, databaseManager).create(importConfig.self());
ImportConfig.SelfSettings selfSettings = importConfig.self();
DatabaseResult dbResult = new DatabaseSettings(folder, databaseManager).create(selfSettings);

try (StandardDatabase database = dbResult.database()) {
try (StandardDatabase peerDb = dbResult.database()) {

if (!dbResult.success()) {
logger.warn("Failed to connect to import source");
return;
}
database.execute((source) -> {
peerDb.execute((peerDbCtx) -> {
DSLContext source, target;
if (selfSettings.reverseDirection()) {
source = currentDbCtx;
target = peerDbCtx;
} else {
source = peerDbCtx;
target = currentDbCtx;
}
var selfImport = new SelfImport(source, target, importConfig.retrievalSize());
selfImport.runTransfer();
selfImport.updateSequences();
Expand All @@ -98,17 +107,7 @@ public CentralisedFuture<Void> transferAllData(Path folder) {
});
}

private static final class SelfImport {

private final DSLContext source;
private final DSLContext target;
private final int maxBatchSize;

private SelfImport(DSLContext source, DSLContext target, int maxBatchSize) {
this.source = source;
this.target = target;
this.maxBatchSize = maxBatchSize;
}
private record SelfImport(DSLContext source, DSLContext target, int maxBatchSize) {

private void runTransfer() {
for (Table<?> table : DatabaseConstants.allTables(DatabaseConstants.TableOrder.REFERENTS_FIRST)) {
Expand Down

0 comments on commit 32b1fc1

Please sign in to comment.