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.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
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.PotentialSchemaSource.Costs;
25 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
26 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
29 public class InMemorySchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T>
30 implements AutoCloseable {
31 private final List<FinalizablePhantomReference<T>> regs = Collections.synchronizedList(new ArrayList<>());
32 private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
33 private final Cache<SourceIdentifier, T> cache;
35 protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation,
36 final CacheBuilder<Object, Object> builder) {
37 super(consumer, representation, Costs.IMMEDIATE);
38 cache = builder.build();
41 public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
42 final SchemaSourceRegistry consumer, final Class<R> representation) {
43 return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
46 public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
47 final SchemaSourceRegistry consumer, final Class<R> representation, final long lifetime,
48 final TimeUnit units) {
49 return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()
50 .expireAfterAccess(lifetime, units));
54 public ListenableFuture<? extends T> getSource(final SourceIdentifier sourceIdentifier) {
55 final T present = cache.getIfPresent(sourceIdentifier);
56 if (present != null) {
57 return Futures.immediateFuture(present);
60 return Futures.immediateFailedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
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<T>(source, queue) {
72 public void finalizeReferent() {
84 while (!regs.isEmpty()) {
85 final FinalizablePhantomReference<?> ref = regs.get(0);
86 ref.finalizeReferent();
89 cache.invalidateAll();