Line data Source code
1 : // Copyright (C) 2018 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.h2; 16 : 17 : import com.google.common.cache.CacheLoader; 18 : import com.google.common.cache.Weigher; 19 : import com.google.gerrit.common.Nullable; 20 : import com.google.gerrit.server.cache.PersistentCacheDef; 21 : import com.google.gerrit.server.cache.h2.H2CacheImpl.ValueHolder; 22 : import com.google.gerrit.server.cache.serialize.CacheSerializer; 23 : import com.google.inject.TypeLiteral; 24 : import java.time.Duration; 25 : 26 : class H2CacheDefProxy<K, V> implements PersistentCacheDef<K, V> { 27 : private final PersistentCacheDef<K, V> source; 28 : 29 15 : H2CacheDefProxy(PersistentCacheDef<K, V> source) { 30 15 : this.source = source; 31 15 : } 32 : 33 : @Override 34 : @Nullable 35 : public Duration expireAfterWrite() { 36 15 : return source.expireAfterWrite(); 37 : } 38 : 39 : @Override 40 : @Nullable 41 : public Duration expireFromMemoryAfterAccess() { 42 15 : return source.expireFromMemoryAfterAccess(); 43 : } 44 : 45 : @Override 46 : public Duration refreshAfterWrite() { 47 15 : return source.refreshAfterWrite(); 48 : } 49 : 50 : @Nullable 51 : @Override 52 : public Weigher<K, V> weigher() { 53 15 : Weigher<K, V> weigher = source.weigher(); 54 15 : if (weigher == null) { 55 15 : return null; 56 : } 57 : 58 : // introduce weigher that performs calculations 59 : // on value that is being stored not on ValueHolder 60 15 : Weigher<K, ValueHolder<V>> holderWeigher = (k, v) -> weigher.weigh(k, v.value); 61 : @SuppressWarnings("unchecked") 62 15 : Weigher<K, V> ret = (Weigher<K, V>) holderWeigher; 63 15 : return ret; 64 : } 65 : 66 : @Override 67 : public String name() { 68 15 : return source.name(); 69 : } 70 : 71 : @Override 72 : public String configKey() { 73 15 : return source.configKey(); 74 : } 75 : 76 : @Override 77 : public TypeLiteral<K> keyType() { 78 15 : return source.keyType(); 79 : } 80 : 81 : @Override 82 : public TypeLiteral<V> valueType() { 83 0 : return source.valueType(); 84 : } 85 : 86 : @Override 87 : public long maximumWeight() { 88 15 : return source.maximumWeight(); 89 : } 90 : 91 : @Override 92 : public long diskLimit() { 93 0 : return source.diskLimit(); 94 : } 95 : 96 : @Override 97 : public CacheLoader<K, V> loader() { 98 0 : return source.loader(); 99 : } 100 : 101 : @Override 102 : public int version() { 103 15 : return source.version(); 104 : } 105 : 106 : @Override 107 : public CacheSerializer<K> keySerializer() { 108 15 : return source.keySerializer(); 109 : } 110 : 111 : @Override 112 : public CacheSerializer<V> valueSerializer() { 113 15 : return source.valueSerializer(); 114 : } 115 : }