public virtual void  TestFSDirectoryClone()
        {
            System.String tempDir = System.IO.Path.GetTempPath();
            if (tempDir == null)
            {
                throw new System.IO.IOException("java.io.tmpdir undefined, cannot run test");
            }
            System.IO.FileInfo indexDir2 = new System.IO.FileInfo(System.IO.Path.Combine(tempDir, "FSDirIndexReaderClone"));

            Directory dir1 = FSDirectory.GetDirectory(indexDir2);

            TestIndexReaderReopen.CreateIndex(dir1, false);

            IndexReader reader         = IndexReader.Open(indexDir2);
            IndexReader readOnlyReader = (IndexReader)reader.Clone();

            reader.Close();
            readOnlyReader.Close();

            // Make sure we didn't pick up too many incRef's along
            // the way -- this close should be the final close:
            dir1.Close();

            try
            {
                dir1.ListAll();
                Assert.Fail("did not hit AlreadyClosedException");
            }
            catch (AlreadyClosedException ace)
            {
                // expected
            }
        }
        public virtual void  TestCloneWriteableToReadOnly()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, true);
            IndexReader reader         = IndexReader.Open(dir1, false);
            IndexReader readOnlyReader = reader.Clone(true);

            if (!IsReadOnly(readOnlyReader))
            {
                Assert.Fail("reader isn't read only");
            }
            if (DeleteWorked(1, readOnlyReader))
            {
                Assert.Fail("deleting from the original should not have worked");
            }
            // this readonly reader shouldn't have a write lock
            if (readOnlyReader.hasChanges_ForNUnit)
            {
                Assert.Fail("readOnlyReader has a write lock");
            }
            reader.Close();
            readOnlyReader.Close();
            dir1.Close();
        }
        public virtual void  TestNormsRefCounting()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, false);
            IndexReader reader1 = IndexReader.Open(dir1, false);

            IndexReader   reader2C        = (IndexReader)reader1.Clone();
            SegmentReader segmentReader2C = SegmentReader.GetOnlySegmentReader(reader2C);

            segmentReader2C.Norms("field1"); // load the norms for the field
            Norm reader2CNorm = segmentReader2C.norms_ForNUnit["field1"];

            Assert.IsTrue(reader2CNorm.BytesRef().RefCount() == 2, "reader2CNorm.bytesRef()=" + reader2CNorm.BytesRef());



            IndexReader   reader3C        = (IndexReader)reader2C.Clone();
            SegmentReader segmentReader3C = SegmentReader.GetOnlySegmentReader(reader3C);
            Norm          reader3CCNorm   = segmentReader3C.norms_ForNUnit["field1"];

            Assert.AreEqual(3, reader3CCNorm.BytesRef().RefCount());

            // edit a norm and the refcount should be 1
            IndexReader   reader4C        = (IndexReader)reader3C.Clone();
            SegmentReader segmentReader4C = SegmentReader.GetOnlySegmentReader(reader4C);

            Assert.AreEqual(4, reader3CCNorm.BytesRef().RefCount());
            reader4C.SetNorm(5, "field1", 0.33f);

            // generate a cannot update exception in reader1
            Assert.Throws <LockObtainFailedException>(() => reader3C.SetNorm(1, "field1", 0.99f), "did not hit expected exception");

            // norm values should be different
            Assert.IsTrue(Similarity.DecodeNorm(segmentReader3C.Norms("field1")[5]) != Similarity.DecodeNorm(segmentReader4C.Norms("field1")[5]));
            Norm reader4CCNorm = segmentReader4C.norms_ForNUnit["field1"];

            Assert.AreEqual(3, reader3CCNorm.BytesRef().RefCount());
            Assert.AreEqual(1, reader4CCNorm.BytesRef().RefCount());

            IndexReader   reader5C        = (IndexReader)reader4C.Clone();
            SegmentReader segmentReader5C = SegmentReader.GetOnlySegmentReader(reader5C);
            Norm          reader5CCNorm   = segmentReader5C.norms_ForNUnit["field1"];

            reader5C.SetNorm(5, "field1", 0.7f);
            Assert.AreEqual(1, reader5CCNorm.BytesRef().RefCount());

            reader5C.Close();
            reader4C.Close();
            reader3C.Close();
            reader2C.Close();
            reader1.Close();
            dir1.Close();
        }
Beispiel #4
0
        public virtual void  TestCloneReadOnlyDirectoryReader()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, true);
            IndexReader reader         = IndexReader.Open(dir1, false);
            IndexReader readOnlyReader = reader.Clone(true);

            Assert.IsTrue(IsReadOnly(readOnlyReader), "reader isn't read only");

            reader.Close();
            readOnlyReader.Close();
            dir1.Close();
        }
Beispiel #5
0
        public virtual void  TestReadOnlyCloneAfterOptimize()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, true);
            IndexReader reader1 = IndexReader.Open(dir1, false);
            IndexWriter w       = new IndexWriter(dir1, new SimpleAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);

            w.Optimize();
            w.Close();
            IndexReader reader2 = reader1.Clone(true);

            Assert.IsTrue(IsReadOnly(reader2));
            reader1.Close();
            reader2.Close();
            dir1.Close();
        }
Beispiel #6
0
        public virtual void  TestReopenSegmentReaderToMultiReader()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, false);
            IndexReader reader1 = IndexReader.Open(dir1, false);

            TestIndexReaderReopen.ModifyIndex(5, dir1);

            IndexReader reader2 = reader1.Reopen();

            Assert.IsTrue(reader1 != reader2);

            Assert.IsTrue(DeleteWorked(1, reader2));
            reader1.Close();
            reader2.Close();
            dir1.Close();
        }
        public virtual void  TestCloneWriteableToReadOnly()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, true);
            IndexReader reader         = IndexReader.Open(dir1, false, null);
            IndexReader readOnlyReader = reader.Clone(true, null);

            Assert.IsTrue(IsReadOnly(readOnlyReader), "reader isn't read only");
            Assert.IsFalse(DeleteWorked(1, readOnlyReader), "deleting from the original should not have worked");

            // this readonly reader shouldn't have a write lock
            Assert.IsFalse(readOnlyReader.hasChanges, "readOnlyReader has a write lock");

            reader.Close();
            readOnlyReader.Close();
            dir1.Close();
        }
Beispiel #8
0
        public virtual void  TestCloneReadOnlyToWriteable()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, true);
            IndexReader reader1 = IndexReader.Open(dir1, true);

            IndexReader reader2 = reader1.Clone(false);

            Assert.IsFalse(IsReadOnly(reader2), "reader should not be read only");

            Assert.IsFalse(DeleteWorked(1, reader1), "deleting from the original reader should not have worked");
            // this readonly reader shouldn't yet have a write lock
            Assert.IsFalse(reader2.hasChanges, "cloned reader should not have write lock");

            Assert.IsTrue(DeleteWorked(1, reader2), "deleting from the cloned reader should have worked");
            reader1.Close();
            reader2.Close();
            dir1.Close();
        }
Beispiel #9
0
        public virtual void  TestParallelReader()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, true);
            Directory dir2 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir2, true);
            IndexReader r1 = IndexReader.Open(dir1, false);
            IndexReader r2 = IndexReader.Open(dir2, false);

            ParallelReader pr1 = new ParallelReader();

            pr1.Add(r1);
            pr1.Add(r2);

            PerformDefaultTests(pr1);
            pr1.Close();
            dir1.Close();
            dir2.Close();
        }
Beispiel #10
0
        public virtual void  TestReopenWriteableToReadOnly()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, true);
            IndexReader reader   = IndexReader.Open(dir1, false);
            int         docCount = reader.NumDocs();

            Assert.IsTrue(DeleteWorked(1, reader));
            Assert.AreEqual(docCount - 1, reader.NumDocs());

            IndexReader readOnlyReader = reader.Reopen(true);

            Assert.IsTrue(IsReadOnly(readOnlyReader), "reader isn't read only");

            Assert.IsFalse(DeleteWorked(1, readOnlyReader));
            Assert.AreEqual(docCount - 1, readOnlyReader.NumDocs());
            reader.Close();
            readOnlyReader.Close();
            dir1.Close();
        }