From 96e1b41127f44119e4140cd46d069e57c8b5f050 Mon Sep 17 00:00:00 2001 From: Christoph Goller Date: Wed, 21 Apr 2004 17:26:28 +0000 Subject: [PATCH] Making bahavior of close() more consistent. git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150307 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/lucene/search/IndexSearcher.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/lucene/search/IndexSearcher.java b/src/java/org/apache/lucene/search/IndexSearcher.java index 0bc21f3be69..2f9829db27a 100644 --- a/src/java/org/apache/lucene/search/IndexSearcher.java +++ b/src/java/org/apache/lucene/search/IndexSearcher.java @@ -32,25 +32,37 @@ import org.apache.lucene.util.PriorityQueue; */ public class IndexSearcher extends Searcher { IndexReader reader; + private boolean closeReader; /** Creates a searcher searching the index in the named directory. */ public IndexSearcher(String path) throws IOException { - this(IndexReader.open(path)); + this(IndexReader.open(path), true); } /** Creates a searcher searching the index in the provided directory. */ public IndexSearcher(Directory directory) throws IOException { - this(IndexReader.open(directory)); + this(IndexReader.open(directory), true); } /** Creates a searcher searching the provided index. */ public IndexSearcher(IndexReader r) { + this(r, false); + } + + private IndexSearcher(IndexReader r, boolean closeReader) { reader = r; + this.closeReader = closeReader; } - // inherit javadoc + /** + * Note that the underlying IndexReader is not closed, if + * IndexSearcher was constructed with IndexSearcher(IndexReader r). + * If the IndexReader was supplied implicitly by specifying a directory, then + * the IndexReader gets closed. + */ public void close() throws IOException { - reader.close(); + if(closeReader) + reader.close(); } // inherit javadoc