/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed 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.
*
* CVS $Id: DocumentCache.java,v 1.14 2004/02/08 02:51:06 vgritsenko Exp $
*/
package org.apache.xindice.core;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.data.Key;
import org.apache.xindice.xml.NodeSource;
import org.apache.xindice.xml.SymbolTable;
import org.apache.xindice.xml.dom.DBDocument;
import org.apache.xindice.xml.dom.DocumentImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.Map;
import java.util.WeakHashMap;
/**
* DocumentCache implements a simple Document caching system for
* Collections.
*
* <small>
* FIXME: Revisit cache implementation. Most probably, commons collections'
* ReferenceMap should be used instead of WeakHashMap.
* </small>
*
* @version CVS $Revision: 1.14 $, $Date: 2004/02/08 02:51:06 $
*/
public final class DocumentCache {
private static final Log log = LogFactory.getLog(DocumentCache.class);
private Map table = new WeakHashMap();
/**
* Obtains document from cache
*
* @param col document collection
* @param key document key
* @return document from the cache or null if not present
*/
public Document getDocument(Collection col, Key key) {
Object v = table.get(new CacheKey(col, key));
if (v == null) {
return null;
} else if (v instanceof Document) {
return (Document) v;
} else if (v instanceof byte[]) {
try {
SymbolTable s = col.getSymbols();
NodeSource ns = new NodeSource(col, key);
return new DocumentImpl((byte[]) v, s, ns);
} catch (Exception e) {
if (log.isWarnEnabled()) {
log.warn("ignored exception", e);
}
}
}
return null;
}
/**
* Stores document in the cache
*
* @param col document collection
* @param key document key
* @param bytes compressed document
*/
public void putDocument(Collection col, Key key, byte[] bytes) {
CacheKey ckey = new CacheKey(col, key);
table.put(ckey, bytes);
}
/**
* Stores document in the cache
*
* @param col document collection
* @param key document key
* @param doc document to store in the cache
*/
public void putDocument(Collection col, Key key, Document doc) {
CacheKey ckey = new CacheKey(col, key);
table.put(ckey, doc);
}
/**
* Remove document from the cache
*
* @param col document collection
* @param key document key
*/
public void removeDocument(Collection col, Key key) {
table.remove(new CacheKey(col, key));
}
/**
* Obtains value of the cache control processing instruction in this document
* @param doc document to inspect for cache control processing instruction
* @return cache control value
*/
public static int getCacheControl(Document doc) {
String cache = DBDocument.CACHE;
NodeList childNodes = doc.getChildNodes();
int size = childNodes.getLength();
for (int i = 0; i < size; i++) {
Node n = childNodes.item(i);
if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE && n.getNodeName().equals(DBDocument.CACHE_CONTROL)) {
cache = n.getNodeValue().trim();
break;
}
}
if (cache != null) {
if (cache.equals(DBDocument.CACHE)) {
return -1;
} else if (cache.equals(DBDocument.NOCACHE)) {
return 0;
} else {
return Integer.parseInt(cache);
}
} else {
return -1;
}
}
/**
* CacheKey
*/
private static class CacheKey {
private Collection col;
private String strVal;
private Key key;
public CacheKey(Collection col, Key key) {
this.col = col;
this.key = key;
}
public Collection getCollection() {
return col;
}
public Key getKey() {
return key;
}
public String toString() {
if (strVal == null) {
strVal = col.getCanonicalDocumentName(key);
}
return strVal;
}
public int hashCode() {
if (strVal == null) {
strVal = col.getCanonicalDocumentName(key);
}
return strVal.hashCode();
}
public boolean equals(Object o) {
return (o instanceof CacheKey) && col == ((CacheKey) o).col && key.equals(((CacheKey) o).key);
}
}
}