Clone() public method

Clones the IndexReader and optionally changes readOnly. A readOnly reader cannot open a writeable reader.
public Clone ( bool openReadOnly ) : IndexReader
openReadOnly bool
return IndexReader
Example #1
0
        public virtual void  TestCloneWithSetNorm()
        {
            Directory dir1 = new MockRAMDirectory();

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

            orig.SetNorm(1, "field1", 17.0f);
            byte encoded = Similarity.EncodeNorm(17.0f);

            Assert.AreEqual(encoded, orig.Norms("field1")[1]);

            // the cloned segmentreader should have 2 references, 1 to itself, and 1 to
            // the original segmentreader
            IndexReader clonedReader = (IndexReader)orig.Clone();

            orig.Close();
            clonedReader.Close();

            IndexReader r = IndexReader.Open(dir1, false);

            Assert.AreEqual(encoded, r.Norms("field1")[1]);
            r.Close();
            dir1.Close();
        }
        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
            }
        }
        /// <summary> 1. Get a norm from the original reader 2. Clone the original reader 3.
        /// Delete a document and set the norm of the cloned reader 4. Verify the norms
        /// are not the same on each reader 5. Verify the doc deleted is only in the
        /// cloned reader 6. Try to delete a document in the original reader, an
        /// exception should be thrown
        ///
        /// </summary>
        /// <param name="r1">IndexReader to perform tests on
        /// </param>
        /// <throws>  Exception </throws>
        private void  PerformDefaultTests(IndexReader r1)
        {
            float norm1 = Similarity.DecodeNorm(r1.Norms("field1")[4]);

            IndexReader pr1Clone = (IndexReader)r1.Clone();

            pr1Clone.DeleteDocument(10);
            pr1Clone.SetNorm(4, "field1", 0.5f);
            Assert.IsTrue(Similarity.DecodeNorm(r1.Norms("field1")[4]) == norm1);
            Assert.IsTrue(Similarity.DecodeNorm(pr1Clone.Norms("field1")[4]) != norm1);

            Assert.IsTrue(!r1.IsDeleted(10));
            Assert.IsTrue(pr1Clone.IsDeleted(10));

            // try to update the original reader, which should throw an exception
            try
            {
                r1.DeleteDocument(11);
                Assert.Fail("Tried to delete doc 11 and an exception should have been thrown");
            }
            catch (System.Exception exception)
            {
                // expectted
            }
            pr1Clone.Close();
        }
        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();
        }
Example #6
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();
        }
Example #7
0
        public virtual void  TestCloneNoChangesStillReadOnly()
        {
            Directory dir1 = new MockRAMDirectory();

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

            Assert.IsTrue(DeleteWorked(1, r2), "deleting from the cloned should have worked");

            r1.Close();
            r2.Close();
            dir1.Close();
        }
Example #8
0
        public virtual void  TestCloneWriteToOrig()
        {
            Directory dir1 = new MockRAMDirectory();

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

            Assert.IsTrue(DeleteWorked(1, r1), "deleting from the original should have worked");

            r1.Close();
            r2.Close();
            dir1.Close();
        }
Example #9
0
        public virtual void  TestCloneReadOnlySegmentReader()
        {
            Directory dir1 = new MockRAMDirectory();

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

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

            reader.Close();
            readOnlyReader.Close();
            dir1.Close();
        }
Example #10
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();
        }
Example #11
0
        public virtual void  TestCloseStoredFields()
        {
            Directory   dir = new MockRAMDirectory();
            IndexWriter w   = new IndexWriter(dir, new SimpleAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);

            w.UseCompoundFile = false;
            Document doc = new Document();

            doc.Add(new Field("field", "yes it's stored", Field.Store.YES, Field.Index.ANALYZED));
            w.AddDocument(doc);
            w.Close();
            IndexReader r1 = IndexReader.Open(dir, false);
            IndexReader r2 = r1.Clone(false);

            r1.Close();
            r2.Close();
            dir.Close();
        }
Example #12
0
        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();
        }
Example #13
0
        /// <summary> 1. Get a norm from the original reader 2. Clone the original reader 3.
        /// Delete a document and set the norm of the cloned reader 4. Verify the norms
        /// are not the same on each reader 5. Verify the doc deleted is only in the
        /// cloned reader 6. Try to delete a document in the original reader, an
        /// exception should be thrown
        ///
        /// </summary>
        /// <param name="r1">IndexReader to perform tests on
        /// </param>
        /// <throws>  Exception </throws>
        private void  PerformDefaultTests(IndexReader r1)
        {
            float norm1 = Similarity.DecodeNorm(r1.Norms("field1")[4]);

            IndexReader pr1Clone = (IndexReader)r1.Clone();

            pr1Clone.DeleteDocument(10);
            pr1Clone.SetNorm(4, "field1", 0.5f);
            Assert.IsTrue(Similarity.DecodeNorm(r1.Norms("field1")[4]) == norm1);
            Assert.IsTrue(Similarity.DecodeNorm(pr1Clone.Norms("field1")[4]) != norm1);

            Assert.IsTrue(!r1.IsDeleted(10));
            Assert.IsTrue(pr1Clone.IsDeleted(10));

            // try to update the original reader, which should throw an exception
            Assert.Throws <LockObtainFailedException>(() => r1.DeleteDocument(11),
                                                      "Tried to delete doc 11 and an exception should have been thrown");
            pr1Clone.Close();
        }
Example #14
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();
        }
Example #15
0
        public virtual void  TestCloneWriteToClone()
        {
            Directory dir1 = new MockRAMDirectory();

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

            Assert.IsTrue(DeleteWorked(1, r2), "deleting from the original should have worked");

            // should fail because reader1 holds the write lock
            Assert.IsTrue(!DeleteWorked(1, r1), "first reader should not be able to delete");
            r2.Close();
            // should fail because we are now stale (reader1
            // committed changes)
            Assert.IsTrue(!DeleteWorked(1, r1), "first reader should not be able to delete");
            r1.Close();

            dir1.Close();
        }
Example #16
0
        public virtual void  TestLucene1516Bug()
        {
            Directory dir1 = new MockRAMDirectory();

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

            r1.IncRef();
            IndexReader r2 = r1.Clone(false);

            r1.DeleteDocument(5);
            r1.DecRef();

            r1.IncRef();

            r2.Close();
            r1.DecRef();
            r1.Close();
            dir1.Close();
        }
Example #17
0
        public virtual void  TestCloneWithDeletes()
        {
            Directory dir1 = new MockRAMDirectory();

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

            origReader.DeleteDocument(1);

            IndexReader clonedReader = (IndexReader)origReader.Clone();

            origReader.Close();
            clonedReader.Close();

            IndexReader r = IndexReader.Open(dir1, false);

            Assert.IsTrue(r.IsDeleted(1));
            r.Close();
            dir1.Close();
        }
        // try cloning and reopening the norms
        private void  DoTestNorms(Directory dir)
        {
            AddDocs(dir, 12, true);
            IndexReader ir = IndexReader.Open(dir);

            VerifyIndex(ir);
            ModifyNormsForF1(ir);
            IndexReader irc = (IndexReader)ir.Clone();              // IndexReader.open(dir);//ir.clone();

            VerifyIndex(irc);

            ModifyNormsForF1(irc);

            IndexReader irc3 = (IndexReader)irc.Clone();

            VerifyIndex(irc3);
            ModifyNormsForF1(irc3);
            VerifyIndex(irc3);
            irc3.Flush();
            irc3.Close();
        }
		/// <summary> 1. Get a norm from the original reader 2. Clone the original reader 3.
		/// Delete a document and set the norm of the cloned reader 4. Verify the norms
		/// are not the same on each reader 5. Verify the doc deleted is only in the
		/// cloned reader 6. Try to delete a document in the original reader, an
		/// exception should be thrown
		/// 
		/// </summary>
		/// <param name="r1">IndexReader to perform tests on
		/// </param>
		/// <throws>  Exception </throws>
		private void  PerformDefaultTests(IndexReader r1)
		{
			float norm1 = Similarity.DecodeNorm(r1.Norms("field1")[4]);
			
			IndexReader pr1Clone = (IndexReader) r1.Clone();
			pr1Clone.DeleteDocument(10);
			pr1Clone.SetNorm(4, "field1", 0.5f);
			Assert.IsTrue(Similarity.DecodeNorm(r1.Norms("field1")[4]) == norm1);
			Assert.IsTrue(Similarity.DecodeNorm(pr1Clone.Norms("field1")[4]) != norm1);
			
			Assert.IsTrue(!r1.IsDeleted(10));
			Assert.IsTrue(pr1Clone.IsDeleted(10));
			
			// try to update the original reader, which should throw an exception
            Assert.Throws<LockObtainFailedException>(() => r1.DeleteDocument(11),
		                             "Tried to delete doc 11 and an exception should have been thrown");
			pr1Clone.Close();
		}
Example #20
0
		/// <summary> 1. Get a norm from the original reader 2. Clone the original reader 3.
		/// Delete a document and set the norm of the cloned reader 4. Verify the norms
		/// are not the same on each reader 5. Verify the doc deleted is only in the
		/// cloned reader 6. Try to delete a document in the original reader, an
		/// exception should be thrown
		/// 
		/// </summary>
		/// <param name="r1">IndexReader to perform tests on
		/// </param>
		/// <throws>  Exception </throws>
		private void  PerformDefaultTests(IndexReader r1)
		{
			float norm1 = Similarity.DecodeNorm(r1.Norms("field1")[4]);
			
			IndexReader pr1Clone = (IndexReader) r1.Clone();
			pr1Clone.DeleteDocument(10);
			pr1Clone.SetNorm(4, "field1", 0.5f);
			Assert.IsTrue(Similarity.DecodeNorm(r1.Norms("field1")[4]) == norm1);
			Assert.IsTrue(Similarity.DecodeNorm(pr1Clone.Norms("field1")[4]) != norm1);
			
			Assert.IsTrue(!r1.IsDeleted(10));
			Assert.IsTrue(pr1Clone.IsDeleted(10));
			
			// try to update the original reader, which should throw an exception
			try
			{
				r1.DeleteDocument(11);
				Assert.Fail("Tried to delete doc 11 and an exception should have been thrown");
			}
			catch (System.Exception exception)
			{
				// expectted
			}
			pr1Clone.Close();
		}
Example #21
0
        private void Initialize(IndexReader[] subReaders, bool closeSubReaders)
        {
            this.subReaders = (IndexReader[]) subReaders.Clone();
            starts = new int[subReaders.Length + 1]; // build starts array
            decrefOnClose = new bool[subReaders.Length];
            for (int i = 0; i < subReaders.Length; i++)
            {
                starts[i] = maxDoc;
                maxDoc += subReaders[i].MaxDoc(); // compute maxDocs

                if (!closeSubReaders)
                {
                    subReaders[i].IncRef();
                    decrefOnClose[i] = true;
                }
                else
                {
                    decrefOnClose[i] = false;
                }

                if (subReaders[i].HasDeletions())
                    hasDeletions = true;
            }
            starts[subReaders.Length] = maxDoc;
        }
Example #22
0
        protected internal virtual IndexReader DoReopen(bool doClone)
        {
            EnsureOpen();

            bool reopened = false;

            System.Collections.IList newReaders = new System.Collections.ArrayList();

            bool success = false;

            try
            {
                for (int i = 0; i < readers.Count; i++)
                {
                    IndexReader oldReader = (IndexReader)readers[i];
                    IndexReader newReader = null;
                    if (doClone)
                    {
                        newReader = (IndexReader)oldReader.Clone();
                    }
                    else
                    {
                        newReader = oldReader.Reopen();
                    }
                    newReaders.Add(newReader);
                    // if at least one of the subreaders was updated we remember that
                    // and return a new ParallelReader
                    if (newReader != oldReader)
                    {
                        reopened = true;
                    }
                }
                success = true;
            }
            finally
            {
                if (!success && reopened)
                {
                    for (int i = 0; i < newReaders.Count; i++)
                    {
                        IndexReader r = (IndexReader)newReaders[i];
                        if (r != readers[i])
                        {
                            try
                            {
                                r.Close();
                            }
                            catch (System.IO.IOException ignore)
                            {
                                // keep going - we want to clean up as much as possible
                            }
                        }
                    }
                }
            }

            if (reopened)
            {
                System.Collections.IList newDecrefOnClose = new System.Collections.ArrayList();
                ParallelReader           pr = new ParallelReader();
                for (int i = 0; i < readers.Count; i++)
                {
                    IndexReader oldReader = (IndexReader)readers[i];
                    IndexReader newReader = (IndexReader)newReaders[i];
                    if (newReader == oldReader)
                    {
                        newDecrefOnClose.Add(true);
                        newReader.IncRef();
                    }
                    else
                    {
                        // this is a new subreader instance, so on close() we don't
                        // decRef but close it
                        newDecrefOnClose.Add(false);
                    }
                    pr.Add(newReader, !storedFieldReaders.Contains(oldReader));
                }
                pr.decrefOnClose = newDecrefOnClose;
                pr.incRefReaders = incRefReaders;
                return(pr);
            }
            else
            {
                // No subreader was refreshed
                return(this);
            }
        }
Example #23
0
        public virtual void  TestSegmentReaderDelDocsReferenceCounting()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, false);

            IndexReader   origReader        = IndexReader.Open(dir1, false);
            SegmentReader origSegmentReader = SegmentReader.GetOnlySegmentReader(origReader);

            // deletedDocsRef should be null because nothing has updated yet
            Assert.IsNull(origSegmentReader.deletedDocsRef_ForNUnit);

            // we deleted a document, so there is now a deletedDocs bitvector and a
            // reference to it
            origReader.DeleteDocument(1);
            AssertDelDocsRefCountEquals(1, origSegmentReader);

            // the cloned segmentreader should have 2 references, 1 to itself, and 1 to
            // the original segmentreader
            IndexReader   clonedReader        = (IndexReader)origReader.Clone();
            SegmentReader clonedSegmentReader = SegmentReader.GetOnlySegmentReader(clonedReader);

            AssertDelDocsRefCountEquals(2, origSegmentReader);
            // deleting a document creates a new deletedDocs bitvector, the refs goes to
            // 1
            clonedReader.DeleteDocument(2);
            AssertDelDocsRefCountEquals(1, origSegmentReader);
            AssertDelDocsRefCountEquals(1, clonedSegmentReader);

            // make sure the deletedocs objects are different (copy
            // on write)
            Assert.IsTrue(origSegmentReader.deletedDocs_ForNUnit != clonedSegmentReader.deletedDocs_ForNUnit);

            AssertDocDeleted(origSegmentReader, clonedSegmentReader, 1);
            Assert.IsTrue(!origSegmentReader.IsDeleted(2));             // doc 2 should not be deleted
            // in original segmentreader
            Assert.IsTrue(clonedSegmentReader.IsDeleted(2));            // doc 2 should be deleted in
            // cloned segmentreader

            // deleting a doc from the original segmentreader should throw an exception
            Assert.Throws <LockObtainFailedException>(() => origReader.DeleteDocument(4), "expected exception");

            origReader.Close();
            // try closing the original segment reader to see if it affects the
            // clonedSegmentReader
            clonedReader.DeleteDocument(3);
            clonedReader.Flush();
            AssertDelDocsRefCountEquals(1, clonedSegmentReader);

            // test a reopened reader
            IndexReader   reopenedReader      = clonedReader.Reopen();
            IndexReader   cloneReader2        = (IndexReader)reopenedReader.Clone();
            SegmentReader cloneSegmentReader2 = SegmentReader.GetOnlySegmentReader(cloneReader2);

            AssertDelDocsRefCountEquals(2, cloneSegmentReader2);
            clonedReader.Close();
            reopenedReader.Close();
            cloneReader2.Close();

            dir1.Close();
        }