From 7c842e3c34d2192643d6bef148ff4b7716a173e6 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Tue, 22 Jul 2008 11:14:36 +0000 Subject: [PATCH] LUCENE-1339: make IndexReader incRef and decRef expert public methods git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@678716 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/index/IndexReader.java | 29 ++++++++++++++----- .../apache/lucene/index/SegmentReader.java | 4 +-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/lucene/index/IndexReader.java b/src/java/org/apache/lucene/index/IndexReader.java index 23cc1fcdbad..1b4978dae53 100644 --- a/src/java/org/apache/lucene/index/IndexReader.java +++ b/src/java/org/apache/lucene/index/IndexReader.java @@ -102,23 +102,38 @@ public abstract class IndexReader { } /** - * Increments the refCount of this IndexReader instance. RefCounts are used to determine - * when a reader can be closed safely, i. e. as soon as no other IndexReader is referencing - * it anymore. + * Expert: increments the refCount of this IndexReader + * instance. RefCounts are used to determine when a + * reader can be closed safely, i.e. as soon as there are + * no more references. Be sure to always call a + * corresponding {@link #decRef}, in a finally clause; + * otherwise the reader may never be closed. Note that + * {@link #close} simply calls decRef(), which means that + * the IndexReader will not really be closed until {@link + * #decRef} has been called for all outstanding + * references. + * + * @see #decRef */ - protected synchronized void incRef() { + public synchronized void incRef() { assert refCount > 0; + ensureOpen(); refCount++; } /** - * Decreases the refCount of this IndexReader instance. If the refCount drops - * to 0, then pending changes are committed to the index and this reader is closed. + * Expert: decreases the refCount of this IndexReader + * instance. If the refCount drops to 0, then pending + * changes (if any) are committed to the index and this + * reader is closed. * * @throws IOException in case an IOException occurs in commit() or doClose() + * + * @see #incRef */ - protected synchronized void decRef() throws IOException { + public synchronized void decRef() throws IOException { assert refCount > 0; + ensureOpen(); if (refCount == 1) { commit(); doClose(); diff --git a/src/java/org/apache/lucene/index/SegmentReader.java b/src/java/org/apache/lucene/index/SegmentReader.java index 351611f3612..caf90687af2 100644 --- a/src/java/org/apache/lucene/index/SegmentReader.java +++ b/src/java/org/apache/lucene/index/SegmentReader.java @@ -138,7 +138,7 @@ class SegmentReader extends DirectoryIndexReader { * Increments the RC of this reader, as well as * of all norms this reader is using */ - protected synchronized void incRef() { + public synchronized void incRef() { super.incRef(); Iterator it = norms.values().iterator(); while (it.hasNext()) { @@ -157,7 +157,7 @@ class SegmentReader extends DirectoryIndexReader { super.incRef(); } - protected synchronized void decRef() throws IOException { + public synchronized void decRef() throws IOException { super.decRef(); Iterator it = norms.values().iterator(); while (it.hasNext()) {