Author: vgritsenko Date: Mon Nov 12 09:22:17 2007 New Revision: 594219 URL: http://svn.apache.org/viewvc?rev=594219&view=rev Log: Refactoring document cache to be more generic entry cache
Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/Collection.java xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCache.java xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCacheImpl.java xml/xindice/trunk/java/src/org/apache/xindice/core/data/Entry.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=594219&r1=594218&r2=594219&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 09:22:17 2007 @@ -719,7 +719,7 @@ * Document and byte[]. */ if (documentCache != null) { - Entry entry = documentCache.getDocument(this, key); + Entry entry = documentCache.getEntry(this, key); if (entry != null) { if (log.isTraceEnabled()) { log.trace(localDebugHeader + "Returning cached: " + entry.getValue()); @@ -764,7 +764,7 @@ } if (documentCache != null) { - documentCache.putDocument(this, key, value.getData(), entryMeta); + documentCache.putDocumentEntry(this, key, value.getData(), entryMeta); } } else { String documentChars = value.toString(); @@ -772,12 +772,12 @@ log.trace(localDebugHeader + "Pre parseDocument(): value=<" + documentChars + ">"); } - // FIXME These should be no reason here to re-compress the document & flush symbols table? + // FIXME There should be no reason here to re-compress the document & flush symbols table? document = parseDocument(key, documentChars); flushSymbolTable(); if (documentCache != null) { - documentCache.putDocument(this, key, documentChars, entryMeta); + documentCache.putDocumentEntry(this, key, documentChars, entryMeta); } } @@ -819,7 +819,7 @@ * and save a disk access. */ if (documentCache != null) { - Entry entry = documentCache.getDocumentMeta(this, key); + Entry entry = documentCache.getEntryMeta(this, key); if (entry != null) { return entry; } @@ -1198,7 +1198,7 @@ if (entry != null && entry.getEntryType() == Entry.DOCUMENT) { // binary resources aren't stored in cache or indexes if (documentCache != null) { - documentCache.removeDocument(this, key); + documentCache.removeEntry(this, key); } indexManager.removeDocument(key, (Document) entry.getValue()); } @@ -1339,9 +1339,9 @@ // Cache Stuff if (documentCache != null) { if (compressed) { - documentCache.putDocument(this, key, documentBytes, Entry.createMetaMap(record)); + documentCache.putDocumentEntry(this, key, documentBytes, Entry.createMetaMap(record)); } else { - documentCache.putDocument(this, key, documentChars, Entry.createMetaMap(record)); + documentCache.putDocumentEntry(this, key, documentChars, Entry.createMetaMap(record)); } } @@ -1437,7 +1437,7 @@ } if (documentCache != null) { - documentCache.removeDocument(this, objKey); + documentCache.removeEntry(this, objKey); } if (!filer.deleteRecord(objKey)) { Modified: 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=594219&r1=594218&r2=594219&view=diff ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCache.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCache.java Mon Nov 12 09:22:17 2007 @@ -26,7 +26,7 @@ import java.util.Map; /** - * DocumentCache implements a simple Document caching system for + * DocumentCache implements a simple database Entry caching system for * Collections. * * @version $Revision$, $Date$ @@ -34,53 +34,7 @@ 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 + * Cache key consists of collection and entry key */ class CacheKey { private final Collection col; @@ -109,7 +63,13 @@ } public boolean equals(Object o) { - return (o instanceof DocumentCacheImpl.CacheKey) && col == ((DocumentCacheImpl.CacheKey) o).col && key.equals(((DocumentCacheImpl.CacheKey) o).key); + if (o instanceof DocumentCache.CacheKey) { + CacheKey k = (CacheKey) o; + + return col == k.col && key.equals(k.key); + } + + return false; } public String toString() { @@ -118,28 +78,58 @@ } /** - * Cache value holds document object and meta data + * Obtains entry from the cache + * + * @param col entry collection + * @param key entry key + * @return entry from the cache or null if not present */ - class CacheValue { - private Object value; - private Map meta; + Entry getEntry(Collection col, Key key); - public CacheValue(byte[] value, Map meta) { - this.value = value; - this.meta = meta; - } + /** + * Obtains entry metadata from cache. + * + * @param col entry collection + * @param key entry key + * @return entry meta data from the cache or null if not present + */ + Entry getEntryMeta(Collection col, Key key); - public CacheValue(String value, Map meta) { - this.value = value; - this.meta = meta; - } + /** + * Stores compressed document's bytes in the cache + * + * @param col document entry collection + * @param key document entry key + * @param bytes compressed document bytes + * @param meta document meta attributes map + */ + void putDocumentEntry(Collection col, Key key, byte[] bytes, Map meta); - public Object getValue() { - return value; - } + /** + * Stores serialized document's text in the cache + * + * @param col document entry collection + * @param key document entry key + * @param chars uncompressed document text + * @param meta document meta attributes map + */ + void putDocumentEntry(Collection col, Key key, String chars, Map meta); - public Map getMeta() { - return meta; - } - } + /** + * Stores binary data bytes in the cache + * + * @param col binary entry collection + * @param key binary entry key + * @param bytes binary entry data + * @param meta entry meta attributes map + */ + void putBinaryEntry(Collection col, Key key, byte[] bytes, Map meta); + + /** + * Remove entry from the cache + * + * @param col entry collection + * @param key entry key + */ + void removeEntry(Collection col, Key key); } Modified: 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=594219&r1=594218&r2=594219&view=diff ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCacheImpl.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/core/cache/DocumentCacheImpl.java Mon Nov 12 09:22:17 2007 @@ -31,8 +31,6 @@ 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; @@ -52,95 +50,73 @@ 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 + * CacheKey to Entry mapping */ - public Entry getDocument(Collection col, Key key) { - CacheValue v = (CacheValue) table.get(new CacheKey(col, key)); + private Map table = new WeakHashMap(); + + public Entry getEntry(Collection col, Key key) { + Entry v = (Entry) 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); + switch (v.getEntryType()) { + case Entry.DOCUMENT: + 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()); + } else { + throw new IllegalStateException("Unexpected object: <" + v.getValue() + ">."); } - } - } 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()); - } + case Entry.BINARY: + return new Entry(Entry.BINARY, key, v.getValue(), v.getMeta()); - return null; + default: + 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()); + public Entry getEntryMeta(Collection col, Key key) { + Entry e = (Entry) table.get(new DocumentCache.CacheKey(col, key)); + if (e != null) { + return new Entry(key, e.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) { + public void putDocumentEntry(Collection col, Key key, byte[] bytes, Map meta) { DocumentCache.CacheKey ckey = new DocumentCache.CacheKey(col, key); - table.put(ckey, new CacheValue(bytes, meta)); + table.put(ckey, new Entry(Entry.DOCUMENT, key, 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) { + public void putDocumentEntry(Collection col, Key key, String chars, Map meta) { DocumentCache.CacheKey ckey = new DocumentCache.CacheKey(col, key); - table.put(ckey, new CacheValue(chars, meta)); + table.put(ckey, new Entry(Entry.DOCUMENT, key, chars, meta)); } - /** - * Remove document from the cache - * - * @param col document collection - * @param key document key - */ - public void removeDocument(Collection col, Key key) { + public void putBinaryEntry(Collection col, Key key, byte[] bytes, Map meta) { + DocumentCache.CacheKey ckey = new DocumentCache.CacheKey(col, key); + table.put(ckey, new Entry(Entry.BINARY, key, bytes, meta)); + } + + public void removeEntry(Collection col, Key key) { table.remove(new DocumentCache.CacheKey(col, key)); } @@ -148,7 +124,6 @@ * 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(); @@ -173,5 +148,5 @@ return -1; } - + */ } Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/data/Entry.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/data/Entry.java?rev=594219&r1=594218&r2=594219&view=diff ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/core/data/Entry.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/core/data/Entry.java Mon Nov 12 09:22:17 2007 @@ -28,7 +28,7 @@ /** * Entry is a high-level representation of a database record, that includes - * its internal meta information + * its internal meta information. * * @version $Revision$, $Date$ */ @@ -42,38 +42,38 @@ public static final String CREATED = "created"; public static final String MODIFIED = "modified"; + private final byte type; private final Key key; private final Object value; private final Map meta; - private final byte type; - public Entry(Key key, Document value, Map meta) { + public Entry(byte type, Key key, Object value, Map meta) { + this.type = type; this.key = key; this.value = value; this.meta = meta; - type = DOCUMENT; + } + + public Entry(Key key, Document value, Map meta) { + this(DOCUMENT, key, value, meta); } public Entry(Key key, byte[] value, Map meta) { - this.key = key; - this.value = value; - this.meta = meta; - type = BINARY; + this(BINARY, key, value, meta); } public Entry(Key key, XMLSerializable value, Map meta) { - this.key = key; - this.value = value; - this.meta = meta; - type = OBJECT; + this(OBJECT, key, value, meta); } public Entry(Key key, Map meta) { - this.key = key; - this.value = null; - this.meta = meta; - type = UNKNOWN; + this(UNKNOWN, key, null, meta); + } + + + public byte getEntryType() { + return type; } public Key getKey() { @@ -96,10 +96,6 @@ public long getModificationTime() { Long date = (Long) meta.get(MODIFIED); return date != null ? date.longValue() : 0; - } - - public byte getEntryType() { - return type; } public static Map createMetaMap(Record record) {