Skip to content

Apache Lucene open-source search software

Shorten getOrdinal synchronized loop (#12870)

Stefan Voditaad9eff3c2759
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 2aa9c1eca1f..a48eea3871b 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -171,7 +171,8 @@ New Features
Improvements
---------------------
-(No changes)
+
+* GITHUB#12870: Tighten synchronized loop in DirectoryTaxonomyReader#getOrdinal. (Stefan Vodita)
Optimizations
---------------------
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java
index d8101ffe8e8..bf4867e0dda 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java
@@ -279,21 +279,22 @@ public class DirectoryTaxonomyReader extends TaxonomyReader implements Accountab
}
// First try to find the answer in the LRU cache:
+ Integer res;
synchronized (ordinalCache) {
- Integer res = ordinalCache.get(cp);
- if (res != null) {
- if (res < indexReader.maxDoc()) {
- // Since the cache is shared with DTR instances allocated from
- // doOpenIfChanged, we need to ensure that the ordinal is one that
- // this DTR instance recognizes.
- return res;
- } else {
- // if we get here, it means that the category was found in the cache,
- // but is not recognized by this TR instance. Therefore, there's no
- // need to continue search for the path on disk, because we won't find
- // it there too.
- return TaxonomyReader.INVALID_ORDINAL;
- }
+ res = ordinalCache.get(cp);
+ }
+ if (res != null) {
+ if (res < indexReader.maxDoc()) {
+ // Since the cache is shared with DTR instances allocated from
+ // doOpenIfChanged, we need to ensure that the ordinal is one that
+ // this DTR instance recognizes.
+ return res;
+ } else {
+ // if we get here, it means that the category was found in the cache,
+ // but is not recognized by this TR instance. Therefore, there's no
+ // need to continue search for the path on disk, because we won't find
+ // it there too.
+ return TaxonomyReader.INVALID_ORDINAL;
}
}