jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r555922 - in /jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi: WorkspaceManager.java nodetype/NodeTypeCache.java
Date Fri, 13 Jul 2007 10:02:25 GMT
Author: mreutegg
Date: Fri Jul 13 03:02:23 2007
New Revision: 555922

URL: http://svn.apache.org/viewvc?view=rev&rev=555922
Log:
JCR-1030: Introduce QNodeTypeDefinition cache per userId

Added:
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/nodetype/NodeTypeCache.java
  (with props)
Modified:
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java

Modified: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java?view=diff&rev=555922&r1=555921&r2=555922
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java
(original)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java
Fri Jul 13 03:02:23 2007
@@ -22,6 +22,7 @@
 import org.apache.jackrabbit.jcr2spi.nodetype.ItemDefinitionProvider;
 import org.apache.jackrabbit.jcr2spi.nodetype.ItemDefinitionProviderImpl;
 import org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeTypeProvider;
+import org.apache.jackrabbit.jcr2spi.nodetype.NodeTypeCache;
 import org.apache.jackrabbit.jcr2spi.name.NamespaceStorage;
 import org.apache.jackrabbit.jcr2spi.name.NamespaceRegistryImpl;
 import org.apache.jackrabbit.jcr2spi.name.NamespaceCache;
@@ -398,6 +399,8 @@
                 throw new UnsupportedOperationException("NodeType registration not yet defined
by the SPI");
             }
         };
+        NodeTypeCache ntCache = NodeTypeCache.getInstance(service, sessionInfo.getUserID());
+        ntst = ntCache.wrap(ntst);
         return NodeTypeRegistryImpl.create(ntst, nsRegistry);
     }
 

Added: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/nodetype/NodeTypeCache.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/nodetype/NodeTypeCache.java?view=auto&rev=555922
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/nodetype/NodeTypeCache.java
(added)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/nodetype/NodeTypeCache.java
Fri Jul 13 03:02:23 2007
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.jcr2spi.nodetype;
+
+import org.apache.commons.collections.map.ReferenceMap;
+import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.spi.QNodeTypeDefinition;
+import org.apache.jackrabbit.spi.RepositoryService;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.nodetype.NoSuchNodeTypeException;
+import java.util.Map;
+import java.util.Iterator;
+import java.util.WeakHashMap;
+import java.util.HashMap;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * <code>NodeTypeCache</code> implements a cache for <code>QNodeTypeDefinition</code>s
+ * on a userId basis.
+ */
+public class NodeTypeCache {
+
+    /**
+     * The caches per repository service instance
+     */
+    private static final Map CACHES_PER_SERVICE = new WeakHashMap();
+
+    /**
+     * Maps node type QNames to QNodeTypeDefinition
+     */
+    private final Map nodeTypes = new HashMap();
+
+    /**
+     * @param service the repository service.
+     * @param userId  the userId. If <code>null</code> this method will return
a
+     *                new cache instance for each such call.
+     * @return the <code>NodeTypeCache</code> instance for the given
+     *         <code>service</code> and <code>userId</code>.
+     */
+    public static NodeTypeCache getInstance(RepositoryService service, String userId) {
+        // if no userId is provided do not keep the cache
+        if (userId == null) {
+            return new NodeTypeCache();
+        }
+        Map caches;
+        synchronized (CACHES_PER_SERVICE) {
+            caches = (Map) CACHES_PER_SERVICE.get(service);
+            if (caches == null) {
+                // use soft references for the node type caches
+                caches = new ReferenceMap(ReferenceMap.HARD, ReferenceMap.SOFT);
+                CACHES_PER_SERVICE.put(service, caches);
+            }
+        }
+        synchronized (caches) {
+            NodeTypeCache cache = (NodeTypeCache) caches.get(userId);
+            if (cache == null) {
+                cache = new NodeTypeCache();
+                caches.put(userId, cache);
+            }
+            return cache;
+        }
+    }
+
+    private NodeTypeCache() {
+    }
+
+    /**
+     * Returns an Iterator over all node type definitions registered.
+     *
+     * @return
+     * @throws javax.jcr.RepositoryException
+     */
+    public Iterator getAllDefinitions(NodeTypeStorage storage)
+            throws RepositoryException {
+        Map allNts = new HashMap();
+        for (Iterator it = storage.getAllDefinitions(); it.hasNext(); ) {
+            QNodeTypeDefinition def = (QNodeTypeDefinition) it.next();
+            allNts.put(def.getQName(), def);
+        }
+        // update the cache
+        synchronized (nodeTypes) {
+            nodeTypes.clear();
+            nodeTypes.putAll(allNts);
+        }
+        return allNts.values().iterator();
+    }
+
+    /**
+     * Returns the <code>QNodeTypeDefinition</code>s for the given node type
+     * names. The implementation is free to return additional definitions e.g.
+     * dependencies.
+     *
+     * @param nodeTypeNames
+     * @return
+     * @throws javax.jcr.nodetype.NoSuchNodeTypeException
+     * @throws RepositoryException
+     */
+    public Iterator getDefinitions(NodeTypeStorage storage, QName[] nodeTypeNames)
+            throws NoSuchNodeTypeException, RepositoryException {
+        List nts = new ArrayList();
+        List missing = null;
+        synchronized (nodeTypes) {
+            for (int i = 0; i < nodeTypeNames.length; i++) {
+                QNodeTypeDefinition def = (QNodeTypeDefinition) nodeTypes.get(nodeTypeNames[i]);
+                if (def == null) {
+                    if (missing == null) {
+                        missing = new ArrayList();
+                    }
+                    missing.add(nodeTypeNames[i]);
+                } else {
+                    nts.add(def);
+                }
+            }
+        }
+        if (missing != null) {
+            QName[] ntNames = (QName[]) missing.toArray(new QName[missing.size()]);
+            Iterator it = storage.getDefinitions(ntNames);
+            synchronized (nodeTypes) {
+                while (it.hasNext()) {
+                    QNodeTypeDefinition def = (QNodeTypeDefinition) it.next();
+                    nts.add(def);
+                    nodeTypes.put(def.getQName(), def);
+                }
+            }
+        }
+        return nts.iterator();
+    }
+
+    public void registerNodeTypes(NodeTypeStorage storage,
+                                  QNodeTypeDefinition[] nodeTypeDefs)
+            throws NoSuchNodeTypeException, RepositoryException {
+        throw new UnsupportedOperationException("NodeType registration not yet defined by
the SPI");
+    }
+
+    public void reregisterNodeTypes(NodeTypeStorage storage,
+                                    QNodeTypeDefinition[] nodeTypeDefs)
+            throws NoSuchNodeTypeException, RepositoryException {
+        throw new UnsupportedOperationException("NodeType registration not yet defined by
the SPI");
+    }
+
+    public void unregisterNodeTypes(NodeTypeStorage storage,
+                                    QName[] nodeTypeNames)
+            throws NoSuchNodeTypeException, RepositoryException {
+        throw new UnsupportedOperationException("NodeType registration not yet defined by
the SPI");
+    }
+
+    /**
+     * Wraps this <code>NodeTypeCache</code> around the passed
+     * <code>storage</code> and exposes itself again as a
+     * <code>NodeTypeStorage</code>.
+     *
+     * @param storage the node type storage to wrap.
+     * @return node type storage instance using this cache.
+     */
+    public NodeTypeStorage wrap(final NodeTypeStorage storage) {
+        return new NodeTypeStorage() {
+            public Iterator getAllDefinitions() throws RepositoryException {
+                return NodeTypeCache.this.getAllDefinitions(storage);
+            }
+            public Iterator getDefinitions(QName[] nodeTypeNames)
+                    throws NoSuchNodeTypeException, RepositoryException {
+                return NodeTypeCache.this.getDefinitions(storage, nodeTypeNames);
+            }
+            public void registerNodeTypes(QNodeTypeDefinition[] nodeTypeDefs)
+                    throws NoSuchNodeTypeException, RepositoryException {
+                NodeTypeCache.this.registerNodeTypes(storage, nodeTypeDefs);
+            }
+            public void reregisterNodeTypes(QNodeTypeDefinition[] nodeTypeDefs)
+                    throws NoSuchNodeTypeException, RepositoryException {
+                NodeTypeCache.this.reregisterNodeTypes(storage, nodeTypeDefs);
+            }
+            public void unregisterNodeTypes(QName[] nodeTypeNames)
+                    throws NoSuchNodeTypeException, RepositoryException {
+                NodeTypeCache.this.unregisterNodeTypes(storage, nodeTypeNames);
+            }
+        };
+    }
+}

Propchange: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/nodetype/NodeTypeCache.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message