2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.repo.util;
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.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.concurrent.TimeUnit;
20 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
21 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.spi.AbstractSchemaSourceCache;
25 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
26 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
27 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
30 public class InMemorySchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T>
31 implements AutoCloseable {
32 // FIXME: 7.0.0: use a java.util.Cleaner?
33 private final List<FinalizablePhantomReference<T>> regs = Collections.synchronizedList(new ArrayList<>());
34 private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
35 private final Cache<SourceIdentifier, T> cache;
37 protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation,
38 final CacheBuilder<Object, Object> builder) {
39 super(consumer, representation, Costs.IMMEDIATE);
40 cache = builder.build();
43 public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
44 final SchemaSourceRegistry consumer, final Class<R> representation) {
45 return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
48 public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
49 final SchemaSourceRegistry consumer, final Class<R> representation, final long lifetime,
50 final TimeUnit units) {
51 return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()
52 .expireAfterAccess(lifetime, units));
56 public FluentFuture<? extends T> getSource(final SourceIdentifier sourceIdentifier) {
57 final T present = cache.getIfPresent(sourceIdentifier);
58 return present != null ? FluentFutures.immediateFluentFuture(present)
59 : FluentFutures.immediateFailedFluentFuture(new MissingSchemaSourceException("Source not found",
64 protected void offer(final T source) {
65 final T present = cache.getIfPresent(source.getIdentifier());
66 if (present == null) {
67 cache.put(source.getIdentifier(), source);
69 final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
70 final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<>(source, queue) {
72 public void finalizeReferent() {
84 while (!regs.isEmpty()) {
85 final FinalizablePhantomReference<?> ref = regs.get(0);
86 ref.finalizeReferent();
89 cache.invalidateAll();