LCOV - code coverage report
Current view: top level - server/cache - PersistentCacheBaseFactory.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 24 31 77.4 %
Date: 2022-11-19 15:00:39 Functions: 7 7 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2021 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.cache;
      16             : 
      17             : import com.google.common.cache.Cache;
      18             : import com.google.common.cache.CacheLoader;
      19             : import com.google.common.cache.LoadingCache;
      20             : import com.google.common.flogger.FluentLogger;
      21             : import com.google.gerrit.common.Nullable;
      22             : import com.google.gerrit.server.config.GerritServerConfig;
      23             : import com.google.gerrit.server.config.SitePaths;
      24             : import java.io.IOException;
      25             : import java.nio.file.Files;
      26             : import java.nio.file.Path;
      27             : import org.eclipse.jgit.lib.Config;
      28             : 
      29             : /**
      30             :  * Base class for persistent cache factory. If the cache.directory property is unset, or disk limit
      31             :  * is zero or negative, it will fall back to in-memory only caches.
      32             :  */
      33             : public abstract class PersistentCacheBaseFactory implements PersistentCacheFactory {
      34         151 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      35             : 
      36             :   protected final MemoryCacheFactory memCacheFactory;
      37             :   protected final Path cacheDir;
      38             :   protected boolean diskEnabled;
      39             :   protected final Config config;
      40             : 
      41             :   public PersistentCacheBaseFactory(
      42         151 :       MemoryCacheFactory memCacheFactory, @GerritServerConfig Config config, SitePaths site) {
      43         151 :     this.cacheDir = getCacheDir(site, config.getString("cache", null, "directory"));
      44         151 :     this.diskEnabled = cacheDir != null;
      45         151 :     this.memCacheFactory = memCacheFactory;
      46         151 :     this.config = config;
      47         151 :   }
      48             : 
      49             :   protected abstract <K, V> Cache<K, V> buildImpl(PersistentCacheDef<K, V> in, long diskLimit);
      50             : 
      51             :   protected abstract <K, V> LoadingCache<K, V> buildImpl(
      52             :       PersistentCacheDef<K, V> in, CacheLoader<K, V> loader, long diskLimit);
      53             : 
      54             :   @Override
      55             :   public <K, V> Cache<K, V> build(PersistentCacheDef<K, V> in) {
      56         151 :     long limit = getDiskLimit(in);
      57             : 
      58         151 :     if (isInMemoryCache(limit)) {
      59         151 :       return memCacheFactory.build(in);
      60             :     }
      61             : 
      62          15 :     return buildImpl(in, limit);
      63             :   }
      64             : 
      65             :   @Override
      66             :   public <K, V> LoadingCache<K, V> build(PersistentCacheDef<K, V> in, CacheLoader<K, V> loader) {
      67         151 :     long limit = getDiskLimit(in);
      68             : 
      69         151 :     if (isInMemoryCache(limit)) {
      70         145 :       return memCacheFactory.build(in, loader);
      71             :     }
      72             : 
      73          15 :     return buildImpl(in, loader, limit);
      74             :   }
      75             : 
      76             :   private <K, V> long getDiskLimit(PersistentCacheDef<K, V> in) {
      77         151 :     return config.getLong("cache", in.configKey(), "diskLimit", in.diskLimit());
      78             :   }
      79             : 
      80             :   private <K, V> boolean isInMemoryCache(long diskLimit) {
      81         151 :     return !diskEnabled || diskLimit <= 0;
      82             :   }
      83             : 
      84             :   @Nullable
      85             :   private static Path getCacheDir(SitePaths site, String name) {
      86         151 :     if (name == null) {
      87         145 :       return null;
      88             :     }
      89          15 :     Path loc = site.resolve(name);
      90          15 :     if (!Files.exists(loc)) {
      91             :       try {
      92           0 :         Files.createDirectories(loc);
      93           0 :       } catch (IOException e) {
      94           0 :         logger.atWarning().log("Can't create disk cache: %s", loc.toAbsolutePath());
      95           0 :         return null;
      96           0 :       }
      97             :     }
      98          15 :     if (!Files.isWritable(loc)) {
      99           0 :       logger.atWarning().log("Can't write to disk cache: %s", loc.toAbsolutePath());
     100           0 :       return null;
     101             :     }
     102          15 :     logger.atInfo().log("Enabling disk cache %s", loc.toAbsolutePath());
     103          15 :     return loc;
     104             :   }
     105             : }

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