cziegeler 02/05/28 01:25:00 Modified: src/java/org/apache/cocoon/caching Cache.java CacheableProcessingComponent.java PipelineCacheKey.java src/java/org/apache/cocoon/caching/impl CacheImpl.java src/java/org/apache/cocoon/components/pipeline/impl CachingProcessingPipeline.java Log: Changed key from String to long Revision Changes Path 1.3 +6 -6 xml-cocoon2/src/java/org/apache/cocoon/caching/Cache.java Index: Cache.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/caching/Cache.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- Cache.java 27 May 2002 09:57:18 -0000 1.2 +++ Cache.java 28 May 2002 08:25:00 -0000 1.3 @@ -62,7 +62,7 @@ * * @since @next-version@ * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Id: Cache.java,v 1.2 2002/05/27 09:57:18 cziegeler Exp $ + * @version CVS $Id: Cache.java,v 1.3 2002/05/28 08:25:00 cziegeler Exp $ */ public interface Cache extends Component { @@ -77,9 +77,9 @@ * request * @param response the cached response */ - void store(Map objectModel, - Object key, - CachedResponse response) + void store(Map objectModel, + PipelineCacheKey key, + CachedResponse response) throws ProcessingException; /** @@ -88,7 +88,7 @@ * @param key the key used by the caching algorithm to identify the * request */ - CachedResponse get(Object key); + CachedResponse get(PipelineCacheKey key); /** * Remove a cached response. @@ -96,5 +96,5 @@ * @param key the key used by the caching algorithm to identify the * request */ - void remove(Object key); + void remove(PipelineCacheKey key); } 1.4 +3 -2 xml-cocoon2/src/java/org/apache/cocoon/caching/CacheableProcessingComponent.java Index: CacheableProcessingComponent.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/caching/CacheableProcessingComponent.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- CacheableProcessingComponent.java 27 May 2002 12:06:12 -0000 1.3 +++ CacheableProcessingComponent.java 28 May 2002 08:25:00 -0000 1.4 @@ -51,6 +51,7 @@ package org.apache.cocoon.caching; import org.apache.excalibur.source.SourceValidity; +import java.io.Serializable; /** * This marker interface declares a (sitemap) component as cacheable. @@ -59,7 +60,7 @@ * * @since @next-version@ * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Id: CacheableProcessingComponent.java,v 1.3 2002/05/27 12:06:12 cziegeler Exp $ + * @version CVS $Id: CacheableProcessingComponent.java,v 1.4 2002/05/28 08:25:00 cziegeler Exp $ */ public interface CacheableProcessingComponent { @@ -71,7 +72,7 @@ * @return The generated key or <code>null</code> if the component * is currently not cacheable. */ - String generateKey(); + Serializable generateKey(); /** * Generate the validity object. 1.8 +14 -1 xml-cocoon2/src/java/org/apache/cocoon/caching/PipelineCacheKey.java Index: PipelineCacheKey.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/caching/PipelineCacheKey.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- PipelineCacheKey.java 28 May 2002 07:11:43 -0000 1.7 +++ PipelineCacheKey.java 28 May 2002 08:25:00 -0000 1.8 @@ -58,7 +58,7 @@ * or more {@link ComponentCacheKey}s. * * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Id: PipelineCacheKey.java,v 1.7 2002/05/28 07:11:43 cziegeler Exp $ + * @version CVS $Id: PipelineCacheKey.java,v 1.8 2002/05/28 08:25:00 cziegeler Exp $ */ public final class PipelineCacheKey implements java.io.Serializable { @@ -80,6 +80,10 @@ this.keys.add(key); } + public void removeLastKey() { + this.keys.remove(this.keys.size()-1); + } + public boolean equals(Object object) { if (object instanceof PipelineCacheKey) { PipelineCacheKey pck = (PipelineCacheKey)object; @@ -100,5 +104,14 @@ public int hashCode() { // FIXME (CZ) we need a good one here... return this.keys.hashCode(); + } + + public PipelineCacheKey copy() { + PipelineCacheKey pck = new PipelineCacheKey(); + final int len = this.keys.size(); + for(int i=0; i < len; i++) { + pck.keys.add(this.keys.get(i)); + } + return pck; } } 1.3 +7 -6 xml-cocoon2/src/java/org/apache/cocoon/caching/impl/CacheImpl.java Index: CacheImpl.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/caching/impl/CacheImpl.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- CacheImpl.java 27 May 2002 09:57:18 -0000 1.2 +++ CacheImpl.java 28 May 2002 08:25:00 -0000 1.3 @@ -59,6 +59,7 @@ import org.apache.cocoon.ProcessingException; import org.apache.cocoon.caching.Cache; import org.apache.cocoon.caching.CachedResponse; +import org.apache.cocoon.caching.PipelineCacheKey; import org.apache.excalibur.store.Store; import java.io.IOException; import java.util.Map; @@ -71,7 +72,7 @@ * * @since @next-version@ * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Id: CacheImpl.java,v 1.2 2002/05/27 09:57:18 cziegeler Exp $ + * @version CVS $Id: CacheImpl.java,v 1.3 2002/05/28 08:25:00 cziegeler Exp $ */ public class CacheImpl extends AbstractLogEnabled @@ -108,9 +109,9 @@ * request * @param response the cached response */ - public void store(Map objectModel, - Object key, - CachedResponse response) + public void store(Map objectModel, + PipelineCacheKey key, + CachedResponse response) throws ProcessingException { try { this.store.store(key, response); @@ -125,7 +126,7 @@ * @param key the key used by the caching algorithm to identify the * request */ - public CachedResponse get(Object key) { + public CachedResponse get(PipelineCacheKey key) { return (CachedResponse)this.store.get(key); } @@ -135,7 +136,7 @@ * @param key the key used by the caching algorithm to identify the * request */ - public void remove(Object key) { + public void remove(PipelineCacheKey key) { this.store.remove(key); } 1.14 +104 -169 xml-cocoon2/src/java/org/apache/cocoon/components/pipeline/impl/CachingProcessingPipeline.java Index: CachingProcessingPipeline.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/pipeline/impl/CachingProcessingPipeline.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- CachingProcessingPipeline.java 27 May 2002 14:56:28 -0000 1.13 +++ CachingProcessingPipeline.java 28 May 2002 08:25:00 -0000 1.14 @@ -64,6 +64,8 @@ import org.apache.cocoon.caching.CacheValidity; import org.apache.cocoon.caching.CacheValidityToSourceValidity; import org.apache.cocoon.caching.CachingOutputStream; +import org.apache.cocoon.caching.ComponentCacheKey; +import org.apache.cocoon.caching.PipelineCacheKey; import org.apache.cocoon.components.pipeline.AbstractProcessingPipeline; import org.apache.cocoon.components.sax.XMLDeserializer; import org.apache.cocoon.components.sax.XMLSerializer; @@ -78,6 +80,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.io.Serializable; import java.net.SocketException; import java.util.ArrayList; import java.util.HashMap; @@ -89,7 +92,7 @@ * * @since @next-version@ * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Id: CachingProcessingPipeline.java,v 1.13 2002/05/27 14:56:28 cziegeler Exp $ + * @version CVS $Id: CachingProcessingPipeline.java,v 1.14 2002/05/28 08:25:00 cziegeler Exp $ */ public class CachingProcessingPipeline extends AbstractProcessingPipeline @@ -110,10 +113,8 @@ /** The role name of the reader */ protected String readerRole; - /** A cached response which is used as input for the pipeline */ - protected CachedResponse cachedResponse; /** The key to store the cached response */ - protected StringBuffer pipelineCacheKey; + protected PipelineCacheKey pipelineCacheKey; /** The index indicating to the first transformer which is not cacheable */ protected int firstNotCacheableTransformerIndex; /** The deserializer */ @@ -123,8 +124,10 @@ /** The serializer */ protected XMLSerializer xmlSerializer; - /** The validity objects */ - protected SourceValidity[] validityObjects; + /** The cached validity objects */ + protected SourceValidity[] cachedValidityObjects; + /** The cached byte stream */ + protected byte[] cachedResponse; /** * Composable Interface @@ -181,10 +184,9 @@ } else if (this.cachedResponse != null && this.completeResponseIsCached) { try { final OutputStream outputStream = environment.getOutputStream(); - byte[] response = this.cachedResponse.getResponse(); - if (response.length > 0) { - environment.setContentLength(response.length); - outputStream.write(response); + if (this.cachedResponse.length > 0) { + environment.setContentLength(this.cachedResponse.length); + outputStream.write(this.cachedResponse); } } catch ( SocketException se ) { if (se.getMessage().indexOf("reset") > 0 @@ -210,7 +212,7 @@ // execute the pipeline: if ( this.xmlDeserializer != null ) { - this.xmlDeserializer.deserialize(this.cachedResponse.getResponse()); + this.xmlDeserializer.deserialize(this.cachedResponse); } else { this.generator.generate(); } @@ -222,20 +224,20 @@ this.serializer.setOutputStream( os ); // execute the pipeline: if ( this.xmlDeserializer != null ) { - this.xmlDeserializer.deserialize(this.cachedResponse.getResponse()); + this.xmlDeserializer.deserialize(this.cachedResponse); } else { this.generator.generate(); } } if (this.xmlSerializer != null) { if ( this.completeResponseIsCached ) { - CachedResponse response = new CachedResponse(this.validityObjects, + CachedResponse response = new CachedResponse(this.cachedValidityObjects, ((CachingOutputStream)os).getContent()); this.cache.store(environment.getObjectModel(), this.pipelineCacheKey, response); } else { - CachedResponse response = new CachedResponse(this.validityObjects, + CachedResponse response = new CachedResponse(this.cachedValidityObjects, (byte[])this.xmlSerializer.getSAXFragment()); this.cache.store(environment.getObjectModel(), this.pipelineCacheKey, @@ -268,64 +270,58 @@ throws ProcessingException { super.setupPipeline( environment ); + PipelineCacheKey cachedPipelineKey = null; + PipelineCacheKey processingPipelineKey = null; + + Serializable key = null; + boolean generatorIsCacheableProcessingComponent = false; + boolean serializerIsCacheableProcessingComponent = false; + boolean[] transformerIsCacheableProcessingComponent = new boolean[this.transformers.size()]; + this.firstNotCacheableTransformerIndex = 0; this.completeResponseIsCached = false; // first step is to generate the key: // All pipeline components starting with the generator // are tested if they are either a CacheableProcessingComponent - // or Cacheable (obsolete). The returned keys are chained together + // or Cacheable (deprecated). The returned keys are chained together // to build a unique key of the request // is the generator cacheable? - long key = 0; - String stringKey = null; - boolean generatorIsCacheableProcessingComponent = false; - boolean serializerIsCacheableProcessingComponent = false; - boolean[] transformerIsCacheableProcessingComponent = new boolean[this.transformers.size()]; - - if (this.generator instanceof CacheableProcessingComponent) { - stringKey = ((CacheableProcessingComponent)this.generator).generateKey(); + if (super.generator instanceof CacheableProcessingComponent) { + key = ((CacheableProcessingComponent)super.generator).generateKey(); generatorIsCacheableProcessingComponent = true; - } else if (this.generator instanceof Cacheable) { - key = ((Cacheable)this.generator).generateKey(); + } else if (super.generator instanceof Cacheable) { + key = new Long(((Cacheable)super.generator).generateKey()); } - if (key != 0 || stringKey != null) { - this.pipelineCacheKey = new StringBuffer(); - this.pipelineCacheKey.append("PCK:CCK:1-") - .append(this.generatorRole).append('-'); - if (key != 0) { - this.pipelineCacheKey.append(key); - } else { - this.pipelineCacheKey.append( stringKey ); - } + if (key != null) { + processingPipelineKey = new PipelineCacheKey(); + processingPipelineKey.addKey(new ComponentCacheKey(ComponentCacheKey.ComponentType_Generator, + this.generatorRole, + key) + ); // now testing transformers - final int transformerSize = this.transformers.size(); + final int transformerSize = super.transformers.size(); boolean continueTest = true; while (this.firstNotCacheableTransformerIndex < transformerSize && continueTest) { final Transformer trans = - (Transformer)this.transformers.get(this.firstNotCacheableTransformerIndex); - key = 0; - stringKey = null; + (Transformer)super.transformers.get(this.firstNotCacheableTransformerIndex); + key = null; if (trans instanceof CacheableProcessingComponent) { - stringKey = ((CacheableProcessingComponent)trans).generateKey(); + key = ((CacheableProcessingComponent)trans).generateKey(); transformerIsCacheableProcessingComponent[this.firstNotCacheableTransformerIndex] = true; } else if (trans instanceof Cacheable) { - key = ((Cacheable)trans).generateKey(); + key = new Long(((Cacheable)trans).generateKey()); } - if (key != 0 || stringKey != null) { - this.pipelineCacheKey.append("CCK:2-") - .append((String)this.transformerRoles.get(this.firstNotCacheableTransformerIndex)) - .append('-'); - if (key != 0) { - this.pipelineCacheKey.append(key); - } else { - this.pipelineCacheKey.append( stringKey ); - } + if (key != null) { + processingPipelineKey.addKey(new ComponentCacheKey(ComponentCacheKey.ComponentType_Transformer, + (String)this.transformerRoles.get(this.firstNotCacheableTransformerIndex), + key) + ); this.firstNotCacheableTransformerIndex++; } else { continueTest = false; @@ -333,57 +329,57 @@ } // all transformers are cacheable => pipeline is cacheable, test serializer if (this.firstNotCacheableTransformerIndex == transformerSize - && this.serializer == this.lastConsumer) { + && super.serializer == this.lastConsumer) { - key = 0; - stringKey = null; - if (this.serializer instanceof CacheableProcessingComponent) { - stringKey = ((CacheableProcessingComponent)this.serializer).generateKey(); + key = null; + if (super.serializer instanceof CacheableProcessingComponent) { + key = ((CacheableProcessingComponent)this.serializer).generateKey(); serializerIsCacheableProcessingComponent = true; } else if (this.serializer instanceof Cacheable) { - key = ((Cacheable)this.serializer).generateKey(); + key = new Long(((Cacheable)this.serializer).generateKey()); } - if (key != 0) { - this.pipelineCacheKey.append("CCK:3-") - .append(this.serializerRole) - .append('-') - .append(key); - this.completeResponseIsCached = true; - } else if (stringKey != null) { - this.pipelineCacheKey.append("CCK:3-") - .append(this.serializerRole) - .append('-') - .append(stringKey); + if (key != null) { + processingPipelineKey.addKey(new ComponentCacheKey(ComponentCacheKey.ComponentType_Serializer, + (String)this.serializerRole, + key) + ); this.completeResponseIsCached = true; } } } - // lets make a working version first, below is the beginning - // of the new solution - if (this.pipelineCacheKey != null) { - CachedResponse response = this.cache.get(this.pipelineCacheKey.toString()); + // now, we have a key representing all cacheable components up to the + // first non cacheable + if (processingPipelineKey != null) { + cachedPipelineKey = processingPipelineKey.copy(); + } + boolean finished = false; + while (cachedPipelineKey != null && !finished) { + finished = true; + CachedResponse response = this.cache.get( cachedPipelineKey ); + this.cachedResponse = response.getResponse(); + // now test validity if (response != null) { boolean responseIsValid = true; boolean responseIsUsable = true; SourceValidity[] validities = response.getValidityObjects(); int i = 0; - while (i < validities.length) { + while (responseIsValid && i < validities.length) { boolean isValid = validities[i].isValid(); if ( !isValid ) { final SourceValidity validity; if (i == 0) { // test generator if (generatorIsCacheableProcessingComponent) { - validity = ((CacheableProcessingComponent)this.generator).generateValidity(); + validity = ((CacheableProcessingComponent)super.generator).generateValidity(); } else { - validity = new CacheValidityToSourceValidity(((Cacheable)this.generator).generateValidity()); + validity = new CacheValidityToSourceValidity(((Cacheable)super.generator).generateValidity()); } } else if (i <= firstNotCacheableTransformerIndex + 1) { // test transformer final Transformer trans = - (Transformer)this.transformers.get(i-1); + (Transformer)super.transformers.get(i-1); if (transformerIsCacheableProcessingComponent[i-1]) { validity = ((CacheableProcessingComponent)trans).generateValidity(); } else { @@ -392,9 +388,9 @@ } else { // test serializer if (serializerIsCacheableProcessingComponent) { - validity = ((CacheableProcessingComponent)this.serializer).generateValidity(); + validity = ((CacheableProcessingComponent)super.serializer).generateValidity(); } else { - validity = new CacheValidityToSourceValidity(((Cacheable)this.serializer).generateValidity()); + validity = new CacheValidityToSourceValidity(((Cacheable)super.serializer).generateValidity()); } } if (validity != null) { @@ -412,73 +408,12 @@ // FIXME if ( responseIsValid ) { // we are valid, ok that's it - this.cachedResponse = response; - this.validityObjects = validities; + this.cachedValidityObjects = validities; } else { + finished = false; this.cachedResponse = null; - // we are not valid! - this.cache.remove(this.pipelineCacheKey.toString()); + if (!responseIsUsable) { - this.pipelineCacheKey = null; - } - } - } - } - /* - // now, if we have a key, let's see if there is a cached response - boolean finished = false; - while (this.pipelineCacheKey != null && !finished) { - finished = true; - CachedResponse response = this.cache.get(this.pipelineCacheKey.toString()); - // now test validity - if (response != null) { - boolean isValid = true; - SourceValidity[] validities = response.getValidityObjects(); - int i = 0; - while (i < validities.length && isValid) { - isValid = validities[i].isValid(); - if ( !isValid ) { - final SourceValidity validity; - if (i == 0) { - // test generator - if (generatorIsCacheableProcessingComponent) { - validity = ((CacheableProcessingComponent)this.generator).generateValidity(); - } else { - validity = new CacheValidityToSourceValidity(((Cacheable)this.generator).generateValidity()); - } - } else if (i <= firstNotCacheableTransformerIndex + 1) { - // test transformer - final Transformer trans = - (Transformer)this.transformers.get(i-1); - if (transformerIsCacheableProcessingComponent[i-1]) { - validity = ((CacheableProcessingComponent)trans).generateValidity(); - } else { - validity = new CacheValidityToSourceValidity(((Cacheable)trans).generateValidity()); - } - } else { - // test serializer - if (serializerIsCacheableProcessingComponent) { - validity = ((CacheableProcessingComponent)this.serializer).generateValidity(); - } else { - validity = new CacheValidityToSourceValidity(((Cacheable)this.serializer).generateValidity()); - } - } - if (validity != null) { - isValid = validities[i].isValid( validity ); - } - if ( !isValid ) { - // update validity - validities[i] = validity; - } - } - if ( isValid ) i++; - } - // FIXME - if ( isValid ) { - // we are valid, ok that's it - } else { - // we are not valid! - if (validities[i] == null) { // FIXME (CZ) : check this // we can try a shorter key now... // but we don't invalidate the current entry! @@ -488,21 +423,25 @@ this.firstNotCacheableTransformerIndex = i-1; } for(int x=0; x < deleteCount; x++) { - final int deletePos = this.pipelineCacheKey.toString().lastIndexOf("CCK:"); - this.pipelineCacheKey.delete(deletePos, this.pipelineCacheKey.length()); + processingPipelineKey.removeLastKey(); } finished = false; } else { - this.pipelineCacheKey = null; + processingPipelineKey = null; } } else { // invalidate entry - this.cache.remove(this.pipelineCacheKey.toString()); + this.cache.remove( cachedPipelineKey ); // FIXME(CZ) let's get the rest of the validities now } + if (i > 0) { + cachedPipelineKey.removeLastKey(); + } else { + cachedPipelineKey = null; + } } } - }*/ + } } /** @@ -583,32 +522,28 @@ boolean usedCache = false; OutputStream outputStream = environment.getOutputStream(); SourceValidity readerValidity = null; - StringBuffer pcKey = null; + PipelineCacheKey pcKey = null; // test if reader is cacheable - long readerKey = 0; - String readerStringKey = null; + Serializable readerKey = null; boolean isCacheableProcessingComponent = false; - if (this.reader instanceof CacheableProcessingComponent) { - readerStringKey = ((CacheableProcessingComponent)this.reader).generateKey(); + if (super.reader instanceof CacheableProcessingComponent) { + readerKey = ((CacheableProcessingComponent)super.reader).generateKey(); isCacheableProcessingComponent = true; - } else if (this.reader instanceof Cacheable) { - readerKey = ((Cacheable)this.reader).generateKey(); + } else if (super.reader instanceof Cacheable) { + readerKey = new Long(((Cacheable)super.reader).generateKey()); } - if ( readerKey != 0 || readerStringKey != null) { + if ( readerKey != null) { // response is cacheable, build the key - pcKey = new StringBuffer(); - pcKey.append("PCK:CCK:4-").append(this.readerRole) - .append('-'); - if (readerKey != 0) { - pcKey.append(readerKey); - } else { - pcKey.append(readerStringKey); - } + pcKey = new PipelineCacheKey(); + pcKey.addKey(new ComponentCacheKey(ComponentCacheKey.ComponentType_Reader, + this.readerRole, + readerKey) + ); // now we have the key to get the cached object - CachedResponse cachedObject = (CachedResponse)this.cache.get(pcKey.toString()); + CachedResponse cachedObject = (CachedResponse)this.cache.get( pcKey ); if (cachedObject != null) { if (this.getLogger().isDebugEnabled()) { @@ -623,9 +558,9 @@ if ( !valid ) { // get reader validity and compare if (isCacheableProcessingComponent) { - readerValidity = ((CacheableProcessingComponent)this.reader).generateValidity(); + readerValidity = ((CacheableProcessingComponent)super.reader).generateValidity(); } else { - CacheValidity cv = ((Cacheable)this.reader).generateValidity(); + CacheValidity cv = ((Cacheable)super.reader).generateValidity(); if ( cv != null ) { readerValidity = new CacheValidityToSourceValidity( cv ); } @@ -650,7 +585,7 @@ this.getLogger().debug("Cached content is invalid for '" + environment.getURI() + "'."); } // remove invalid cached object - this.cache.remove(pcKey.toString()); + this.cache.remove(pcKey); } } } @@ -663,9 +598,9 @@ } if (readerValidity == null) { if (isCacheableProcessingComponent) { - readerValidity = ((CacheableProcessingComponent)this.reader).generateValidity(); + readerValidity = ((CacheableProcessingComponent)super.reader).generateValidity(); } else { - CacheValidity cv = ((Cacheable)this.reader).generateValidity(); + CacheValidity cv = ((Cacheable)super.reader).generateValidity(); if ( cv != null ) { readerValidity = new CacheValidityToSourceValidity( cv ); } @@ -684,7 +619,7 @@ if (pcKey != null) { this.cache.store( environment.getObjectModel(), - pcKey.toString(), + pcKey, new CachedResponse( new SourceValidity[] {readerValidity}, ((CachingOutputStream)outputStream).getContent()) ); @@ -736,7 +671,7 @@ this.pipelineCacheKey = null; this.cachedResponse = null; - this.validityObjects = null; + this.cachedValidityObjects = null; } /**
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]