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> implements AutoCloseable {
31 private final List<FinalizablePhantomReference<T>> regs = Collections.synchronizedList(new ArrayList<FinalizablePhantomReference<T>>());
32 private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
33 private final Cache<SourceIdentifier, T> cache;
35 protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation, final CacheBuilder<Object, Object> builder) {
36 super(consumer, representation, Costs.IMMEDIATE);
37 cache = builder.build();
40 public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(final SchemaSourceRegistry consumer, final Class<R> representation) {
41 return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
44 public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
45 final SchemaSourceRegistry consumer, final Class<R> representation, final long lifetime, final TimeUnit units) {
46 return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()
47 .expireAfterAccess(lifetime, units));
51 public CheckedFuture<? extends T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
52 final T present = cache.getIfPresent(sourceIdentifier);
53 if (present != null) {
54 return Futures.immediateCheckedFuture(present);
57 return Futures.<T, SchemaSourceException>immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
61 protected void offer(final T source) {
62 final T present = cache.getIfPresent(source.getIdentifier());
63 if (present == null) {
64 cache.put(source.getIdentifier(), source);
66 final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
67 final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<T>(source, queue) {
69 public void finalizeReferent() {
81 while (!regs.isEmpty()) {
82 final FinalizablePhantomReference<?> ref = regs.get(0);
83 ref.finalizeReferent();
86 cache.invalidateAll();