FixIndex() публичный Метод

Repairs the index using previously returned result from CheckIndex. Note that this does not remove any of the unreferenced files after it's done; you must separately open an IndexWriter, which deletes unreferenced files when it's created.

WARNING: this writes a new segments file into the index, effectively removing all documents in broken segments from the index. BE CAREFUL.

WARNING: Make sure you only call this when the index is not opened by any writer.

public FixIndex ( Status result ) : void
result Status
Результат void
Пример #1
0
        public static bool Check(Directory dir, bool doFix, System.Collections.IList onlySegments)
        {
            CheckIndex checker = new CheckIndex(dir);
            Status     status  = checker.CheckIndex_Renamed_Method(onlySegments);

            if (doFix && !status.clean)
            {
                checker.FixIndex(status);
            }

            return(status.clean);
        }
Пример #2
0
		private static void CheckIndexAndTryToFix(Lucene.Net.Store.Directory directory, IndexDefinition indexDefinition)
		{
			startupLog.Warn("Unclean shutdown detected on {0}, checking the index for errors. This may take a while.", indexDefinition.Name);

			var memoryStream = new MemoryStream();
			var stringWriter = new StreamWriter(memoryStream);
			var checkIndex = new CheckIndex(directory);

			if (startupLog.IsWarnEnabled)
				checkIndex.SetInfoStream(stringWriter);

			var sp = Stopwatch.StartNew();
			var status = checkIndex.CheckIndex_Renamed_Method();
			sp.Stop();
			if (startupLog.IsWarnEnabled)
			{
				startupLog.Warn("Checking index {0} took: {1}, clean: {2}", indexDefinition.Name, sp.Elapsed, status.clean);
				memoryStream.Position = 0;

				log.Warn(new StreamReader(memoryStream).ReadToEnd());
			}

			if (status.clean)
				return;

			startupLog.Warn("Attempting to fix index: {0}", indexDefinition.Name);
			sp.Restart();
			checkIndex.FixIndex(status);
			startupLog.Warn("Fixed index {0} in {1}", indexDefinition.Name, sp.Elapsed);
		}
Пример #3
0
 public static void  Main(System.String[] args)
 {
     
     bool doFix = false;
     var onlySegments = new List<string>();
     System.String indexPath = null;
     int i = 0;
     while (i < args.Length)
     {
         if (args[i].Equals("-fix"))
         {
             doFix = true;
             i++;
         }
         else if (args[i].Equals("-segment"))
         {
             if (i == args.Length - 1)
             {
                 System.Console.Out.WriteLine("ERROR: missing name for -segment option");
                 System.Environment.Exit(1);
             }
             onlySegments.Add(args[i + 1]);
             i += 2;
         }
         else
         {
             if (indexPath != null)
             {
                 System.Console.Out.WriteLine("ERROR: unexpected extra argument '" + args[i] + "'");
                 System.Environment.Exit(1);
             }
             indexPath = args[i];
             i++;
         }
     }
     
     if (indexPath == null)
     {
         System.Console.Out.WriteLine("\nERROR: index path not specified");
         System.Console.Out.WriteLine("\nUsage: java Lucene.Net.Index.CheckIndex pathToIndex [-fix] [-segment X] [-segment Y]\n" + "\n" + "  -fix: actually write a new segments_N file, removing any problematic segments\n" + "  -segment X: only check the specified segments.  This can be specified multiple\n" + "              times, to check more than one segment, eg '-segment _2 -segment _a'.\n" + "              You can't use this with the -fix option\n" + "\n" + "**WARNING**: -fix should only be used on an emergency basis as it will cause\n" + "documents (perhaps many) to be permanently removed from the index.  Always make\n" + "a backup copy of your index before running this!  Do not run this tool on an index\n" + "that is actively being written to.  You have been warned!\n" + "\n" + "Run without -fix, this tool will open the index, report version information\n" + "and report any exceptions it hits and what action it would take if -fix were\n" + "specified.  With -fix, this tool will remove any segments that have issues and\n" + "write a new segments_N file.  This means all documents contained in the affected\n" + "segments will be removed.\n" + "\n" + "This tool exits with exit code 1 if the index cannot be opened or has any\n" + "corruption, else 0.\n");
         System.Environment.Exit(1);
     }
     
     if (!AssertsOn())
         System.Console.Out.WriteLine("\nNOTE: testing will be more thorough if you run java with '-ea:Lucene.Net...', so assertions are enabled");
     
     if (onlySegments.Count == 0)
         onlySegments = null;
     else if (doFix)
     {
         System.Console.Out.WriteLine("ERROR: cannot specify both -fix and -segment");
         System.Environment.Exit(1);
     }
     
     System.Console.Out.WriteLine("\nOpening index @ " + indexPath + "\n");
     Directory dir = null;
     try
     {
         dir = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath));
     }
     catch (Exception t)
     {
         Console.Out.WriteLine("ERROR: could not open directory \"" + indexPath + "\"; exiting");
         Console.Out.WriteLine(t.StackTrace);
         Environment.Exit(1);
     }
     
     var checker = new CheckIndex(dir);
     var tempWriter = new System.IO.StreamWriter(System.Console.OpenStandardOutput(), System.Console.Out.Encoding)
                          {AutoFlush = true};
     checker.SetInfoStream(tempWriter);
     
     Status result = checker.CheckIndex_Renamed_Method(onlySegments);
     if (result.missingSegments)
     {
         System.Environment.Exit(1);
     }
     
     if (!result.clean)
     {
         if (!doFix)
         {
             System.Console.Out.WriteLine("WARNING: would write new segments file, and " + result.totLoseDocCount + " documents would be lost, if -fix were specified\n");
         }
         else
         {
             Console.Out.WriteLine("WARNING: " + result.totLoseDocCount + " documents will be lost\n");
             Console.Out.WriteLine("NOTE: will write new segments file in 5 seconds; this will remove " + result.totLoseDocCount + " docs from the index. THIS IS YOUR LAST CHANCE TO CTRL+C!");
             for (var s = 0; s < 5; s++)
             {
                 System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 1000));
                 System.Console.Out.WriteLine("  " + (5 - s) + "...");
             }
             Console.Out.WriteLine("Writing...");
             checker.FixIndex(result);
             Console.Out.WriteLine("OK");
             Console.Out.WriteLine("Wrote new segments file \"" + result.newSegments.GetCurrentSegmentFileName() + "\"");
         }
     }
     System.Console.Out.WriteLine("");
     
     int exitCode;
     if (result != null && result.clean == true)
         exitCode = 0;
     else
         exitCode = 1;
     System.Environment.Exit(exitCode);
 }
Пример #4
0
        private void CheckIndexAndTryToFix(LuceneDirectory directory)
        {
            StartupLog.Warn(string.Format("Unclean shutdown detected on file system '{0}', checking the index for errors. This may take a while.", name));

            var memoryStream = new MemoryStream();
            var stringWriter = new StreamWriter(memoryStream);
            var checkIndex = new CheckIndex(directory);

            if (StartupLog.IsWarnEnabled)
                checkIndex.SetInfoStream(stringWriter);

            var sp = Stopwatch.StartNew();
            var status = checkIndex.CheckIndex_Renamed_Method();
            sp.Stop();

            if (StartupLog.IsWarnEnabled)
            {
                StartupLog.Warn("Checking index for file system '{0}' took: {1}, clean: {2}", name, sp.Elapsed, status.clean);
                memoryStream.Position = 0;

                Log.Warn(new StreamReader(memoryStream).ReadToEnd());
            }

            if (status.clean)
                return;

            StartupLog.Warn("Attempting to fix index of file system: '{0}'", name);
            sp.Restart();
            checkIndex.FixIndex(status);
            StartupLog.Warn("Fixed index of file system '{0}' in {1}", name, sp.Elapsed);
        }
Пример #5
0
        public static void  Main(System.String[] args)
        {
            bool doFix = false;

            System.Collections.IList onlySegments = new System.Collections.ArrayList();
            System.String            indexPath    = null;
            int i = 0;

            while (i < args.Length)
            {
                if (args[i].Equals("-fix"))
                {
                    doFix = true;
                    i++;
                }
                else if (args[i].Equals("-segment"))
                {
                    if (i == args.Length - 1)
                    {
                        System.Console.Out.WriteLine("ERROR: missing name for -segment option");
                        System.Environment.Exit(1);
                    }
                    onlySegments.Add(args[i + 1]);
                    i += 2;
                }
                else
                {
                    if (indexPath != null)
                    {
                        System.Console.Out.WriteLine("ERROR: unexpected extra argument '" + args[i] + "'");
                        System.Environment.Exit(1);
                    }
                    indexPath = args[i];
                    i++;
                }
            }

            if (indexPath == null)
            {
                System.Console.Out.WriteLine("\nERROR: index path not specified");
                System.Console.Out.WriteLine("\nUsage: java Lucene.Net.Index.CheckIndex pathToIndex [-fix] [-segment X] [-segment Y]\n" + "\n" + "  -fix: actually write a new segments_N file, removing any problematic segments\n" + "  -segment X: only check the specified segments.  This can be specified multiple\n" + "              times, to check more than one segment, eg '-segment _2 -segment _a'.\n" + "              You can't use this with the -fix option\n" + "\n" + "**WARNING**: -fix should only be used on an emergency basis as it will cause\n" + "documents (perhaps many) to be permanently removed from the index.  Always make\n" + "a backup copy of your index before running this!  Do not run this tool on an index\n" + "that is actively being written to.  You have been warned!\n" + "\n" + "Run without -fix, this tool will open the index, report version information\n" + "and report any exceptions it hits and what action it would take if -fix were\n" + "specified.  With -fix, this tool will remove any segments that have issues and\n" + "write a new segments_N file.  This means all documents contained in the affected\n" + "segments will be removed.\n" + "\n" + "This tool exits with exit code 1 if the index cannot be opened or has any\n" + "corruption, else 0.\n");
                System.Environment.Exit(1);
            }

            if (!AssertsOn())
            {
                System.Console.Out.WriteLine("\nNOTE: testing will be more thorough if you run java with '-ea:Lucene.Net...', so assertions are enabled");
            }

            if (onlySegments.Count == 0)
            {
                onlySegments = null;
            }
            else if (doFix)
            {
                System.Console.Out.WriteLine("ERROR: cannot specify both -fix and -segment");
                System.Environment.Exit(1);
            }

            System.Console.Out.WriteLine("\nOpening index @ " + indexPath + "\n");
            Directory dir = null;

            try
            {
                dir = FSDirectory.Open(new System.IO.FileInfo(indexPath));
            }
            catch (System.Exception t)
            {
                System.Console.Out.WriteLine("ERROR: could not open directory \"" + indexPath + "\"; exiting");
                System.Console.Out.WriteLine(t.StackTrace);
                System.Environment.Exit(1);
            }

            CheckIndex checker = new CheckIndex(dir);

            System.IO.StreamWriter temp_writer;
            temp_writer           = new System.IO.StreamWriter(System.Console.OpenStandardOutput(), System.Console.Out.Encoding);
            temp_writer.AutoFlush = true;
            checker.SetInfoStream(temp_writer);

            Status result = checker.CheckIndex_Renamed_Method(onlySegments);

            if (result.missingSegments)
            {
                System.Environment.Exit(1);
            }

            if (!result.clean)
            {
                if (!doFix)
                {
                    System.Console.Out.WriteLine("WARNING: would write new segments file, and " + result.totLoseDocCount + " documents would be lost, if -fix were specified\n");
                }
                else
                {
                    System.Console.Out.WriteLine("WARNING: " + result.totLoseDocCount + " documents will be lost\n");
                    System.Console.Out.WriteLine("NOTE: will write new segments file in 5 seconds; this will remove " + result.totLoseDocCount + " docs from the index. THIS IS YOUR LAST CHANCE TO CTRL+C!");
                    for (int s = 0; s < 5; s++)
                    {
                        System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 1000));
                        System.Console.Out.WriteLine("  " + (5 - s) + "...");
                    }
                    System.Console.Out.WriteLine("Writing...");
                    checker.FixIndex(result);
                    System.Console.Out.WriteLine("OK");
                    System.Console.Out.WriteLine("Wrote new segments file \"" + result.newSegments.GetCurrentSegmentFileName() + "\"");
                }
            }
            System.Console.Out.WriteLine("");

            int exitCode;

            if (result != null && result.clean == true)
            {
                exitCode = 0;
            }
            else
            {
                exitCode = 1;
            }
            System.Environment.Exit(exitCode);
        }
Пример #6
0
		public static bool Check(Directory dir, bool doFix, System.Collections.IList onlySegments)
		{
			CheckIndex checker = new CheckIndex(dir);
			Status status = checker.CheckIndex_Renamed_Method(onlySegments);
			if (doFix && !status.clean)
				checker.FixIndex(status);
			
			return status.clean;
		}