blob: 9df1b3bd26cc9c1ad85bc867700e63df8c54a770 [file] [log] [blame]
Madan Jampanid3520102015-08-14 11:06:03 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 Jampanif4c88502016-01-21 12:35:36 -080016package org.onosproject.store.primitives.impl;
Madan Jampani3d6a2f62015-08-12 07:19:07 -070017
18import java.util.concurrent.CompletableFuture;
19
20import org.onosproject.core.ApplicationId;
21import org.onosproject.store.service.Serializer;
22import org.onosproject.store.service.Versioned;
23
24import com.google.common.cache.CacheBuilder;
25import com.google.common.cache.CacheLoader;
26import com.google.common.cache.LoadingCache;
27
28/**
Madan Jampani14a38da2015-11-16 11:29:00 -080029 * Extension of {@link DefaultAsyncConsistentMap} that provides a weaker read consistency
Madan Jampani3d6a2f62015-08-12 07:19:07 -070030 * guarantee in return for better read performance.
Madan Jampani14a38da2015-11-16 11:29:00 -080031 * <p>
32 * For read/write operations that are local to a node this map implementation provides
33 * guarantees similar to a ConsistentMap. However for read/write operations executed
34 * across multiple nodes this implementation only provides eventual consistency.
Madan Jampani3d6a2f62015-08-12 07:19:07 -070035 *
36 * @param <K> key type
37 * @param <V> value type
38 */
39public class AsyncCachingConsistentMap<K, V> extends DefaultAsyncConsistentMap<K, V> {
40
41 private final LoadingCache<K, CompletableFuture<Versioned<V>>> cache =
42 CacheBuilder.newBuilder()
43 .maximumSize(10000) // TODO: make configurable
44 .build(new CacheLoader<K, CompletableFuture<Versioned<V>>>() {
45 @Override
46 public CompletableFuture<Versioned<V>> load(K key)
47 throws Exception {
48 return AsyncCachingConsistentMap.super.get(key);
49 }
50 });
51
52 public AsyncCachingConsistentMap(String name,
53 ApplicationId applicationId,
54 Database database,
55 Serializer serializer,
56 boolean readOnly,
57 boolean purgeOnUninstall,
58 boolean meteringEnabled) {
59 super(name, applicationId, database, serializer, readOnly, purgeOnUninstall, meteringEnabled);
60 addListener(event -> cache.invalidate(event.key()));
61 }
62
63 @Override
64 public CompletableFuture<Versioned<V>> get(K key) {
65 CompletableFuture<Versioned<V>> cachedValue = cache.getIfPresent(key);
66 if (cachedValue != null) {
67 if (cachedValue.isCompletedExceptionally()) {
68 cache.invalidate(key);
69 } else {
70 return cachedValue;
71 }
72 }
73 return cache.getUnchecked(key);
74 }
Madan Jampani14a38da2015-11-16 11:29:00 -080075
76 @Override
77 protected void beforeUpdate(K key) {
78 super.beforeUpdate(key);
79 cache.invalidate(key);
80 }
Madan Jampani3d6a2f62015-08-12 07:19:07 -070081}