BUG-994: introduce ObjectCache for QNameModule/QName 26/6926/19
authorRobert Varga <rovarga@cisco.com>
Tue, 13 May 2014 09:21:55 +0000 (11:21 +0200)
committerRobert Varga <rovarga@cisco.com>
Mon, 4 Aug 2014 07:32:03 +0000 (09:32 +0200)
This introduces caching for QNameModule using object-cache-api. This
will allow for efficient object reuse, at the cost of requiring a cache
implementation being available on the class path.

The use of cache has been not propagated to the static creator methods,
simply because the caching lookup introduces a cost which is not
acceptable in all contexts.

Change-Id: I5af9c7e54c726c05a5824521f5ed5183dc4899b4
Signed-off-by: Robert Varga <rovarga@cisco.com>
integration-test/bundle-test/src/test/java/org/opendaylight/yangtools/bundle/test/BundleStartTest.java
yang/yang-common/pom.xml
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java

index d72ec7f2df4db51b2da8e056c808a27af674c1d2..1b5e1d85483f4222e557d5d6dacf62f5e0189f38 100644 (file)
@@ -49,6 +49,7 @@ public class BundleStartTest {
 
         options.add(mavenBundle(GROUP, "concepts").versionAsInProject());
         options.add(mavenBundle(GROUP, "util").versionAsInProject());
+        options.add(mavenBundle(GROUP, "object-cache-api").versionAsInProject());
         options.add(mavenBundle(GROUP, "yang-binding").versionAsInProject());
         options.add(mavenBundle(GROUP, "yang-common").versionAsInProject());
         options.add(mavenBundle(GROUP, "yang-data-api").versionAsInProject());
index 8d4f608fb5da29020723188c4eb8f88a4a57ba38..3b814bf84c9c417ea22d48ca0e1d3e648098c364 100644 (file)
             <groupId>org.opendaylight.yangtools</groupId>
             <artifactId>concepts</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.opendaylight.yangtools</groupId>
+            <artifactId>object-cache-api</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.opendaylight.yangtools</groupId>
+            <artifactId>object-cache-guava</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
index 0ac89ae8da2c1d634d134bc07977877e5a9c4484..f3164abe140ed797db1c0904df79a91629c594a9 100644 (file)
@@ -19,6 +19,8 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.objcache.ObjectCache;
+import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
 
 /**
  * The QName from XML consists of local name of element and XML namespace, but
@@ -45,6 +47,7 @@ import org.opendaylight.yangtools.concepts.Immutable;
  *
  */
 public final class QName implements Immutable, Serializable, Comparable<QName> {
+    private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QName.class);
     private static final long serialVersionUID = 5398411242927766414L;
 
     static final String QNAME_REVISION_DELIMITER = "?revision=";
@@ -55,7 +58,6 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
             + "(.+)\\)(.+)$");
     private static final Pattern QNAME_PATTERN_NO_REVISION = Pattern.compile("^\\((.+)\\)(.+)$");
     private static final Pattern QNAME_PATTERN_NO_NAMESPACE_NO_REVISION = Pattern.compile("^(.+)$");
-
     private static final char[] ILLEGAL_CHARACTERS = new char[] { '?', '(', ')', '&' };
 
     // Mandatory
@@ -71,6 +73,16 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
         this.module = module;
     }
 
+    /**
+     * Look up specified QName in the global cache and return a shared reference.
+     *
+     * @param module QName instance
+     * @return Cached instance, according to {@link ObjectCache} policy.
+     */
+    public static QName cachedReference(final QName qname) {
+        return CACHE.getReference(qname);
+    }
+
     /**
      * QName Constructor.
      *
@@ -228,7 +240,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
     }
 
     public static QName create(final QName base, final String localName) {
-        return new QName(base.getModule(), base.getPrefix(), localName);
+        return create(base.getModule(), base.getPrefix(), localName);
     }
 
     /**
@@ -259,7 +271,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
      * @return Instance of QName
      */
     public static QName create(final QNameModule qnameModule, final String localName) {
-        return new QName(qnameModule, null, localName);
+        return create(qnameModule, null, localName);
     }
 
     /**
@@ -274,7 +286,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
      * @return Instance of QName
      */
     public static QName create(final URI namespace, final Date revision, final String localName) {
-        return new QName(QNameModule.create(namespace, revision), null, localName);
+        return create(QNameModule.create(namespace, revision), null, localName);
     }
 
     /**
@@ -345,7 +357,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
      * @return copy of this QName with revision and prefix unset.
      */
     public QName withoutRevision() {
-        return QName.create(getNamespace(), null, localName);
+        return create(getNamespace(), null, localName);
     }
 
     public static Date parseRevision(final String formatedDate) {
index a45c91b923b13c8ddb26fb5e81ad16d12d54f9cc..bec56bf76fe77f54467ab5fc00e46d9f03eb1a59 100644 (file)
@@ -13,10 +13,13 @@ import java.net.URISyntaxException;
 import java.util.Date;
 
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.objcache.ObjectCache;
+import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public final class QNameModule implements Immutable, Serializable {
+    private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QNameModule.class);
     private static final Logger LOG = LoggerFactory.getLogger(QNameModule.class);
     private static final QNameModule NULL_INSTANCE = new QNameModule(null, null);
     private static final long serialVersionUID = 1L;
@@ -35,6 +38,23 @@ public final class QNameModule implements Immutable, Serializable {
         this.revision = revision;
     }
 
+    /**
+     * Look up specified module in the global cache and return a shared reference.
+     *
+     * @param module Module instance
+     * @return Cached instance, according to {@link ObjectCache} policy.
+     */
+    public static QNameModule cachedReference(final QNameModule module) {
+        return CACHE.getReference(module);
+    }
+
+    /**
+     * Create a new QName module instance with specified namespace/revision.
+     *
+     * @param namespace Module namespace
+     * @param revision Module revision
+     * @return A new, potentially shared, QNameModule instance
+     */
     public static QNameModule create(final URI namespace, final Date revision) {
         if (namespace == null && revision == null) {
             return NULL_INSTANCE;