Author: vgritsenko Date: Mon Nov 12 05:58:46 2007 New Revision: 594145 URL: http://svn.apache.org/viewvc?rev=594145&view=rev Log: Move DocumentCache to the separate package. Introduce interface.
Added: xml/xindice/trunk/java/src/org/apache/xindice/core/cache/ xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCache.java (with props) xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCacheImpl.java (with props) Removed: xml/xindice/trunk/java/src/org/apache/xindice/core/DocumentCache.java Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/Collection.java xml/xindice/trunk/java/src/org/apache/xindice/core/Database.java Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/Collection.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/Collection.java?rev=594145&r1=594144&r2=594145&view=diff ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/core/Collection.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/core/Collection.java Mon Nov 12 05:58:46 2007 @@ -38,6 +38,7 @@ import org.apache.xindice.core.meta.inline.InlineMetaService; import org.apache.xindice.core.meta.inline.ResourceTypeReader; import org.apache.xindice.core.query.QueryEngine; +import org.apache.xindice.core.cache.DocumentCache; import org.apache.xindice.util.Configurable; import org.apache.xindice.util.Configuration; import org.apache.xindice.util.Named; @@ -1084,6 +1085,7 @@ public boolean isOpened() { // Collection without filer is always open ... for now. + //noinspection SimplifiableIfStatement if (filer == null) { return true; } @@ -1216,10 +1218,9 @@ * This is the lowest-level method for storing a record into the backing store. * It now does update non-inline metadata if the user has configured it. * - * <br/><br/> - * putDocuemnt attempts to perform requested action, and success depends on action - * and presense of the key in the collection. - * <br/><br/> + * <p>putDocument attempts to perform requested action, and success depends on + * action and presense of the key in the collection. + * * @param key Entry key * @param document Document to store * @param action It can be either ACTION_INSERT, ACTION_UPDATE or ACTION_STORE @@ -1627,6 +1628,7 @@ * * @param docKey The Document Key * @param document The Document + * @return True if new document entry was created, false otherwise * @throws DBException if operation failed */ public final boolean setDocument(Object docKey, Document document) throws DBException { @@ -1635,7 +1637,6 @@ } boolean res = putDocument(createNewKey(docKey), document, ACTION_STORE); - if (res) { updateCollectionMeta(); } @@ -1664,7 +1665,6 @@ } boolean res = putBinary(createNewKey(docKey), bytes, ACTION_STORE); - if (res) { updateCollectionMeta(); } Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/Database.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/Database.java?rev=594145&r1=594144&r2=594145&view=diff ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/core/Database.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/core/Database.java Mon Nov 12 05:58:46 2007 @@ -21,6 +21,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.xindice.core.cache.DocumentCache; +import org.apache.xindice.core.cache.DocumentCacheImpl; import org.apache.xindice.core.query.QueryEngine; import org.apache.xindice.server.Xindice; import org.apache.xindice.util.Configuration; @@ -162,7 +164,7 @@ public Database() { super(); - docCache = new DocumentCache(); + docCache = new DocumentCacheImpl(); engine = new QueryEngine(this); closed = true; } Added: xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCache.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCache.java?rev=594145&view=auto ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCache.java (added) +++ xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCache.java Mon Nov 12 05:58:46 2007 @@ -0,0 +1,145 @@ +/* + * 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. + * + * $Id$ + */ + +package org.apache.xindice.core.cache; + +import org.apache.xindice.core.Collection; +import org.apache.xindice.core.data.Entry; +import org.apache.xindice.core.data.Key; + +import java.util.Map; + +/** + * DocumentCache implements a simple Document caching system for + * Collections. + * + * @version $Revision$, $Date$ + */ +public interface DocumentCache { + + /** + * Obtains document from cache + * + * @param col document collection + * @param key document key + * @return document from the cache or null if not present + */ + Entry getDocument(Collection col, Key key); + + /** + * Obtains entry metadata from cache. + * + * @param col document collection + * @param key document key + * @return document from the cache or null if not present + */ + Entry getDocumentMeta(Collection col, Key key); + + /** + * Stores compressed document's bytes in the cache + * + * @param col document collection + * @param key document key + * @param bytes compressed document + * @param meta document meta attributes map + */ + void putDocument(Collection col, Key key, byte[] bytes, Map meta); + + /** + * Stores serialized document's text in the cache + * + * @param col document collection + * @param key document key + * @param chars uncompressed document + * @param meta document meta attributes map + */ + void putDocument(Collection col, Key key, String chars, Map meta); + + /** + * Remove document from the cache + * + * @param col document collection + * @param key document key + */ + void removeDocument(Collection col, Key key); + + /** + * Cache key consists of collection and document key + */ + class CacheKey { + private final Collection col; + private final Key key; + private transient int hashCode; + + public CacheKey(Collection col, Key key) { + this.col = col; + this.key = key; + } + + public Collection getCollection() { + return col; + } + + public Key getKey() { + return key; + } + + public int hashCode() { + if (hashCode == 0) { + hashCode = col.getCanonicalDocumentName(key).hashCode(); + } + + return hashCode; + } + + public boolean equals(Object o) { + return (o instanceof DocumentCacheImpl.CacheKey) && col == ((DocumentCacheImpl.CacheKey) o).col && key.equals(((DocumentCacheImpl.CacheKey) o).key); + } + + public String toString() { + return col.getCanonicalDocumentName(key); + } + } + + /** + * Cache value holds document object and meta data + */ + class CacheValue { + private Object value; + private Map meta; + + public CacheValue(byte[] value, Map meta) { + this.value = value; + this.meta = meta; + } + + public CacheValue(String value, Map meta) { + this.value = value; + this.meta = meta; + } + + public Object getValue() { + return value; + } + + public Map getMeta() { + return meta; + } + } +} Propchange: xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCache.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCache.java ------------------------------------------------------------------------------ svn:keywords = Id Revision Author Date Added: xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCacheImpl.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCacheImpl.java?rev=594145&view=auto ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCacheImpl.java (added) +++ xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCacheImpl.java Mon Nov 12 05:58:46 2007 @@ -0,0 +1,177 @@ +/* + * 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. + * + * $Id$ + */ + +package org.apache.xindice.core.cache; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.xindice.core.Collection; +import org.apache.xindice.core.data.Entry; +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.DOMParser; +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 $Revision$, $Date$ + */ +public class DocumentCacheImpl implements DocumentCache { + + private static final Log log = LogFactory.getLog(DocumentCacheImpl.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 Entry getDocument(Collection col, Key key) { + CacheValue v = (CacheValue) table.get(new CacheKey(col, key)); + + if (v == null) { + return null; + + } else if (v.getValue() instanceof Document) { + return new Entry(key, (Document) v.getValue(), v.getMeta()); + + } else if (v.getValue() instanceof String) { + try { + Document doc = DOMParser.toDocument((String) v.getValue()); + ((DBDocument) doc).setSource(new NodeSource(col, key)); + return new Entry(key, doc, v.getMeta()); + } catch (Exception e) { + if (log.isWarnEnabled()) { + log.warn("ignored exception", e); + } + } + + } else if (v.getValue() instanceof byte[]) { + SymbolTable s = col.getSymbols(); + NodeSource ns = new NodeSource(col, key); + Document doc = new DocumentImpl((byte[]) v.getValue(), s, ns); + return new Entry(key, doc, v.getMeta()); + } + + return null; + } + + /** + * Obtains entry metadata from cache. + * + * @param col document collection + * @param key document key + * @return document from the cache or null if not present + */ + public Entry getDocumentMeta(Collection col, Key key) { + DocumentCache.CacheValue v = (DocumentCache.CacheValue) table.get(new DocumentCache.CacheKey(col, key)); + + if (v != null) { + return new Entry(key, v.getMeta()); + } + + return null; + } + + /** + * Stores compressed document's bytes in the cache + * + * @param col document collection + * @param key document key + * @param bytes compressed document + * @param meta document meta attributes map + */ + public void putDocument(Collection col, Key key, byte[] bytes, Map meta) { + DocumentCache.CacheKey ckey = new DocumentCache.CacheKey(col, key); + table.put(ckey, new CacheValue(bytes, meta)); + } + + /** + * Stores serialized document's text in the cache + * + * @param col document collection + * @param key document key + * @param chars uncompressed document + * @param meta document meta attributes map + */ + public void putDocument(Collection col, Key key, String chars, Map meta) { + DocumentCache.CacheKey ckey = new DocumentCache.CacheKey(col, key); + table.put(ckey, new CacheValue(chars, meta)); + } + + /** + * Remove document from the cache + * + * @param col document collection + * @param key document key + */ + public void removeDocument(Collection col, Key key) { + table.remove(new DocumentCache.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); + } + } + + return -1; + } + +} Propchange: xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCacheImpl.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCacheImpl.java ------------------------------------------------------------------------------ svn:keywords = Id Revision Author Date