You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2017/04/08 15:48:24 UTC
[4/8] lucenenet git commit: SWEEP: Removed FEATURE_EMBEDDED_RESOURCE
and changed all code that accesses embedded resources to use a new set of
extension methods that aggressively search the available resources for a
string match or partial match. Since t
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net.Tests.Suggest/Suggest/LookupBenchmarkTest.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.Suggest/Suggest/LookupBenchmarkTest.cs b/src/Lucene.Net.Tests.Suggest/Suggest/LookupBenchmarkTest.cs
index 58cf223..f3d84d1 100644
--- a/src/Lucene.Net.Tests.Suggest/Suggest/LookupBenchmarkTest.cs
+++ b/src/Lucene.Net.Tests.Suggest/Suggest/LookupBenchmarkTest.cs
@@ -90,7 +90,8 @@ namespace Lucene.Net.Search.Suggest
List<Input> input = new List<Input>();
//URL resource = LookupBenchmarkTest.class.getResource("Top50KWiki.utf8");
- var resource = typeof(LookupBenchmarkTest).GetTypeInfo().Assembly.GetManifestResourceStream("Lucene.Net.Tests.Suggest.Suggest.Top50KWiki.utf8");
+ //var resource = typeof(LookupBenchmarkTest).GetTypeInfo().Assembly.GetManifestResourceStream("Lucene.Net.Tests.Suggest.Suggest.Top50KWiki.utf8");
+ var resource = typeof(LookupBenchmarkTest).GetTypeInfo().Assembly.FindAndGetManifestResourceStream(typeof(LookupBenchmarkTest), "Top50KWiki.utf8");
Debug.Assert(resource != null, "Resource missing: Top50KWiki.utf8");
string line = null;
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs b/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs
index d4602e0..77a6401 100644
--- a/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs
+++ b/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs
@@ -158,9 +158,6 @@ namespace Lucene.Net.Index
}
*/
- // LUCENENET specific to load resources for this type
- internal const string CURRENT_RESOURCE_DIRECTORY = "Lucene.Net.Tests.Index.";
-
internal static readonly string[] OldNames = new string[] {
"40.cfs", "40.nocfs", "41.cfs", "41.nocfs", "42.cfs",
"42.nocfs", "45.cfs", "45.nocfs", "461.cfs", "461.nocfs"
@@ -213,7 +210,7 @@ namespace Lucene.Net.Index
foreach (string name in names)
{
DirectoryInfo dir = CreateTempDir(name);
- using (Stream zipFileStream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + "index." + name + ".zip"))
+ using (Stream zipFileStream = this.GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), "index." + name + ".zip"))
{
TestUtil.Unzip(zipFileStream, dir);
}
@@ -244,7 +241,7 @@ namespace Lucene.Net.Index
Console.WriteLine("TEST: index " + UnsupportedNames[i]);
}
DirectoryInfo oldIndxeDir = CreateTempDir(UnsupportedNames[i]);
- using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + "unsupported." + UnsupportedNames[i] + ".zip"))
+ using (Stream dataFile = GetDataFile("unsupported." + UnsupportedNames[i] + ".zip"))
{
TestUtil.Unzip(dataFile, oldIndxeDir);
}
@@ -953,7 +950,7 @@ namespace Lucene.Net.Index
foreach (string name in OldIndexDirs.Keys)
{
DirectoryInfo dir = CreateTempDir(name);
- using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + "index." + name + ".zip"))
+ using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), "index." + name + ".zip"))
{
TestUtil.Unzip(dataFile, dir);
}
@@ -1061,7 +1058,7 @@ namespace Lucene.Net.Index
public virtual void TestMoreTerms()
{
DirectoryInfo oldIndexDir = CreateTempDir("moreterms");
- using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + MoreTermsIndex))
+ using (Stream dataFile = GetDataFile(MoreTermsIndex))
{
TestUtil.Unzip(dataFile, oldIndexDir);
}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs b/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs
index 3dd44a9..f497ae3 100644
--- a/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs
+++ b/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs
@@ -142,7 +142,7 @@ namespace Lucene.Net.Index
foreach (string name in names)
{
DirectoryInfo dir = CreateTempDir(name);
- using (Stream zipFileStream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + "index." + name + ".zip"))
+ using (Stream zipFileStream = this.GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), "index." + name + ".zip"))
{
TestUtil.Unzip(zipFileStream, dir);
}
@@ -173,7 +173,7 @@ namespace Lucene.Net.Index
Console.WriteLine("TEST: index " + UnsupportedNames[i]);
}
DirectoryInfo oldIndexDir = CreateTempDir(UnsupportedNames[i]);
- using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + "unsupported." + UnsupportedNames[i] + ".zip"))
+ using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), "unsupported." + UnsupportedNames[i] + ".zip"))
{
TestUtil.Unzip(dataFile, oldIndexDir);
}
@@ -960,7 +960,7 @@ namespace Lucene.Net.Index
public virtual void TestSurrogates()
{
DirectoryInfo oldIndexDir = CreateTempDir("surrogates");
- using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + SurrogatesIndexName))
+ using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), SurrogatesIndexName))
{
TestUtil.Unzip(dataFile, oldIndexDir);
}
@@ -1022,7 +1022,7 @@ namespace Lucene.Net.Index
public virtual void TestNegativePositions()
{
DirectoryInfo oldIndexDir = CreateTempDir("negatives");
- using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + Bogus24IndexName))
+ using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), Bogus24IndexName))
{
TestUtil.Unzip(dataFile, oldIndexDir);
}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net.Tests/Support/TestDataInputStream.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/Support/TestDataInputStream.cs b/src/Lucene.Net.Tests/Support/TestDataInputStream.cs
index 614338e..cbb480a 100644
--- a/src/Lucene.Net.Tests/Support/TestDataInputStream.cs
+++ b/src/Lucene.Net.Tests/Support/TestDataInputStream.cs
@@ -13,10 +13,10 @@ namespace Lucene.Net.Support
[Test, LuceneNetSpecific]
public void TestReadFully()
{
- const string READFULLY_TEST_FILE = "Lucene.Net.Tests.Support.ReadFully.txt";
+ const string READFULLY_TEST_FILE = "ReadFully.txt";
byte[] buffer = new byte[1367];
- Stream @in = GetType().GetTypeInfo().Assembly.GetManifestResourceStream(READFULLY_TEST_FILE);
+ Stream @in = GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), READFULLY_TEST_FILE);
DataInputStream dis;
using (dis = new DataInputStream(@in))
{
@@ -25,7 +25,7 @@ namespace Lucene.Net.Support
}
// Read past the end of the stream
- @in = GetType().GetTypeInfo().Assembly.GetManifestResourceStream(READFULLY_TEST_FILE);
+ @in = GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), READFULLY_TEST_FILE);
dis = new DataInputStream(@in);
bool caughtException = false;
try
@@ -47,7 +47,7 @@ namespace Lucene.Net.Support
// Ensure we get an IndexOutOfRangeException exception when length is negative
@in = GetType().GetTypeInfo().
- Assembly.GetManifestResourceStream(READFULLY_TEST_FILE);
+ Assembly.FindAndGetManifestResourceStream(GetType(), READFULLY_TEST_FILE);
dis = new DataInputStream(@in);
caughtException = false;
try
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net/Lucene.Net.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Lucene.Net.csproj b/src/Lucene.Net/Lucene.Net.csproj
index d420b07..756bfe7 100644
--- a/src/Lucene.Net/Lucene.Net.csproj
+++ b/src/Lucene.Net/Lucene.Net.csproj
@@ -595,6 +595,7 @@
<Compile Include="Store\TrackingDirectoryWrapper.cs" />
<Compile Include="Store\VerifyingLockFactory.cs" />
<Compile Include="Support\AppSettings.cs" />
+ <Compile Include="Support\AssemblyExtensions.cs" />
<Compile Include="Support\AtomicBoolean.cs" />
<Compile Include="Support\AtomicInteger.cs" />
<Compile Include="Support\AtomicLong.cs" />
@@ -889,7 +890,7 @@
<Compile Include="Util\VirtualMethod.cs" />
<Compile Include="Util\WAH8DocIdSet.cs" />
<Compile Include="Util\WeakIdentityMap.cs" />
- <Compile Include="..\CommonAssemblyInfo.cs">
+ <Compile Include="..\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
</ItemGroup>
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net/Support/AssemblyExtensions.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Support/AssemblyExtensions.cs b/src/Lucene.Net/Support/AssemblyExtensions.cs
new file mode 100644
index 0000000..1269ce1
--- /dev/null
+++ b/src/Lucene.Net/Support/AssemblyExtensions.cs
@@ -0,0 +1,142 @@
+\ufeffusing System;
+using System.Collections.Concurrent;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+
+namespace Lucene.Net.Support
+{
+ public static class AssemblyExtensions
+ {
+ private static ConcurrentDictionary<TypeAndResource, string> resourceCache = new ConcurrentDictionary<TypeAndResource, string>();
+
+ /// <summary>
+ /// Aggressively searches for a resource and, if found, returns an open <see cref="Stream"/>
+ /// where it can be read.
+ /// </summary>
+ /// <param name="assembly">this assembly</param>
+ /// <param name="type">a type in the same namespace as the resource</param>
+ /// <param name="name">the resource name to locate</param>
+ /// <returns>an open <see cref="Stream"/> that can be used to read the resource, or <c>null</c> if the resource cannot be found.</returns>
+ public static Stream FindAndGetManifestResourceStream(this Assembly assembly, Type type, string name)
+ {
+ string resourceName = FindResource(assembly, type, name);
+ if (string.IsNullOrEmpty(resourceName))
+ {
+ return null;
+ }
+
+ return assembly.GetManifestResourceStream(resourceName);
+ }
+
+ /// <summary>
+ /// Aggressively searches to find a resource based on a <see cref="Type"/> and resource name.
+ /// </summary>
+ /// <param name="assembly">this assembly</param>
+ /// <param name="type">a type in the same namespace as the resource</param>
+ /// <param name="name">the resource name to locate</param>
+ /// <returns>the resource, if found; if not found, returns <c>null</c></returns>
+ public static string FindResource(this Assembly assembly, Type type, string name)
+ {
+ string resourceName;
+ TypeAndResource key = new TypeAndResource(type, name);
+ if (!resourceCache.TryGetValue(key, out resourceName))
+ {
+ string[] resourceNames = assembly.GetManifestResourceNames();
+ resourceName = resourceNames.Where(x => x.Equals(name)).FirstOrDefault();
+
+ // If resourceName is not null, we have an exact match, don't search
+ if (resourceName == null)
+ {
+ string assemblyName = type.GetTypeInfo().Assembly.GetName().Name;
+ string namespaceName = type.GetTypeInfo().Namespace;
+
+ // Search by assembly + namespace
+ string resourceToFind = string.Concat(namespaceName, ".", name);
+ if (!TryFindResource(resourceNames, assemblyName, resourceToFind, name, out resourceName))
+ {
+ string found1 = resourceName;
+
+ // Search by namespace only
+ if (!TryFindResource(resourceNames, null, resourceToFind, name, out resourceName))
+ {
+ string found2 = resourceName;
+
+ // Search by assembly name only
+ resourceToFind = string.Concat(assemblyName, ".", name);
+ if (!TryFindResource(resourceNames, null, resourceToFind, name, out resourceName))
+ {
+ // Take the first match of multiple, if there are any
+ resourceName = found1 ?? found2 ?? resourceName;
+ }
+ }
+ }
+ }
+
+ resourceCache[key] = resourceName;
+ }
+
+ return resourceName;
+ }
+
+ private static bool TryFindResource(string[] resourceNames, string prefix, string resourceName, string exactResourceName, out string result)
+ {
+ if (!resourceNames.Contains(resourceName))
+ {
+ string nameToFind = null;
+ while (resourceName.Length > 0 && resourceName.Contains('.') && (!(string.IsNullOrEmpty(prefix)) || resourceName.Equals(exactResourceName)))
+ {
+ resourceName = resourceName.Substring(resourceName.IndexOf('.') + 1);
+ nameToFind = string.IsNullOrEmpty(prefix)
+ ? resourceName
+ : string.Concat(prefix, ".", resourceName);
+ string[] matches = resourceNames.Where(x => x.EndsWith(nameToFind, StringComparison.Ordinal)).ToArray();
+ if (matches.Length == 1)
+ {
+ result = matches[0]; // Exact match
+ return true;
+ }
+ else if (matches.Length > 1)
+ {
+ result = matches[0]; // First of many
+ return false;
+ }
+ }
+ result = null; // No match
+ return false;
+ }
+
+ result = resourceName;
+ return true;
+ }
+
+ private class TypeAndResource
+ {
+ private readonly Type type;
+ private readonly string name;
+
+ public TypeAndResource(Type type, string name)
+ {
+ this.type = type;
+ this.name = name;
+ }
+
+ public override bool Equals(object obj)
+ {
+ if (!(obj is TypeAndResource))
+ {
+ return false;
+ }
+
+ var other = obj as TypeAndResource;
+ return this.type.Equals(other.type)
+ && this.name.Equals(other.name);
+ }
+
+ public override int GetHashCode()
+ {
+ return this.type.GetHashCode() ^ this.name.GetHashCode();
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net/Support/BundleResourceManagerFactory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Support/BundleResourceManagerFactory.cs b/src/Lucene.Net/Support/BundleResourceManagerFactory.cs
index 8af2212..85b5414 100644
--- a/src/Lucene.Net/Support/BundleResourceManagerFactory.cs
+++ b/src/Lucene.Net/Support/BundleResourceManagerFactory.cs
@@ -29,7 +29,7 @@ namespace Lucene.Net.Support
/// <returns>A new <see cref="ResourceManager"/> instance.</returns>
public virtual ResourceManager Create(Type resourceSource)
{
- return new ResourceManager(GetResourceType(resourceSource));
+ return new ResourceManager(GetResourceName(resourceSource), resourceSource.GetTypeInfo().Assembly);
}
/// <summary>
@@ -46,17 +46,20 @@ namespace Lucene.Net.Support
}
/// <summary>
- /// Converts the Java NLS type to the .NET resource type.
- /// In Java, these were one and the same, but in .NET it is not possible to create resources
- /// in Visual Studio with the same class name as a resource class because the resource generation process already
- /// creates a backing class with the same name as the resource. So, by convention the resources must be
- /// named <messages class name> + <see cref="ResourceSuffix"/> (default value "Bundle") in order to be found by NLS.
+ /// Gets the fully-qualified name of the bundle as it would appear
+ /// using <see cref="Assembly.GetManifestResourceNames()"/>, without the
+ /// <c>.resources</c> extension. This is the name that is passed to the
+ /// <c>baseName</c> parameter of
+ /// <see cref="ResourceManager.ResourceManager(string, Assembly)"/>.
/// </summary>
- /// <param name="clazz">The type of the NLS class where the field strings are located that identify resources.</param>
- /// <returns>The type of resources (the class name + <see cref="ResourceSuffix"/>), as a .NET <see cref="Type"/> instance.</returns>
- protected virtual Type GetResourceType(Type clazz)
+ /// <param name="clazz">The type of the NLS-derived class where the field strings are located that identify resources.</param>
+ /// <returns>The resource name.</returns>
+ protected virtual string GetResourceName(Type clazz)
{
- return Type.GetType(string.Concat(clazz.Namespace, ".", clazz.Name, ResourceSuffix, ", ", clazz.GetTypeInfo().Assembly.FullName));
+ string resource = clazz.GetTypeInfo().Assembly.FindResource(clazz, string.Concat(clazz.Name, ResourceSuffix, ".resources"));
+ return !string.IsNullOrEmpty(resource)
+ ? resource.Substring(0, resource.Length - 10)
+ : null;
}
/// <summary>
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net/Util/IOUtils.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Util/IOUtils.cs b/src/Lucene.Net/Util/IOUtils.cs
index afc3c09..380f54c 100644
--- a/src/Lucene.Net/Util/IOUtils.cs
+++ b/src/Lucene.Net/Util/IOUtils.cs
@@ -336,7 +336,7 @@ namespace Lucene.Net.Util
bool success = false;
try
{
- stream = clazz.GetTypeInfo().Assembly.GetManifestResourceStream(resource);
+ stream = clazz.GetTypeInfo().Assembly.FindAndGetManifestResourceStream(clazz, resource);
TextReader reader = GetDecodingReader(stream, charSet);
success = true;
return reader;