LCOV - code coverage report
Current view: top level - server/cache/mem - PassthroughLoadingCache.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 21 36 58.3 %
Date: 2022-11-19 15:00:39 Functions: 11 20 55.0 %

          Line data    Source code
       1             : // Copyright (C) 2022 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.mem;
      16             : 
      17             : import com.google.common.cache.CacheLoader;
      18             : import com.google.common.cache.CacheLoader.UnsupportedLoadingOperationException;
      19             : import com.google.common.cache.CacheStats;
      20             : import com.google.common.cache.LoadingCache;
      21             : import com.google.common.collect.ImmutableMap;
      22             : import com.google.gerrit.common.Nullable;
      23             : import java.util.Map;
      24             : import java.util.concurrent.Callable;
      25             : import java.util.concurrent.ConcurrentHashMap;
      26             : import java.util.concurrent.ConcurrentMap;
      27             : import java.util.concurrent.ExecutionException;
      28             : 
      29             : /** Implementation of a NOOP cache that just passes all gets to the loader */
      30             : public class PassthroughLoadingCache<K, V> implements LoadingCache<K, V> {
      31             : 
      32             :   private final CacheLoader<? super K, V> cacheLoader;
      33             : 
      34         153 :   public PassthroughLoadingCache(CacheLoader<? super K, V> cacheLoader) {
      35         153 :     this.cacheLoader = cacheLoader;
      36         153 :   }
      37             : 
      38             :   @Override
      39             :   public @Nullable V getIfPresent(Object key) {
      40           0 :     return null;
      41             :   }
      42             : 
      43             :   @Override
      44             :   public V get(K key, Callable<? extends V> loader) throws ExecutionException {
      45             :     try {
      46           0 :       return loader.call();
      47           0 :     } catch (Exception e) {
      48           0 :       throw new ExecutionException(e);
      49             :     }
      50             :   }
      51             : 
      52             :   @Override
      53             :   public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
      54           0 :     return ImmutableMap.of();
      55             :   }
      56             : 
      57             :   @Override
      58           0 :   public void put(K key, V value) {}
      59             : 
      60             :   @Override
      61           0 :   public void putAll(Map<? extends K, ? extends V> m) {}
      62             : 
      63             :   @Override
      64         104 :   public void invalidate(Object key) {}
      65             : 
      66             :   @Override
      67           0 :   public void invalidateAll(Iterable<?> keys) {}
      68             : 
      69             :   @Override
      70           1 :   public void invalidateAll() {}
      71             : 
      72             :   @Override
      73             :   public long size() {
      74          17 :     return 0;
      75             :   }
      76             : 
      77             :   @Override
      78             :   public CacheStats stats() {
      79          17 :     return new CacheStats(0, 0, 0, 0, 0, 0);
      80             :   }
      81             : 
      82             :   @Override
      83           0 :   public void cleanUp() {}
      84             : 
      85             :   @Override
      86             :   public V get(K key) throws ExecutionException {
      87             :     try {
      88         151 :       return cacheLoader.load(key);
      89           3 :     } catch (Exception e) {
      90           3 :       throw new ExecutionException(e);
      91             :     }
      92             :   }
      93             : 
      94             :   @Override
      95             :   public V getUnchecked(K key) {
      96             :     try {
      97           1 :       return cacheLoader.load(key);
      98           0 :     } catch (Exception e) {
      99           0 :       throw new IllegalStateException(e);
     100             :     }
     101             :   }
     102             : 
     103             :   @Override
     104             :   public ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException {
     105             :     try {
     106             :       try {
     107          16 :         return getAllBulk(keys);
     108         145 :       } catch (UnsupportedLoadingOperationException e) {
     109         145 :         return getAllIndividually(keys);
     110             :       }
     111           0 :     } catch (Exception e) {
     112           0 :       throw new ExecutionException(e);
     113             :     }
     114             :   }
     115             : 
     116             :   private ImmutableMap<K, V> getAllIndividually(Iterable<? extends K> keys) throws Exception {
     117         145 :     ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
     118         145 :     for (K k : keys) {
     119         145 :       builder.put(k, cacheLoader.load(k));
     120         145 :     }
     121         145 :     return builder.build();
     122             :   }
     123             : 
     124             :   @SuppressWarnings("unchecked")
     125             :   private ImmutableMap<K, V> getAllBulk(Iterable<? extends K> keys) throws Exception {
     126          16 :     return (ImmutableMap<K, V>) ImmutableMap.copyOf(cacheLoader.loadAll(keys));
     127             :   }
     128             : 
     129             :   @Override
     130             :   public V apply(K key) {
     131           0 :     return getUnchecked(key);
     132             :   }
     133             : 
     134             :   @Override
     135           0 :   public void refresh(K key) {}
     136             : 
     137             :   @Override
     138             :   public ConcurrentMap<K, V> asMap() {
     139          15 :     return new ConcurrentHashMap<>();
     140             :   }
     141             : }

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