Return-Path: X-Original-To: apmail-jackrabbit-oak-commits-archive@minotaur.apache.org Delivered-To: apmail-jackrabbit-oak-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B7E45101CD for ; Wed, 2 Oct 2013 14:42:36 +0000 (UTC) Received: (qmail 51622 invoked by uid 500); 2 Oct 2013 14:42:36 -0000 Delivered-To: apmail-jackrabbit-oak-commits-archive@jackrabbit.apache.org Received: (qmail 51586 invoked by uid 500); 2 Oct 2013 14:42:36 -0000 Mailing-List: contact oak-commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: oak-dev@jackrabbit.apache.org Delivered-To: mailing list oak-commits@jackrabbit.apache.org Received: (qmail 51578 invoked by uid 99); 2 Oct 2013 14:42:35 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Oct 2013 14:42:35 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Oct 2013 14:42:34 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id EA889238883D; Wed, 2 Oct 2013 14:42:13 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1528499 - /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/SessionNamespaces.java Date: Wed, 02 Oct 2013 14:42:13 -0000 To: oak-commits@jackrabbit.apache.org From: thomasm@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131002144213.EA889238883D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: thomasm Date: Wed Oct 2 14:42:13 2013 New Revision: 1528499 URL: http://svn.apache.org/r1528499 Log: OAK-715 Namespace - small performance improvement Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/SessionNamespaces.java Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/SessionNamespaces.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/SessionNamespaces.java?rev=1528499&r1=1528498&r2=1528499&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/SessionNamespaces.java (original) +++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/SessionNamespaces.java Wed Oct 2 14:42:13 2013 @@ -16,6 +16,8 @@ */ package org.apache.jackrabbit.oak.jcr.session; +import static com.google.common.base.Preconditions.checkNotNull; + import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -33,8 +35,6 @@ import org.apache.jackrabbit.util.XMLCha import com.google.common.collect.Maps; -import static com.google.common.base.Preconditions.checkNotNull; - /** * SessionNamespaces implements namespace handling on the JCR * Session level. That is, it maintains a map of session local namespace @@ -53,9 +53,16 @@ class SessionNamespaces { /** * A snapshot of the namespace registry when these SessionNamespaces - * are first accessed. + * are first accessed. Key: prefix, value: uri + */ + private Map snapshotPrefixUri; + + /** + * A snapshot of the namespace registry when these SessionNamespaces + * are first accessed. Key: uri, value: prefix */ - private Map snapshot; + private Map snapshotUriPrefix; + private final SessionContext sessionContext; @@ -107,10 +114,10 @@ class SessionNamespaces { namespaces.put(prefix, uri); } - if (snapshot.containsKey(prefix)) { + if (snapshotPrefixUri.containsKey(prefix)) { // make sure we have a prefix in case an existing // namespace uri is re-mapped - getNamespacePrefix(snapshot.get(prefix)); + getNamespacePrefix(snapshotPrefixUri.get(prefix)); } } @@ -121,12 +128,12 @@ class SessionNamespaces { init(); synchronized (namespaces) { if (namespaces.isEmpty()) { - Set prefixes = snapshot.keySet(); + Set prefixes = snapshotPrefixUri.keySet(); return prefixes.toArray(new String[prefixes.size()]); } } Set uris = new HashSet(); - uris.addAll(snapshot.values()); + uris.addAll(snapshotPrefixUri.values()); synchronized (namespaces) { // Add namespace uris only visible to session uris.addAll(namespaces.values()); @@ -148,7 +155,7 @@ class SessionNamespaces { if (uri == null) { // Not in local mappings, try snapshot ones - uri = snapshot.get(prefix); + uri = snapshotPrefixUri.get(prefix); if (uri == null) { // Not in snapshot mappings, try the global ones uri = getNamespaceRegistry().getURI(prefix); @@ -170,22 +177,22 @@ class SessionNamespaces { String getNamespacePrefix(String uri) throws RepositoryException { init(); synchronized (namespaces) { - for (Map.Entry entry : namespaces.entrySet()) { - if (entry.getValue().equals(uri)) { - return entry.getKey(); + if (namespaces.size() > 0) { + for (Map.Entry entry : namespaces.entrySet()) { + if (entry.getValue().equals(uri)) { + return entry.getKey(); + } } } // try snapshot - for (Map.Entry entry : snapshot.entrySet()) { - if (entry.getValue().equals(uri) - && !namespaces.containsKey(entry.getKey())) { - return entry.getKey(); - } + String prefix = snapshotUriPrefix.get(uri); + if (prefix != null && !namespaces.containsKey(prefix)) { + return prefix; } // The following throws an exception if the URI is not found, that's OK - String prefix = getNamespaceRegistry().getPrefix(uri); + prefix = getNamespaceRegistry().getPrefix(uri); // Generate a new prefix if the global mapping is already taken String base = prefix; @@ -207,9 +214,8 @@ class SessionNamespaces { synchronized (namespaces) { if (namespaces.isEmpty()) { return Collections.emptyMap(); - } else { - return new HashMap(namespaces); } + return new HashMap(namespaces); } } @@ -222,19 +228,22 @@ class SessionNamespaces { } } - private NamespaceRegistry getNamespaceRegistry() - throws RepositoryException { + private NamespaceRegistry getNamespaceRegistry() { return sessionContext.getWorkspace().getNamespaceRegistry(); } private void init() throws RepositoryException { - if (snapshot == null) { + if (snapshotPrefixUri == null) { NamespaceRegistry registry = getNamespaceRegistry(); - Map map = new HashMap(); + Map prefixUri = new HashMap(); + Map uriPrefix = new HashMap(); for (String prefix : registry.getPrefixes()) { - map.put(prefix, registry.getURI(prefix)); + String uri = registry.getURI(prefix); + prefixUri.put(prefix, uri); + uriPrefix.put(uri, prefix); } - snapshot = map; + snapshotPrefixUri = prefixUri; + snapshotUriPrefix = uriPrefix; } } }