Merge "BUG-1796: implement SourceIdentifier/PotentialSchemaSource caching"
authorTony Tkacik <ttkacik@cisco.com>
Thu, 11 Sep 2014 09:02:01 +0000 (09:02 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 11 Sep 2014 09:02:01 +0000 (09:02 +0000)
yang/yang-model-api/pom.xml
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SourceIdentifier.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/PotentialSchemaSource.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/AbstractSchemaRepository.java

index 4ad092e582d0196c434625a7a2dc3493bcc3642b..a993efa6cf4437bf3fa5ee71a305ede41d95ccf3 100644 (file)
     <description>${project.artifactId}</description>
 
     <dependencies>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>object-cache-api</artifactId>
+        </dependency>
         <dependency>
             <groupId>${project.groupId}</groupId>
             <artifactId>util</artifactId>
index 4e1fd00f20750388fb4af2579bc7af8f65ef9916..44a8cceee7609533e079e4aa4c64fa318469918e 100644 (file)
@@ -13,6 +13,8 @@ import com.google.common.base.Preconditions;
 
 import org.opendaylight.yangtools.concepts.Identifier;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.objcache.ObjectCache;
+import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
 
 /**
  * YANG Schema source identifier
@@ -37,26 +39,24 @@ import org.opendaylight.yangtools.concepts.Immutable;
  */
 @Beta
 public final class SourceIdentifier implements Identifier, Immutable {
-
     /**
      * Default revision for sources without specified revision.
      * Marks the source as oldest.
      */
     public static final String NOT_PRESENT_FORMATTED_REVISION = "0000-00-00";
 
+    private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(SourceIdentifier.class);
     private static final long serialVersionUID = 1L;
     private final String revision;
     private final String name;
 
     /**
-     *
      * Creates new YANG Schema source identifier.
      *
      * @param name Name of schema
      * @param formattedRevision Revision of source in format YYYY-mm-dd
      */
     public SourceIdentifier(final String name, final String formattedRevision) {
-        super();
         this.name = Preconditions.checkNotNull(name);
         this.revision = Preconditions.checkNotNull(formattedRevision);
     }
@@ -72,6 +72,15 @@ public final class SourceIdentifier implements Identifier, Immutable {
         this(name, formattedRevision.or(NOT_PRESENT_FORMATTED_REVISION));
     }
 
+    /**
+     * Return a cached reference to an object equal to this object.
+     *
+     * @return A potentially shared reference, not guaranteed to be unique.
+     */
+    public SourceIdentifier cachedReference() {
+        return CACHE.getReference(this);
+    }
+
     /**
      *
      * Creates new YANG Schema source identifier for sources without revision.
@@ -83,7 +92,6 @@ public final class SourceIdentifier implements Identifier, Immutable {
         this(name, NOT_PRESENT_FORMATTED_REVISION);
     }
 
-
     /**
      * Returns model name
      *
index fae44679e2a750b413980003dddaa93b94d5cc25..fa2758f4e3ab5679ef47dca107170a3ab761032d 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.yangtools.yang.model.repo.spi;
 import com.google.common.annotations.Beta;
 import com.google.common.base.Preconditions;
 
+import org.opendaylight.yangtools.objcache.ObjectCache;
+import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 
@@ -62,6 +64,7 @@ public final class PotentialSchemaSource<T extends SchemaSourceRepresentation> {
         }
     }
 
+    private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(PotentialSchemaSource.class);
     private final Class<? extends T> representation;
     private final SourceIdentifier sourceIdentifier;
     private final int cost;
@@ -77,6 +80,15 @@ public final class PotentialSchemaSource<T extends SchemaSourceRepresentation> {
         return new PotentialSchemaSource<>(sourceIdentifier, representation, cost);
     }
 
+    /**
+     * Return a cached reference to an object equal to this object.
+     *
+     * @return A potentially shared reference, not guaranteed to be unique.
+     */
+    public PotentialSchemaSource<T> cachedReference() {
+        return CACHE.getReference(this);
+    }
+
     public SourceIdentifier getSourceIdentifier() {
         return sourceIdentifier;
     }
index 722ac918e08c995d391546d085fcf6b4aa36b95b..d6058292afb5ad4646d0fc6e31ca96673e207195 100644 (file)
@@ -153,14 +153,16 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
 
     @Override
     public <T extends SchemaSourceRepresentation> SchemaSourceRegistration<T> registerSchemaSource(final SchemaSourceProvider<? super T> provider, final PotentialSchemaSource<T> source) {
-        final AbstractSchemaSourceRegistration<T> ret = new AbstractSchemaSourceRegistration<T>(provider, source) {
+        final PotentialSchemaSource<T> src = source.cachedReference();
+
+        final AbstractSchemaSourceRegistration<T> ret = new AbstractSchemaSourceRegistration<T>(provider, src) {
             @Override
             protected void removeRegistration() {
-                removeSource(source, this);
+                removeSource(src, this);
             }
         };
 
-        addSource(source, ret);
+        addSource(src, ret);
         return ret;
     }