LCOV - code coverage report
Current view: top level - server/project - ProjectHierarchyIterator.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 23 34 67.6 %
Date: 2022-11-19 15:00:39 Functions: 6 7 85.7 %

          Line data    Source code
       1             : // Copyright (C) 2013 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.project;
      16             : 
      17             : import com.google.common.base.Joiner;
      18             : import com.google.common.collect.Lists;
      19             : import com.google.common.collect.Sets;
      20             : import com.google.common.flogger.FluentLogger;
      21             : import com.google.gerrit.common.Nullable;
      22             : import com.google.gerrit.entities.Project;
      23             : import com.google.gerrit.server.config.AllProjectsName;
      24             : import java.util.Iterator;
      25             : import java.util.List;
      26             : import java.util.NoSuchElementException;
      27             : import java.util.Optional;
      28             : import java.util.Set;
      29             : 
      30             : /**
      31             :  * Iterates from a project up through its parents to All-Projects.
      32             :  *
      33             :  * <p>If a cycle is detected the cycle is broken and All-Projects is visited.
      34             :  */
      35             : class ProjectHierarchyIterator implements Iterator<ProjectState> {
      36         148 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      37             : 
      38             :   private final ProjectCache cache;
      39             :   private final AllProjectsName allProjectsName;
      40             :   private final Set<Project.NameKey> seen;
      41             :   private ProjectState next;
      42             : 
      43         148 :   ProjectHierarchyIterator(ProjectCache c, AllProjectsName all, ProjectState firstResult) {
      44         148 :     cache = c;
      45         148 :     allProjectsName = all;
      46             : 
      47         148 :     seen = Sets.newLinkedHashSet();
      48         148 :     seen.add(firstResult.getNameKey());
      49         148 :     next = firstResult;
      50         148 :   }
      51             : 
      52             :   @Override
      53             :   public boolean hasNext() {
      54         148 :     return next != null;
      55             :   }
      56             : 
      57             :   @Override
      58             :   public ProjectState next() {
      59         148 :     ProjectState n = next;
      60         148 :     if (n == null) {
      61           0 :       throw new NoSuchElementException();
      62             :     }
      63         148 :     next = computeNext(n);
      64         148 :     return n;
      65             :   }
      66             : 
      67             :   @Nullable
      68             :   private ProjectState computeNext(ProjectState n) {
      69         148 :     Project.NameKey parentName = n.getProject().getParent();
      70         148 :     if (parentName != null && visit(parentName)) {
      71         144 :       Optional<ProjectState> p = cache.get(parentName);
      72         144 :       if (p.isPresent()) {
      73         144 :         return p.get();
      74             :       }
      75             :     }
      76             : 
      77             :     // Parent does not exist or was already visited.
      78             :     // Fall back to visit All-Projects exactly once.
      79         148 :     if (seen.add(allProjectsName)) {
      80         142 :       return cache.getAllProjects();
      81             :     }
      82         148 :     return null;
      83             :   }
      84             : 
      85             :   private boolean visit(Project.NameKey parentName) {
      86         144 :     if (seen.add(parentName)) {
      87         144 :       return true;
      88             :     }
      89             : 
      90           0 :     List<String> order = Lists.newArrayListWithCapacity(seen.size() + 1);
      91           0 :     for (Project.NameKey p : seen) {
      92           0 :       order.add(p.get());
      93           0 :     }
      94           0 :     int idx = order.lastIndexOf(parentName.get());
      95           0 :     order.add(parentName.get());
      96           0 :     logger.atWarning().log(
      97           0 :         "Cycle detected in projects: %s", Joiner.on(" -> ").join(order.subList(idx, order.size())));
      98           0 :     return false;
      99             :   }
     100             : 
     101             :   @Override
     102             :   public void remove() {
     103           0 :     throw new UnsupportedOperationException();
     104             :   }
     105             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750