Line data Source code
1 : // Copyright (C) 2009 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 : 16 : package com.google.gerrit.server.patch; 17 : 18 : import com.google.common.cache.Cache; 19 : import com.google.common.flogger.FluentLogger; 20 : import com.google.common.util.concurrent.UncheckedExecutionException; 21 : import com.google.gerrit.entities.Project; 22 : import com.google.gerrit.server.cache.CacheModule; 23 : import com.google.gerrit.server.config.GerritServerConfig; 24 : import com.google.inject.Inject; 25 : import com.google.inject.Module; 26 : import com.google.inject.Singleton; 27 : import com.google.inject.name.Named; 28 : import java.util.concurrent.ExecutionException; 29 : import org.eclipse.jgit.errors.LargeObjectException; 30 : import org.eclipse.jgit.lib.Config; 31 : 32 : /** Provides a cached list of {@link PatchListEntry}. */ 33 : @Singleton 34 : public class PatchListCacheImpl implements PatchListCache { 35 152 : public static final FluentLogger logger = FluentLogger.forEnclosingClass(); 36 : 37 : static final String INTRA_NAME = "diff_intraline"; 38 : static final String DIFF_SUMMARY = "diff_summary"; 39 : 40 : public static Module module() { 41 152 : return new CacheModule() { 42 : @Override 43 : protected void configure() { 44 152 : factory(IntraLineLoader.Factory.class); 45 152 : persist(INTRA_NAME, IntraLineDiffKey.class, IntraLineDiff.class) 46 152 : .maximumWeight(10 << 20) 47 152 : .weigher(IntraLineWeigher.class); 48 : 49 152 : factory(DiffSummaryLoader.Factory.class); 50 152 : persist(DIFF_SUMMARY, DiffSummaryKey.class, DiffSummary.class) 51 152 : .maximumWeight(10 << 20) 52 152 : .weigher(DiffSummaryWeigher.class) 53 152 : .diskLimit(1 << 30); 54 : 55 152 : bind(PatchListCacheImpl.class); 56 152 : bind(PatchListCache.class).to(PatchListCacheImpl.class); 57 152 : } 58 : }; 59 : } 60 : 61 : private final Cache<IntraLineDiffKey, IntraLineDiff> intraCache; 62 : private final Cache<DiffSummaryKey, DiffSummary> diffSummaryCache; 63 : private final IntraLineLoader.Factory intraLoaderFactory; 64 : private final DiffSummaryLoader.Factory diffSummaryLoaderFactory; 65 : private final boolean computeIntraline; 66 : 67 : @Inject 68 : PatchListCacheImpl( 69 : @Named(INTRA_NAME) Cache<IntraLineDiffKey, IntraLineDiff> intraCache, 70 : @Named(DIFF_SUMMARY) Cache<DiffSummaryKey, DiffSummary> diffSummaryCache, 71 : IntraLineLoader.Factory intraLoaderFactory, 72 : DiffSummaryLoader.Factory diffSummaryLoaderFactory, 73 150 : @GerritServerConfig Config cfg) { 74 150 : this.intraCache = intraCache; 75 150 : this.diffSummaryCache = diffSummaryCache; 76 150 : this.intraLoaderFactory = intraLoaderFactory; 77 150 : this.diffSummaryLoaderFactory = diffSummaryLoaderFactory; 78 : 79 150 : this.computeIntraline = 80 150 : cfg.getBoolean( 81 150 : "cache", INTRA_NAME, "enabled", cfg.getBoolean("cache", "diff", "intraline", true)); 82 150 : } 83 : 84 : @Override 85 : public IntraLineDiff getIntraLineDiff(IntraLineDiffKey key, IntraLineDiffArgs args) { 86 5 : if (computeIntraline) { 87 : try { 88 5 : return intraCache.get(key, intraLoaderFactory.create(key, args)); 89 0 : } catch (ExecutionException | LargeObjectException e) { 90 0 : IntraLineLoader.logger.atWarning().withCause(e).log("Error computing %s", key); 91 0 : return new IntraLineDiff(IntraLineDiff.Status.ERROR); 92 : } 93 : } 94 0 : return new IntraLineDiff(IntraLineDiff.Status.DISABLED); 95 : } 96 : 97 : @Override 98 : public DiffSummary getDiffSummary(DiffSummaryKey key, Project.NameKey project) 99 : throws PatchListNotAvailableException { 100 : try { 101 103 : return diffSummaryCache.get(key, diffSummaryLoaderFactory.create(key, project)); 102 0 : } catch (ExecutionException e) { 103 0 : logger.atWarning().withCause(e).log("Error computing %s", key); 104 0 : throw new PatchListNotAvailableException(e); 105 0 : } catch (UncheckedExecutionException e) { 106 0 : if (e.getCause() instanceof LargeObjectException) { 107 0 : logger.atWarning().withCause(e).log("Error computing %s", key); 108 0 : throw new PatchListNotAvailableException(e); 109 : } 110 0 : throw e; 111 : } 112 : } 113 : }