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();
        }
        public virtual void  TestNormsClose()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, false);
            SegmentReader reader1 = SegmentReader.GetOnlySegmentReader(dir1);

            reader1.Norms("field1");
            Norm r1norm = (Norm)reader1.norms_ForNUnit["field1"];

            SegmentReader.Ref r1BytesRef = r1norm.BytesRef();
            SegmentReader     reader2    = (SegmentReader)reader1.Clone();

            Assert.AreEqual(2, r1norm.BytesRef().RefCount());
            reader1.Close();
            Assert.AreEqual(1, r1BytesRef.RefCount());
            reader2.Norms("field1");
            reader2.Close();
            dir1.Close();
        }
示例#3
0
        public virtual void  TestSegmentReaderCloseReferencing()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, false);
            SegmentReader origSegmentReader = SegmentReader.GetOnlySegmentReader(dir1);

            origSegmentReader.DeleteDocument(1);
            origSegmentReader.SetNorm(4, "field1", 0.5f);

            SegmentReader clonedSegmentReader = (SegmentReader)origSegmentReader.Clone();

            AssertDelDocsRefCountEquals(2, origSegmentReader);
            origSegmentReader.Close();
            AssertDelDocsRefCountEquals(1, origSegmentReader);
            // check the norm refs
            Norm norm = clonedSegmentReader.norms_ForNUnit["field1"];

            Assert.AreEqual(1, norm.BytesRef().RefCount());
            clonedSegmentReader.Close();
            dir1.Close();
        }