use of org.apache.cayenne.ashwood.graph.StrongConnection in project cayenne by apache.
the class AshwoodEntitySorter method doIndexSorter.
/**
* Reindexes internal sorter without synchronization.
*/
protected void doIndexSorter() {
Map<DbEntity, List<DbRelationship>> reflexiveDbEntities = new HashMap<>();
Digraph<DbEntity, List<DbAttribute>> referentialDigraph = new MapDigraph<>();
if (entityResolver != null) {
for (DbEntity entity : entityResolver.getDbEntities()) {
referentialDigraph.addVertex(entity);
}
}
for (DbEntity destination : entityResolver.getDbEntities()) {
for (DbRelationship candidate : destination.getRelationships()) {
if ((!candidate.isToMany() && !candidate.isToDependentPK()) || candidate.isToMasterPK()) {
DbEntity origin = candidate.getTargetEntity();
boolean newReflexive = destination.equals(origin);
for (DbJoin join : candidate.getJoins()) {
DbAttribute targetAttribute = join.getTarget();
if (targetAttribute.isPrimaryKey()) {
if (newReflexive) {
List<DbRelationship> reflexiveRels = reflexiveDbEntities.get(destination);
if (reflexiveRels == null) {
reflexiveRels = new ArrayList<>(1);
reflexiveDbEntities.put(destination, reflexiveRels);
}
reflexiveRels.add(candidate);
newReflexive = false;
}
List<DbAttribute> fks = referentialDigraph.getArc(origin, destination);
if (fks == null) {
fks = new ArrayList<>();
referentialDigraph.putArc(origin, destination, fks);
}
fks.add(targetAttribute);
}
}
}
}
}
StrongConnection<DbEntity, List<DbAttribute>> contractor = new StrongConnection<>(referentialDigraph);
Digraph<Collection<DbEntity>, Collection<List<DbAttribute>>> contractedReferentialDigraph = new MapDigraph<>();
contractor.contract(contractedReferentialDigraph);
IndegreeTopologicalSort<Collection<DbEntity>> sorter = new IndegreeTopologicalSort<>(contractedReferentialDigraph);
Map<DbEntity, ComponentRecord> components = new HashMap<>(contractedReferentialDigraph.order());
int componentIndex = 0;
while (sorter.hasNext()) {
Collection<DbEntity> component = sorter.next();
ComponentRecord rec = new ComponentRecord(componentIndex++, component);
for (DbEntity table : component) {
components.put(table, rec);
}
}
this.reflexiveDbEntities = reflexiveDbEntities;
this.components = components;
}
Aggregations