You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2008/03/04 17:45:18 UTC

svn commit: r633541 - in /lenya/trunk/src: impl/java/org/apache/lenya/cms/metadata/ impl/java/org/apache/lenya/cms/publication/ webapp/lenya/config/cocoon-xconf/metadata/

Author: andreas
Date: Tue Mar  4 08:45:18 2008
New Revision: 633541

URL: http://svn.apache.org/viewvc?rev=633541&view=rev
Log:
Adding meta data cache.

Added:
    lenya/trunk/src/impl/java/org/apache/lenya/cms/metadata/CacheableMetaData.java
    lenya/trunk/src/impl/java/org/apache/lenya/cms/metadata/MetaDataCache.java
    lenya/trunk/src/webapp/lenya/config/cocoon-xconf/metadata/metadata-cache.xconf
Modified:
    lenya/trunk/src/impl/java/org/apache/lenya/cms/publication/DocumentImpl.java

Added: lenya/trunk/src/impl/java/org/apache/lenya/cms/metadata/CacheableMetaData.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/impl/java/org/apache/lenya/cms/metadata/CacheableMetaData.java?rev=633541&view=auto
==============================================================================
--- lenya/trunk/src/impl/java/org/apache/lenya/cms/metadata/CacheableMetaData.java (added)
+++ lenya/trunk/src/impl/java/org/apache/lenya/cms/metadata/CacheableMetaData.java Tue Mar  4 08:45:18 2008
@@ -0,0 +1,122 @@
+/*
+ * 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.lenya.cms.metadata;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Cached, read-only meta data.
+ */
+public class CacheableMetaData implements MetaData {
+
+    /**
+     * Maps keys to value arrays.
+     */
+    private Map key2values = new HashMap();
+    private long lastModified;
+    private ElementSet elementSet;
+
+    /**
+     * @param meta The meta data to build this object from.
+     */
+    public CacheableMetaData(MetaData meta) {
+        this.elementSet = meta.getElementSet();
+        String[] keys = meta.getAvailableKeys();
+        try {
+            this.lastModified = meta.getLastModified();
+            for (int i = 0; i < keys.length; i++) {
+                String[] values = meta.getValues(keys[i]);
+                this.key2values.put(keys[i], values);
+            }
+        } catch (MetaDataException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public void addValue(String key, String value) throws MetaDataException {
+        throw new UnsupportedOperationException();
+    }
+
+    public void forcedReplaceBy(MetaData other) throws MetaDataException {
+        throw new UnsupportedOperationException();
+    }
+
+    public String[] getAvailableKeys() {
+        Set keys = key2values.keySet();
+        return (String[]) keys.toArray(new String[keys.size()]);
+    }
+
+    public ElementSet getElementSet() {
+        return this.elementSet;
+    }
+
+    public String getFirstValue(String key) throws MetaDataException {
+        String value = null;
+        String[] values = getValues(key);
+        if (values.length > 0) {
+            value = values[0];
+        }
+        return value;
+    }
+
+    public long getLastModified() throws MetaDataException {
+        return this.lastModified;
+    }
+    
+    private String[] possibleKeys;
+
+    public String[] getPossibleKeys() {
+        if (this.possibleKeys == null) {
+            Element[] elements = getElementSet().getElements();
+            this.possibleKeys = new String[elements.length];
+            for (int i = 0; i < possibleKeys.length; i++) {
+                possibleKeys[i] = elements[i].getName();
+            }
+        }
+        return this.possibleKeys;
+    }
+
+    public String[] getValues(String key) throws MetaDataException {
+        if (this.key2values.containsKey(key)) {
+            return (String[]) this.key2values.get(key);
+        }
+        else {
+            return new String[0];
+        }
+    }
+
+    public boolean isValidAttribute(String key) {
+        return Arrays.asList(getPossibleKeys()).contains(key);
+    }
+
+    public void removeAllValues(String key) throws MetaDataException {
+        throw new UnsupportedOperationException();
+    }
+
+    public void replaceBy(MetaData other) throws MetaDataException {
+        throw new UnsupportedOperationException();
+    }
+
+    public void setValue(String key, String value) throws MetaDataException {
+        throw new UnsupportedOperationException();
+    }
+
+}

Added: lenya/trunk/src/impl/java/org/apache/lenya/cms/metadata/MetaDataCache.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/impl/java/org/apache/lenya/cms/metadata/MetaDataCache.java?rev=633541&view=auto
==============================================================================
--- lenya/trunk/src/impl/java/org/apache/lenya/cms/metadata/MetaDataCache.java (added)
+++ lenya/trunk/src/impl/java/org/apache/lenya/cms/metadata/MetaDataCache.java Tue Mar  4 08:45:18 2008
@@ -0,0 +1,85 @@
+/*
+ * 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.lenya.cms.metadata;
+
+import org.apache.avalon.framework.component.Component;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.excalibur.store.impl.MRUMemoryStore;
+import org.apache.lenya.cms.publication.Document;
+
+/**
+ * Cache for meta data.
+ */
+public class MetaDataCache implements ThreadSafe, Serviceable, Component {
+
+    public static final String ROLE = MetaDataCache.class.getName();
+    protected static final String STORE_ROLE = MetaDataCache.ROLE + "Store";
+    private MRUMemoryStore store;
+    protected ServiceManager manager;
+
+    /**
+     * Get a meta data object from the cache.
+     * @param document The document.
+     * @param namespaceUri The namespace URI.
+     * @return A meta data object.
+     * @throws MetaDataException if an error occurs.
+     */
+    public synchronized MetaData getMetaData(String cacheKey, MetaData meta, String namespaceUri)
+            throws MetaDataException {
+        MRUMemoryStore store = getStore();
+        String key = getCacheKey(cacheKey, namespaceUri);
+
+        MetaData cachedMeta = null;
+        if (store.containsKey(key)) {
+            cachedMeta = (MetaData) store.get(key);
+            if (meta.getLastModified() > cachedMeta.getLastModified()) {
+                cachedMeta = null;
+            }
+        }
+        if (cachedMeta == null) {
+            cachedMeta = new CacheableMetaData(meta);
+            store.hold(key, cachedMeta);
+        }
+        return cachedMeta;
+    }
+
+    protected String getCacheKey(String cacheKey, String namespaceUri) {
+        return cacheKey + ":" + namespaceUri;
+    }
+
+    protected MRUMemoryStore getStore() {
+        if (this.store == null) {
+            synchronized (this) {
+                try {
+                    this.store = (MRUMemoryStore) this.manager.lookup(STORE_ROLE);
+                } catch (ServiceException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        }
+        return this.store;
+    }
+
+    public void service(ServiceManager manager) throws ServiceException {
+        this.manager = manager;
+    }
+
+}

Modified: lenya/trunk/src/impl/java/org/apache/lenya/cms/publication/DocumentImpl.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/impl/java/org/apache/lenya/cms/publication/DocumentImpl.java?rev=633541&r1=633540&r2=633541&view=diff
==============================================================================
--- lenya/trunk/src/impl/java/org/apache/lenya/cms/publication/DocumentImpl.java (original)
+++ lenya/trunk/src/impl/java/org/apache/lenya/cms/publication/DocumentImpl.java Tue Mar  4 08:45:18 2008
@@ -28,10 +28,12 @@
 import org.apache.avalon.framework.container.ContainerUtil;
 import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.ServiceSelector;
 import org.apache.lenya.cms.cocoon.source.SourceUtil;
 import org.apache.lenya.cms.metadata.MetaData;
+import org.apache.lenya.cms.metadata.MetaDataCache;
 import org.apache.lenya.cms.metadata.MetaDataException;
 import org.apache.lenya.cms.publication.util.DocumentVisitor;
 import org.apache.lenya.cms.repository.ContentHolder;
@@ -420,6 +422,7 @@
     protected static final String IDENTIFIABLE_TYPE = "document";
 
     private ResourceType resourceType;
+    private MetaDataCache metaDataCache;
 
     /**
      * Convenience method to read the document's resource type from the meta-data.
@@ -444,7 +447,26 @@
     }
 
     public MetaData getMetaData(String namespaceUri) throws MetaDataException {
-        return getContentHolder().getMetaData(namespaceUri);
+        MetaData meta = getContentHolder().getMetaData(namespaceUri);
+        if (getSession().isModifiable()) {
+            return meta;
+        }
+        else {
+            String cacheKey = getPublication().getId() + ":" + getArea() + ":"
+                    + getUUID() + ":" + getLanguage();
+            return getMetaDataCache().getMetaData(cacheKey, meta, namespaceUri);
+        }
+    }
+
+    protected MetaDataCache getMetaDataCache() {
+        if (this.metaDataCache == null) {
+            try {
+                this.metaDataCache = (MetaDataCache) this.manager.lookup(MetaDataCache.ROLE);
+            } catch (ServiceException e) {
+                throw new RuntimeException(e);
+            }
+        }
+        return this.metaDataCache;
     }
 
     public String[] getMetaDataNamespaceUris() throws MetaDataException {

Added: lenya/trunk/src/webapp/lenya/config/cocoon-xconf/metadata/metadata-cache.xconf
URL: http://svn.apache.org/viewvc/lenya/trunk/src/webapp/lenya/config/cocoon-xconf/metadata/metadata-cache.xconf?rev=633541&view=auto
==============================================================================
--- lenya/trunk/src/webapp/lenya/config/cocoon-xconf/metadata/metadata-cache.xconf (added)
+++ lenya/trunk/src/webapp/lenya/config/cocoon-xconf/metadata/metadata-cache.xconf Tue Mar  4 08:45:18 2008
@@ -0,0 +1,28 @@
+<?xml version="1.0"?>
+<!--
+  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.
+-->
+
+<xconf xpath="/cocoon" unless="/cocoon/component[@role = 'org.apache.lenya.cms.metadata.MetaDataCache']">
+  <component role="org.apache.lenya.cms.metadata.MetaDataCache"
+    class="org.apache.lenya.cms.metadata.MetaDataCache"
+    logger="lenya.metadata.cache"/>
+  <component role="org.apache.lenya.cms.metadata.MetaDataCacheStore"
+    class="org.apache.excalibur.store.impl.MRUMemoryStore"
+    logger="lenya.metadata.cache">
+    <parameter name="maxobjects" value="10000"/>
+  </component>
+</xconf>



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org