package org.apache.solr.core;

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

import java.io.File;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Set;

import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.DirectoryReader;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.util.RefCounted;
import org.junit.AfterClass;
import org.junit.BeforeClass;

public class TestNonNRTOpen extends SolrTestCaseJ4 {
  
  @BeforeClass
  public static void beforeClass() throws Exception {
    // use a filesystem, because we need to create an index, then "start up solr"
    System.setProperty("solr.directoryFactory", "solr.StandardDirectoryFactory");
    // and dont delete it initially
    System.setProperty("solr.test.leavedatadir", "true");
    // turn off nrt
    System.setProperty("solr.tests.nrtMode", "false");
    // set these so that merges won't break the test
    System.setProperty("solr.tests.maxBufferedDocs", "100000");
    System.setProperty("solr.tests.mergePolicy", "org.apache.lucene.index.LogDocMergePolicy");
    initCore("solrconfig-basic.xml", "schema-minimal.xml");
    // add a doc
    assertU(adoc("foo", "bar"));
    assertU(commit());
    File myDir = dataDir;
    deleteCore();
    // boot up again over the same index
    dataDir = myDir;
    initCore("solrconfig-basic.xml", "schema-minimal.xml");
    // startup
    assertNotNRT(1);
  }
  
  public void setUp() throws Exception {
    super.setUp();
    // delete all, then add initial doc
    assertU(delQ("*:*"));
    assertU(adoc("foo", "bar"));
    assertU(commit());
  }
  
  @AfterClass
  public static void afterClass() throws Exception {
    // ensure we clean up after ourselves, this will fire before superclass...
    System.clearProperty("solr.test.leavedatadir");
    System.clearProperty("solr.directoryFactory");
    System.clearProperty("solr.tests.maxBufferedDocs");
    System.clearProperty("solr.tests.mergePolicy");
    System.clearProperty("solr.tests.nrtMode");
  }

  public void testReaderIsNotNRT() {
    // startup
    assertNotNRT(1);
    
    // core reload
    String core = h.getCore().getName();
    h.getCoreContainer().reload(core);
    assertNotNRT(1);
    
    // add a doc and commit
    assertU(adoc("baz", "doc"));
    assertU(commit());
    assertNotNRT(2);
    
    // add a doc and core reload
    assertU(adoc("bazz", "doc2"));
    h.getCoreContainer().reload(core);
    assertNotNRT(3);
  }

  public void testSharedCores() {
    // clear out any junk
    assertU(optimize());
    
    Set<Object> s1 = getCoreCacheKeys();
    assertEquals(1, s1.size());
    
    // add a doc, will go in a new segment
    assertU(adoc("baz", "doc"));
    assertU(commit());
    
    Set<Object> s2 = getCoreCacheKeys();
    assertEquals(2, s2.size());
    assertTrue(s2.containsAll(s1));
    
    // add two docs, will go in a new segment
    assertU(adoc("foo", "doc"));
    assertU(adoc("foo2", "doc"));
    assertU(commit());
    
    Set<Object> s3 = getCoreCacheKeys();
    assertEquals(3, s3.size());
    assertTrue(s3.containsAll(s2));
    
    // delete a doc
    assertU(delQ("foo2:doc"));
    assertU(commit());
    
    // same cores
    assertEquals(s3, getCoreCacheKeys());
  }
  
  static void assertNotNRT(int maxDoc) {
    RefCounted<SolrIndexSearcher> searcher = h.getCore().getSearcher();
    try {
      DirectoryReader ir = searcher.get().getIndexReader();
      assertEquals(maxDoc, ir.maxDoc());
      assertFalse("expected non-NRT reader, got: " + ir, ir.toString().contains(":nrt"));
    } finally {
      searcher.decref();
    }
  }

  private Set<Object> getCoreCacheKeys() {
    RefCounted<SolrIndexSearcher> searcher = h.getCore().getSearcher();
    Set<Object> set = Collections.newSetFromMap(new IdentityHashMap<Object,Boolean>());
    try {
      DirectoryReader ir = searcher.get().getIndexReader();
      for (AtomicReaderContext context : ir.leaves()) {
        set.add(context.reader().getCoreCacheKey());
      }
    } finally {
      searcher.decref();
    }
    return set;
  }
}


@@ -1227,63 +1227,202 @@
 s;%0A%0A
-public class TestNonNRTOpen extends SolrTestCaseJ4 %7B%0A  
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Apublic class TestNonNRTOpen extends SolrTestCaseJ4 %7B%0A  private static final Logger log = LoggerFactory.getLogger(TestNonNRTOpen.class);%0A
 %0A  @
@@ -3107,24 +3107,83 @@
 .getName();%0A
+    log.info(%22Reloading core: %22 + h.getCore().toString());%0A
     h.getCor
@@ -3409,24 +3409,83 @@
 , %22doc2%22));%0A
+    log.info(%22Reloading core: %22 + h.getCore().toString());%0A
     h.getCor
@@ -4361,24 +4361,141 @@
 t maxDoc) %7B%0A
+    SolrCore core = h.getCore();%0A    log.info(%22Checking notNRT & maxDoc=%22 + maxDoc + %22 of core=%22 + core.toString());%0A
     RefCount
@@ -4523,35 +4523,28 @@
  searcher = 
-h.getC
+c
 ore
-()
 .getSearcher
@@ -4563,34 +4563,35 @@
 %7B%0A      
-DirectoryReader ir
+SolrIndexSearcher s
  = searc
@@ -4591,32 +4591,62 @@
 = searcher.get()
+;%0A      DirectoryReader ir = s
 .getIndexReader(
@@ -4667,16 +4667,118 @@
 tEquals(
+%22SOLR-5815? : wrong maxDoc: core=%22 + core.toString() +%22 searcher=%22 + s.toString(),%0A                   
 maxDoc, 
@@ -4810,16 +4810,29 @@
 tFalse(%22
+SOLR-5815? : 
 expected

add some test logging to try and figure out WTF (merge r1574273)