Search in sources :

Example 1 with MostSpecificComparator

use of com.google.gerrit.server.util.MostSpecificComparator in project gerrit by GerritCodeReview.

the class SectionSortCache method sort.

void sort(String ref, List<AccessSection> sections) {
    final int cnt = sections.size();
    if (cnt <= 1) {
        return;
    }
    EntryKey key = EntryKey.create(ref, sections);
    EntryVal val = cache.getIfPresent(key);
    if (val != null) {
        int[] srcIdx = val.order;
        if (srcIdx != null) {
            AccessSection[] srcList = copy(sections);
            for (int i = 0; i < cnt; i++) {
                sections.set(i, srcList[srcIdx[i]]);
            }
        } else {
        // Identity transform. No sorting is required.
        }
    } else {
        boolean poison = false;
        IdentityHashMap<AccessSection, Integer> srcMap = new IdentityHashMap<>();
        for (int i = 0; i < cnt; i++) {
            poison |= srcMap.put(sections.get(i), i) != null;
        }
        Collections.sort(sections, new MostSpecificComparator(ref));
        int[] srcIdx;
        if (isIdentityTransform(sections, srcMap)) {
            srcIdx = null;
        } else {
            srcIdx = new int[cnt];
            for (int i = 0; i < cnt; i++) {
                srcIdx[i] = srcMap.get(sections.get(i));
            }
        }
        if (poison) {
            log.error("Received duplicate AccessSection instances, not caching sort");
        } else {
            cache.put(key, new EntryVal(srcIdx));
        }
    }
}
Also used : MostSpecificComparator(com.google.gerrit.server.util.MostSpecificComparator) IdentityHashMap(java.util.IdentityHashMap) AccessSection(com.google.gerrit.common.data.AccessSection)

Aggregations

AccessSection (com.google.gerrit.common.data.AccessSection)1 MostSpecificComparator (com.google.gerrit.server.util.MostSpecificComparator)1 IdentityHashMap (java.util.IdentityHashMap)1