Skip to content

Commit

Permalink
added more transaction modes to import and delete operation
Browse files Browse the repository at this point in the history
  • Loading branch information
clausnagel committed Dec 13, 2024
1 parent 0263967 commit 965b341
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ public Integer call() throws ExecutionException {

if (preview) {
logger.info("Delete is running in preview mode. Features will not be deleted.");
deleter.setTransactionMode(Deleter.TransactionMode.AUTO_ROLLBACK);
} else if (autoCommit) {
logger.info("Committing delete operation after {} feature(s).", commitAfter);
deleter.setAutoCommit(true);
deleter.setTransactionMode(Deleter.TransactionMode.AUTO_COMMIT);
deleteOptions.setBatchSize(commitAfter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ protected boolean doImport() throws ExecutionException {

try {
Importer importer = Importer.newInstance()
.setAutoCommit(!preview)
.setTransactionMode(preview ?
Importer.TransactionMode.AUTO_ROLLBACK :
Importer.TransactionMode.AUTO_COMMIT)
.setImportLogger(importLogger);

AtomicLong counter = new AtomicLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ public class DeleteHelper {
private final DeleteLogger logger;
private final TableHelper tableHelper;
private final List<DeleteLogEntry> logEntries = new ArrayList<>();
private final Deleter.TransactionMode transactionMode;
private final int batchSize;
private final boolean autoCommit;

private int batchCounter;

DeleteHelper(DatabaseAdapter adapter, Connection connection, DeleteOptions options, DeleteLogger logger,
boolean autoCommit) {
Deleter.TransactionMode transactionMode) {
this.adapter = adapter;
this.connection = connection;
this.options = options;
this.logger = logger;
this.autoCommit = autoCommit;
this.transactionMode = transactionMode;

tableHelper = new TableHelper(this);
batchSize = options.getBatchSize() > 0 ?
Expand Down Expand Up @@ -84,7 +84,7 @@ void deleteFeature(long id) throws DeleteException {
logEntries.add(DeleteLogEntry.of(Table.FEATURE, id));
}

executeBatch(false, autoCommit);
executeBatch(false, transactionMode == Deleter.TransactionMode.AUTO_COMMIT);
} catch (Exception e) {
throw new DeleteException("Failed to delete feature (ID: " + id + ").", e);
}
Expand All @@ -101,6 +101,8 @@ void executeBatch(boolean force, boolean commit) throws DeleteException, SQLExce

if (commit) {
connection.commit();
} else if (transactionMode == Deleter.TransactionMode.AUTO_ROLLBACK) {
connection.rollback();
}

updateDeleteLog(commit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ public class Deleter {
private DeleteLogger logger;
private CountLatch countLatch;
private Throwable exception;
private boolean autoCommit = false;
private TransactionMode transactionMode = TransactionMode.NO_COMMIT;

private volatile State state = State.SESSION_NOT_STARTED;
private volatile boolean shouldRun;

public enum TransactionMode {
AUTO_COMMIT,
AUTO_ROLLBACK,
NO_COMMIT
}

public enum State {
SESSION_NOT_STARTED,
SESSION_STARTED,
Expand All @@ -71,12 +77,12 @@ public Deleter setDeleteLogger(DeleteLogger logger) {
return this;
}

public boolean isAutoCommit() {
return autoCommit;
public TransactionMode getTransactionMode() {
return transactionMode;
}

public Deleter setAutoCommit(boolean autoCommit) {
this.autoCommit = autoCommit;
public Deleter setTransactionMode(TransactionMode transactionMode) {
this.transactionMode = transactionMode;
return this;
}

Expand Down Expand Up @@ -105,7 +111,7 @@ public Deleter startSession(DatabaseAdapter adapter, DeleteOptions options) thro
countLatch = new CountLatch();
contexts = ThreadLocal.withInitial(() -> {
try {
DeleteHelper helper = new DeleteHelper(adapter, connection, options, logger, autoCommit);
DeleteHelper helper = new DeleteHelper(adapter, connection, options, logger, transactionMode);
helpers.add(helper);
return helper;
} catch (Exception e) {
Expand Down Expand Up @@ -151,6 +157,8 @@ public void commitSession() throws DeleteException {
|| state == State.SESSION_COMMITTED
|| state == State.SESSION_ABORTED) {
return;
} else if (transactionMode == TransactionMode.AUTO_ROLLBACK) {
throw new DeleteException("Illegal to commit a session in auto-rollback mode.");
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ public class ImportHelper {
private final SequenceHelper sequenceHelper;
private final Map<CacheType, ReferenceCache> caches = new EnumMap<>(CacheType.class);
private final List<ImportLogEntry> logEntries = new ArrayList<>();
private final Importer.TransactionMode transactionMode;
private final int batchSize;
private final boolean autoCommit;

private SequenceValues sequenceValues;
private int batchCounter;

ImportHelper(DatabaseAdapter adapter, ImportOptions options, ReferenceManager referenceManager,
ImportLogger logger, boolean autoCommit) throws SQLException {
ImportLogger logger, Importer.TransactionMode transactionMode) throws SQLException {
this.adapter = adapter;
this.referenceManager = referenceManager;
this.logger = logger;
this.autoCommit = autoCommit;
this.transactionMode = transactionMode;

connection = adapter.getPool().getConnection(false);
schemaMapping = adapter.getSchemaAdapter().getSchemaMapping();
Expand Down Expand Up @@ -117,7 +117,7 @@ FeatureDescriptor importFeature(Feature feature) throws ImportException {
logEntries.add(ImportLogEntry.of(feature, descriptor));
}

executeBatch(false, autoCommit);
executeBatch(false, transactionMode == Importer.TransactionMode.AUTO_COMMIT);
return descriptor;
} catch (Exception e) {
throw new ImportException("Failed to import feature.", e);
Expand Down Expand Up @@ -145,6 +145,8 @@ void executeBatch(boolean force, boolean commit) throws ImportException, SQLExce

if (commit) {
connection.commit();
} else if (transactionMode == Importer.TransactionMode.AUTO_ROLLBACK) {
connection.rollback();
}

updateImportLog(commit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ public class Importer {
private ImportLogger logger;
private CountLatch countLatch;
private Throwable exception;
private boolean autoCommit = true;
private TransactionMode transactionMode = TransactionMode.AUTO_COMMIT;

private volatile State state = State.SESSION_NOT_STARTED;
private volatile boolean shouldRun;

public enum TransactionMode {
AUTO_COMMIT,
AUTO_ROLLBACK,
NO_COMMIT
}

public enum State {
SESSION_NOT_STARTED,
SESSION_STARTED,
Expand All @@ -72,12 +78,12 @@ public Importer setImportLogger(ImportLogger logger) {
return this;
}

public boolean isAutoCommit() {
return autoCommit;
public TransactionMode getTransactionMode() {
return transactionMode;
}

public Importer setAutoCommit(boolean autoCommit) {
this.autoCommit = autoCommit;
public Importer setTransactionMode(TransactionMode transactionMode) {
this.transactionMode = transactionMode;
return this;
}

Expand Down Expand Up @@ -107,7 +113,7 @@ public Importer startSession(DatabaseAdapter adapter, ImportOptions options) thr
countLatch = new CountLatch();
contexts = ThreadLocal.withInitial(() -> {
try {
ImportHelper helper = new ImportHelper(adapter, options, referenceManager, logger, autoCommit);
ImportHelper helper = new ImportHelper(adapter, options, referenceManager, logger, transactionMode);
helpers.add(helper);
return helper;
} catch (Exception e) {
Expand Down Expand Up @@ -152,6 +158,8 @@ public void commitSession() throws ImportException {
|| state == State.SESSION_COMMITTED
|| state == State.SESSION_ABORTED) {
return;
} else if (transactionMode == TransactionMode.AUTO_ROLLBACK) {
throw new ImportException("Illegal to commit a session in auto-rollback mode.");
}

try {
Expand Down

0 comments on commit 965b341

Please sign in to comment.