/*
 * 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;

import java.util.List;

import com.google.common.collect.Lists;

import org.apache.jackrabbit.oak.plugins.document.util.Utils;
import org.junit.Before;
import org.junit.Test;

import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES;
import static org.apache.jackrabbit.oak.plugins.document.util.Utils.getIdFromPath;
import static org.apache.jackrabbit.oak.plugins.document.util.Utils.getKeyLowerLimit;
import static org.apache.jackrabbit.oak.plugins.document.util.Utils.getKeyUpperLimit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

public class ConcurrentQueryAndInvalidateIT extends AbstractDocumentStoreTest {

    public ConcurrentQueryAndInvalidateIT(DocumentStoreFixture dsf) {
        super(dsf);
        assumeTrue(dsf.hasSinglePersistence());
        assumeFalse(dsf instanceof DocumentStoreFixture.RDBFixture);
    }

    protected static final int NUM_NODES = 50;

    private volatile long counter;

    private DocumentStore ds2;

    @Before
    public void setupSecondDocumentStore() {
        ds2 = dsf.createDocumentStore(2);
    }

    @Test
    public void cacheUpdate() throws Exception {
        Revision r = newRevision();
        List<UpdateOp> ops = Lists.newArrayList();
        List<String> ids = Lists.newArrayList();
        for (int i = 0; i < NUM_NODES; i++) {
            String id = Utils.getIdFromPath("/node-" + i);
            ids.add(id);
            UpdateOp op = new UpdateOp(id, true);
            op.set(Document.ID, id);
            NodeDocument.setLastRev(op, r);
            ops.add(op);
            removeMe.add(id);
        }
        ds.remove(NODES, ids);
        ds.create(NODES, ops);

        for (int i = 0; i < 100; i++) {
            Thread q = new Thread(new Runnable() {
                @Override
                public void run() {
                    // query docs on ds1
                    queryDocuments();
                }
            });
            Thread u = new Thread(new Runnable() {
                @Override
                public void run() {
                    // update docs on ds2
                    Iterable<String> ids = updateDocuments();
                    // invalidate docs on ds1
                    invalidateDocuments(ids);
                }
            });
            q.start();
            u.start();
            q.join();
            u.join();
            for (int j = 0; j < NUM_NODES; j++) {
                NodeDocument doc = ds.find(NODES, Utils.getIdFromPath("/node-" + j));
                assertNotNull(doc);
                assertEquals("Unexpected revision timestamp for " + doc.getId(),
                        counter, doc.getLastRev().get(1).getTimestamp());
            }
        }
    }

    private Revision newRevision() {
        return new Revision(++counter, 0, 1);
    }

    private void queryDocuments() {
        ds.query(NODES, getKeyLowerLimit("/"), getKeyUpperLimit("/"), 100);
    }

    private void invalidateDocuments(Iterable<String> ids) {
        ds.invalidateCache(ids);
    }

    private Iterable<String> updateDocuments() {
        List<String> ids = Lists.newArrayList();
        for (int i = 0; i < NUM_NODES; i++) {
            ids.add(getIdFromPath("/node-" + i));
        }
        UpdateOp op = new UpdateOp("foo", false);
        NodeDocument.setLastRev(op, newRevision());
        ds2.update(NODES, ids, op);
        return ids;
    }
}

@@ -979,33 +979,8 @@
 ls;%0A
-import org.junit.Before;%0A
 impo
@@ -1467,51 +1467,8 @@
 lse;
-%0Aimport static org.junit.Assume.assumeTrue;
 %0A%0Apu
@@ -1525,16 +1525,21 @@
 Abstract
+Multi
 Document
@@ -1645,56 +1645,8 @@
 f);%0A
-        assumeTrue(dsf.hasSinglePersistence());%0A
     
@@ -1809,180 +1809,260 @@
     
-private DocumentStore ds2;%0A%0A    @Before%0A    public void setupSecondDocumentStore() %7B%0A        ds2 = dsf.createDocumentStore(2);%0A    %7D%0A%0A    @Test%0A    public void cacheUpdate(
+@Test%0A    public void cacheUpdate() throws Exception %7B%0A        cacheUpdate(false);%0A    %7D%0A%0A    @Test%0A    public void cacheUpdateInvalidateAll() throws Exception %7B%0A        cacheUpdate(true);%0A    %7D%0A%0A    private void cacheUpdate(final boolean invalidateAll
 ) th
@@ -2550,16 +2550,17 @@
       ds
+1
 .remove(
@@ -2582,16 +2582,17 @@
       ds
+1
 .create(
@@ -2633,17 +2633,17 @@
  0; i %3C 
-1
+2
 00; i++)
@@ -3154,16 +3154,20 @@
         
+if (
 invalida
@@ -3172,23 +3172,162 @@
 date
-Documents(ids);
+All) %7B%0A                        ds1.invalidateCache();%0A                    %7D else %7B%0A                        ds1.invalidateCache(ids);%0A                    %7D
 %0A   
@@ -3534,16 +3534,17 @@
 doc = ds
+1
 .find(NO
@@ -3942,16 +3942,17 @@
       ds
+1
 .query(N
@@ -4020,109 +4020,8 @@
  %7D%0A%0A
-    private void invalidateDocuments(Iterable%3CString%3E ids) %7B%0A        ds.invalidateCache(ids);%0A    %7D%0A%0A
     

Improve test coverage on DocumentStore for concurrent query and invalidate