Lucene.Net.Index.IndexUpgrader.Upgrade C# (CSharp) Method

Upgrade() public method

Perform the upgrade.
public Upgrade ( ) : void
return void
        public void Upgrade()
        {
            if (!DirectoryReader.IndexExists(Dir))
            {
                throw new IndexNotFoundException(Dir.ToString());
            }

            if (!DeletePriorCommits)
            {
                ICollection<IndexCommit> commits = DirectoryReader.ListCommits(Dir);
                if (commits.Count > 1)
                {
                    throw new System.ArgumentException("this tool was invoked to not delete prior commit points, but the following commits were found: " + commits);
                }
            }

            IndexWriterConfig c = (IndexWriterConfig)Iwc.Clone();
            c.SetMergePolicy(new UpgradeIndexMergePolicy(c.MergePolicy));
            c.SetIndexDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());

            IndexWriter w = new IndexWriter(Dir, c);
            try
            {
                InfoStream infoStream = c.InfoStream;
                if (infoStream.IsEnabled("IndexUpgrader"))
                {
                    infoStream.Message("IndexUpgrader", "Upgrading all pre-" + Constants.LUCENE_MAIN_VERSION + " segments of index directory '" + Dir + "' to version " + Constants.LUCENE_MAIN_VERSION + "...");
                }
                w.ForceMerge(1);
                if (infoStream.IsEnabled("IndexUpgrader"))
                {
                    infoStream.Message("IndexUpgrader", "All segments upgraded to version " + Constants.LUCENE_MAIN_VERSION);
                }
            }
            finally
            {
                w.Dispose();
            }
        }
    }

Usage Example

        public virtual void TestCommandLineArgs()
        {
            foreach (string name in OldIndexDirs.Keys)
            {
                DirectoryInfo dir = CreateTempDir(name);
                using (Stream dataFile = this.GetType().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + "index." + name + ".zip"))
                {
                    TestUtil.Unzip(dataFile, dir);
                }

                string path = dir.FullName;

                IList <string> args = new List <string>();
                if (Random().NextBoolean())
                {
                    args.Add("-verbose");
                }
                if (Random().NextBoolean())
                {
                    args.Add("-delete-prior-commits");
                }
                if (Random().NextBoolean())
                {
                    // TODO: need to better randomize this, but ...
                    //  - LuceneTestCase.FS_DIRECTORIES is private
                    //  - newFSDirectory returns BaseDirectoryWrapper
                    //  - BaseDirectoryWrapper doesn't expose delegate
                    Type dirImpl = Random().NextBoolean() ? typeof(SimpleFSDirectory) : typeof(NIOFSDirectory);

                    args.Add("-dir-impl");
                    args.Add(dirImpl.Name);
                }
                args.Add(path);

                IndexUpgrader upgrader = null;
                try
                {
                    upgrader = IndexUpgrader.ParseArgs(args.ToArray());
                }
                catch (Exception e)
                {
                    throw new Exception("unable to parse args: " + args, e);
                }
                upgrader.Upgrade();

                Directory upgradedDir = NewFSDirectory(dir);
                try
                {
                    CheckAllSegmentsUpgraded(upgradedDir);
                }
                finally
                {
                    upgradedDir.Dispose();
                }
            }
        }