Author: reschke
Date: Thu Feb 13 17:29:38 2014
New Revision: 1567981
URL: http://svn.apache.org/r1567981
Log:
OAK-1266 - add a very simple cache for the root node
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java?rev=1567981&r1=1567980&r2=1567981&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
(original)
+++
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
Thu Feb 13 17:29:38 2014
@@ -38,6 +38,7 @@ import org.apache.jackrabbit.mk.api.Micr
import org.apache.jackrabbit.oak.plugins.document.Collection;
import org.apache.jackrabbit.oak.plugins.document.Document;
import org.apache.jackrabbit.oak.plugins.document.DocumentStore;
+import org.apache.jackrabbit.oak.plugins.document.NodeDocument;
import org.apache.jackrabbit.oak.plugins.document.Revision;
import org.apache.jackrabbit.oak.plugins.document.StableRevisionComparator;
import org.apache.jackrabbit.oak.plugins.document.UpdateOp;
@@ -96,6 +97,16 @@ public class RDBDocumentStore implements
@Override
public <T extends Document> T find(Collection<T> collection, String id,
int maxCacheAge) {
+ if (collection == Collection.NODES && id.equals(ROOT)) {
+ synchronized(this) {
+ if (this.root != null) {
+ long age = System.currentTimeMillis() - rootWritten;
+ if (age < maxCacheAge) {
+ return (T)this.root;
+ }
+ }
+ }
+ }
// TODO handle maxCacheAge
return readDocument(collection, id);
}
@@ -138,12 +149,14 @@ public class RDBDocumentStore implements
@Override
public void invalidateCache() {
- // TODO
+ this.root = null;
}
@Override
public <T extends Document> void invalidateCache(Collection<T> collection,
String id) {
- // TODO
+ if (collection == Collection.NODES && ROOT.equals(id)) {
+ this.root = null;
+ }
}
@Override
@@ -158,7 +171,11 @@ public class RDBDocumentStore implements
@Override
public <T extends Document> T getIfCached(Collection<T> collection, String
id) {
- return null;
+ if (collection == Collection.NODES && ROOT.equals(id)) {
+ return (T) this.root;
+ } else {
+ return null;
+ }
}
// implementation
@@ -203,6 +220,7 @@ public class RDBDocumentStore implements
update.increment(MODCOUNT, 1);
UpdateUtils.applyChanges(doc, update, comparator);
insertDocument(collection, doc);
+ updateCache(collection, doc);
}
return true;
} catch (MicroKernelException ex) {
@@ -227,6 +245,7 @@ public class RDBDocumentStore implements
UpdateUtils.applyChanges(doc, update, comparator);
doc.seal();
insertDocument(collection, doc);
+ updateCache(collection, doc);
} else {
T doc = applyChanges(collection, oldDoc, update, checkConditions);
if (doc == null) {
@@ -247,6 +266,9 @@ public class RDBDocumentStore implements
return null;
}
}
+ else {
+ updateCache(collection, doc);
+ }
}
if (!success) {
@@ -536,4 +558,15 @@ public class RDBDocumentStore implements
// ignored
}
+ // very poor man's cache
+ private NodeDocument root;
+ private long rootWritten;
+ private static final String ROOT = "0:/";
+
+ private synchronized void updateCache(Collection collection, Document doc)
{
+ if (collection == Collection.NODES && doc.getId().equals(ROOT)) {
+ this.root = (NodeDocument)doc;
+ this.rootWritten = System.currentTimeMillis();
+ }
+ }
}