/// <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> Current version number from segments file.</summary> public static long ReadCurrentVersion(Directory directory) { InputStream input = directory.OpenFile("segments"); int format = 0; long version = 0; try { format = input.ReadInt(); if (format < 0) { if (format < FORMAT) { throw new System.IO.IOException("Unknown format version: " + format); } version = input.ReadLong(); // read version } } finally { input.Close(); } if (format < 0) { return(version); } // We cannot be sure about the format of the file. // Therefore we have to read the whole file and cannot simply seek to the version entry. SegmentInfos sis = new SegmentInfos(); sis.Read(directory); return(sis.GetVersion()); }
/// <summary> Current version number from segments file.</summary> public static long ReadCurrentVersion(Directory directory) { InputStream input = directory.OpenFile("segments"); int format = 0; long version = 0; try { format = input.ReadInt(); if (format < 0) { if (format < FORMAT) throw new System.IO.IOException("Unknown format version: " + format); version = input.ReadLong(); // read version } } finally { input.Close(); } if (format < 0) return version; // We cannot be sure about the format of the file. // Therefore we have to read the whole file and cannot simply seek to the version entry. SegmentInfos sis = new SegmentInfos(); sis.Read(directory); return sis.GetVersion(); }