/// <summary> Release the segment reader (i.e. decRef it and close if there /// are no more references. /// </summary> /// <param name="sr"> /// </param> /// <throws> IOException </throws> public virtual void Release(SegmentReader sr, bool drop) { lock (this) { bool pooled = readerMap.Contains(sr.GetSegmentInfo()); System.Diagnostics.Debug.Assert(!pooled || readerMap[sr.GetSegmentInfo()] == sr); // Drop caller's ref; for an external reader (not // pooled), this decRef will close it sr.DecRef(); if (pooled && (drop || (!Enclosing_Instance.poolReaders && sr.GetRefCount() == 1))) { // We invoke deleter.checkpoint below, so we must be // sync'd on IW if there are changes: // TODO: java 5 // assert !sr.hasChanges || Thread.holdsLock(IndexWriter.this); // Discard (don't save) changes when we are dropping // the reader; this is used only on the sub-readers // after a successful merge. sr.hasChanges &= !drop; bool hasChanges = sr.hasChanges; // Drop our ref -- this will commit any pending // changes to the dir sr.Close(); // We are the last ref to this reader; since we're // not pooling readers, we release it: readerMap.Remove(sr.GetSegmentInfo()); if (hasChanges) { // Must checkpoint w/ deleter, because this // segment reader will have created new _X_N.del // file. enclosingInstance.deleter.Checkpoint(enclosingInstance.segmentInfos, false); } } } }
/// <summary> Release the segment reader (i.e. decRef it and close if there /// are no more references. /// </summary> /// <param name="sr"> /// </param> /// <throws> IOException </throws> public virtual void Release(SegmentReader sr, bool drop) { lock (this) { bool pooled = readerMap.Contains(sr.GetSegmentInfo()); System.Diagnostics.Debug.Assert(!pooled | readerMap[sr.GetSegmentInfo()] == sr); // Drop caller's ref sr.DecRef(); if (pooled && (drop || (!Enclosing_Instance.poolReaders && sr.GetRefCount() == 1))) { // We are the last ref to this reader; since we're // not pooling readers, we release it: readerMap.Remove(sr.GetSegmentInfo()); // TODO: java 5 // assert !sr.hasChanges || Thread.holdsLock(IndexWriter.this); // Drop our ref -- this will commit any pending // changes to the dir bool success = false; try { sr.Close(); success = true; } finally { if (!success && sr.hasChanges) { // Abandon the changes & retry closing: sr.hasChanges = false; try { sr.Close(); } catch (System.Exception ignore) { // Keep throwing original exception } } } } } }