This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git

commit 921d9bb175d643f5d61476b3b992f97211ae4a1c
Author: Shad Storhaug <[email protected]>
AuthorDate: Mon Jul 8 07:24:00 2019 +0700

    BUG: LUCENENET-602 - Some platforms fail to load codecs seemingly because 
their types are discovered by using reflection. Supplying local codec types 
explicitly will be faster, and possibly also more reliable across platforms.
---
 .../Support/Codecs/DefaultCodecFactory.cs          | 29 +++++++++++++++-----
 .../Codecs/DefaultDocValuesFormatFactory.cs        | 31 +++++++++++++++-------
 .../Support/Codecs/DefaultPostingsFormatFactory.cs | 25 ++++++++++++-----
 3 files changed, 61 insertions(+), 24 deletions(-)

diff --git a/src/Lucene.Net/Support/Codecs/DefaultCodecFactory.cs 
b/src/Lucene.Net/Support/Codecs/DefaultCodecFactory.cs
index a08acc8..833239b 100644
--- a/src/Lucene.Net/Support/Codecs/DefaultCodecFactory.cs
+++ b/src/Lucene.Net/Support/Codecs/DefaultCodecFactory.cs
@@ -47,6 +47,17 @@ namespace Lucene.Net.Codecs
     /// </summary>
     public class DefaultCodecFactory : NamedServiceFactory<Codec>, 
ICodecFactory, IServiceListable
     {
+        private static Type[] localCodecTypes = new Type[] {
+            typeof(Lucene46.Lucene46Codec),
+#pragma warning disable 612, 618
+            typeof(Lucene3x.Lucene3xCodec), // Optimize 3.x codec over < 4.6 
codecs
+            typeof(Lucene45.Lucene45Codec),
+            typeof(Lucene42.Lucene42Codec),
+            typeof(Lucene41.Lucene41Codec),
+            typeof(Lucene40.Lucene40Codec),
+#pragma warning restore 612, 618
+        };
+
         // NOTE: The following 2 dictionaries are static, since this instance 
is stored in a static
         // variable in the Codec class.
         private readonly IDictionary<string, Type> codecNameToTypeMap = new 
Dictionary<string, Type>();
@@ -64,10 +75,12 @@ namespace Lucene.Net.Codecs
         /// </summary>
         protected override void Initialize()
         {
-            ScanForCodecs(new Assembly[] {
-                typeof(Codec).GetTypeInfo().Assembly,
-                this.CodecsAssembly
-            });
+            foreach (var codecType in localCodecTypes)
+            {
+                PutCodecTypeImpl(codecType);
+            }
+            ScanForCodecs(this.CodecsAssembly);
+
         }
 
         /// <summary>
@@ -179,9 +192,11 @@ namespace Lucene.Net.Codecs
             Type codecType;
             if (!codecNameToTypeMap.TryGetValue(name, out codecType) && 
codecType == null)
             {
-                throw new ArgumentException(string.Format("Codec '{0}' cannot 
be loaded. If the codec is not " +
-                    "in a Lucene.Net assembly, you must subclass 
DefaultCodecFactory and call PutCodecType() or " + 
-                    "ScanForCodecs() from the Initialize() method.", name));
+                throw new ArgumentException($"Codec '{name}' cannot be loaded. 
If the codec is not " +
+                    $"in a Lucene.Net assembly, you must subclass 
{typeof(DefaultCodecFactory).FullName}, " +
+                    "override the Initialize() method, and call PutCodecType() 
or ScanForCodecs() to add " +
+                    $"the type manually. Call 
{typeof(Codec).FullName}.SetCodecFactory() at application " + 
+                    "startup to initialize your custom codec.");
             }
 
             return codecType;
diff --git a/src/Lucene.Net/Support/Codecs/DefaultDocValuesFormatFactory.cs 
b/src/Lucene.Net/Support/Codecs/DefaultDocValuesFormatFactory.cs
index f03c3ae..649e502 100644
--- a/src/Lucene.Net/Support/Codecs/DefaultDocValuesFormatFactory.cs
+++ b/src/Lucene.Net/Support/Codecs/DefaultDocValuesFormatFactory.cs
@@ -47,6 +47,14 @@ namespace Lucene.Net.Codecs
     /// </summary>
     public class DefaultDocValuesFormatFactory : 
NamedServiceFactory<DocValuesFormat>, IDocValuesFormatFactory, IServiceListable
     {
+        private static Type[] localDocValuesFormatTypes = new Type[] {
+            typeof(Lucene45.Lucene45DocValuesFormat),
+#pragma warning disable 612, 618
+            typeof(Lucene42.Lucene42DocValuesFormat),
+            typeof(Lucene40.Lucene40DocValuesFormat),
+#pragma warning restore 612, 618
+        };
+
         // NOTE: The following 2 dictionaries are static, since this instance 
is stored in a static
         // variable in the Codec class.
         private readonly IDictionary<string, Type> 
docValuesFormatNameToTypeMap = new Dictionary<string, Type>();
@@ -64,10 +72,11 @@ namespace Lucene.Net.Codecs
         /// </summary>
         protected override void Initialize()
         {
-            ScanForDocValuesFormats(new Assembly[] {
-                typeof(Codec).GetTypeInfo().Assembly,
-                this.CodecsAssembly
-            });
+            foreach (var docValuesFormatType in localDocValuesFormatTypes)
+            {
+                PutDocValuesFormatTypeImpl(docValuesFormatType);
+            }
+            ScanForDocValuesFormats(this.CodecsAssembly);
         }
 
         /// <summary>
@@ -99,7 +108,7 @@ namespace Lucene.Net.Codecs
             {
                 if (IsServiceType(c))
                 {
-                    PutCodecTypeImpl(c);
+                    PutDocValuesFormatTypeImpl(c);
                 }
             }
         }
@@ -124,10 +133,10 @@ namespace Lucene.Net.Codecs
                 throw new ArgumentException("The supplied docValuesFormat does 
not subclass DocValuesFormat.");
             }
 
-            PutCodecTypeImpl(docValuesFormat);
+            PutDocValuesFormatTypeImpl(docValuesFormat);
         }
 
-        private void PutCodecTypeImpl(Type docValuesFormat)
+        private void PutDocValuesFormatTypeImpl(Type docValuesFormat)
         {
             string name = GetServiceName(docValuesFormat);
             docValuesFormatNameToTypeMap[name] = docValuesFormat;
@@ -179,9 +188,11 @@ namespace Lucene.Net.Codecs
             Type codecType;
             if (!docValuesFormatNameToTypeMap.TryGetValue(name, out codecType) 
&& codecType == null)
             {
-                throw new ArgumentException(string.Format("DocValuesFormat 
'{0}' cannot be loaded. If the format is not " +
-                    "in a Lucene.Net assembly, you must subclass 
DefaultDocValuesFormatFactory and call PutDocValuesFormatType() " + 
-                    "or ScanForDocValuesFormats() from the Initialize() 
method.", name));
+                throw new ArgumentException($"DocValuesFormat '{name}' cannot 
be loaded. If the format is not " +
+                    $"in a Lucene.Net assembly, you must subclass 
{typeof(DefaultDocValuesFormatFactory).FullName}, " +
+                    "override the Initialize() method, and call 
PutDocValuesFormatType() or ScanForDocValuesFormats() to add " +
+                    $"the type manually. Call 
{typeof(DocValuesFormat).FullName}.SetDocValuesFormatFactory() at application " 
+
+                    "startup to initialize your custom format.");
             }
 
             return codecType;
diff --git a/src/Lucene.Net/Support/Codecs/DefaultPostingsFormatFactory.cs 
b/src/Lucene.Net/Support/Codecs/DefaultPostingsFormatFactory.cs
index 08fb60e..3484ced 100644
--- a/src/Lucene.Net/Support/Codecs/DefaultPostingsFormatFactory.cs
+++ b/src/Lucene.Net/Support/Codecs/DefaultPostingsFormatFactory.cs
@@ -47,6 +47,14 @@ namespace Lucene.Net.Codecs
     /// </summary>
     public class DefaultPostingsFormatFactory : 
NamedServiceFactory<PostingsFormat>, IPostingsFormatFactory, IServiceListable
     {
+        private static Type[] localPostingsFormatTypes = new Type[]
+        {
+            typeof(Lucene41.Lucene41PostingsFormat),
+#pragma warning disable 612, 618
+            typeof(Lucene40.Lucene40PostingsFormat),
+#pragma warning restore 612, 618
+        };
+
         // NOTE: The following 2 dictionaries are static, since this instance 
is stored in a static
         // variable in the Codec class.
         private readonly IDictionary<string, Type> postingsFormatNameToTypeMap 
= new Dictionary<string, Type>();
@@ -64,10 +72,11 @@ namespace Lucene.Net.Codecs
         /// </summary>
         protected override void Initialize()
         {
-            ScanForPostingsFormats(new Assembly[] {
-                typeof(Codec).GetTypeInfo().Assembly,
-                this.CodecsAssembly
-            });
+            foreach (var postingsFormatType in localPostingsFormatTypes)
+            {
+                PutPostingsFormatTypeImpl(postingsFormatType);
+            }
+            ScanForPostingsFormats(this.CodecsAssembly);
         }
 
         /// <summary>
@@ -179,9 +188,11 @@ namespace Lucene.Net.Codecs
             Type codecType;
             if (!postingsFormatNameToTypeMap.TryGetValue(name, out codecType) 
&& codecType == null)
             {
-                throw new ArgumentException(string.Format("PostingsFormat 
'{0}' cannot be loaded. If the format is not " +
-                    "in a Lucene.Net assembly, you must subclass 
DefaultPostingsFormatFactory and call PutPostingsFormatType() " + 
-                    "or ScanForPostingsFormats() from the Initialize() 
method.", name));
+                throw new ArgumentException($"PostingsFormat '{name}' cannot 
be loaded. If the format is not " +
+                    $"in a Lucene.Net assembly, you must subclass 
{typeof(DefaultPostingsFormatFactory).FullName}, " +
+                    "override the Initialize() method, and call 
PutPostingsFormatType() or ScanForPostingsFormats() to add " +
+                    $"the type manually. Call 
{typeof(PostingsFormat).FullName}.SetPostingsFormatFactory() at application " +
+                    "startup to initialize your custom format.");
             }
 
             return codecType;

Reply via email to