07c17e5f111ea032331052aaea97f7ed115ce98d
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / CachingImportPolicy.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.Interner;
13 import com.google.common.collect.Interners;
14 import com.google.common.collect.MapMaker;
15 import java.util.concurrent.ConcurrentMap;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import javax.annotation.concurrent.NotThreadSafe;
19 import org.opendaylight.protocol.bgp.rib.impl.spi.AbstractImportPolicy;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.tables.Attributes;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23
24 /**
25  * A caching decorator for {@link AbstractImportPolicy}. Performs caching of effective
26  * attributes using an identity-and-hashCode-based map for fast lookup and reuse of resulting
27  * objects.
28  */
29 @NotThreadSafe
30 final class CachingImportPolicy extends AbstractImportPolicy {
31
32     // A dummy ContainerNode, stored in the cache to indicate null effective attributes
33     private static final ContainerNode MASKED_NULL = ImmutableNodes.containerNode(Attributes.QNAME);
34
35     // We maintain a weak cache of returned effective attributes, so we end up reusing
36     // the same instance when asked. We set concurrency level to 1, as we do not expect
37     // the cache to be accessed from multiple threads. That may need to be changed
38     // if we end up sharing the cache across peers.
39     private final ConcurrentMap<ContainerNode, ContainerNode> cache =
40             new MapMaker().concurrencyLevel(1).weakKeys().weakValues().makeMap();
41
42     /*
43      * The cache itself is weak, which means we end up with identity hash/comparisons.
44      * That is good, but we want the cache to be effective even when equivalent attributes
45      * are presented. For that purpose we maintain a weak interner, which will allow us
46      * to map attributes to a canonical object without preventing garbage collection.
47      */
48     private final Interner<ContainerNode> interner = Interners.newWeakInterner();
49
50     private final AbstractImportPolicy delegate;
51
52     CachingImportPolicy(final AbstractImportPolicy delegate) {
53         this.delegate = requireNonNull(delegate);
54     }
55
56     @Nonnull private static ContainerNode maskNull(@Nullable final ContainerNode unmasked) {
57         return unmasked == null ? MASKED_NULL : unmasked;
58     }
59
60     @Nullable private static ContainerNode unmaskNull(@Nonnull final ContainerNode masked) {
61         return MASKED_NULL.equals(masked) ? null : masked;
62     }
63
64     @Override
65     public ContainerNode effectiveAttributes(final ContainerNode attributes) {
66         ContainerNode ret = this.cache.get(attributes);
67         if (ret != null) {
68             return unmaskNull(ret);
69         }
70
71         /*
72          * The cache returned empty. The reason for that may be that the attributes
73          * passed in are not identical to the ones forming the cache's key. Intern
74          * the passed attributes, which will result in a canonical reference.
75          *
76          * If the returned reference is different, attempt to look up in the cache
77          * again. If the reference is the same, we have just populated the interner
78          * and thus are on the path to create a new cache entry.
79          */
80         final ContainerNode interned = this.interner.intern(attributes);
81         if (!interned.equals(attributes)) {
82             final ContainerNode retry = this.cache.get(interned);
83             if (retry != null) {
84                 return unmaskNull(retry);
85             }
86         }
87
88         final ContainerNode effective = this.delegate.effectiveAttributes(interned);
89
90         /*
91          * Populate the cache. Note that this may have raced with another thread,
92          * in which case we want to reuse the previous entry without replacing it.
93          * Check the result of conditional put and return it unmasked if it happens
94          * to be non-null. That will throw away the attributes we just created,
95          * but that's fine, as they have not leaked to heap yet and will be GC'd
96          * quickly.
97          */
98         final ContainerNode existing = this.cache.putIfAbsent(interned, maskNull(effective));
99         return existing != null ? unmaskNull(existing) : effective;
100
101     }
102 }