Author: mreutegg
Date: Tue Dec  8 08:28:56 2015
New Revision: 1718528

URL: http://svn.apache.org/viewvc?rev=1718528&view=rev
Log:
OAK-3649: Extract node document cache from Mongo and RDB document stores

Applied patch from Tomek Rekawek with minor modifications

Added:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/cache/NodeDocumentCache.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/NodeDocumentLocks.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/StripedNodeDocumentLocks.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/TreeNodeDocumentLocks.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyRDBTest.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheConsistencyIT.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBCacheConsistencyIT.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java?rev=1718528&r1=1718527&r2=1718528&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java
 Tue Dec  8 08:28:56 2015
@@ -37,6 +37,7 @@ import com.google.common.util.concurrent
 import com.mongodb.DB;
 import org.apache.jackrabbit.oak.api.CommitFailedException;
 import org.apache.jackrabbit.oak.cache.CacheLIRS;
+import org.apache.jackrabbit.oak.cache.CacheStats;
 import org.apache.jackrabbit.oak.cache.CacheValue;
 import org.apache.jackrabbit.oak.cache.EmpiricalWeigher;
 import org.apache.jackrabbit.oak.commons.PathUtils;
@@ -46,6 +47,8 @@ import org.apache.jackrabbit.oak.commons
 import org.apache.jackrabbit.oak.json.JsopDiff;
 import org.apache.jackrabbit.oak.plugins.blob.ReferencedBlob;
 import org.apache.jackrabbit.oak.plugins.document.DocumentNodeState.Children;
+import org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache;
+import org.apache.jackrabbit.oak.plugins.document.locks.NodeDocumentLocks;
 import org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore;
 import 
org.apache.jackrabbit.oak.plugins.document.mongo.MongoBlobReferenceIterator;
 import org.apache.jackrabbit.oak.plugins.document.mongo.MongoBlobStore;
@@ -938,6 +941,12 @@ public class DocumentMK {
             return buildCache(CacheType.DOCUMENT, getDocumentCacheSize(), 
null, docStore);
         }
 
+        public NodeDocumentCache buildNodeDocumentCache(DocumentStore 
docStore, NodeDocumentLocks locks) {
+            Cache<CacheValue, NodeDocument> cache = 
buildDocumentCache(docStore);
+            CacheStats cacheStats = new CacheStats(cache, 
"Document-Documents", getWeigher(), getDocumentCacheSize());
+            return new NodeDocumentCache(cache, cacheStats, locks);
+        }
+
         private <K extends CacheValue, V extends CacheValue> Cache<K, V> 
buildCache(
                 CacheType cacheType,
                 long maxWeight,

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/cache/NodeDocumentCache.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/cache/NodeDocumentCache.java?rev=1718528&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/cache/NodeDocumentCache.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/cache/NodeDocumentCache.java
 Tue Dec  8 08:28:56 2015
@@ -0,0 +1,296 @@
+/*
+ * 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.
+ */
+package org.apache.jackrabbit.oak.plugins.document.cache;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.locks.Lock;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnegative;
+import javax.annotation.Nonnull;
+
+import org.apache.jackrabbit.oak.cache.CacheStats;
+import org.apache.jackrabbit.oak.cache.CacheValue;
+import org.apache.jackrabbit.oak.plugins.document.Document;
+import org.apache.jackrabbit.oak.plugins.document.NodeDocument;
+import org.apache.jackrabbit.oak.plugins.document.locks.NodeDocumentLocks;
+import org.apache.jackrabbit.oak.plugins.document.util.StringValue;
+
+import com.google.common.base.Objects;
+import com.google.common.cache.Cache;
+
+/**
+ * Cache for the NodeDocuments. This class is thread-safe and uses the 
provided NodeDocumentLock.
+ */
+public class NodeDocumentCache implements Closeable {
+
+    private final Cache<CacheValue, NodeDocument> nodesCache;
+
+    private final CacheStats cacheStats;
+
+    private final NodeDocumentLocks locks;
+
+    public NodeDocumentCache(@Nonnull Cache<CacheValue, NodeDocument> 
nodesCache,
+                             @Nonnull CacheStats cacheStats,
+                             @Nonnull NodeDocumentLocks locks) {
+        this.nodesCache = nodesCache;
+        this.cacheStats = cacheStats;
+        this.locks = locks;
+    }
+
+    /**
+     * Invalidate document with given key.
+     *
+     * @param key to invalidate
+     */
+    public void invalidate(@Nonnull String key) {
+        Lock lock = locks.acquire(key);
+        try {
+            nodesCache.invalidate(new StringValue(key));
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    /**
+     * Invalidate document with given keys iff their mod counts are different 
as
+     * passed in the map.
+     *
+     * @param modCounts map where key is the document id and the value is the 
mod count
+     * @return number of invalidated entries
+     */
+    @Nonnegative
+    public int invalidateOutdated(@Nonnull Map<String, Number> modCounts) {
+        int invalidatedCount = 0;
+        for (Entry<String, Number> e : modCounts.entrySet()) {
+            String id = e.getKey();
+            Number modCount = e.getValue();
+            NodeDocument doc = getIfPresent(id);
+            if (doc == null) {
+                continue;
+            }
+            if (!Objects.equal(modCount, doc.getModCount())) {
+                invalidate(id);
+                invalidatedCount++;
+            }
+        }
+        return invalidatedCount;
+    }
+
+    /**
+     * Return the cached value or null.
+     *
+     * @param key document key
+     * @return cached value of null if there's no document with given key 
cached
+     */
+    @CheckForNull
+    public NodeDocument getIfPresent(@Nonnull String key) {
+        return nodesCache.getIfPresent(new StringValue(key));
+    }
+
+    /**
+     * Return the document matching given key, optionally loading it from an
+     * external source.
+     *
+     * @see Cache#get(Object, Callable)
+     * @param key document key
+     * @param valueLoader object used to retrieve the document
+     * @return document matching given key
+     */
+    @Nonnull
+    public NodeDocument get(@Nonnull String key, @Nonnull 
Callable<NodeDocument> valueLoader)
+            throws ExecutionException {
+        return nodesCache.get(new StringValue(key), valueLoader);
+    }
+
+    /**
+     * Puts document into cache.
+     *
+     * @param doc document to put
+     */
+    public void put(@Nonnull NodeDocument doc) {
+        if (doc != NodeDocument.NULL) {
+            Lock lock = locks.acquire(doc.getId());
+            try {
+                putInternal(doc);
+            } finally {
+                lock.unlock();
+            }
+        }
+    }
+
+    /**
+     * Puts document into cache iff no entry with the given key is cached
+     * already or the cached document is older (has smaller {@link 
Document#MOD_COUNT}).
+     *
+     * @param doc the document to add to the cache
+     * @return either the given <code>doc</code> or the document already 
present
+     *         in the cache if it's newer
+     */
+    @Nonnull
+    public NodeDocument putIfNewer(@Nonnull final NodeDocument doc) {
+        if (doc == NodeDocument.NULL) {
+            throw new IllegalArgumentException("doc must not be NULL 
document");
+        }
+        doc.seal();
+
+        NodeDocument newerDoc;
+
+        String id = doc.getId();
+        Lock lock = locks.acquire(id);
+        try {
+            NodeDocument cachedDoc = getIfPresent(id);
+            if (cachedDoc == null || cachedDoc == NodeDocument.NULL) {
+                newerDoc = doc;
+                putInternal(doc);
+            } else {
+                Number cachedModCount = cachedDoc.getModCount();
+                Number modCount = doc.getModCount();
+
+                if (cachedModCount == null || modCount == null) {
+                    throw new IllegalStateException("Missing " + 
Document.MOD_COUNT);
+                }
+
+                if (modCount.longValue() > cachedModCount.longValue()) {
+                    newerDoc = doc;
+                    putInternal(doc);
+                } else {
+                    newerDoc = cachedDoc;
+                }
+            }
+        } finally {
+            lock.unlock();
+        }
+        return newerDoc;
+    }
+
+    /**
+     * Puts document into cache iff no entry with the given key is cached
+     * already. This operation is atomic.
+     *
+     * @param doc the document to add to the cache.
+     * @return either the given <code>doc</code> or the document already 
present
+     *         in the cache.
+     */
+    @Nonnull
+    public NodeDocument putIfAbsent(@Nonnull final NodeDocument doc) {
+        if (doc == NodeDocument.NULL) {
+            throw new IllegalArgumentException("doc must not be NULL 
document");
+        }
+        doc.seal();
+
+        String id = doc.getId();
+
+        // make sure we only cache the document if it wasn't
+        // changed and cached by some other thread in the
+        // meantime. That is, use get() with a Callable,
+        // which is only used when the document isn't there
+        Lock lock = locks.acquire(id);
+        try {
+            for (;;) {
+                NodeDocument cached = get(id, new Callable<NodeDocument>() {
+                    @Override
+                    public NodeDocument call() {
+                        return doc;
+                    }
+                });
+                if (cached != NodeDocument.NULL) {
+                    return cached;
+                } else {
+                    invalidate(id);
+                }
+            }
+        } catch (ExecutionException e) {
+            // will never happen because call() just returns
+            // the already available doc
+            throw new IllegalStateException(e);
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    /**
+     * Replaces the cached value if the old document is currently present in
+     * the cache. If the {@code oldDoc} is not cached, nothing will happen. If
+     * {@code oldDoc} does not match the document currently in the cache, then
+     * the cached document is invalidated.
+     *
+     * @param oldDoc the old document
+     * @param newDoc the replacement
+     */
+    public void replaceCachedDocument(@Nonnull final NodeDocument oldDoc,
+                                      @Nonnull final NodeDocument newDoc) {
+        if (newDoc == NodeDocument.NULL) {
+            throw new IllegalArgumentException("doc must not be NULL 
document");
+        }
+        String key = oldDoc.getId();
+
+        Lock lock = locks.acquire(key);
+        try {
+            NodeDocument cached = getIfPresent(key);
+            if (cached != null) {
+                if (Objects.equal(cached.getModCount(), oldDoc.getModCount())) 
{
+                    putInternal(newDoc);
+                } else {
+                    // the cache entry was modified by some other thread in
+                    // the meantime. the updated cache entry may or may not
+                    // include this update. we cannot just apply our update
+                    // on top of the cached entry.
+                    // therefore we must invalidate the cache entry
+                    invalidate(key);
+                }
+            }
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    /**
+     * Returns a view of the entries stored in this cache as a thread-safe map.
+     * Modifications made to the map directly affect the cache.
+     */
+    public Map<CacheValue, NodeDocument> asMap() {
+        return nodesCache.asMap();
+    }
+
+    public CacheStats getCacheStats() {
+        return cacheStats;
+    }
+
+    @Override
+    public void close() throws IOException {
+        if (nodesCache instanceof Closeable) {
+            ((Closeable) nodesCache).close();
+        }
+    }
+
+    //----------------------------< internal 
>----------------------------------
+
+    /**
+     * Puts a document into the cache without acquiring a lock.
+     *
+     * @param doc the document to put into the cache.
+     */
+    protected final void putInternal(@Nonnull NodeDocument doc) {
+        nodesCache.put(new StringValue(doc.getId()), doc);
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/cache/NodeDocumentCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/NodeDocumentLocks.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/NodeDocumentLocks.java?rev=1718528&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/NodeDocumentLocks.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/NodeDocumentLocks.java
 Tue Dec  8 08:28:56 2015
@@ -0,0 +1,15 @@
+package org.apache.jackrabbit.oak.plugins.document.locks;
+
+import java.util.concurrent.locks.Lock;
+
+public interface NodeDocumentLocks {
+
+    /**
+     * Acquires a log for the given key.
+     *
+     * @param key a key.
+     * @return the acquired lock for the given key.
+     */
+    Lock acquire(String key);
+
+}

Propchange: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/NodeDocumentLocks.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/StripedNodeDocumentLocks.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/StripedNodeDocumentLocks.java?rev=1718528&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/StripedNodeDocumentLocks.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/StripedNodeDocumentLocks.java
 Tue Dec  8 08:28:56 2015
@@ -0,0 +1,36 @@
+/*
+ * 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.
+ */
+package org.apache.jackrabbit.oak.plugins.document.locks;
+
+import java.util.concurrent.locks.Lock;
+
+import com.google.common.util.concurrent.Striped;
+
+public class StripedNodeDocumentLocks implements NodeDocumentLocks {
+
+    /**
+     * Locks to ensure cache consistency on reads, writes and invalidation.
+     */
+    private final Striped<Lock> locks = Striped.lock(4096);
+
+    @Override
+    public Lock acquire(String key) {
+        Lock lock = locks.get(key);
+        lock.lock();
+        return lock;
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/StripedNodeDocumentLocks.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/TreeNodeDocumentLocks.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/TreeNodeDocumentLocks.java?rev=1718528&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/TreeNodeDocumentLocks.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/TreeNodeDocumentLocks.java
 Tue Dec  8 08:28:56 2015
@@ -0,0 +1,171 @@
+/*
+ * 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.
+ */
+package org.apache.jackrabbit.oak.plugins.document.locks;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+
+import javax.annotation.Nonnull;
+
+import org.apache.jackrabbit.oak.plugins.document.util.Utils;
+
+import com.google.common.util.concurrent.Striped;
+
+public class TreeNodeDocumentLocks implements NodeDocumentLocks {
+
+    /**
+     * Locks to ensure cache consistency on reads, writes and invalidation.
+     */
+    private final Striped<Lock> locks = Striped.lock(4096);
+
+    /**
+     * ReadWriteLocks to synchronize cache access when child documents are
+     * requested from MongoDB and put into the cache. Accessing a single
+     * document in the cache will acquire a read (shared) lock for the parent
+     * key in addition to the lock (from {@link #locks}) for the individual
+     * document. Reading multiple sibling documents will acquire a write
+     * (exclusive) lock for the parent key. See OAK-1897.
+     */
+    private final Striped<ReadWriteLock> parentLocks = 
Striped.readWriteLock(2048);
+
+    /**
+     * Counts how many times {@link TreeLock}s were acquired.
+     */
+    private volatile AtomicLong lockAcquisitionCounter;
+
+    /**
+     * Acquires a log for the given key. The returned tree lock will also hold
+     * a shared lock on the parent key.
+     *
+     * @param key a key.
+     * @return the acquired lock for the given key.
+     */
+    @Override
+    public TreeLock acquire(String key) {
+        if (lockAcquisitionCounter != null) {
+            lockAcquisitionCounter.incrementAndGet();
+        }
+        TreeLock lock = TreeLock.shared(parentLocks.get(getParentId(key)), 
locks.get(key));
+        lock.lock();
+        return lock;
+    }
+
+    /**
+     * Acquires an exclusive lock on the given parent key. Use this method to
+     * block cache access for child keys of the given parent key.
+     *
+     * @param parentKey the parent key.
+     * @return the acquired lock for the given parent key.
+     */
+    public TreeLock acquireExclusive(String parentKey) {
+        if (lockAcquisitionCounter != null) {
+            lockAcquisitionCounter.incrementAndGet();
+        }
+        TreeLock lock = TreeLock.exclusive(parentLocks.get(parentKey));
+        lock.lock();
+        return lock;
+    }
+
+    /**
+     * Returns the parent id for the given id. An empty String is returned if
+     * the given value is the id of the root document or the id for a long 
path.
+     *
+     * @param id an id for a document.
+     * @return the id of the parent document or the empty String.
+     */
+    @Nonnull
+    private static String getParentId(@Nonnull String id) {
+        String parentId = Utils.getParentId(checkNotNull(id));
+        if (parentId == null) {
+            parentId = "";
+        }
+        return parentId;
+    }
+
+    public void resetLockAcquisitionCount() {
+        lockAcquisitionCounter = new AtomicLong();
+    }
+
+    public long getLockAcquisitionCount() {
+        if (lockAcquisitionCounter == null) {
+            throw new IllegalStateException("The counter hasn't been 
initialized");
+        }
+        return lockAcquisitionCounter.get();
+    }
+
+    private final static class TreeLock implements Lock {
+
+        private final Lock parentLock;
+
+        private final Lock lock;
+
+        private TreeLock(Lock parentLock, Lock lock) {
+            this.parentLock = parentLock;
+            this.lock = lock;
+        }
+
+        private static TreeLock shared(ReadWriteLock parentLock, Lock lock) {
+            return new TreeLock(parentLock.readLock(), lock);
+        }
+
+        private static TreeLock exclusive(ReadWriteLock parentLock) {
+            return new TreeLock(parentLock.writeLock(), null);
+        }
+
+        @Override
+        public void lock() {
+            parentLock.lock();
+            if (lock != null) {
+                lock.lock();
+            }
+        }
+
+        @Override
+        public void unlock() {
+            if (lock != null) {
+                lock.unlock();
+            }
+            parentLock.unlock();
+        }
+
+        @Override
+        public void lockInterruptibly() throws InterruptedException {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public boolean tryLock() {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public boolean tryLock(long time, TimeUnit unit) throws 
InterruptedException {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public Condition newCondition() {
+            throw new UnsupportedOperationException();
+        }
+    }
+
+}

Propchange: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/locks/TreeNodeDocumentLocks.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java?rev=1718528&r1=1718527&r2=1718528&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
 Tue Dec  8 08:28:56 2015
@@ -16,12 +16,12 @@
  */
 package org.apache.jackrabbit.oak.plugins.document.mongo;
 
-import java.io.Closeable;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -30,10 +30,7 @@ import java.util.TreeMap;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -51,7 +48,6 @@ import com.mongodb.ReadPreference;
 
 import org.apache.jackrabbit.oak.cache.CacheStats;
 import org.apache.jackrabbit.oak.cache.CacheValue;
-import org.apache.jackrabbit.oak.plugins.document.CachedNodeDocument;
 import org.apache.jackrabbit.oak.plugins.document.Collection;
 import org.apache.jackrabbit.oak.plugins.document.Document;
 import org.apache.jackrabbit.oak.plugins.document.DocumentMK;
@@ -67,17 +63,15 @@ import org.apache.jackrabbit.oak.plugins
 import org.apache.jackrabbit.oak.plugins.document.UpdateOp.Operation;
 import org.apache.jackrabbit.oak.plugins.document.UpdateUtils;
 import org.apache.jackrabbit.oak.plugins.document.cache.CacheInvalidationStats;
-import org.apache.jackrabbit.oak.plugins.document.util.StringValue;
+import org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache;
+import org.apache.jackrabbit.oak.plugins.document.locks.TreeNodeDocumentLocks;
 import org.apache.jackrabbit.oak.plugins.document.util.Utils;
 import org.apache.jackrabbit.oak.stats.Clock;
 import org.apache.jackrabbit.oak.util.PerfLogger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Objects;
-import com.google.common.cache.Cache;
 import com.google.common.collect.Maps;
-import com.google.common.util.concurrent.Striped;
 import com.mongodb.BasicDBObject;
 import com.mongodb.DB;
 import com.mongodb.DBCollection;
@@ -89,7 +83,6 @@ import com.mongodb.WriteConcern;
 import com.mongodb.WriteResult;
 
 import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
  * A document store that uses MongoDB as the backend.
@@ -119,28 +112,9 @@ public class MongoDocumentStore implemen
 
     private final DB db;
 
-    private final Cache<CacheValue, NodeDocument> nodesCache;
-    private final CacheStats cacheStats;
+    private final NodeDocumentCache nodesCache;
 
-    /**
-     * Locks to ensure cache consistency on reads, writes and invalidation.
-     */
-    private final Striped<Lock> locks = Striped.lock(4096);
-
-    /**
-     * ReadWriteLocks to synchronize cache access when child documents are
-     * requested from MongoDB and put into the cache. Accessing a single
-     * document in the cache will acquire a read (shared) lock for the parent
-     * key in addition to the lock (from {@link #locks}) for the individual
-     * document. Reading multiple sibling documents will acquire a write
-     * (exclusive) lock for the parent key. See OAK-1897.
-     */
-    private final Striped<ReadWriteLock> parentLocks = 
Striped.readWriteLock(2048);
-
-    /**
-     * Counts how many times {@link TreeLock}s were acquired.
-     */
-    private final AtomicLong lockAcquisitionCounter = new AtomicLong();
+    private final TreeNodeDocumentLocks nodeLocks;
 
     private Clock clock = Clock.SIMPLE;
 
@@ -241,10 +215,9 @@ public class MongoDocumentStore implemen
         options.put("unique", Boolean.FALSE);
         this.journal.createIndex(index, options);
 
+        this.nodeLocks = new TreeNodeDocumentLocks();
+        this.nodesCache = builder.buildNodeDocumentCache(this, nodeLocks);
 
-        nodesCache = builder.buildDocumentCache(this);
-        cacheStats = new CacheStats(nodesCache, "Document-Documents", 
builder.getWeigher(),
-                builder.getDocumentCacheSize());
         LOG.info("Configuration maxReplicationLagMillis {}, " +
                 "maxDeltaForModTimeIdxSecs {}, disableIndexHint {}, {}",
                 maxReplicationLagMillis, maxDeltaForModTimeIdxSecs,
@@ -288,7 +261,7 @@ public class MongoDocumentStore implemen
         }
         return result;
     }
-    
+
     @Override
     public CacheInvalidationStats invalidateCache(Iterable<String> keys) {
         LOG.debug("invalidateCache: start");
@@ -301,7 +274,7 @@ public class MongoDocumentStore implemen
             final List<String> ids = new 
ArrayList<String>(IN_CLAUSE_BATCH_SIZE);
             while(it.hasNext() && ids.size() < IN_CLAUSE_BATCH_SIZE) {
                 final String id = it.next();
-                if (getCachedNodeDoc(id) != null) {
+                if (nodesCache.getIfPresent(id) != null) {
                     // only add those that we actually do have cached
                     ids.add(id);
                 }
@@ -320,21 +293,18 @@ public class MongoDocumentStore implemen
             DBCursor cursor = nodes.find(query.get(), fields);
             cursor.setReadPreference(ReadPreference.primary());
             result.queryCount++;
-            
+
+            Map<String, Number> modCounts = new HashMap<String, Number>();
             for (DBObject obj : cursor) {
-                result.cacheEntriesProcessedCount++;
                 String id = (String) obj.get(Document.ID);
                 Number modCount = (Number) obj.get(Document.MOD_COUNT);
-                
-                CachedNodeDocument cachedDoc = getCachedNodeDoc(id);
-                if (cachedDoc != null
-                        && !Objects.equal(cachedDoc.getModCount(), modCount)) {
-                    invalidateCache(Collection.NODES, id);
-                    result.invalidationCount++;
-                } else {
-                    result.upToDateCount++;
-                }
+                modCounts.put(id, modCount);
             }
+
+            int invalidated = nodesCache.invalidateOutdated(modCounts);
+            result.cacheEntriesProcessedCount += modCounts.size();
+            result.invalidationCount += invalidated;
+            result.upToDateCount = modCounts.size() - invalidated;
         }
 
         result.cacheSize = size;
@@ -345,22 +315,10 @@ public class MongoDocumentStore implemen
     @Override
     public <T extends Document> void invalidateCache(Collection<T> collection, 
String key) {
         if (collection == Collection.NODES) {
-            TreeLock lock = acquire(key, collection);
-            try {
-                nodesCache.invalidate(new StringValue(key));
-            } finally {
-                lock.unlock();
-            }
-        }
-    }
-
-    public <T extends Document> void invalidateCache(Collection<T> collection, 
List<String> keys) {
-        for(String key : keys){
-            invalidateCache(collection, key);
+            nodesCache.invalidate(key);
         }
     }
 
-
     @Override
     public <T extends Document> T find(Collection<T> collection, String key) {
         final long start = PERFLOG.start();
@@ -388,11 +346,10 @@ public class MongoDocumentStore implemen
             return findUncachedWithRetry(collection, key,
                     DocumentReadPreference.PRIMARY, 2);
         }
-        CacheValue cacheKey = new StringValue(key);
         NodeDocument doc;
         if (maxCacheAge > 0 || preferCached) {
             // first try without lock
-            doc = nodesCache.getIfPresent(cacheKey);
+            doc = nodesCache.getIfPresent(key);
             if (doc != null) {
                 if (preferCached ||
                         getTime() - doc.getCreated() < maxCacheAge) {
@@ -405,12 +362,12 @@ public class MongoDocumentStore implemen
         }
         Throwable t;
         try {
-            TreeLock lock = acquire(key, collection);
+            Lock lock = nodeLocks.acquire(key);
             try {
                 if (maxCacheAge > 0 || preferCached) {
                     // try again some other thread may have populated
                     // the cache by now
-                    doc = nodesCache.getIfPresent(cacheKey);
+                    doc = nodesCache.getIfPresent(key);
                     if (doc != null) {
                         if (preferCached ||
                                 getTime() - doc.getCreated() < maxCacheAge) {
@@ -425,7 +382,7 @@ public class MongoDocumentStore implemen
                         collection, key,
                         getReadPreference(maxCacheAge), 2);
                 invalidateCache(collection, key);
-                doc = nodesCache.get(cacheKey, new Callable<NodeDocument>() {
+                doc = nodesCache.get(key, new Callable<NodeDocument>() {
                     @Override
                     public NodeDocument call() throws Exception {
                         return d == null ? NodeDocument.NULL : d;
@@ -608,7 +565,7 @@ public class MongoDocumentStore implemen
         String parentId = Utils.getParentIdFromLowerLimit(fromKey);
         long lockTime = -1;
         final long start = PERFLOG.start();
-        TreeLock lock = withLock ? acquireExclusive(parentId != null ? 
parentId : "") : null;
+        Lock lock = withLock ? nodeLocks.acquireExclusive(parentId != null ? 
parentId : "") : null;
         try {
             if (start != -1) {
                 lockTime = System.currentTimeMillis() - start;
@@ -639,26 +596,7 @@ public class MongoDocumentStore implemen
                     if (collection == Collection.NODES
                             && doc != null
                             && lock != null) {
-                        doc.seal();
-                        String id = doc.getId();
-                        CacheValue cacheKey = new StringValue(id);
-                        // do not overwrite document in cache if the
-                        // existing one in the cache is newer
-                        NodeDocument cached = 
nodesCache.getIfPresent(cacheKey);
-                        if (cached != null && cached != NodeDocument.NULL) {
-                            // check mod count
-                            Number cachedModCount = cached.getModCount();
-                            Number modCount = doc.getModCount();
-                            if (cachedModCount == null || modCount == null) {
-                                throw new IllegalStateException(
-                                        "Missing " + Document.MOD_COUNT);
-                            }
-                            if (modCount.longValue() > 
cachedModCount.longValue()) {
-                                nodesCache.put(cacheKey, (NodeDocument) doc);
-                            }
-                        } else {
-                            nodesCache.put(cacheKey, (NodeDocument) doc);
-                        }
+                        nodesCache.putIfNewer((NodeDocument) doc);
                     }
                     list.add(doc);
                 }
@@ -709,7 +647,11 @@ public class MongoDocumentStore implemen
                 } catch (Exception e) {
                     throw DocumentStoreException.convert(e, "Remove failed for 
" + keyBatch);
                 } finally {
-                    invalidateCache(collection, keyBatch);
+                    if (collection == Collection.NODES) {
+                        for (String key : keyBatch) {
+                            invalidateCache(collection, key);
+                        }
+                    }
                 }
             }
         } finally {
@@ -742,7 +684,9 @@ public class MongoDocumentStore implemen
                     } catch (Exception e) {
                         throw DocumentStoreException.convert(e, "Remove failed 
for " + batch);
                     } finally {
-                        invalidateCache(collection, 
Lists.newArrayList(batchIds));
+                        if (collection == Collection.NODES) {
+                            invalidateCache(batchIds);
+                        }
                     }
                     batchIds.clear();
                     batch.clear();
@@ -754,6 +698,7 @@ public class MongoDocumentStore implemen
         return num;
     }
 
+    @SuppressWarnings("unchecked")
     @CheckForNull
     private <T extends Document> T findAndModify(Collection<T> collection,
                                                  UpdateOp updateOp,
@@ -764,16 +709,17 @@ public class MongoDocumentStore implemen
         updateOp = updateOp.copy();
         DBObject update = createUpdate(updateOp);
 
-        TreeLock lock = acquire(updateOp.getId(), collection);
+        Lock lock = null;
+        if (collection == Collection.NODES) {
+            lock = nodeLocks.acquire(updateOp.getId());
+        }
         final long start = PERFLOG.start();
         try {
             // get modCount of cached document
             Number modCount = null;
             T cachedDoc = null;
             if (collection == Collection.NODES) {
-                @SuppressWarnings("unchecked")
-                T doc = (T) nodesCache.getIfPresent(new 
StringValue(updateOp.getId()));
-                cachedDoc = doc;
+                cachedDoc = (T) nodesCache.getIfPresent(updateOp.getId());
                 if (cachedDoc != null) {
                     modCount = cachedDoc.getModCount();
                 }
@@ -790,7 +736,10 @@ public class MongoDocumentStore implemen
                 WriteResult result = dbCollection.update(query.get(), update);
                 if (result.getN() > 0) {
                     // success, update cached document
-                    putToCache(collection, cachedDoc, updateOp);
+                    if (collection == Collection.NODES) {
+                        NodeDocument newDoc = (NodeDocument) 
applyChanges(collection, cachedDoc, updateOp);
+                        nodesCache.put(newDoc);
+                    }
                     // return previously cached document
                     return cachedDoc;
                 }
@@ -805,13 +754,16 @@ public class MongoDocumentStore implemen
             }
             T oldDoc = convertFromDBObject(collection, oldNode);
             if (oldDoc != null) {
-                putToCache(collection, oldDoc, updateOp);
+                if (collection == Collection.NODES) {
+                    NodeDocument newDoc = (NodeDocument) 
applyChanges(collection, oldDoc, updateOp);
+                    nodesCache.put(newDoc);
+                }
                 oldDoc.seal();
             } else if (upsert) {
                 if (collection == Collection.NODES) {
                     NodeDocument doc = (NodeDocument) 
collection.newDocument(this);
                     UpdateUtils.applyChanges(doc, updateOp);
-                    addToCache(doc);
+                    nodesCache.putIfAbsent(doc);
                 }
             } else {
                 // updateOp without conditions and not an upsert
@@ -821,7 +773,9 @@ public class MongoDocumentStore implemen
         } catch (Exception e) {
             throw DocumentStoreException.convert(e);
         } finally {
-            lock.unlock();
+            if (lock != null) {
+                lock.unlock();
+            }
             PERFLOG.end(start, 1, "findAndModify [{}]", updateOp.getId());
         }
     }
@@ -908,12 +862,7 @@ public class MongoDocumentStore implemen
                 dbCollection.insert(inserts);
                 if (collection == Collection.NODES) {
                     for (T doc : docs) {
-                        TreeLock lock = acquire(doc.getId(), collection);
-                        try {
-                            addToCache((NodeDocument) doc);
-                        } finally {
-                            lock.unlock();
-                        }
+                        nodesCache.putIfAbsent((NodeDocument) doc);
                     }
                 }
                 return true;
@@ -942,7 +891,7 @@ public class MongoDocumentStore implemen
             if (collection == Collection.NODES) {
                 cachedDocs = Maps.newHashMap();
                 for (String key : keys) {
-                    cachedDocs.put(key, nodesCache.getIfPresent(new 
StringValue(key)));
+                    cachedDocs.put(key, nodesCache.getIfPresent(key));
                 }
             }
             try {
@@ -950,14 +899,16 @@ public class MongoDocumentStore implemen
                 if (collection == Collection.NODES) {
                     // update cache
                     for (Entry<String, NodeDocument> entry : 
cachedDocs.entrySet()) {
-                        TreeLock lock = acquire(entry.getKey(), collection);
+                        // the cachedDocs is not empty, so the collection = 
NODES
+                        Lock lock = nodeLocks.acquire(entry.getKey());
                         try {
-                            if (entry.getValue() == null
-                                    || entry.getValue() == NodeDocument.NULL) {
-                                // make sure concurrently loaded document is 
invalidated
-                                nodesCache.invalidate(new 
StringValue(entry.getKey()));
+                            if (entry.getValue() == null || entry.getValue() 
== NodeDocument.NULL) {
+                                // make sure concurrently loaded document is
+                                // invalidated
+                                nodesCache.invalidate(entry.getKey());
                             } else {
-                                updateCache(Collection.NODES, 
entry.getValue(), updateOp.shallowCopy(entry.getKey()));
+                                NodeDocument newDoc = 
applyChanges(Collection.NODES, entry.getValue(), 
updateOp.shallowCopy(entry.getKey()));
+                                
nodesCache.replaceCachedDocument(entry.getValue(), newDoc);
                             }
                         } finally {
                             lock.unlock();
@@ -1006,7 +957,7 @@ public class MongoDocumentStore implemen
                 ReadPreference readPreference = ReadPreference.primary();
                 if (parentId != null) {
                     long replicationSafeLimit = getTime() - 
maxReplicationLagMillis;
-                    NodeDocument cachedDoc = (NodeDocument) 
getIfCached(collection, parentId);
+                    NodeDocument cachedDoc = nodesCache.getIfPresent(parentId);
                     // FIXME: this is not quite accurate, because ancestors
                     // are updated in a background thread (_lastRev). We
                     // will need to revise this for low maxReplicationLagMillis
@@ -1090,20 +1041,16 @@ public class MongoDocumentStore implemen
     @Override
     public void dispose() {
         nodes.getDB().getMongo().close();
-
-        if (nodesCache instanceof Closeable) {
-            try {
-                ((Closeable) nodesCache).close();
-            } catch (IOException e) {
-
-                LOG.warn("Error occurred while closing Off Heap Cache", e);
-            }
+        try {
+            nodesCache.close();
+        } catch (IOException e) {
+            LOG.warn("Error occurred while closing Off Heap Cache", e);
         }
     }
 
     @Override
     public CacheStats getCacheStats() {
-        return cacheStats;
+        return nodesCache.getCacheStats();
     }
 
     @Override
@@ -1119,18 +1066,6 @@ public class MongoDocumentStore implemen
         return disableIndexHint;
     }
 
-    Iterable<? extends Map.Entry<CacheValue, ? extends CachedNodeDocument>> 
getCacheEntries() {
-        return nodesCache.asMap().entrySet();
-    }
-
-    CachedNodeDocument getCachedNodeDoc(String id) {
-        return nodesCache.getIfPresent(new StringValue(id));
-    }
-
-    protected Cache<CacheValue, NodeDocument> getNodeDocumentCache() {
-        return nodesCache;
-    }
-
     private static void log(String message, Object... args) {
         if (LOG.isDebugEnabled()) {
             String argList = Arrays.toString(args);
@@ -1147,124 +1082,10 @@ public class MongoDocumentStore implemen
             return null;
         }
         @SuppressWarnings("unchecked")
-        T doc = (T) nodesCache.getIfPresent(new StringValue(key));
+        T doc = (T) nodesCache.getIfPresent(key);
         return doc;
     }
 
-    /**
-     * Applies an update to the nodes cache. This method does not acquire
-     * a lock for the document. The caller must ensure it holds a lock for
-     * the updated document. See striped {@link #locks}.
-     *
-     * @param <T> the document type.
-     * @param collection the document collection.
-     * @param oldDoc the old document.
-     * @param updateOp the update operation.
-     */
-    private <T extends Document> void updateCache(@Nonnull Collection<T> 
collection,
-                                                  @Nonnull T oldDoc,
-                                                  @Nonnull UpdateOp updateOp) {
-        // cache the new document
-        if (collection == Collection.NODES) {
-            checkNotNull(oldDoc);
-            checkNotNull(updateOp);
-            // we can only update the cache based on the oldDoc if we
-            // still have the oldDoc in the cache, otherwise we may
-            // update the cache with an outdated document
-            CacheValue key = new StringValue(updateOp.getId());
-            NodeDocument cached = nodesCache.getIfPresent(key);
-            if (cached == null) {
-                // cannot use oldDoc to update cache
-                return;
-            }
-
-            // check if the currently cached document matches oldDoc
-            if (Objects.equal(cached.getModCount(), oldDoc.getModCount())) {
-                NodeDocument newDoc = (NodeDocument) 
collection.newDocument(this);
-                oldDoc.deepCopy(newDoc);
-
-                UpdateUtils.applyChanges(newDoc, updateOp);
-                newDoc.seal();
-
-                nodesCache.put(key, newDoc);
-            } else {
-                // the cache entry was modified by some other thread in
-                // the meantime. the updated cache entry may or may not
-                // include this update. we cannot just apply our update
-                // on top of the cached entry.
-                // therefore we must invalidate the cache entry
-                nodesCache.invalidate(key);
-            }
-        }
-    }
-
-    /**
-     * Adds a document to the {@link #nodesCache} iff there is no document
-     * in the cache with the document key. This method does not acquire a lock
-     * from {@link #locks}! The caller must ensure a lock is held for the
-     * given document.
-     *
-     * @param doc the document to add to the cache.
-     * @return either the given <code>doc</code> or the document already 
present
-     *          in the cache.
-     */
-    @Nonnull
-    private NodeDocument addToCache(@Nonnull final NodeDocument doc) {
-        if (doc == NodeDocument.NULL) {
-            throw new IllegalArgumentException("doc must not be NULL 
document");
-        }
-        doc.seal();
-        // make sure we only cache the document if it wasn't
-        // changed and cached by some other thread in the
-        // meantime. That is, use get() with a Callable,
-        // which is only used when the document isn't there
-        try {
-            CacheValue key = new StringValue(doc.getId());
-            for (;;) {
-                NodeDocument cached = nodesCache.get(key,
-                        new Callable<NodeDocument>() {
-                    @Override
-                    public NodeDocument call() {
-                        return doc;
-                    }
-                });
-                if (cached != NodeDocument.NULL) {
-                    return cached;
-                } else {
-                    nodesCache.invalidate(key);
-                }
-            }
-        } catch (ExecutionException e) {
-            // will never happen because call() just returns
-            // the already available doc
-            throw new IllegalStateException(e);
-        }
-    }
-
-    /**
-     * Unconditionally puts a document into the cache if {@code collection} is
-     * {@link Collection#NODES}. The document put into the cache is
-     * {@code oldDoc} with the {@code updateOp} applied. This method does not
-     * acquire a lock from {@link #locks}! The caller must ensure a lock is 
held
-     * for the given document.
-     *
-     * @param collection the collection where oldDoc belongs to.
-     * @param oldDoc how the document looked before the update.
-     * @param updateOp the update just applied to the document.
-     */
-    private <T extends Document> void putToCache(@Nonnull Collection<T> 
collection,
-                                                 @Nonnull T oldDoc,
-                                                 @Nonnull UpdateOp updateOp) {
-        if (collection == Collection.NODES) {
-            CacheValue key = new StringValue(oldDoc.getId());
-            NodeDocument newDoc = (NodeDocument) collection.newDocument(this);
-            oldDoc.deepCopy(newDoc);
-            UpdateUtils.applyChanges(newDoc, updateOp);
-            newDoc.seal();
-            nodesCache.put(key, newDoc);
-        }
-    }
-
     @Nonnull
     private static QueryBuilder createQueryForUpdate(String key,
                                                      Map<Key, Condition> 
conditions) {
@@ -1351,50 +1172,13 @@ public class MongoDocumentStore implemen
         return update;
     }
 
-    /**
-     * Returns the parent id for the given id. An empty String is returned if
-     * the given value is the id of the root document or the id for a long 
path.
-     *
-     * @param id an id for a document.
-     * @return the id of the parent document or the empty String.
-     */
     @Nonnull
-    private static String getParentId(@Nonnull String id) {
-        String parentId = Utils.getParentId(checkNotNull(id));
-        if (parentId == null) {
-            parentId = "";
-        }
-        return parentId;
-    }
-
-    /**
-     * Acquires a log for the given key. The returned tree lock will also hold
-     * a shared lock on the parent key.
-     *
-     * @param key a key.
-     * @param collection the collection for which the lock is acquired.
-     * @return the acquired lock for the given key.
-     */
-    private TreeLock acquire(String key, Collection<?> collection) {
-        lockAcquisitionCounter.incrementAndGet();
-        if (collection == Collection.NODES) {
-            return TreeLock.shared(parentLocks.get(getParentId(key)), 
locks.get(key));
-        } else {
-            // return a dummy lock
-            return TreeLock.exclusive(new ReentrantReadWriteLock());
-        }
-    }
-
-    /**
-     * Acquires an exclusive lock on the given parent key. Use this method to
-     * block cache access for child keys of the given parent key.
-     *
-     * @param parentKey the parent key.
-     * @return the acquired lock for the given parent key.
-     */
-    private TreeLock acquireExclusive(String parentKey) {
-        lockAcquisitionCounter.incrementAndGet();
-        return TreeLock.exclusive(parentLocks.get(parentKey));
+    private <T extends Document> T applyChanges(Collection<T> collection, T 
oldDoc, UpdateOp update) {
+        T doc = collection.newDocument(this);
+        oldDoc.deepCopy(doc);
+        UpdateUtils.applyChanges(doc, update);
+        doc.seal();
+        return doc;
     }
 
     @Override
@@ -1438,42 +1222,16 @@ public class MongoDocumentStore implemen
         this.maxLockedQueryTimeMS = maxLockedQueryTimeMS;
     }
 
-    long getLockAcquisitionCount() {
-        return lockAcquisitionCounter.get();
+    void resetLockAcquisitionCount() {
+        nodeLocks.resetLockAcquisitionCount();
     }
 
-    private final static class TreeLock {
-
-        private final Lock parentLock;
-        private final Lock lock;
-
-        private TreeLock(Lock parentLock, Lock lock) {
-            this.parentLock = parentLock;
-            this.lock = lock;
-        }
-
-        static TreeLock shared(ReadWriteLock parentLock, Lock lock) {
-            return new TreeLock(parentLock.readLock(), lock).lock();
-        }
-
-        static TreeLock exclusive(ReadWriteLock parentLock) {
-            return new TreeLock(parentLock.writeLock(), null).lock();
-        }
-
-        private TreeLock lock() {
-            parentLock.lock();
-            if (lock != null) {
-                lock.lock();
-            }
-            return this;
-        }
+    long getLockAcquisitionCount() {
+        return nodeLocks.getLockAcquisitionCount();
+    }
 
-        private void unlock() {
-            if (lock != null) {
-                lock.unlock();
-            }
-            parentLock.unlock();
-        }
+    NodeDocumentCache getNodeDocumentCache() {
+        return nodesCache;
     }
 
     @Override

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=1718528&r1=1718527&r2=1718528&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
 Tue Dec  8 08:28:56 2015
@@ -58,7 +58,6 @@ import javax.annotation.Nonnull;
 import javax.sql.DataSource;
 
 import org.apache.jackrabbit.oak.cache.CacheStats;
-import org.apache.jackrabbit.oak.cache.CacheValue;
 import org.apache.jackrabbit.oak.plugins.document.Collection;
 import org.apache.jackrabbit.oak.plugins.document.Document;
 import org.apache.jackrabbit.oak.plugins.document.DocumentMK;
@@ -71,21 +70,20 @@ import org.apache.jackrabbit.oak.plugins
 import org.apache.jackrabbit.oak.plugins.document.UpdateOp.Operation;
 import org.apache.jackrabbit.oak.plugins.document.UpdateUtils;
 import org.apache.jackrabbit.oak.plugins.document.cache.CacheInvalidationStats;
+import org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache;
+import org.apache.jackrabbit.oak.plugins.document.locks.NodeDocumentLocks;
+import 
org.apache.jackrabbit.oak.plugins.document.locks.StripedNodeDocumentLocks;
 import org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore;
-import org.apache.jackrabbit.oak.plugins.document.util.StringValue;
 import org.apache.jackrabbit.oak.util.OakVersion;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Objects;
-import com.google.common.cache.Cache;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.hash.BloomFilter;
 import com.google.common.hash.Funnel;
 import com.google.common.hash.PrimitiveSink;
-import com.google.common.util.concurrent.Striped;
 
 /**
  * Implementation of {@link DocumentStore} for relational databases.
@@ -292,6 +290,7 @@ public class RDBDocumentStore implements
         }
         return null;
     }
+
     @Override
     public CacheInvalidationStats invalidateCache(Iterable<String> keys) {
         //TODO: optimize me
@@ -303,22 +302,6 @@ public class RDBDocumentStore implements
         invalidateCache(collection, id, false);
     }
 
-    @Override
-    public long determineServerTimeDifferenceMillis() {
-        Connection connection = null;
-        try {
-            connection = this.ch.getROConnection();
-            long result = 
this.db.determineServerTimeDifferenceMillis(connection, 
getTable(Collection.NODES));
-            connection.commit();
-            return result;
-        } catch (SQLException ex) {
-            LOG.error("Trying to determine time difference to server", ex);
-            throw new DocumentStoreException(ex);
-        } finally {
-            this.ch.closeConnection(connection);
-        }
-    }
-
     private <T extends Document> void invalidateCache(Collection<T> 
collection, String id, boolean remove) {
         if (collection == Collection.NODES) {
             invalidateNodesCache(id, remove);
@@ -326,13 +309,12 @@ public class RDBDocumentStore implements
     }
 
     private void invalidateNodesCache(String id, boolean remove) {
-        StringValue key = new StringValue(id);
-        Lock lock = getAndLock(id);
+        Lock lock = locks.acquire(id);
         try {
             if (remove) {
-                nodesCache.invalidate(key);
+                nodesCache.invalidate(id);
             } else {
-                NodeDocument entry = nodesCache.getIfPresent(key);
+                NodeDocument entry = nodesCache.getIfPresent(id);
                 if (entry != null) {
                     entry.markUpToDate(0);
                 }
@@ -342,6 +324,22 @@ public class RDBDocumentStore implements
         }
     }
 
+    @Override
+    public long determineServerTimeDifferenceMillis() {
+        Connection connection = null;
+        try {
+            connection = this.ch.getROConnection();
+            long result = 
this.db.determineServerTimeDifferenceMillis(connection, 
getTable(Collection.NODES));
+            connection.commit();
+            return result;
+        } catch (SQLException ex) {
+            LOG.error("Trying to determine time difference to server", ex);
+            throw new DocumentStoreException(ex);
+        } finally {
+            this.ch.closeConnection(connection);
+        }
+    }
+
     // used for diagnostics
     private String droppedTables = "";
 
@@ -448,14 +446,14 @@ public class RDBDocumentStore implements
         if (collection != Collection.NODES) {
             return null;
         } else {
-            NodeDocument doc = nodesCache.getIfPresent(new StringValue(id));
+            NodeDocument doc = nodesCache.getIfPresent(id);
             return castAsT(doc);
         }
     }
 
     @Override
     public CacheStats getCacheStats() {
-        return this.cacheStats;
+        return nodesCache.getCacheStats();
     }
 
     @Override
@@ -530,8 +528,8 @@ public class RDBDocumentStore implements
         this.ch = new RDBConnectionHandler(ds);
         this.callStack = LOG.isDebugEnabled() ? new Exception("call stack of 
RDBDocumentStore creation") : null;
 
-        this.nodesCache = builder.buildDocumentCache(this);
-        this.cacheStats = new CacheStats(nodesCache, "Document-Documents", 
builder.getWeigher(), builder.getDocumentCacheSize());
+        this.locks = new StripedNodeDocumentLocks();
+        this.nodesCache = builder.buildNodeDocumentCache(this, locks);
 
         Connection con = this.ch.getRWConnection();
 
@@ -831,11 +829,10 @@ public class RDBDocumentStore implements
         if (collection != Collection.NODES) {
             return readDocumentUncached(collection, id, null);
         } else {
-            CacheValue cacheKey = new StringValue(id);
             NodeDocument doc = null;
             if (maxCacheAge > 0) {
                 // first try without lock
-                doc = nodesCache.getIfPresent(cacheKey);
+                doc = nodesCache.getIfPresent(id);
                 if (doc != null) {
                     long lastCheckTime = doc.getLastCheckTime();
                     if (lastCheckTime != 0) {
@@ -846,7 +843,7 @@ public class RDBDocumentStore implements
                 }
             }
             try {
-                Lock lock = getAndLock(id);
+                Lock lock = locks.acquire(id);
                 try {
                     // caller really wants the cache to be cleared
                     if (maxCacheAge == 0) {
@@ -854,7 +851,7 @@ public class RDBDocumentStore implements
                         doc = null;
                     }
                     final NodeDocument cachedDoc = doc;
-                    doc = nodesCache.get(cacheKey, new 
Callable<NodeDocument>() {
+                    doc = nodesCache.get(id, new Callable<NodeDocument>() {
                         @Override
                         public NodeDocument call() throws Exception {
                             NodeDocument doc = (NodeDocument) 
readDocumentUncached(collection, id, cachedDoc);
@@ -878,7 +875,7 @@ public class RDBDocumentStore implements
                             ndoc.seal();
                         }
                         doc = wrap(ndoc);
-                        nodesCache.put(cacheKey, doc);
+                        nodesCache.put(doc);
                     }
                 } finally {
                     lock.unlock();
@@ -910,9 +907,9 @@ public class RDBDocumentStore implements
                     docs.add(doc);
                 }
                 boolean done = insertDocuments(collection, docs);
-                if (done) {
+                if (done && collection == Collection.NODES) {
                     for (T doc : docs) {
-                        addToCache(collection, doc);
+                        nodesCache.putIfAbsent((NodeDocument) doc);
                     }
                 }
                 else {
@@ -944,7 +941,9 @@ public class RDBDocumentStore implements
             UpdateUtils.applyChanges(doc, update);
             try {
                 insertDocuments(collection, Collections.singletonList(doc));
-                addToCache(collection, doc);
+                if (collection == Collection.NODES) {
+                    nodesCache.putIfAbsent((NodeDocument) doc);
+                }
                 return oldDoc;
             } catch (DocumentStoreException ex) {
                 // may have failed due to a race condition; try update instead
@@ -982,7 +981,7 @@ public class RDBDocumentStore implements
             maintainUpdateStats(collection, update.getId());
             addUpdateCounters(update);
             T doc = createNewDocument(collection, oldDoc, update);
-            Lock l = getAndLock(update.getId());
+            Lock l = locks.acquire(update.getId());
             try {
                 boolean success = false;
 
@@ -1016,7 +1015,9 @@ public class RDBDocumentStore implements
                             doc = createNewDocument(collection, oldDoc, 
update);
                         }
                     } else {
-                        updateCache(collection, oldDoc, doc);
+                        if (collection == Collection.NODES) {
+                            nodesCache.replaceCachedDocument((NodeDocument) 
oldDoc, (NodeDocument) doc);
+                        }
                     }
                 }
 
@@ -1066,7 +1067,7 @@ public class RDBDocumentStore implements
                     // remember what we already have in the cache
                     cachedDocs = new HashMap<String, NodeDocument>();
                     for (String key : chunkedIds) {
-                        cachedDocs.put(key, nodesCache.getIfPresent(new 
StringValue(key)));
+                        cachedDocs.put(key, nodesCache.getIfPresent(key));
                     }
 
                     // keep concurrently running queries from updating
@@ -1104,16 +1105,16 @@ public class RDBDocumentStore implements
                     for (Entry<String, NodeDocument> entry : 
cachedDocs.entrySet()) {
                         T oldDoc = castAsT(entry.getValue());
                         String id = entry.getKey();
-                        Lock lock = getAndLock(id);
+                        Lock lock = locks.acquire(id);
                         try {
                             if (oldDoc == null) {
                                 // make sure concurrently loaded document is
                                 // invalidated
-                                nodesCache.invalidate(new StringValue(id));
+                                nodesCache.invalidate(id);
                             } else {
                                 addUpdateCounters(update);
                                 T newDoc = createNewDocument(collection, 
oldDoc, update);
-                                updateCache(collection, oldDoc, newDoc);
+                                
nodesCache.replaceCachedDocument((NodeDocument) oldDoc, (NodeDocument) newDoc);
                             }
                         } finally {
                             lock.unlock();
@@ -1548,16 +1549,9 @@ public class RDBDocumentStore implements
         return (T) doc;
     }
 
-    // Memory Cache
-    private Cache<CacheValue, NodeDocument> nodesCache;
-    private CacheStats cacheStats;
-    private final Striped<Lock> locks = Striped.lock(64);
-
-    private Lock getAndLock(String key) {
-        Lock l = locks.get(key);
-        l.lock();
-        return l;
-    }
+    private NodeDocumentCache nodesCache;
+
+    private NodeDocumentLocks locks;
 
     @CheckForNull
     private static NodeDocument unwrap(@Nonnull NodeDocument doc) {
@@ -1583,100 +1577,6 @@ public class RDBDocumentStore implements
         return n != null ? n.longValue() : -1;
     }
 
-    private <T extends Document> void addToCache(Collection<T> collection, T 
doc) {
-        if (collection == Collection.NODES) {
-            Lock lock = getAndLock(idOf(doc));
-            try {
-                addToCache((NodeDocument) doc);
-            } finally {
-                lock.unlock();
-            }
-        }
-    }
-
-    /**
-     * Applies an update to the nodes cache. This method does not acquire
-     * a lock for the document. The caller must ensure it holds a lock for
-     * the updated document. See striped {@link #locks}.
-     *
-     * @param <T> the document type.
-     * @param collection the document collection.
-     * @param oldDoc the old document.
-     * @param updateOp the update operation.
-     */
-    private <T extends Document> void updateCache(@Nonnull Collection<T> 
collection,
-                                                  @Nonnull T oldDoc,
-                                                  @Nonnull T newDoc) {
-        // cache the new document
-        if (collection == Collection.NODES) {
-            checkNotNull(oldDoc);
-            checkNotNull(newDoc);
-            // we can only update the cache based on the oldDoc if we
-            // still have the oldDoc in the cache, otherwise we may
-            // update the cache with an outdated document
-            CacheValue key = new StringValue(idOf(newDoc));
-            NodeDocument cached = nodesCache.getIfPresent(key);
-            if (cached == null) {
-                // cannot use oldDoc to update cache
-                return;
-            }
-
-            // check if the currently cached document matches oldDoc
-            if (Objects.equal(cached.getModCount(), oldDoc.getModCount())) {
-                nodesCache.put(key, (NodeDocument)newDoc);
-            } else {
-                // the cache entry was modified by some other thread in
-                // the meantime. the updated cache entry may or may not
-                // include this update. we cannot just apply our update
-                // on top of the cached entry.
-                // therefore we must invalidate the cache entry
-                nodesCache.invalidate(key);
-            }
-        }
-    }
-
-    /**
-     * Adds a document to the {@link #nodesCache} iff there is no document
-     * in the cache with the document key. This method does not acquire a lock
-     * from {@link #locks}! The caller must ensure a lock is held for the
-     * given document.
-     *
-     * @param doc the document to add to the cache.
-     * @return either the given <code>doc</code> or the document already 
present
-     *         in the cache.
-     */
-    @Nonnull
-    private NodeDocument addToCache(@Nonnull final NodeDocument doc) {
-        if (doc == NodeDocument.NULL) {
-            throw new IllegalArgumentException("doc must not be NULL 
document");
-        }
-        doc.seal();
-        // make sure we only cache the document if it wasn't
-        // changed and cached by some other thread in the
-        // meantime. That is, use get() with a Callable,
-        // which is only used when the document isn't there
-        try {
-            CacheValue key = new StringValue(idOf(doc));
-            for (;;) {
-                NodeDocument cached = nodesCache.get(key, new 
Callable<NodeDocument>() {
-                    @Override
-                    public NodeDocument call() {
-                        return doc;
-                    }
-                });
-                if (cached != NodeDocument.NULL) {
-                    return cached;
-                } else {
-                    nodesCache.invalidate(key);
-                }
-            }
-        } catch (ExecutionException e) {
-            // will never happen because call() just returns
-            // the already available doc
-            throw new IllegalStateException(e);
-        }
-    }
-
     @Nonnull
     protected <T extends Document> T convertFromDBObject(@Nonnull 
Collection<T> collection, @Nonnull RDBRow row) {
         // this method is present here in order to facilitate unit testing for 
OAK-3566
@@ -1691,8 +1591,7 @@ public class RDBDocumentStore implements
         }
 
         String id = row.getId();
-        CacheValue cacheKey = new StringValue(id);
-        NodeDocument inCache = nodesCache.getIfPresent(cacheKey);
+        NodeDocument inCache = nodesCache.getIfPresent(id);
         Number modCount = row.getModcount();
 
         // do not overwrite document in cache if the
@@ -1717,26 +1616,7 @@ public class RDBDocumentStore implements
             return castAsT(fresh);
         }
 
-        Lock lock = getAndLock(id);
-        try {
-            inCache = nodesCache.getIfPresent(cacheKey);
-            if (inCache != null && inCache != NodeDocument.NULL) {
-                // check mod count
-                Number cachedModCount = inCache.getModCount();
-                if (cachedModCount == null) {
-                    throw new IllegalStateException("Missing " + 
Document.MOD_COUNT);
-                }
-                if (modCount.longValue() > cachedModCount.longValue()) {
-                    nodesCache.put(cacheKey, fresh);
-                } else {
-                    fresh = inCache;
-                }
-            } else {
-                nodesCache.put(cacheKey, fresh);
-            }
-        } finally {
-            lock.unlock();
-        }
+        nodesCache.putIfNewer(fresh);
         return castAsT(fresh);
     }
 
@@ -1785,7 +1665,7 @@ public class RDBDocumentStore implements
         }
     }
 
-    protected Cache<CacheValue, NodeDocument> getNodeDocumentCache() {
+    protected NodeDocumentCache getNodeDocumentCache() {
         return nodesCache;
     }
 }

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyRDBTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyRDBTest.java?rev=1718528&r1=1718527&r2=1718528&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyRDBTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyRDBTest.java
 Tue Dec  8 08:28:56 2015
@@ -33,7 +33,6 @@ import org.apache.jackrabbit.oak.plugins
 import org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore;
 import org.apache.jackrabbit.oak.plugins.document.rdb.RDBOptions;
 import org.apache.jackrabbit.oak.plugins.document.rdb.RDBRow;
-import org.apache.jackrabbit.oak.plugins.document.util.StringValue;
 import org.apache.jackrabbit.oak.plugins.document.util.Utils;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.junit.Before;
@@ -124,7 +123,7 @@ public class CacheConsistencyRDBTest ext
         }
 
         public void invalidateNodeDocument(String key) {
-            getNodeDocumentCache().invalidate(new StringValue(key));
+            getNodeDocumentCache().invalidate(key);
         }
     }
 }

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheConsistencyIT.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheConsistencyIT.java?rev=1718528&r1=1718527&r2=1718528&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheConsistencyIT.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheConsistencyIT.java
 Tue Dec  8 08:28:56 2015
@@ -20,17 +20,15 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
-import com.google.common.cache.Cache;
 import com.google.common.collect.Lists;
 import com.mongodb.DB;
 
-import org.apache.jackrabbit.oak.cache.CacheValue;
 import org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest;
 import org.apache.jackrabbit.oak.plugins.document.DocumentMK;
 import org.apache.jackrabbit.oak.plugins.document.MongoUtils;
 import org.apache.jackrabbit.oak.plugins.document.NodeDocument;
 import org.apache.jackrabbit.oak.plugins.document.UpdateOp;
-import org.apache.jackrabbit.oak.plugins.document.util.StringValue;
+import org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache;
 import org.apache.jackrabbit.oak.plugins.document.util.Utils;
 import org.junit.Before;
 import org.junit.Test;
@@ -145,18 +143,17 @@ public class CacheConsistencyIT extends
         }, "reader");
         t3.start();
 
-        Cache<CacheValue, NodeDocument> cache = store.getNodeDocumentCache();
+        NodeDocumentCache cache = store.getNodeDocumentCache();
 
         // run for at most five seconds
         long end = System.currentTimeMillis() + 1000;
         String id = Utils.getIdFromPath("/test/foo");
-        CacheValue key = new StringValue(id);
         while (t1.isAlive() && t2.isAlive() && t3.isAlive()
                 && System.currentTimeMillis() < end) {
-            if (cache.getIfPresent(key) != null) {
+            if (cache.getIfPresent(id) != null) {
                 Thread.sleep(0, (int) (Math.random() * 100));
                 // simulate eviction
-                cache.invalidate(key);
+                cache.invalidate(id);
             }
         }
         for (Throwable e : exceptions) {

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java?rev=1718528&r1=1718527&r2=1718528&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java
 Tue Dec  8 08:28:56 2015
@@ -19,8 +19,6 @@
 
 package org.apache.jackrabbit.oak.plugins.document.mongo;
 
-import com.google.common.collect.Iterables;
-
 import org.apache.jackrabbit.oak.api.CommitFailedException;
 import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest;
@@ -47,7 +45,6 @@ public class CacheInvalidationIT extends
     private DocumentNodeStore c1;
     private DocumentNodeStore c2;
     private int initialCacheSizeC1;
-    private int initialCacheSizeC2;
 
     @Before
     public void prepareStores() throws Exception {
@@ -56,7 +53,6 @@ public class CacheInvalidationIT extends
         c1 = createNS(2);
         c2 = createNS(3);
         initialCacheSizeC1 = getCurrentCacheSize(c1);
-        initialCacheSizeC2 = getCurrentCacheSize(c2);
     }
 
     private int createScenario() throws CommitFailedException {
@@ -77,7 +73,6 @@ public class CacheInvalidationIT extends
                 "/a/d",
                 "/a/d/h",
         };
-        final int totalPaths = paths.length + 1; // 1 extra for root
         NodeBuilder root = getRoot(c1).builder();
         createTree(root, paths);
         c1.merge(root, EmptyHook.INSTANCE, CommitInfo.EMPTY);
@@ -105,11 +100,9 @@ public class CacheInvalidationIT extends
         //Only 2 entries /a and /a/d would be invalidated
         // '/' would have been added to cache in start of backgroundRead
         //itself
-        assertEquals(initialCacheSizeC1+ totalPaths - 2, 
Iterables.size(ds(c1).getCacheEntries()));
+        assertEquals(initialCacheSizeC1 + totalPaths - 2, 
ds(c1).getNodeDocumentCache().asMap().size());
     }
 
-
-
     @Test
     public void testCacheInvalidationHierarchicalNotExist()
             throws CommitFailedException {
@@ -148,7 +141,7 @@ public class CacheInvalidationIT extends
     }
 
     private int getCurrentCacheSize(DocumentNodeStore ds){
-        return Iterables.size(ds(ds).getCacheEntries());
+        return ds(ds).getNodeDocumentCache().asMap().size();
     }
 
     private static void refreshHead(DocumentNodeStore store) {

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java?rev=1718528&r1=1718527&r2=1718528&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java
 Tue Dec  8 08:28:56 2015
@@ -67,12 +67,12 @@ public class MongoDocumentStoreTest exte
             }
             mk.commit("/", sb.toString(), null, null);
             store.queriesWithoutLock.set(0);
-            long lockCount = store.getLockAcquisitionCount();
+            store.resetLockAcquisitionCount();
             List<NodeDocument> docs = store.query(Collection.NODES, fromId, 
toId,
                     "foo", System.currentTimeMillis(), Integer.MAX_VALUE);
             assertTrue(docs.isEmpty());
             if (store.queriesWithoutLock.get() > 0) {
-                assertEquals(lockCount + 1, store.getLockAcquisitionCount());
+                assertEquals(1, store.getLockAcquisitionCount());
                 return;
             }
         }

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBCacheConsistencyIT.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBCacheConsistencyIT.java?rev=1718528&r1=1718527&r2=1718528&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBCacheConsistencyIT.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBCacheConsistencyIT.java
 Tue Dec  8 08:28:56 2015
@@ -25,18 +25,16 @@ import java.util.Collections;
 import java.util.List;
 import java.util.UUID;
 
-import org.apache.jackrabbit.oak.cache.CacheValue;
 import org.apache.jackrabbit.oak.plugins.document.AbstractRDBConnectionTest;
 import org.apache.jackrabbit.oak.plugins.document.DocumentMK;
 import org.apache.jackrabbit.oak.plugins.document.DocumentStoreException;
 import org.apache.jackrabbit.oak.plugins.document.NodeDocument;
 import org.apache.jackrabbit.oak.plugins.document.UpdateOp;
-import org.apache.jackrabbit.oak.plugins.document.util.StringValue;
+import org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache;
 import org.apache.jackrabbit.oak.plugins.document.util.Utils;
 import org.junit.Before;
 import org.junit.Test;
 
-import com.google.common.cache.Cache;
 import com.google.common.collect.Lists;
 
 /**
@@ -165,19 +163,17 @@ public class RDBCacheConsistencyIT exten
         }, "reader");
         t3.start();
 
-        Cache<CacheValue, NodeDocument> cache = store.getNodeDocumentCache();
+        NodeDocumentCache cache = store.getNodeDocumentCache();
 
         // run for at most five seconds
         long end = System.currentTimeMillis() + 1000;
         String id = Utils.getIdFromPath("/test/foo");
-        CacheValue key = new StringValue(id);
         while (t1.isAlive() && t2.isAlive() && t3.isAlive()
                 && System.currentTimeMillis() < end) {
-            if (cache.getIfPresent(key) != null) {
+            if (cache.getIfPresent(id) != null) {
                 Thread.sleep(0, (int) (Math.random() * 100));
                 // simulate eviction
-                // System.out.println("EVICT");
-                cache.invalidate(key);
+                cache.invalidate(id);
             }
         }
         for (Throwable e : exceptions) {


Reply via email to