Author: vgritsenko Date: Tue Nov 13 06:26:23 2007 New Revision: 594549 URL: http://svn.apache.org/viewvc?rev=594549&view=rev Log: reintroducing cache entry object, storing entry type and Value object
Modified: 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/tests/src/org/apache/xindice/core/CollectionTest.java 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=594549&r1=594548&r2=594549&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 Tue Nov 13 06:26:23 2007 @@ -33,6 +33,17 @@ */ public interface DocumentCache { + // Cache Entry types constants + + /** Entry type for the compressed XML document entry */ + int COMPRESSED = 1; + + /** Entry type for the uncompressed XML document entry */ + int UNCOMPRESSED = 2; + + /** Entry type for the binary entry */ + int BINARY = 3; + /** * Cache key consists of collection and entry 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=594549&r1=594548&r2=594549&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 Tue Nov 13 06:26:23 2007 @@ -24,6 +24,7 @@ import org.apache.xindice.core.Collection; import org.apache.xindice.core.data.Entry; import org.apache.xindice.core.data.Key; +import org.apache.xindice.core.data.Value; import org.apache.xindice.xml.NodeSource; import org.apache.xindice.xml.SymbolTable; import org.apache.xindice.xml.dom.DBDocument; @@ -51,73 +52,104 @@ private static final Log log = LogFactory.getLog(DocumentCacheImpl.class); /** - * CacheKey to Entry mapping + * CacheKey to CacheEntry mapping */ private Map table = new WeakHashMap(); + private class CacheEntry { + private final int type; + private final Key key; + private final Value value; + private final Map meta; + + public CacheEntry(int type, Key key, Value value, Map meta) { + this.type = type; + this.key = key; + this.value = value; + this.meta = meta; + } + + public int getType() { + return type; + } + + public Key getKey() { + return key; + } + + public Value getValue() { + return value; + } + + public Map getMeta() { + return meta; + } + } + public Entry getEntry(Collection col, Key key) { - Entry v = (Entry) table.get(new CacheKey(col, key)); - if (v == null) { + CacheEntry e = (CacheEntry) table.get(new CacheKey(col, key)); + if (e == null) { return null; } - 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[]) { + switch (e.getType()) { + case DocumentCache.COMPRESSED: + { 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() + ">."); + Document doc = new DocumentImpl(e.getValue().getData(), s, ns); + return new Entry(key, doc, e.getMeta()); + } + + case DocumentCache.UNCOMPRESSED: + try { + Document doc = DOMParser.toDocument(e.getValue()); + ((DBDocument) doc).setSource(new NodeSource(col, key)); + return new Entry(key, doc, e.getMeta()); + } catch (Exception ex) { + if (log.isWarnEnabled()) { + log.warn("ignored exception", ex); + } } + break; - case Entry.BINARY: - return new Entry(Entry.BINARY, key, v.getValue(), v.getMeta()); + case DocumentCache.BINARY: + return new Entry(Entry.BINARY, key, e.getValue().getData(), e.getMeta()); default: - return null; + throw new IllegalStateException("Invalid cache entry type: <" + e.getType() + ">"); } + + return null; } 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()); + CacheEntry e = (CacheEntry) table.get(new DocumentCache.CacheKey(col, key)); + if (e == null) { + return null; } - - return null; + + return new Entry(key, e.getMeta()); } public void putDocumentEntry(Collection col, Key key, byte[] bytes, Map meta) { - DocumentCache.CacheKey ckey = new DocumentCache.CacheKey(col, key); - table.put(ckey, new Entry(Entry.DOCUMENT, key, bytes, meta)); + CacheKey ckey = new CacheKey(col, key); + table.put(ckey, new CacheEntry(DocumentCache.COMPRESSED, key, new Value(bytes), meta)); } public void putDocumentEntry(Collection col, Key key, String chars, Map meta) { - DocumentCache.CacheKey ckey = new DocumentCache.CacheKey(col, key); - table.put(ckey, new Entry(Entry.DOCUMENT, key, chars, meta)); + CacheKey ckey = new CacheKey(col, key); + table.put(ckey, new CacheEntry(DocumentCache.UNCOMPRESSED, key, new Value(chars), meta)); } 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)); + CacheKey ckey = new CacheKey(col, key); + table.put(ckey, new CacheEntry(DocumentCache.BINARY, key, new Value(bytes), meta)); } public void removeEntry(Collection col, Key key) { - table.remove(new DocumentCache.CacheKey(col, key)); + table.remove(new CacheKey(col, key)); } /** Modified: xml/xindice/trunk/java/tests/src/org/apache/xindice/core/CollectionTest.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/core/CollectionTest.java?rev=594549&r1=594548&r2=594549&view=diff ============================================================================== --- xml/xindice/trunk/java/tests/src/org/apache/xindice/core/CollectionTest.java (original) +++ xml/xindice/trunk/java/tests/src/org/apache/xindice/core/CollectionTest.java Tue Nov 13 06:26:23 2007 @@ -23,15 +23,17 @@ import org.apache.xindice.core.query.XPathQueryResolver; import org.apache.xindice.util.Configuration; import org.apache.xindice.util.XindiceException; -import org.apache.xindice.xml.TextWriter; import org.apache.xindice.xml.SymbolTable; -import org.apache.xindice.xml.dom.DOMParser; +import org.apache.xindice.xml.TextWriter; import org.apache.xindice.xml.dom.DOMCompressor; +import org.apache.xindice.xml.dom.DOMParser; import org.apache.xindice.xml.dom.DocumentImpl; import junit.framework.TestCase; import org.w3c.dom.Document; +import java.util.Arrays; + /** * Tests Xindice Core API (org.apache.xindice.core.Database, * org.apache.xindice.core.Collection) @@ -222,13 +224,16 @@ } } -// FIXME Define semantics of document cache, and write tests for it -// public void testDocumentCache() throws Exception { -// Document in = collection.getConfig().getElement().getOwnerDocument(); -// collection.insertDocument("document", in); -// -// Document out1 = collection.getDocument("document"); -// Document out2 = collection.getDocument("document"); -// assertTrue(out1 == out2); -// } + + public void testDocumentCache() throws Exception { + Document in = collection.getConfig().getElement().getOwnerDocument(); + collection.insertDocument("document", in); + + DocumentImpl out1 = (DocumentImpl) collection.getDocument("document"); + DocumentImpl out2 = (DocumentImpl) collection.getDocument("document"); + assertNotSame(out1, out2); + // TODO This is subject to change + assertNotSame(out1.getDataBytes(), out2.getDataBytes()); + assertTrue(Arrays.equals(out1.getDataBytes(), out2.getDataBytes())); + } }