From 1329dfbfcb4da0e2e877b23f29fea418ad7c66bb Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 13 May 2014 11:21:55 +0200 Subject: [PATCH] BUG-994: introduce ObjectCache for QNameModule/QName 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 --- .../bundle/test/BundleStartTest.java | 1 + yang/yang-common/pom.xml | 10 +++++++++ .../yangtools/yang/common/QName.java | 22 ++++++++++++++----- .../yangtools/yang/common/QNameModule.java | 20 +++++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/integration-test/bundle-test/src/test/java/org/opendaylight/yangtools/bundle/test/BundleStartTest.java b/integration-test/bundle-test/src/test/java/org/opendaylight/yangtools/bundle/test/BundleStartTest.java index d72ec7f2df..1b5e1d8548 100644 --- a/integration-test/bundle-test/src/test/java/org/opendaylight/yangtools/bundle/test/BundleStartTest.java +++ b/integration-test/bundle-test/src/test/java/org/opendaylight/yangtools/bundle/test/BundleStartTest.java @@ -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()); diff --git a/yang/yang-common/pom.xml b/yang/yang-common/pom.xml index 8d4f608fb5..3b814bf84c 100644 --- a/yang/yang-common/pom.xml +++ b/yang/yang-common/pom.xml @@ -34,10 +34,20 @@ org.opendaylight.yangtools concepts + + org.opendaylight.yangtools + object-cache-api + + junit junit test + + org.opendaylight.yangtools + object-cache-guava + test + diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java index 0ac89ae8da..f3164abe14 100644 --- a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java +++ b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java @@ -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 { + 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 { + "(.+)\\)(.+)$"); 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 { 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 { } 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 { * @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 { * @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 { * @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) { diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java index a45c91b923..bec56bf76f 100644 --- a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java +++ b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java @@ -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; -- 2.36.6