public virtual void TestSnapshotLastCommitTwice()
        {
            Directory dir = NewDirectory();

            IndexWriter            writer = new IndexWriter(dir, GetConfig(Random(), DeletionPolicy));
            SnapshotDeletionPolicy sdp    = (SnapshotDeletionPolicy)writer.Config.DelPolicy;

            writer.AddDocument(new Document());
            writer.Commit();

            IndexCommit s1 = sdp.Snapshot();
            IndexCommit s2 = sdp.Snapshot();

            Assert.AreSame(s1, s2); // should be the same instance

            // create another commit
            writer.AddDocument(new Document());
            writer.Commit();

            // release "s1" should not delete "s2"
            sdp.Release(s1);
            writer.DeleteUnusedFiles();
            CheckSnapshotExists(dir, s2);

            writer.Dispose();
            dir.Dispose();
        }
 protected internal virtual void PrepareIndexAndSnapshots(SnapshotDeletionPolicy sdp, IndexWriter writer, int numSnapshots)
 {
     for (int i = 0; i < numSnapshots; i++)
     {
         // create dummy document to trigger commit.
         writer.AddDocument(new Document());
         writer.Commit();
         Snapshots.Add(sdp.Snapshot());
     }
 }
 protected internal virtual void PrepareIndexAndSnapshots(SnapshotDeletionPolicy sdp, IndexWriter writer, int numSnapshots)
 {
     for (int i = 0; i < numSnapshots; i++)
     {
         // create dummy document to trigger commit.
         writer.AddDocument(new Document());
         writer.Commit();
         Snapshots.Add(sdp.Snapshot());
     }
 }
Пример #4
0
        private void RunTest(Random random, Directory dir)
        {
            // Run for ~1 seconds
            long stopTime = Environment.TickCount + 1000;

            SnapshotDeletionPolicy dp     = DeletionPolicy;
            IndexWriter            writer = new IndexWriter(dir, (IndexWriterConfig)NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).SetIndexDeletionPolicy(dp).SetMaxBufferedDocs(2));

            // Verify we catch misuse:
            try
            {
                dp.Snapshot();
                Assert.Fail("did not hit exception");
            }
#pragma warning disable 168
            catch (InvalidOperationException ise)
#pragma warning restore 168
            {
                // expected
            }
            dp = (SnapshotDeletionPolicy)writer.Config.IndexDeletionPolicy;
            writer.Commit();

            ThreadClass t = new ThreadAnonymousInnerClassHelper(stopTime, writer, NewField);

            t.Start();

            // While the above indexing thread is running, take many
            // backups:
            do
            {
                BackupIndex(dir, dp);
                Thread.Sleep(20);
            } while (t.IsAlive);

            t.Join();

            // Add one more document to force writer to commit a
            // final segment, so deletion policy has a chance to
            // delete again:
            Document  doc        = new Document();
            FieldType customType = new FieldType(TextField.TYPE_STORED);
            customType.StoreTermVectors         = true;
            customType.StoreTermVectorPositions = true;
            customType.StoreTermVectorOffsets   = true;
            doc.Add(NewField("content", "aaa", customType));
            writer.AddDocument(doc);

            // Make sure we don't have any leftover files in the
            // directory:
            writer.Dispose();
            TestIndexWriter.AssertNoUnreferencedFiles(dir, "some files were not deleted but should have been");
        }
        private void RunTest(Random random, Directory dir)
        {
            // Run for ~1 seconds
            long stopTime = (J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond) + 1000; // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results

            SnapshotDeletionPolicy dp     = DeletionPolicy;
            IndexWriter            writer = new IndexWriter(dir, (IndexWriterConfig)NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).SetIndexDeletionPolicy(dp).SetMaxBufferedDocs(2));

            // Verify we catch misuse:
            try
            {
                dp.Snapshot();
                Assert.Fail("did not hit exception");
            }
            catch (Exception ise) when(ise.IsIllegalStateException())
            {
                // expected
            }
            dp = (SnapshotDeletionPolicy)writer.Config.IndexDeletionPolicy;
            writer.Commit();

            ThreadJob t = new ThreadAnonymousClass(stopTime, writer, NewField);

            t.Start();

            // While the above indexing thread is running, take many
            // backups:
            do
            {
                BackupIndex(dir, dp);
                Thread.Sleep(20);
            } while (t.IsAlive);

            t.Join();

            // Add one more document to force writer to commit a
            // final segment, so deletion policy has a chance to
            // delete again:
            Document  doc        = new Document();
            FieldType customType = new FieldType(TextField.TYPE_STORED);

            customType.StoreTermVectors         = true;
            customType.StoreTermVectorPositions = true;
            customType.StoreTermVectorOffsets   = true;
            doc.Add(NewField("content", "aaa", customType));
            writer.AddDocument(doc);

            // Make sure we don't have any leftover files in the
            // directory:
            writer.Dispose();
            TestIndexWriter.AssertNoUnreferencedFiles(dir, "some files were not deleted but should have been");
        }
 public override void Run()
 {
     try
     {
         Writer.AddDocument(new Document());
         Writer.Commit();
         Snapshots[FinalI] = Sdp.Snapshot();
     }
     catch (Exception e)
     {
         throw new Exception(e.Message, e);
     }
 }
 public override void Run()
 {
     try
     {
         writer.AddDocument(new Document());
         writer.Commit();
         snapshots[finalI] = sdp.Snapshot();
     }
     catch (Exception e) when(e.IsException())
     {
         throw RuntimeException.Create(e);
     }
 }
 /// <summary>
 /// Example showing how to use the SnapshotDeletionPolicy to take a backup.
 /// this method does not really do a backup; instead, it reads every byte of
 /// every file just to test that the files indeed exist and are readable even
 /// while the index is changing.
 /// </summary>
 public virtual void BackupIndex(Directory dir, SnapshotDeletionPolicy dp)
 {
     // To backup an index we first take a snapshot:
     IndexCommit snapshot = dp.Snapshot();
     try
     {
         CopyFiles(dir, snapshot);
     }
     finally
     {
         // Make sure to release the snapshot, otherwise these
         // files will never be deleted during this IndexWriter
         // session:
         dp.Release(snapshot);
     }
 }
        /// <summary>
        /// Example showing how to use the SnapshotDeletionPolicy to take a backup.
        /// this method does not really do a backup; instead, it reads every byte of
        /// every file just to test that the files indeed exist and are readable even
        /// while the index is changing.
        /// </summary>
        public virtual void BackupIndex(Directory dir, SnapshotDeletionPolicy dp)
        {
            // To backup an index we first take a snapshot:
            IndexCommit snapshot = dp.Snapshot();

            try
            {
                CopyFiles(dir, snapshot);
            }
            finally
            {
                // Make sure to release the snapshot, otherwise these
                // files will never be deleted during this IndexWriter
                // session:
                dp.Release(snapshot);
            }
        }
        public virtual void TestMissingCommits()
        {
            // Tests the behavior of SDP when commits that are given at ctor are missing
            // on onInit().
            Directory              dir    = NewDirectory();
            IndexWriter            writer = new IndexWriter(dir, GetConfig(Random(), DeletionPolicy));
            SnapshotDeletionPolicy sdp    = (SnapshotDeletionPolicy)writer.Config.DelPolicy;

            writer.AddDocument(new Document());
            writer.Commit();
            IndexCommit s1 = sdp.Snapshot();

            // create another commit, not snapshotted.
            writer.AddDocument(new Document());
            writer.Dispose();

            // open a new writer w/ KeepOnlyLastCommit policy, so it will delete "s1"
            // commit.
            (new IndexWriter(dir, GetConfig(Random(), null))).Dispose();

            Assert.IsFalse(SlowFileExists(dir, s1.SegmentsFileName), "snapshotted commit should not exist");
            dir.Dispose();
        }