X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-model-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fmodel%2Frepo%2Futil%2FInMemorySchemaSourceCache.java;h=1e2fc94f14d6ae2223e33f4276a86550cb1edfa1;hb=d559b39d9c161ab05f7304f522eae0ce31fdb67a;hp=ad594e422116463671bb760fd75945ff647f0660;hpb=470116942ccde46bbfd27e111617ba3505739e01;p=yangtools.git diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/InMemorySchemaSourceCache.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/InMemorySchemaSourceCache.java index ad594e4221..1e2fc94f14 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/InMemorySchemaSourceCache.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/InMemorySchemaSourceCache.java @@ -1,5 +1,6 @@ /* - * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html @@ -7,16 +8,17 @@ package org.opendaylight.yangtools.yang.model.repo.util; import com.google.common.annotations.Beta; -import com.google.common.base.Preconditions; +import com.google.common.base.FinalizablePhantomReference; +import com.google.common.base.FinalizableReferenceQueue; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; -import com.google.common.cache.RemovalListener; -import com.google.common.cache.RemovalNotification; -import com.google.common.util.concurrent.CheckedFuture; -import com.google.common.util.concurrent.Futures; - +import com.google.common.util.concurrent.FluentFuture; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.TimeUnit; +import org.opendaylight.yangtools.util.concurrent.FluentFutures; import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException; -import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException; import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation; import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs; @@ -24,51 +26,65 @@ import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration; import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry; @Beta -public class InMemorySchemaSourceCache extends AbstractSchemaSourceCache { - private static final class CacheEntry { - private final SchemaSourceRegistration reg; - private final T source; +public class InMemorySchemaSourceCache extends AbstractSchemaSourceCache + implements AutoCloseable { + private final List> regs = Collections.synchronizedList(new ArrayList<>()); + private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue(); + private final Cache cache; - public CacheEntry(final T source, final SchemaSourceRegistration reg) { - this.source = Preconditions.checkNotNull(source); - this.reg = Preconditions.checkNotNull(reg); - } - } - - private static final RemovalListener> LISTENER = new RemovalListener>() { - @Override - public void onRemoval(final RemovalNotification> notification) { - notification.getValue().reg.close(); - } - }; - - private final Cache> cache; - - protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class representation, final CacheBuilder builder) { + protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class representation, + final CacheBuilder builder) { super(consumer, representation, Costs.IMMEDIATE); - cache = builder.removalListener(LISTENER).build(); + cache = builder.build(); } - public static InMemorySchemaSourceCache createSoftCache(final SchemaSourceRegistry consumer, final Class representation) { + public static InMemorySchemaSourceCache createSoftCache( + final SchemaSourceRegistry consumer, final Class representation) { return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()); } - @Override - public CheckedFuture getSource(final SourceIdentifier sourceIdentifier) { - final CacheEntry present = cache.getIfPresent(sourceIdentifier); - if (present != null) { - return Futures.immediateCheckedFuture(present.source); - } + public static InMemorySchemaSourceCache createSoftCache( + final SchemaSourceRegistry consumer, final Class representation, final long lifetime, + final TimeUnit units) { + return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues() + .expireAfterAccess(lifetime, units)); + } - return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier)); + @Override + public FluentFuture getSource(final SourceIdentifier sourceIdentifier) { + final T present = cache.getIfPresent(sourceIdentifier); + return present != null ? FluentFutures.immediateFluentFuture(present) + : FluentFutures.immediateFailedFluentFuture(new MissingSchemaSourceException("Source not found", + sourceIdentifier)); } @Override protected void offer(final T source) { - final CacheEntry present = cache.getIfPresent(source.getIdentifier()); + final T present = cache.getIfPresent(source.getIdentifier()); if (present == null) { + cache.put(source.getIdentifier(), source); + final SchemaSourceRegistration reg = register(source.getIdentifier()); - cache.put(source.getIdentifier(), new CacheEntry(source, reg)); + final FinalizablePhantomReference ref = new FinalizablePhantomReference(source, queue) { + @Override + public void finalizeReferent() { + reg.close(); + regs.remove(this); + } + }; + + regs.add(ref); } } + + @Override + public void close() { + while (!regs.isEmpty()) { + final FinalizablePhantomReference ref = regs.get(0); + ref.finalizeReferent(); + } + + cache.invalidateAll(); + queue.close(); + } }