You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by pa...@apache.org on 2026/10/22 23:34:12 UTC
(lucenenet) branch master updated: Backport LUCENE-7891: default LruTaxonomyWriterCache to LRU_STRING, #1444 (#1502)
This is an automated email from the ASF dual-hosted git repository.
paulirwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git
The following commit(s) were added to refs/heads/master by this push:
new 76de0decb Backport LUCENE-7891: default LruTaxonomyWriterCache to LRU_STRING, #1444 (#1502)
76de0decb is described below
commit 76de0decb7a27336a30e30c3a1cdc2a82d7a9663
Author: Paul Irwin <pa...@gmail.com>
AuthorDate: Thu Oct 22 17:34:07 2026 -0600
Backport LUCENE-7891: default LruTaxonomyWriterCache to LRU_STRING, #1444 (#1502)
Co-authored-by: Claude Opus 5.5 <no...@anthropic.com>
---
.../Taxonomy/WriterCache/LruTaxonomyWriterCache.cs | 30 +++++++---
.../WriterCache/TestLruTaxonomyWriterCache.cs | 69 ++++++++++++++++++++++
2 files changed, 90 insertions(+), 9 deletions(-)
diff --git a/src/Lucene.Net.Facet/Taxonomy/WriterCache/LruTaxonomyWriterCache.cs b/src/Lucene.Net.Facet/Taxonomy/WriterCache/LruTaxonomyWriterCache.cs
index 880ad4835..ffdfb892c 100644
--- a/src/Lucene.Net.Facet/Taxonomy/WriterCache/LruTaxonomyWriterCache.cs
+++ b/src/Lucene.Net.Facet/Taxonomy/WriterCache/LruTaxonomyWriterCache.cs
@@ -1,4 +1,4 @@
-// Lucene version compatibility level 4.8.1
+// Lucene version compatibility level 4.8.1 + LUCENE-7891
using Lucene.Net.Support.Threading;
using System;
@@ -36,8 +36,11 @@ public class LruTaxonomyWriterCache : ITaxonomyWriterCache
public enum LRUType
{
/// <summary>
- /// Use the label's hash as the key; this can lead to
- /// silent conflicts!
+ /// Use only the label's 64 bit <see cref="FacetLabel.Int64HashCode()"/> as the hash key. Do not
+ /// check equals, unlike most hash maps.
+ /// Note that while these hashes are very likely to be unique, the chance
+ /// of a collision is still greater than zero. If such an unlikely event
+ /// occurs, your document will get an incorrect facet.
/// </summary>
LRU_HASHED,
@@ -53,30 +56,39 @@ public enum LRUType
private bool isDisposed = false;
/// <summary>
- /// Creates this with <see cref="LRUType.LRU_HASHED"/> method.
+ /// Creates this with <see cref="LRUType.LRU_STRING"/> method.
/// </summary>
public LruTaxonomyWriterCache(int cacheSize)
- : this(cacheSize, LRUType.LRU_HASHED)
+ : this(cacheSize, LRUType.LRU_STRING)
{
// TODO (Facet): choose between NameHashIntCacheLRU and NameIntCacheLRU.
// For guaranteed correctness - not relying on no-collisions in the hash
// function, NameIntCacheLRU should be used:
// On the other hand, NameHashIntCacheLRU takes less RAM but if there
- // are collisions (which we never found) two different paths would be
- // mapped to the same ordinal...
+ // are collisions two different paths would be mapped to the same
+ // ordinal...
}
/// <summary>
/// Creates this with the specified method.
/// </summary>
+ /// <param name="cacheSize">The maximum number of entries to keep in the cache.</param>
+ /// <param name="lruType">The cache type to use. <see cref="LRUType.LRU_STRING"/> is
+ /// recommended and is the default for <see cref="LruTaxonomyWriterCache(int)"/>.
+ /// <para/>
+ /// <b>LUCENE.NET WARNING:</b> <see cref="LRUType.LRU_HASHED"/> uses only the label's 64-bit
+ /// <see cref="FacetLabel.Int64HashCode()"/> as the key and does not check equality. If two
+ /// different labels produce the same hash, they map to the same ordinal, and documents
+ /// silently get the wrong facet. Use it only if you accept that risk for the lower memory use.
+ /// </param>
public LruTaxonomyWriterCache(int cacheSize, LRUType lruType)
{
// TODO (Facet): choose between NameHashIntCacheLRU and NameIntCacheLRU.
// For guaranteed correctness - not relying on no-collisions in the hash
// function, NameIntCacheLRU should be used:
// On the other hand, NameHashIntCacheLRU takes less RAM but if there
- // are collisions (which we never found) two different paths would be
- // mapped to the same ordinal...
+ // are collisions two different paths would be mapped to the same
+ // ordinal...
if (lruType == LRUType.LRU_HASHED)
{
this.cache = new NameHashInt32CacheLru(cacheSize);
diff --git a/src/Lucene.Net.Tests.Facet/Taxonomy/WriterCache/TestLruTaxonomyWriterCache.cs b/src/Lucene.Net.Tests.Facet/Taxonomy/WriterCache/TestLruTaxonomyWriterCache.cs
new file mode 100644
index 000000000..b079f37ce
--- /dev/null
+++ b/src/Lucene.Net.Tests.Facet/Taxonomy/WriterCache/TestLruTaxonomyWriterCache.cs
@@ -0,0 +1,69 @@
+// Lucene version compatibility level 7.1.0
+using NUnit.Framework;
+using Assert = Lucene.Net.TestFramework.Assert;
+
+namespace Lucene.Net.Facet.Taxonomy.WriterCache
+{
+ /*
+ * 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.
+ */
+
+ [TestFixture]
+ public class TestLruTaxonomyWriterCache : FacetTestCase
+ {
+ [Test]
+ public virtual void TestDefaultLRUTypeIsCollisionSafe()
+ {
+ // These labels are clearly different, but have identical longHashCodes.
+ // Note that these labels are clearly contrived. We did encounter
+ // collisions in actual production data, but we aren't allowed to publish
+ // those.
+ // LUCENENET specific: The upstream labels only collide using Java's
+ // String.hashCode(), and .NET's string.GetHashCode() is randomized per
+ // process, so we use a subclass of FacetLabel that produces collisions
+ // (as the comment below suggests) instead.
+ //FacetLabel a = new FacetLabel("\0", "\u0003¬");
+ //FacetLabel b = new FacetLabel("\u0001", "\0");
+ FacetLabel a = new CollidingFacetLabel("\0", "\u0003¬");
+ FacetLabel b = new CollidingFacetLabel("\u0001", "\0");
+ // If this fails, then the longHashCode implementation has changed. This
+ // cannot prevent collisions. (All hashes must allow for collisions.) It
+ // will however stop the rest of this test from making sense. To fix, find
+ // new colliding labels, or make a subclass of FacetLabel that produces
+ // collisions.
+ Assert.AreEqual(a.Int64HashCode(), b.Int64HashCode());
+ // Make a cache with capacity > 2 so both our labels will fit. Don't
+ // specify an LRUType, since we want to check if the default is
+ // collision-safe.
+ LruTaxonomyWriterCache cache = new LruTaxonomyWriterCache(10);
+ cache.Put(a, 0);
+ cache.Put(b, 1);
+ Assert.AreEqual(0, cache.Get(a));
+ Assert.AreEqual(1, cache.Get(b));
+ }
+
+ // LUCENENET specific - FacetLabel whose Int64HashCode() always collides
+ private sealed class CollidingFacetLabel : FacetLabel
+ {
+ public CollidingFacetLabel(params string[] components)
+ : base(components)
+ {
+ }
+
+ public override long Int64HashCode() => 0;
+ }
+ }
+}