All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.data.relational.core.conversion.DeleteBatchingAggregateChange Maven / Gradle / Ivy

There is a newer version: 3.3.2
Show newest version
package org.springframework.data.relational.core.conversion;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * A {@link BatchingAggregateChange} implementation for delete changes that can contain actions for one or more delete
 * operations. When consumed, actions are yielded in the appropriate entity tree order with deletes carried out from
 * leaves to root. All operations that can be batched are grouped and combined to offer the ability for an optimized
 * batch operation to be used.
 *
 * @author Chirag Tailor
 * @since 3.0
 */
public class DeleteBatchingAggregateChange implements BatchingAggregateChange> {

	private final Class entityType;
	private final List> rootActionsWithoutVersion = new ArrayList<>();
	private final List> rootActionsWithVersion = new ArrayList<>();
	private final List> lockActions = new ArrayList<>();
	private final BatchedActions deleteActions = BatchedActions.batchedDeletes();

	DeleteBatchingAggregateChange(Class entityType) {
		this.entityType = entityType;
	}

	@Override
	public Kind getKind() {
		return Kind.DELETE;
	}

	@Override
	public Class getEntityType() {
		return entityType;
	}

	@Override
	public void forEachAction(Consumer> consumer) {

		lockActions.forEach(consumer);
		deleteActions.forEach(consumer);
		if (rootActionsWithoutVersion.size() > 1) {
			consumer.accept(new DbAction.BatchDeleteRoot<>(rootActionsWithoutVersion));
		} else {
			rootActionsWithoutVersion.forEach(consumer);
		}
		rootActionsWithVersion.forEach(consumer);
	}

	@Override
	public void add(DeleteAggregateChange aggregateChange) {

		aggregateChange.forEachAction(action -> {
			if (action instanceof DbAction.DeleteRoot deleteRootAction) {
				// noinspection unchecked
				addDeleteRoot((DbAction.DeleteRoot) deleteRootAction);
			} else if (action instanceof DbAction.Delete deleteAction) {
				deleteActions.add(deleteAction);
			} else if (action instanceof DbAction.AcquireLockRoot lockRootAction) {
				lockActions.add(lockRootAction);
			}
		});
	}

	private void addDeleteRoot(DbAction.DeleteRoot action) {

		if (action.getPreviousVersion() == null) {
			rootActionsWithoutVersion.add(action);
		} else {
			rootActionsWithVersion.add(action);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy