lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [11/22] lucenenet git commit: Fixed error reporting, documentation, and added missing Dispose() calls in CLI tools
Date Tue, 11 Jul 2017 07:42:36 GMT
Fixed error reporting, documentation, and added missing Dispose() calls in CLI tools


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/faa85eb3
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/faa85eb3
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/faa85eb3

Branch: refs/heads/master
Commit: faa85eb360e413c36ab91f93fc4d65300586bf55
Parents: 70f1559
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Mon Jul 10 14:51:32 2017 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Mon Jul 10 14:51:32 2017 +0700

----------------------------------------------------------------------
 .../Index/CompoundFileExtractor.cs              |   8 +-
 .../Index/MultiPassIndexSplitter.cs             | 138 +++++++++++--------
 src/Lucene.Net.Misc/Misc/IndexMergeTool.cs      |  36 ++---
 3 files changed, 102 insertions(+), 80 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/faa85eb3/src/Lucene.Net.Misc/Index/CompoundFileExtractor.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Misc/Index/CompoundFileExtractor.cs b/src/Lucene.Net.Misc/Index/CompoundFileExtractor.cs
index db9c5be..e8452f5 100644
--- a/src/Lucene.Net.Misc/Index/CompoundFileExtractor.cs
+++ b/src/Lucene.Net.Misc/Index/CompoundFileExtractor.cs
@@ -32,7 +32,7 @@ namespace Lucene.Net.Index
         /// Add the -extract flag to extract files to the current working directory.
         /// In order to make the extracted version of the index work, you have to copy
         /// the segments file from the compound index into the directory where the extracted
files are stored. </summary>
-        /// <param name="args"> Usage: org.apache.lucene.index.IndexReader [-extract]
&lt;cfsfile&gt; </param>
+        ///// <param name="args"> Usage: org.apache.lucene.index.IndexReader [-extract]
&lt;cfsfile&gt; </param>
         public static void Main(string[] args)
         {
             string filename = null;
@@ -51,8 +51,10 @@ namespace Lucene.Net.Index
                 {
                     if (j == args.Length - 1)
                     {
-                        Console.WriteLine("ERROR: missing value for -dir-impl option");
-                        Environment.Exit(1);
+                        // LUCENENET specific - our wrapper console shows the correct usage
+                        throw new ArgumentException("ERROR: missing value for --directory-type
option");
+                        //Console.WriteLine("ERROR: missing value for -dir-impl option");
+                        //Environment.Exit(1);
                     }
                     j++;
                     dirImpl = args[j];

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/faa85eb3/src/Lucene.Net.Misc/Index/MultiPassIndexSplitter.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Misc/Index/MultiPassIndexSplitter.cs b/src/Lucene.Net.Misc/Index/MultiPassIndexSplitter.cs
index 4f40250..2a03d89 100644
--- a/src/Lucene.Net.Misc/Index/MultiPassIndexSplitter.cs
+++ b/src/Lucene.Net.Misc/Index/MultiPassIndexSplitter.cs
@@ -133,84 +133,102 @@ namespace Lucene.Net.Index
                 //Environment.Exit(-1);
             }
             List<IndexReader> indexes = new List<IndexReader>();
-            string outDir = null;
-            int numParts = -1;
-            bool seq = false;
-            for (int i = 0; i < args.Length; i++)
+            try
             {
-                if (args[i].Equals("-out"))
+                string outDir = null;
+                int numParts = -1;
+                bool seq = false;
+                for (int i = 0; i < args.Length; i++)
                 {
-                    outDir = args[++i];
+                    if (args[i].Equals("-out"))
+                    {
+                        outDir = args[++i];
+                    }
+                    else if (args[i].Equals("-num"))
+                    {
+                        numParts = Convert.ToInt32(args[++i]);
+                    }
+                    else if (args[i].Equals("-seq"))
+                    {
+                        seq = true;
+                    }
+                    else
+                    {
+                        DirectoryInfo file = new DirectoryInfo(args[i]);
+                        if (!file.Exists)
+                        {
+                            Console.Error.WriteLine("Invalid input path - skipping: " + file);
+                            continue;
+                        }
+                        using (Store.Directory dir = FSDirectory.Open(new DirectoryInfo(args[i])))
+                        {
+                            try
+                            {
+                                if (!DirectoryReader.IndexExists(dir))
+                                {
+                                    Console.Error.WriteLine("Invalid input index - skipping:
" + file);
+                                    continue;
+                                }
+                            }
+                            catch (Exception)
+                            {
+                                Console.Error.WriteLine("Invalid input index - skipping:
" + file);
+                                continue;
+                            }
+                            indexes.Add(DirectoryReader.Open(dir));
+                        }
+                    }
                 }
-                else if (args[i].Equals("-num"))
+                if (outDir == null)
                 {
-                    numParts = Convert.ToInt32(args[++i]);
+                    throw new Exception("Required argument missing: -out outputDir");
                 }
-                else if (args[i].Equals("-seq"))
+                if (numParts < 2)
                 {
-                    seq = true;
+                    throw new Exception("Invalid value of required argument: -num numParts");
                 }
-                else
+                if (indexes.Count == 0)
+                {
+                    throw new Exception("No input indexes to process");
+                }
+                DirectoryInfo @out = new DirectoryInfo(outDir);
+                @out.Create();
+                if (!new DirectoryInfo(outDir).Exists)
+                {
+                    throw new Exception("Can't create output directory: " + @out);
+                }
+                Store.Directory[] dirs = new Store.Directory[numParts];
+                try
                 {
-                    DirectoryInfo file = new DirectoryInfo(args[i]);
-                    if (!file.Exists)
+                    for (int i = 0; i < numParts; i++)
                     {
-                        Console.Error.WriteLine("Invalid input path - skipping: " + file);
-                        continue;
+                        dirs[i] = FSDirectory.Open(new DirectoryInfo(Path.Combine(@out.FullName,
"part-" + i)));
                     }
-                    Store.Directory dir = FSDirectory.Open(new DirectoryInfo(args[i]));
-                    try
+                    MultiPassIndexSplitter splitter = new MultiPassIndexSplitter();
+                    IndexReader input;
+                    if (indexes.Count == 1)
                     {
-                        if (!DirectoryReader.IndexExists(dir))
-                        {
-                            Console.Error.WriteLine("Invalid input index - skipping: " +
file);
-                            continue;
-                        }
+                        input = indexes[0];
                     }
-                    catch (Exception)
+                    else
                     {
-                        Console.Error.WriteLine("Invalid input index - skipping: " + file);
-                        continue;
+                        input = new MultiReader(indexes.ToArray());
                     }
-                    indexes.Add(DirectoryReader.Open(dir));
+#pragma warning disable 612, 618
+                    splitter.Split(LuceneVersion.LUCENE_CURRENT, input, dirs, seq);
+#pragma warning restore 612, 618
+                }
+                finally
+                {
+                    // LUCENENET specific - properly dispose directories to prevent resource
leaks
+                    IOUtils.Dispose(dirs);
                 }
             }
-            if (outDir == null)
-            {
-                throw new Exception("Required argument missing: -out outputDir");
-            }
-            if (numParts < 2)
-            {
-                throw new Exception("Invalid value of required argument: -num numParts");
-            }
-            if (indexes.Count == 0)
-            {
-                throw new Exception("No input indexes to process");
-            }
-            DirectoryInfo @out = new DirectoryInfo(outDir);
-            @out.Create();
-            if (!new DirectoryInfo(outDir).Exists)
-            {
-                throw new Exception("Can't create output directory: " + @out);
-            }
-            Store.Directory[] dirs = new Store.Directory[numParts];
-            for (int i = 0; i < numParts; i++)
-            {
-                dirs[i] = FSDirectory.Open(new DirectoryInfo(Path.Combine(@out.FullName,
"part-" + i)));
-            }
-            MultiPassIndexSplitter splitter = new MultiPassIndexSplitter();
-            IndexReader input;
-            if (indexes.Count == 1)
-            {
-                input = indexes[0];
-            }
-            else
+            finally
             {
-                input = new MultiReader(indexes.ToArray());
+                // LUCENENET specific - properly dispose index readers to prevent resource
leaks
+                IOUtils.Dispose(indexes);
             }
-#pragma warning disable 612, 618
-            splitter.Split(LuceneVersion.LUCENE_CURRENT, input, dirs, seq);
-#pragma warning restore 612, 618
         }
 
         /// <summary>

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/faa85eb3/src/Lucene.Net.Misc/Misc/IndexMergeTool.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Misc/Misc/IndexMergeTool.cs b/src/Lucene.Net.Misc/Misc/IndexMergeTool.cs
index de79ff5..2c1b305 100644
--- a/src/Lucene.Net.Misc/Misc/IndexMergeTool.cs
+++ b/src/Lucene.Net.Misc/Misc/IndexMergeTool.cs
@@ -32,31 +32,33 @@ namespace Lucene.Net.Misc
         {
             if (args.Length < 3)
             {
-                Console.Error.WriteLine("Usage: IndexMergeTool <mergedIndex> <index1>
<index2> [index3] ...");
-                Environment.Exit(1);
+                // LUCENENET specific - our wrapper console shows the correct usage
+                throw new ArgumentException();
+                //Console.Error.WriteLine("Usage: IndexMergeTool <mergedIndex> <index1>
<index2> [index3] ...");
+                //Environment.Exit(1);
             }
-            FSDirectory mergedIndex = FSDirectory.Open(new System.IO.DirectoryInfo(args[0]));
-
-            using (IndexWriter writer = new IndexWriter(mergedIndex, 
-#pragma warning disable 612, 618                
+            using (FSDirectory mergedIndex = FSDirectory.Open(new System.IO.DirectoryInfo(args[0])))
+            {
+                using (IndexWriter writer = new IndexWriter(mergedIndex,
+#pragma warning disable 612, 618
                 new IndexWriterConfig(LuceneVersion.LUCENE_CURRENT, null)
 #pragma warning restore 612, 618
                 { OpenMode = OpenMode.CREATE }))
-            {
-
-                Directory[] indexes = new Directory[args.Length - 1];
-                for (int i = 1; i < args.Length; i++)
                 {
-                    indexes[i - 1] = FSDirectory.Open(new System.IO.DirectoryInfo(args[i]));
-                }
+                    Directory[] indexes = new Directory[args.Length - 1];
+                    for (int i = 1; i < args.Length; i++)
+                    {
+                        indexes[i - 1] = FSDirectory.Open(new System.IO.DirectoryInfo(args[i]));
+                    }
 
-                Console.WriteLine("Merging...");
-                writer.AddIndexes(indexes);
+                    Console.WriteLine("Merging...");
+                    writer.AddIndexes(indexes);
 
-                Console.WriteLine("Full merge...");
-                writer.ForceMerge(1);
+                    Console.WriteLine("Full merge...");
+                    writer.ForceMerge(1);
+                }
+                Console.WriteLine("Done.");
             }
-            Console.WriteLine("Done.");
         }
     }
 }
\ No newline at end of file


Mime
View raw message