public virtual void TestReleaseSnapshot()
        {
            Directory              dir    = NewDirectory();
            IndexWriter            writer = new IndexWriter(dir, GetConfig(Random(), DeletionPolicy));
            SnapshotDeletionPolicy sdp    = (SnapshotDeletionPolicy)writer.Config.DelPolicy;

            PrepareIndexAndSnapshots(sdp, writer, 1);

            // Create another commit - we must do that, because otherwise the "snapshot"
            // files will still remain in the index, since it's the last commit.
            writer.AddDocument(new Document());
            writer.Commit();

            // Release
            string segFileName = Snapshots[0].SegmentsFileName;

            sdp.Release(Snapshots[0]);
            writer.DeleteUnusedFiles();
            writer.Dispose();
            Assert.IsFalse(SlowFileExists(dir, segFileName), "segments file should not be found in dirctory: " + segFileName);
            dir.Dispose();
        }
        public virtual void TestMultiThreadedSnapshotting()
        {
            Directory              dir    = NewDirectory();
            IndexWriter            writer = new IndexWriter(dir, GetConfig(Random(), DeletionPolicy));
            SnapshotDeletionPolicy sdp    = (SnapshotDeletionPolicy)writer.Config.DelPolicy;

            ThreadClass[] threads   = new ThreadClass[10];
            IndexCommit[] snapshots = new IndexCommit[threads.Length];
            for (int i = 0; i < threads.Length; i++)
            {
                int finalI = i;
                threads[i]      = new ThreadAnonymousInnerClassHelper2(this, writer, sdp, snapshots, finalI);
                threads[i].Name = "t" + i;
            }

            foreach (ThreadClass t in threads)
            {
                t.Start();
            }

            foreach (ThreadClass t in threads)
            {
                t.Join();
            }

            // Do one last commit, so that after we release all snapshots, we stay w/ one commit
            writer.AddDocument(new Document());
            writer.Commit();

            for (int i = 0; i < threads.Length; i++)
            {
                sdp.Release(snapshots[i]);
                writer.DeleteUnusedFiles();
            }
            Assert.AreEqual(1, DirectoryReader.ListCommits(dir).Count);
            writer.Dispose();
            dir.Dispose();
        }
 /// <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);
     }
 }