From 2280dde754e2263e2b4d447b941039be8c52daaa Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Fri, 17 Dec 2010 15:15:01 +0000 Subject: [PATCH] LUCENE-2815: fix MultiFields thread safety, don't cache null terms(field) git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1050415 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/index/IndexReader.java | 2 +- .../org/apache/lucene/index/MultiFields.java | 47 ++++++++++--------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/lucene/src/java/org/apache/lucene/index/IndexReader.java b/lucene/src/java/org/apache/lucene/index/IndexReader.java index 3b92a53c6ba..2e3a93ebaac 100644 --- a/lucene/src/java/org/apache/lucene/index/IndexReader.java +++ b/lucene/src/java/org/apache/lucene/index/IndexReader.java @@ -1420,7 +1420,7 @@ public abstract class IndexReader implements Cloneable,Closeable { } - private Fields fields; + private volatile Fields fields; /** @lucene.internal */ void storeFields(Fields fields) { diff --git a/lucene/src/java/org/apache/lucene/index/MultiFields.java b/lucene/src/java/org/apache/lucene/index/MultiFields.java index fc3beb7e74c..6abf54346bc 100644 --- a/lucene/src/java/org/apache/lucene/index/MultiFields.java +++ b/lucene/src/java/org/apache/lucene/index/MultiFields.java @@ -19,9 +19,10 @@ package org.apache.lucene.index; import java.io.IOException; import java.util.Map; -import java.util.HashMap; import java.util.List; import java.util.ArrayList; +import java.util.concurrent.ConcurrentHashMap; + import org.apache.lucene.util.ReaderUtil; import org.apache.lucene.util.ReaderUtil.Gather; // for javadocs import org.apache.lucene.util.Bits; @@ -45,7 +46,7 @@ import org.apache.lucene.util.BytesRef; public final class MultiFields extends Fields { private final Fields[] subs; private final ReaderUtil.Slice[] subSlices; - private final Map terms = new HashMap(); + private final Map terms = new ConcurrentHashMap(); /** Returns a single {@link Fields} instance for this * reader, merging fields/terms/docs/positions on the @@ -240,32 +241,32 @@ public final class MultiFields extends Fields { @Override public Terms terms(String field) throws IOException { - final Terms result; + Terms result = terms.get(field); + if (result != null) + return result; - if (!terms.containsKey(field)) { - // Lazy init: first time this field is requested, we - // create & add to terms: - final List subs2 = new ArrayList(); - final List slices2 = new ArrayList(); + // Lazy init: first time this field is requested, we + // create & add to terms: + final List subs2 = new ArrayList(); + final List slices2 = new ArrayList(); - // Gather all sub-readers that share this field - for(int i=0;i