Skip to content

Apache Lucene open-source search software

Add code comment about use of Collections#synchronizedMap in LRUQueryCache. (#14749)

Adrien Grandadb44bb4394e
diff --git a/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java b/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
index 088a47bf0a9..05ef52ff327 100644
--- a/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
+++ b/lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java
@@ -131,6 +131,9 @@ public class LRUQueryCache implements QueryCache, Accountable {
}
this.skipCacheFactor = skipCacheFactor;
+ // Note that reads on this LinkedHashMap trigger modifications on the linked list under the
+ // hood, so reading from multiple threads is not thread-safe. This is why it is wrapped in a
+ // Collections#synchronizedMap.
uniqueQueries = Collections.synchronizedMap(new LinkedHashMap<>(16, 0.75f, true));
mostRecentlyUsedQueries = uniqueQueries.keySet();
cache = new IdentityHashMap<>();