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 513b8446c9ebebc9215631b783e73532c4727483 Author: Shad Storhaug <[email protected]> AuthorDate: Mon Dec 16 02:04:41 2019 +0700 Lucene.Net.Support.StringExtensions::Intern(): Factored out StringInterner and SimpleStringInterner classes and use J2N for all non-production components. Drop string interning support for production components on .NET Standard 1.6. --- .../Support/Sax/Helpers/NamespaceSupport.cs | 2 +- .../Support/TagSoup/ElementType.cs | 2 +- src/Lucene.Net.Benchmark/Support/TagSoup/Parser.cs | 2 +- .../Codecs/Lucene3x/TestSurrogates.cs | 8 +- src/Lucene.Net/Support/SimpleStringInterner.cs | 100 --------------------- src/Lucene.Net/Support/StringExtensions.cs | 19 ++-- src/Lucene.Net/Support/StringInterner.cs | 50 ----------- 7 files changed, 12 insertions(+), 171 deletions(-) diff --git a/src/Lucene.Net.Benchmark/Support/Sax/Helpers/NamespaceSupport.cs b/src/Lucene.Net.Benchmark/Support/Sax/Helpers/NamespaceSupport.cs index ac79d0a..42e5564 100644 --- a/src/Lucene.Net.Benchmark/Support/Sax/Helpers/NamespaceSupport.cs +++ b/src/Lucene.Net.Benchmark/Support/Sax/Helpers/NamespaceSupport.cs @@ -4,7 +4,7 @@ // This class is in the Public Domain. NO WARRANTY! // $Id: NamespaceSupport.java,v 1.15 2004/04/26 17:34:35 dmegginson Exp $ -using Lucene.Net.Support; +using J2N.Text; using System; using System.Collections; using System.Collections.Generic; diff --git a/src/Lucene.Net.Benchmark/Support/TagSoup/ElementType.cs b/src/Lucene.Net.Benchmark/Support/TagSoup/ElementType.cs index 92bb5a1..49c97dc 100644 --- a/src/Lucene.Net.Benchmark/Support/TagSoup/ElementType.cs +++ b/src/Lucene.Net.Benchmark/Support/TagSoup/ElementType.cs @@ -11,7 +11,7 @@ // OF ANY KIND, either express or implied; not even the implied warranty // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -using Lucene.Net.Support; +using J2N.Text; using Sax.Helpers; using System; using System.Text; diff --git a/src/Lucene.Net.Benchmark/Support/TagSoup/Parser.cs b/src/Lucene.Net.Benchmark/Support/TagSoup/Parser.cs index b132904..d51b5bf 100644 --- a/src/Lucene.Net.Benchmark/Support/TagSoup/Parser.cs +++ b/src/Lucene.Net.Benchmark/Support/TagSoup/Parser.cs @@ -14,7 +14,7 @@ // // The TagSoup parser -using Lucene.Net.Support; +using J2N.Text; using Sax; using Sax.Ext; using Sax.Helpers; diff --git a/src/Lucene.Net.Tests/Codecs/Lucene3x/TestSurrogates.cs b/src/Lucene.Net.Tests/Codecs/Lucene3x/TestSurrogates.cs index 45ed277..9a600b4 100644 --- a/src/Lucene.Net.Tests/Codecs/Lucene3x/TestSurrogates.cs +++ b/src/Lucene.Net.Tests/Codecs/Lucene3x/TestSurrogates.cs @@ -1,9 +1,8 @@ +using J2N.Text; using Lucene.Net.Analysis; -using Lucene.Net.Attributes; using Lucene.Net.Documents; using Lucene.Net.Index; using Lucene.Net.Store; -using Lucene.Net.Support; using Lucene.Net.Util; using NUnit.Framework; using System; @@ -344,8 +343,9 @@ namespace Lucene.Net.Codecs.Lucene3x public virtual void TestSurrogatesOrder() { Directory dir = NewDirectory(); - RandomIndexWriter w = new RandomIndexWriter(Random, dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)) - .SetCodec(new PreFlexRWCodec())); + var config = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)); + config.Codec = new PreFlexRWCodec(); + RandomIndexWriter w = new RandomIndexWriter(Random, dir, config); int numField = TestUtil.NextInt32(Random, 2, 5); diff --git a/src/Lucene.Net/Support/SimpleStringInterner.cs b/src/Lucene.Net/Support/SimpleStringInterner.cs deleted file mode 100644 index 33c0f3e..0000000 --- a/src/Lucene.Net/Support/SimpleStringInterner.cs +++ /dev/null @@ -1,100 +0,0 @@ -using Lucene.Net.Util; -using System; - -namespace Lucene.Net.Support -{ - /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - // LUCENENET specific - - /// <summary> Simple lockless and memory barrier free String intern cache that is guaranteed - /// to return the same String instance as String.intern() does. - /// </summary> - public class SimpleStringInterner : StringInterner - { - - internal /*private*/ class Entry - { - internal /*private*/ System.String str; - internal /*private*/ int hash; - internal /*private*/ Entry next; - internal Entry(System.String str, int hash, Entry next) - { - this.str = str; - this.hash = hash; - this.next = next; - } - } - - private Entry[] cache; - private int maxChainLength; - - /// <param name="tableSize"> Size of the hash table, should be a power of two. - /// </param> - /// <param name="maxChainLength"> Maximum length of each bucket, after which the oldest item inserted is dropped. - /// </param> - public SimpleStringInterner(int tableSize, int maxChainLength) - { - cache = new Entry[System.Math.Max(1, BitUtil.NextHighestPowerOfTwo(tableSize))]; - this.maxChainLength = System.Math.Max(2, maxChainLength); - } - - // @Override - public override System.String Intern(System.String s) - { - int h = s.GetHashCode(); - // In the future, it may be worth augmenting the string hash - // if the lower bits need better distribution. - int slot = h & (cache.Length - 1); - - Entry first = this.cache[slot]; - Entry nextToLast = null; - - int chainLength = 0; - - for (Entry e = first; e != null; e = e.next) - { - if (e.hash == h && (ReferenceEquals(e.str, s) || String.CompareOrdinal(e.str, s) == 0)) - { - // if (e.str == s || (e.hash == h && e.str.compareTo(s)==0)) { - return e.str; - } - - chainLength++; - if (e.next != null) - { - nextToLast = e; - } - } - - // insertion-order cache: add new entry at head - -#if !NETSTANDARD1_6 - s = String.Intern(s); -#endif - - this.cache[slot] = new Entry(s, h, first); - if (chainLength >= maxChainLength) - { - // prune last entry - nextToLast.next = null; - } - return s; - } - } -} \ No newline at end of file diff --git a/src/Lucene.Net/Support/StringExtensions.cs b/src/Lucene.Net/Support/StringExtensions.cs index 0b7c435..7d4e948 100644 --- a/src/Lucene.Net/Support/StringExtensions.cs +++ b/src/Lucene.Net/Support/StringExtensions.cs @@ -163,19 +163,6 @@ namespace Lucene.Net.Support return input; } - - /// <summary> Expert: - /// The StringInterner implementation used by Lucene. - /// This shouldn't be changed to an incompatible implementation after other Lucene APIs have been used. - /// LUCENENET specific. - /// </summary> - private static StringInterner interner = -#if NETSTANDARD1_6 - new SimpleStringInterner(1024, 8); -#else - new StringInterner(); -#endif - /// <summary> /// Searches an internal table of strings for a string equal to this string. /// If the string is not in the table, it is added. Returns the string @@ -185,7 +172,11 @@ namespace Lucene.Net.Support /// <returns>The interned string equal to this string.</returns> public static string Intern(this string s) { - return interner.Intern(s); +#if NETSTANDARD1_6 + return s; // For .NET Standard 1.x, we only support interning using J2N +#else + return string.Intern(s); +#endif } } } \ No newline at end of file diff --git a/src/Lucene.Net/Support/StringInterner.cs b/src/Lucene.Net/Support/StringInterner.cs deleted file mode 100644 index 2549395..0000000 --- a/src/Lucene.Net/Support/StringInterner.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; - -namespace Lucene.Net.Support -{ - /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - /// <summary> Subclasses of StringInterner are required to - /// return the same single String object for all equal strings. - /// Depending on the implementation, this may not be - /// the same object returned as String.intern(). - /// - /// This StringInterner base class simply delegates to String.intern(). - /// - /// LUCENENET specific. - /// </summary> - public class StringInterner - { - /// <summary>Returns a single object instance for each equal string. </summary> - public virtual System.String Intern(System.String s) - { -#if !NETSTANDARD1_6 - return String.Intern(s); -#else - throw new PlatformNotSupportedException("String.Intern not supported. Use SimpleStringInterner."); -#endif - - } - - /// <summary>Returns a single object instance for each equal string. </summary> - public virtual System.String Intern(char[] arr, int offset, int len) - { - return Intern(new System.String(arr, offset, len)); - } - } -} \ No newline at end of file
