Search in sources :

Example 1 with StrongConnection

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;
}
Also used : HashMap(java.util.HashMap) DbAttribute(org.apache.cayenne.map.DbAttribute) StrongConnection(org.apache.cayenne.ashwood.graph.StrongConnection) IndegreeTopologicalSort(org.apache.cayenne.ashwood.graph.IndegreeTopologicalSort) MapDigraph(org.apache.cayenne.ashwood.graph.MapDigraph) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 IndegreeTopologicalSort (org.apache.cayenne.ashwood.graph.IndegreeTopologicalSort)1 MapDigraph (org.apache.cayenne.ashwood.graph.MapDigraph)1 StrongConnection (org.apache.cayenne.ashwood.graph.StrongConnection)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 DbEntity (org.apache.cayenne.map.DbEntity)1 DbJoin (org.apache.cayenne.map.DbJoin)1 DbRelationship (org.apache.cayenne.map.DbRelationship)1