public virtual void TestRefCounts2()
        {
            Directory       dir1 = GetDir1(Random());
            Directory       dir2 = GetDir2(Random());
            DirectoryReader ir1  = DirectoryReader.Open(dir1);
            DirectoryReader ir2  = DirectoryReader.Open(dir2);

            // don't close subreaders, so ParallelReader will increment refcounts
            ParallelCompositeReader pr    = new ParallelCompositeReader(false, ir1, ir2);
            IndexReader             psub1 = pr.GetSequentialSubReaders()[0];

            // check RefCounts
            Assert.AreEqual(2, ir1.RefCount);
            Assert.AreEqual(2, ir2.RefCount);
            Assert.AreEqual(1, psub1.RefCount, "refCount must be 1, as the synthetic reader was created by ParallelCompositeReader");
            pr.Dispose();
            Assert.AreEqual(1, ir1.RefCount);
            Assert.AreEqual(1, ir2.RefCount);
            Assert.AreEqual(0, psub1.RefCount, "refcount must be 0 because parent was closed");
            ir1.Dispose();
            ir2.Dispose();
            Assert.AreEqual(0, ir1.RefCount);
            Assert.AreEqual(0, ir2.RefCount);
            Assert.AreEqual(0, psub1.RefCount, "refcount should not change anymore");
            dir1.Dispose();
            dir2.Dispose();
        }
        public virtual void TestToStringCompositeComposite()
        {
            Directory               dir1 = GetDir1(Random());
            CompositeReader         ir1  = DirectoryReader.Open(dir1);
            ParallelCompositeReader pr   = new ParallelCompositeReader(new CompositeReader[] { new MultiReader(ir1) });

            string s = pr.ToString();

            Assert.IsTrue(s.StartsWith("ParallelCompositeReader(ParallelCompositeReaderAnonymousInnerClassHelper(ParallelAtomicReader("), "toString incorrect: " + s);

            pr.Dispose();
            dir1.Dispose();
        }
        public virtual void TestToString()
        {
            Directory               dir1 = GetDir1(Random);
            CompositeReader         ir1  = DirectoryReader.Open(dir1);
            ParallelCompositeReader pr   = new ParallelCompositeReader(new CompositeReader[] { ir1 });

            string s = pr.ToString();

            Assert.IsTrue(s.StartsWith("ParallelCompositeReader(ParallelAtomicReader(", StringComparison.Ordinal), "toString incorrect: " + s);

            pr.Dispose();
            dir1.Dispose();
        }
        public virtual void TestCloseInnerReader()
        {
            Directory       dir1 = GetDir1(Random);
            CompositeReader ir1  = DirectoryReader.Open(dir1);

            Assert.AreEqual(1, ir1.GetSequentialSubReaders()[0].RefCount);

            // with overlapping
            ParallelCompositeReader pr = new ParallelCompositeReader(true, new CompositeReader[] { ir1 }, new CompositeReader[] { ir1 });

            IndexReader psub = pr.GetSequentialSubReaders()[0];

            Assert.AreEqual(1, psub.RefCount);

            ir1.Dispose();

            Assert.AreEqual(1, psub.RefCount, "refCount of synthetic subreader should be unchanged");
            try
            {
                psub.Document(0);
                Assert.Fail("Subreader should be already closed because inner reader was closed!");
            }
#pragma warning disable 168
            catch (ObjectDisposedException e)
#pragma warning restore 168
            {
                // pass
            }

            try
            {
                pr.Document(0);
                Assert.Fail("ParallelCompositeReader should be already closed because inner reader was closed!");
            }
#pragma warning disable 168
            catch (ObjectDisposedException e)
#pragma warning restore 168
            {
                // pass
            }

            // noop:
            pr.Dispose();
            Assert.AreEqual(0, psub.RefCount);
            dir1.Dispose();
        }
        public virtual void TestReaderClosedListener2()
        {
            Directory       dir1 = GetDir1(Random());
            CompositeReader ir1  = DirectoryReader.Open(dir1);

            // with overlapping
            ParallelCompositeReader pr = new ParallelCompositeReader(true, new CompositeReader[] { ir1 }, new CompositeReader[] { ir1 });

            int[] listenerClosedCount = new int[1];

            Assert.AreEqual(3, pr.Leaves.Count);

            foreach (AtomicReaderContext cxt in pr.Leaves)
            {
                cxt.Reader.AddReaderClosedListener(new ReaderClosedListenerAnonymousInnerClassHelper2(this, listenerClosedCount));
            }
            pr.Dispose();
            Assert.AreEqual(3, listenerClosedCount[0]);
            dir1.Dispose();
        }
        public virtual void TestRefCounts1()
        {
            Directory       dir1 = GetDir1(Random());
            Directory       dir2 = GetDir2(Random());
            DirectoryReader ir1, ir2;
            // close subreaders, ParallelReader will not change refCounts, but close on its own close
            ParallelCompositeReader pr    = new ParallelCompositeReader(ir1 = DirectoryReader.Open(dir1), ir2 = DirectoryReader.Open(dir2));
            IndexReader             psub1 = pr.GetSequentialSubReaders()[0];

            // check RefCounts
            Assert.AreEqual(1, ir1.RefCount);
            Assert.AreEqual(1, ir2.RefCount);
            Assert.AreEqual(1, psub1.RefCount);
            pr.Dispose();
            Assert.AreEqual(0, ir1.RefCount);
            Assert.AreEqual(0, ir2.RefCount);
            Assert.AreEqual(0, psub1.RefCount);
            dir1.Dispose();
            dir2.Dispose();
        }
        public virtual void TestEmptyIndex()
        {
            Directory   rd1 = NewDirectory();
            IndexWriter iw  = new IndexWriter(rd1, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)));

            iw.Dispose();
            // create a copy:
            Directory rd2 = NewDirectory(rd1);

            Directory rdOut = NewDirectory();

            IndexWriter iwOut = new IndexWriter(rdOut, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)));

            ParallelAtomicReader apr = new ParallelAtomicReader(SlowCompositeReaderWrapper.Wrap(DirectoryReader.Open(rd1)), SlowCompositeReaderWrapper.Wrap(DirectoryReader.Open(rd2)));

            // When unpatched, Lucene crashes here with a NoSuchElementException (caused by ParallelTermEnum)
            iwOut.AddIndexes(apr);
            iwOut.ForceMerge(1);

            // 2nd try with a readerless parallel reader
            iwOut.AddIndexes(new ParallelAtomicReader());
            iwOut.ForceMerge(1);

            ParallelCompositeReader cpr = new ParallelCompositeReader(DirectoryReader.Open(rd1), DirectoryReader.Open(rd2));

            // When unpatched, Lucene crashes here with a NoSuchElementException (caused by ParallelTermEnum)
            iwOut.AddIndexes(cpr);
            iwOut.ForceMerge(1);

            // 2nd try with a readerless parallel reader
            iwOut.AddIndexes(new ParallelCompositeReader());
            iwOut.ForceMerge(1);

            iwOut.Dispose();
            rdOut.Dispose();
            rd1.Dispose();
            rd2.Dispose();
        }
        // Fields 1 & 2 in one index, 3 & 4 in other, with ParallelReader:
        private IndexSearcher Parallel(Random random, bool compositeComposite)
        {
            Dir1 = GetDir1(random);
            Dir2 = GetDir2(random);
            CompositeReader rd1, rd2;

            if (compositeComposite)
            {
                rd1 = new MultiReader(DirectoryReader.Open(Dir1), DirectoryReader.Open(Dir1));
                rd2 = new MultiReader(DirectoryReader.Open(Dir2), DirectoryReader.Open(Dir2));
                Assert.AreEqual(2, rd1.Context.Children.Count);
                Assert.AreEqual(2, rd2.Context.Children.Count);
            }
            else
            {
                rd1 = DirectoryReader.Open(Dir1);
                rd2 = DirectoryReader.Open(Dir2);
                Assert.AreEqual(3, rd1.Context.Children.Count);
                Assert.AreEqual(3, rd2.Context.Children.Count);
            }
            ParallelCompositeReader pr = new ParallelCompositeReader(rd1, rd2);

            return(NewSearcher(pr));
        }
        public virtual void TestIgnoreStoredFields()
        {
            Directory       dir1 = GetDir1(Random());
            Directory       dir2 = GetDir2(Random());
            CompositeReader ir1  = DirectoryReader.Open(dir1);
            CompositeReader ir2  = DirectoryReader.Open(dir2);

            // with overlapping
            ParallelCompositeReader pr = new ParallelCompositeReader(false, new CompositeReader[] { ir1, ir2 }, new CompositeReader[] { ir1 });

            Assert.AreEqual("v1", pr.Document(0).Get("f1"));
            Assert.AreEqual("v1", pr.Document(0).Get("f2"));
            Assert.IsNull(pr.Document(0).Get("f3"));
            Assert.IsNull(pr.Document(0).Get("f4"));
            // check that fields are there
            AtomicReader slow = SlowCompositeReaderWrapper.Wrap(pr);

            Assert.IsNotNull(slow.Terms("f1"));
            Assert.IsNotNull(slow.Terms("f2"));
            Assert.IsNotNull(slow.Terms("f3"));
            Assert.IsNotNull(slow.Terms("f4"));
            pr.Dispose();

            // no stored fields at all
            pr = new ParallelCompositeReader(false, new CompositeReader[] { ir2 }, new CompositeReader[0]);
            Assert.IsNull(pr.Document(0).Get("f1"));
            Assert.IsNull(pr.Document(0).Get("f2"));
            Assert.IsNull(pr.Document(0).Get("f3"));
            Assert.IsNull(pr.Document(0).Get("f4"));
            // check that fields are there
            slow = SlowCompositeReaderWrapper.Wrap(pr);
            Assert.IsNull(slow.Terms("f1"));
            Assert.IsNull(slow.Terms("f2"));
            Assert.IsNotNull(slow.Terms("f3"));
            Assert.IsNotNull(slow.Terms("f4"));
            pr.Dispose();

            // without overlapping
            pr = new ParallelCompositeReader(true, new CompositeReader[] { ir2 }, new CompositeReader[] { ir1 });
            Assert.AreEqual("v1", pr.Document(0).Get("f1"));
            Assert.AreEqual("v1", pr.Document(0).Get("f2"));
            Assert.IsNull(pr.Document(0).Get("f3"));
            Assert.IsNull(pr.Document(0).Get("f4"));
            // check that fields are there
            slow = SlowCompositeReaderWrapper.Wrap(pr);
            Assert.IsNull(slow.Terms("f1"));
            Assert.IsNull(slow.Terms("f2"));
            Assert.IsNotNull(slow.Terms("f3"));
            Assert.IsNotNull(slow.Terms("f4"));
            pr.Dispose();

            // no main readers
            try
            {
                new ParallelCompositeReader(true, new CompositeReader[0], new CompositeReader[] { ir1 });
                Assert.Fail("didn't get expected exception: need a non-empty main-reader array");
            }
            catch (System.ArgumentException iae)
            {
                // pass
            }

            dir1.Dispose();
            dir2.Dispose();
        }