Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 169   Methods: 5
NCLOC: 89   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DocumentCacheImpl.java 42.9% 68.2% 80% 63.5%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: DocumentCacheImpl.java 712567 2008-11-09 21:53:28Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.core.cache;
 21   
 22    import org.apache.commons.logging.Log;
 23    import org.apache.commons.logging.LogFactory;
 24    import org.apache.xindice.core.Collection;
 25    import org.apache.xindice.core.data.Entry;
 26    import org.apache.xindice.core.data.Key;
 27    import org.apache.xindice.core.data.Value;
 28    import org.apache.xindice.xml.NodeSource;
 29    import org.apache.xindice.xml.SymbolTable;
 30    import org.apache.xindice.xml.dom.DBDocument;
 31    import org.apache.xindice.xml.dom.DOMParser;
 32    import org.apache.xindice.xml.dom.DocumentImpl;
 33   
 34    import org.w3c.dom.Document;
 35   
 36    import java.util.Map;
 37    import java.util.WeakHashMap;
 38   
 39    /**
 40    * DocumentCache implements a simple Document caching system for
 41    * Collections.
 42    *
 43    * <small>
 44    * FIXME: Revisit cache implementation. Most probably, commons collections'
 45    * ReferenceMap should be used instead of WeakHashMap.
 46    * </small>
 47    *
 48    * @version $Revision: 712567 $, $Date: 2008-11-09 21:53:28 +0000 (Sun, 09 Nov 2008) $
 49    */
 50    public class DocumentCacheImpl implements DocumentCache {
 51   
 52    private static final Log log = LogFactory.getLog(DocumentCacheImpl.class);
 53   
 54    /**
 55    * CacheKey to CacheEntry mapping
 56    */
 57    private final Map table = new WeakHashMap();
 58   
 59   
 60  62340 public Entry getEntry(Collection col, Key key) {
 61  62340 final CacheKey ckey = new CacheKey(col, key);
 62  62340 Entry e;
 63  62340 synchronized (table) {
 64  62340 e = (Entry) table.get(ckey);
 65    }
 66  62340 if (log.isDebugEnabled()) {
 67  0 log.debug("Get " + ckey + ": " + (e == null ? "MISS" : "HIT"));
 68    }
 69  62340 if (e == null) {
 70  44231 return null;
 71    }
 72   
 73  18109 switch (e.getEntryType()) {
 74  18087 case Entry.DOCUMENT:
 75  18087 Document doc;
 76  18087 if (col.isCompressed()) {
 77  14944 SymbolTable s = col.getSymbols();
 78  14944 NodeSource ns = new NodeSource(col, key);
 79  14944 doc = new DocumentImpl(((Value) e.getValue()).getData(), s, ns);
 80    } else {
 81  3143 try {
 82  3143 doc = DOMParser.toDocument((Value) e.getValue());
 83  3143 ((DBDocument) doc).setSource(new NodeSource(col, key));
 84    } catch (Exception ex) {
 85  0 if (log.isWarnEnabled()) {
 86  0 log.warn("ignored exception", ex);
 87    }
 88  0 return null;
 89    }
 90    }
 91  18087 return new Entry(key, doc, e.getMeta());
 92   
 93  22 case Entry.BINARY:
 94  22 return new Entry(key, ((Value) e.getValue()).getData(), e.getMeta());
 95   
 96  0 default:
 97  0 throw new IllegalStateException("Invalid cache entry type: <" + e.getEntryType() + ">");
 98    }
 99    }
 100   
 101  49 public Entry getEntryMeta(Collection col, Key key) {
 102  49 Entry e;
 103  49 synchronized (table) {
 104  49 e = (Entry) table.get(new CacheKey(col, key));
 105    }
 106  49 if (e == null) {
 107  0 return null;
 108    }
 109   
 110  49 return new Entry(key, e.getMeta());
 111    }
 112   
 113  45449 public void putEntry(Collection col, Key key, byte type, Value value, Map meta) {
 114  45449 CacheKey ckey = new CacheKey(col, key);
 115  45449 synchronized (table) {
 116  45449 table.put(ckey, new Entry(type, key, value, meta));
 117    }
 118    }
 119   
 120  0 public void putEntryMeta(Collection col, Key key, byte type, Map meta) {
 121  0 CacheKey ckey = new CacheKey(col, key);
 122  0 synchronized (table) {
 123  0 Entry e = (Entry) table.get(ckey);
 124  0 if (e == null) {
 125  0 e = new Entry(type, key, null, meta);
 126    } else {
 127  0 e = new Entry(type, key, e.getValue(), meta);
 128    }
 129   
 130  0 table.put(ckey, e);
 131    }
 132    }
 133   
 134  1959 public void removeEntry(Collection col, Key key) {
 135  1959 synchronized (table) {
 136  1959 table.remove(new CacheKey(col, key));
 137    }
 138    }
 139   
 140    /**
 141    * Obtains value of the cache control processing instruction in this document
 142    * @param doc document to inspect for cache control processing instruction
 143    * @return cache control value
 144    public static int getCacheControl(Document doc) {
 145    String cache = DBDocument.CACHE;
 146    NodeList childNodes = doc.getChildNodes();
 147    int size = childNodes.getLength();
 148    for (int i = 0; i < size; i++) {
 149    Node n = childNodes.item(i);
 150    if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE && n.getNodeName().equals(DBDocument.CACHE_CONTROL)) {
 151    cache = n.getNodeValue().trim();
 152    break;
 153    }
 154    }
 155   
 156    if (cache != null) {
 157    if (cache.equals(DBDocument.CACHE)) {
 158    return -1;
 159    } else if (cache.equals(DBDocument.NOCACHE)) {
 160    return 0;
 161    } else {
 162    return Integer.parseInt(cache);
 163    }
 164    }
 165   
 166    return -1;
 167    }
 168    */
 169    }