org.nuiton.topia.templates.sql.order.ReplicationOrderBuilderWithStandalone Maven / Gradle / Ivy
package org.nuiton.topia.templates.sql.order;
/*-
* #%L
* Toolkit :: Templates
* %%
* Copyright (C) 2017 - 2024 Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import com.google.common.collect.ArrayListMultimap;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nuiton.topia.service.sql.metadata.TopiaMetadataEntity;
import org.nuiton.topia.service.sql.metadata.TopiaMetadataModel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/**
* To compute replication order for a set of entities without any entry point.
*
* Created on 24/09/2020.
*
* @author Tony Chemit - [email protected]
* @since 1.27
*/
public class ReplicationOrderBuilderWithStandalone extends ReplicationOrderBuilder {
private static final Logger log = LogManager.getLogger(ReplicationOrderBuilderWithStandalone.class);
public static List build(TopiaMetadataModel metadataModel) {
return new ReplicationOrderBuilderWithStandalone(Objects.requireNonNull(metadataModel)).build();
}
public static List build(TopiaMetadataModel metadataModel, Set entities) {
return new ReplicationOrderBuilderWithStandalone(Objects.requireNonNull(metadataModel), entities).build();
}
protected ReplicationOrderBuilderWithStandalone(TopiaMetadataModel metadataModel, Set entities) {
super(metadataModel, entities);
}
protected ReplicationOrderBuilderWithStandalone(TopiaMetadataModel metadataModel) {
super(metadataModel, metadataModel.streamWithStandalone().collect(Collectors.toSet()));
}
@Override
public List build() {
Set entities = getEntities();
List result = new ArrayList<>(entities.size());
List remainingEntities = new ArrayList<>();
ArrayListMultimap dependenciesMapping = computeDependencies(entities);
ArrayListMultimap dependenciesCount = ArrayListMultimap.create();
int maxDepth = 0;
for (TopiaMetadataEntity entity : TopiaMetadataEntity.sortByFqn(entities)) {
List dependencies = dependenciesMapping.get(entity);
int size = dependencies == null ? 0 : dependencies.size();
maxDepth = Math.max(maxDepth, size);
dependenciesCount.put(new MutableInt(size), entity);
}
log.debug(String.format("Found %d max depth to process", maxDepth));
int i = 0;
int resultSize = entities.size();
while (result.size() != resultSize) {
if (i <= maxDepth) {
List entitiesForThisRound = dependenciesCount.get(new MutableInt(i));
if (entitiesForThisRound != null) {
remainingEntities.addAll(entitiesForThisRound);
}
}
addRound(i++, remainingEntities, dependenciesMapping, result);
}
return Collections.unmodifiableList(result);
}
private void addRound(int roundIndex, List entitiesForThisRound, ArrayListMultimap dependenciesMapping, List result) {
if (roundIndex == 0) {
// entities with no dependencies
result.addAll(entitiesForThisRound);
entitiesForThisRound.clear();
return;
}
Iterator iterator = entitiesForThisRound.iterator();
while (iterator.hasNext()) {
TopiaMetadataEntity entity = iterator.next();
int indexOf = result.indexOf(entity);
boolean alreadyIn = indexOf > -1;
if (alreadyIn) {
iterator.remove();
continue;
}
List dependencies = dependenciesMapping.get(entity);
if (result.containsAll(dependencies)) {
result.add(entity);
iterator.remove();
}
}
}
}