assertU(adoc("id","1", "v_t","Hello Dude", "v_s","string1"));
assertU(adoc("id","2", "v_t","Hello Yonik", "v_s","string2"));
assertU(commit());
SolrQueryRequest sr1 = req("q","foo");
SolrIndexReader r1 = sr1.getSearcher().getReader();
String sval1 = getStringVal(sr1, "v_s",0);
assertEquals("string1", sval1);
assertU(adoc("id","3", "v_s","{!literal}"));
assertU(adoc("id","4", "v_s","other stuff"));
assertU(commit());
SolrQueryRequest sr2 = req("q","foo");
SolrIndexReader r2 = sr2.getSearcher().getReader();
// make sure the readers share the first segment
// Didn't work w/ older versions of lucene2.9 going from segment -> multi
assertEquals(r1.getLeafReaders()[0], r2.getLeafReaders()[0]);
// make sure the String returned is the exact same instance (i.e. same FieldCache instance)
assertTrue(sval1 == getStringVal(sr2,"v_s",0));
assertU(adoc("id","5", "v_f","3.14159"));
assertU(adoc("id","6", "v_f","8983", "v_s","string6"));
assertU(commit());
SolrQueryRequest sr3 = req("q","foo");
SolrIndexReader r3 = sr3.getSearcher().getReader();
// make sure the readers share segments
// assertEquals(r1.getLeafReaders()[0], r3.getLeafReaders()[0]);
assertEquals(r2.getLeafReaders()[0], r3.getLeafReaders()[0]);
assertEquals(r2.getLeafReaders()[1], r3.getLeafReaders()[1]);
sr1.close();
sr2.close();
// should currently be 1, but this could change depending on future index management
int baseRefCount = r3.getRefCount();
assertEquals(1, baseRefCount);
assertU(commit());
SolrQueryRequest sr4 = req("q","foo");
SolrIndexReader r4 = sr4.getSearcher().getReader();
// force an index change so the registered searcher won't be the one we are testing (and
// then we should be able to test the refCount going all the way to 0
assertU(adoc("id","7", "v_f","7574"));
assertU(commit());
// test that reader didn't change (according to equals at least... which uses the wrapped reader)
assertEquals(r3,r4);
assertEquals(baseRefCount+1, r4.getRefCount());
sr3.close();
assertEquals(baseRefCount, r4.getRefCount());
sr4.close();
assertEquals(baseRefCount-1, r4.getRefCount());
SolrQueryRequest sr5 = req("q","foo");
SolrIndexReader r5 = sr5.getSearcher().getReader();
String beforeDelete = getStringVal(sr5, "v_s",1);
assertU(delI("1"));
assertU(commit());
SolrQueryRequest sr6 = req("q","foo");
SolrIndexReader r6 = sr4.getSearcher().getReader();
assertEquals(1, r6.getLeafReaders()[0].numDocs()); // only a single doc left in the first segment
assertTrue( !r5.getLeafReaders()[0].equals(r6.getLeafReaders()[0]) ); // readers now different
String afterDelete = getStringVal(sr6, "v_s",1);
assertTrue( beforeDelete == afterDelete ); // same field cache is used even though deletions are different
sr5.close();
sr6.close();
}