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.CheckedFuture;
16 import com.google.common.util.concurrent.Futures;
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.SchemaSourceException;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
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 private final List<FinalizablePhantomReference<T>> regs = Collections.synchronizedList(new ArrayList<>());
33 private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
34 private final Cache<SourceIdentifier, T> cache;
36 protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation,
37 final CacheBuilder<Object, Object> builder) {
38 super(consumer, representation, Costs.IMMEDIATE);
39 cache = builder.build();
42 public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
43 final SchemaSourceRegistry consumer, final Class<R> representation) {
44 return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
47 public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
48 final SchemaSourceRegistry consumer, final Class<R> representation, final long lifetime,
49 final TimeUnit units) {
50 return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()
51 .expireAfterAccess(lifetime, units));
55 public CheckedFuture<? extends T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
56 final T present = cache.getIfPresent(sourceIdentifier);
57 if (present != null) {
58 return Futures.immediateCheckedFuture(present);
61 return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found",
66 protected void offer(final T source) {
67 final T present = cache.getIfPresent(source.getIdentifier());
68 if (present == null) {
69 cache.put(source.getIdentifier(), source);
71 final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
72 final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<T>(source, queue) {
74 public void finalizeReferent() {
86 while (!regs.isEmpty()) {
87 final FinalizablePhantomReference<?> ref = regs.get(0);
88 ref.finalizeReferent();
91 cache.invalidateAll();