Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.concurrent.CompletableFuture; |
| 19 | import java.util.function.BiFunction; |
| 20 | import java.util.function.Predicate; |
| 21 | |
| 22 | import org.onosproject.store.service.AsyncConsistentMap; |
Madan Jampani | 62f1533 | 2016-02-01 13:18:39 -0800 | [diff] [blame] | 23 | import org.onosproject.store.service.MapEventListener; |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 24 | import org.onosproject.store.service.Versioned; |
| 25 | |
| 26 | import com.google.common.cache.CacheBuilder; |
| 27 | import com.google.common.cache.CacheLoader; |
| 28 | import com.google.common.cache.LoadingCache; |
| 29 | |
| 30 | /** |
| 31 | * {@code AsyncConsistentMap} that caches entries on read. |
| 32 | * <p> |
| 33 | * The cache entries are automatically invalidated when updates are detected either locally or |
| 34 | * remotely. |
| 35 | * <p> This implementation only attempts to serve cached entries for {@link AsyncConsistentMap#get get} |
| 36 | * calls. All other calls skip the cache and directly go the backing map. |
| 37 | * |
| 38 | * @param <K> key type |
| 39 | * @param <V> value type |
| 40 | */ |
| 41 | public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMap<K, V> { |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 42 | private int maxCacheSize = 10000; |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 43 | |
| 44 | private final LoadingCache<K, CompletableFuture<Versioned<V>>> cache = |
| 45 | CacheBuilder.newBuilder() |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 46 | .maximumSize(maxCacheSize) |
| 47 | .build(new CacheLoader<K, CompletableFuture<Versioned<V>>>() { |
| 48 | @Override |
| 49 | public CompletableFuture<Versioned<V>> load(K key) |
| 50 | throws Exception { |
| 51 | return CachingAsyncConsistentMap.super.get(key); |
| 52 | } |
| 53 | }); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 54 | |
Madan Jampani | 62f1533 | 2016-02-01 13:18:39 -0800 | [diff] [blame] | 55 | private final MapEventListener<K, V> cacheInvalidator = event -> cache.invalidate(event.key()); |
| 56 | |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 57 | /** |
| 58 | * Default constructor. |
| 59 | * |
| 60 | * @param backingMap a distributed, strongly consistent map for backing |
| 61 | */ |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 62 | public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap) { |
| 63 | super(backingMap); |
Madan Jampani | 62f1533 | 2016-02-01 13:18:39 -0800 | [diff] [blame] | 64 | super.addListener(cacheInvalidator); |
| 65 | } |
| 66 | |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 67 | /** |
| 68 | * Constructor to configure cache size of LoadingCache. |
| 69 | * |
| 70 | * @param backingMap a distributed, strongly consistent map for backing |
| 71 | * @param cacheSize the maximum size of the cache |
| 72 | */ |
| 73 | public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap, int cacheSize) { |
| 74 | super(backingMap); |
| 75 | super.addListener(cacheInvalidator); |
| 76 | maxCacheSize = cacheSize; |
| 77 | } |
| 78 | |
Madan Jampani | 62f1533 | 2016-02-01 13:18:39 -0800 | [diff] [blame] | 79 | @Override |
| 80 | public CompletableFuture<Void> destroy() { |
| 81 | return super.destroy().thenCompose(v -> removeListener(cacheInvalidator)); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public CompletableFuture<Versioned<V>> get(K key) { |
| 86 | return cache.getUnchecked(key); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public CompletableFuture<Versioned<V>> computeIf(K key, |
| 91 | Predicate<? super V> condition, |
| 92 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
| 93 | return super.computeIf(key, condition, remappingFunction) |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 94 | .whenComplete((r, e) -> cache.invalidate(key)); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public CompletableFuture<Versioned<V>> put(K key, V value) { |
| 99 | return super.put(key, value) |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 100 | .whenComplete((r, e) -> cache.invalidate(key)); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public CompletableFuture<Versioned<V>> putAndGet(K key, V value) { |
Simon Hunt | 5829c34 | 2016-03-07 17:01:43 -0800 | [diff] [blame] | 105 | return super.putAndGet(key, value) |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 106 | .whenComplete((r, e) -> cache.invalidate(key)); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public CompletableFuture<Versioned<V>> remove(K key) { |
| 111 | return super.remove(key) |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 112 | .whenComplete((r, e) -> cache.invalidate(key)); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public CompletableFuture<Void> clear() { |
| 117 | return super.clear() |
sangyun-han | cce07c5 | 2016-04-06 11:07:59 +0900 | [diff] [blame] | 118 | .whenComplete((r, e) -> cache.invalidateAll()); |
Madan Jampani | 1007367 | 2016-01-21 19:13:59 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public CompletableFuture<Boolean> remove(K key, V value) { |
| 123 | return super.remove(key, value) |
| 124 | .whenComplete((r, e) -> { |
| 125 | if (r) { |
| 126 | cache.invalidate(key); |
| 127 | } |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public CompletableFuture<Boolean> remove(K key, long version) { |
| 133 | return super.remove(key, version) |
| 134 | .whenComplete((r, e) -> { |
| 135 | if (r) { |
| 136 | cache.invalidate(key); |
| 137 | } |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public CompletableFuture<Versioned<V>> replace(K key, V value) { |
| 143 | return super.replace(key, value) |
| 144 | .whenComplete((r, e) -> cache.invalidate(key)); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) { |
| 149 | return super.replace(key, oldValue, newValue) |
| 150 | .whenComplete((r, e) -> { |
| 151 | if (r) { |
| 152 | cache.invalidate(key); |
| 153 | } |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) { |
| 159 | return super.replace(key, oldVersion, newValue) |
| 160 | .whenComplete((r, e) -> { |
| 161 | if (r) { |
| 162 | cache.invalidate(key); |
| 163 | } |
| 164 | }); |
| 165 | } |
| 166 | } |