/// <summary> Trys to acquire the WriteLock on this directory. /// this method is only valid if this IndexReader is directory owner. /// /// </summary> /// <throws> IOException If WriteLock cannot be acquired. </throws> private void AquireWriteLock() { if (stale) { throw new System.IO.IOException("IndexReader out of date and no longer valid for delete, undelete, or setNorm operations"); } if (this.writeLock == null) { Lock writeLock = directory.MakeLock(IndexWriter.WRITE_LOCK_NAME); if (!writeLock.Obtain(IndexWriter.WRITE_LOCK_TIMEOUT)) // obtain write lock { throw new System.IO.IOException("Index locked for write: " + writeLock); } this.writeLock = writeLock; // we have to check whether index has changed since this reader was opened. // if so, this reader is no longer valid for deletion if (SegmentInfos.ReadCurrentVersion(directory) > segmentInfos.GetVersion()) { stale = true; this.writeLock.Release(); this.writeLock = null; throw new System.IO.IOException("IndexReader out of date and no longer valid for delete, undelete, or setNorm operations"); } } }
/// <summary> Reads version number from segments files. The version number counts the /// number of changes of the index. /// /// </summary> /// <param name="directory">where the index resides. /// </param> /// <returns> version number. /// </returns> /// <throws> IOException if segments file cannot be read. </throws> public static long GetCurrentVersion(Directory directory) { return(SegmentInfos.ReadCurrentVersion(directory)); }