7747c93a6bfd76b69b1698bf63c3e0a1c38c3c82
[yangtools.git] / yang / yang-repo-spi / src / main / java / org / opendaylight / yangtools / yang / model / repo / spi / GuavaSchemaSourceCache.java
1 /*
2  * Copyright (c) 2014 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.yangtools.yang.model.repo.spi;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.FinalizablePhantomReference;
12 import com.google.common.base.FinalizableReferenceQueue;
13 import com.google.common.cache.Cache;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.util.concurrent.FluentFuture;
16 import java.time.Duration;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
23 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
25 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
26
27 /**
28  * A simple {@link AbstractSchemaSourceCache} based on {@link Cache Guava Cache}.
29  *
30  * @param <T> {@link SchemaSourceRepresentation} type stored in this cache
31  */
32 @Beta
33 public final class GuavaSchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T>
34         implements AutoCloseable {
35     // FIXME: 7.0.0: use a java.util.Cleaner?
36     private final List<FinalizablePhantomReference<T>> regs = Collections.synchronizedList(new ArrayList<>());
37     private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
38     private final Cache<SourceIdentifier, T> cache;
39
40     private GuavaSchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation,
41             final CacheBuilder<Object, Object> cacheBuilder) {
42         super(consumer, representation, PotentialSchemaSource.Costs.IMMEDIATE);
43         cache = cacheBuilder.build();
44     }
45
46     public static <R extends SchemaSourceRepresentation> @NonNull GuavaSchemaSourceCache<R> createSoftCache(
47             final SchemaSourceRegistry consumer, final Class<R> representation) {
48         return new GuavaSchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
49     }
50
51     public static <R extends SchemaSourceRepresentation> @NonNull GuavaSchemaSourceCache<R> createSoftCache(
52             final SchemaSourceRegistry consumer, final Class<R> representation, final long lifetime,
53             final TimeUnit units) {
54         return new GuavaSchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()
55             .expireAfterAccess(lifetime, units));
56     }
57
58     public static <R extends SchemaSourceRepresentation> @NonNull GuavaSchemaSourceCache<R> createSoftCache(
59             final SchemaSourceRegistry consumer, final Class<R> representation, final Duration duration) {
60         return new GuavaSchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()
61             .expireAfterAccess(duration));
62     }
63
64     @Override
65     public FluentFuture<? extends T> getSource(final SourceIdentifier sourceIdentifier) {
66         final T present = cache.getIfPresent(sourceIdentifier);
67         return present != null ? FluentFutures.immediateFluentFuture(present)
68                 : FluentFutures.immediateFailedFluentFuture(new MissingSchemaSourceException("Source not found",
69                     sourceIdentifier));
70     }
71
72     @Override
73     protected void offer(final T source) {
74         final T present = cache.getIfPresent(source.getIdentifier());
75         if (present == null) {
76             cache.put(source.getIdentifier(), source);
77
78             final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
79             final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<>(source, queue) {
80                 @Override
81                 public void finalizeReferent() {
82                     reg.close();
83                     regs.remove(this);
84                 }
85             };
86
87             regs.add(ref);
88         }
89     }
90
91     @Override
92     public void close() {
93         while (!regs.isEmpty()) {
94             final FinalizablePhantomReference<?> ref = regs.get(0);
95             ref.finalizeReferent();
96         }
97
98         cache.invalidateAll();
99         queue.close();
100     }
101 }