diff options
Diffstat (limited to 'sonar-batch/src/main/java/org/sonar/batch/index/ResourceCache.java')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/index/ResourceCache.java | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ResourceCache.java b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceCache.java index 329a0184b46..088a50de84c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ResourceCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceCache.java @@ -23,25 +23,51 @@ import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.common.collect.Maps; import org.sonar.api.BatchComponent; +import org.sonar.api.database.model.Snapshot; +import org.sonar.api.resources.Library; import org.sonar.api.resources.Resource; +import javax.annotation.CheckForNull; + +import java.util.Collection; import java.util.Map; -/** - * @since 3.6 - */ public class ResourceCache implements BatchComponent { // resource by component key - private final Map<String, Resource> resources = Maps.newHashMap(); + private final Map<String, BatchResource> resources = Maps.newHashMap(); + // dedicated cache for libraries + private final Map<Library, BatchResource> libraries = Maps.newHashMap(); - public Resource get(String componentKey) { + @CheckForNull + public BatchResource get(String componentKey) { return resources.get(componentKey); } - public ResourceCache add(Resource resource) { + @CheckForNull + public BatchResource get(Resource resource) { + if (!(resource instanceof Library)) { + return resources.get(resource.getEffectiveKey()); + } else { + return libraries.get(resource); + } + } + + public BatchResource add(Resource resource, Snapshot s) { String componentKey = resource.getEffectiveKey(); Preconditions.checkState(!Strings.isNullOrEmpty(componentKey), "Missing resource effective key"); - resources.put(componentKey, resource); - return this; + Resource parentResource = resource.getParent(); + BatchResource parent = parentResource != null ? get(parentResource.getEffectiveKey()) : null; + BatchResource batchResource = new BatchResource((long) resources.size() + 1, resource, s, parent); + if (!(resource instanceof Library)) { + // Libraries can have the same effective key than a project so we can't cache by effectiveKey + resources.put(componentKey, batchResource); + } else { + libraries.put((Library) resource, batchResource); + } + return batchResource; + } + + public Collection<BatchResource> all() { + return resources.values(); } } |